message_differencer_unittest.cc 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: jschorr@google.com (Joseph Schorr)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // TODO(ksroka): Move some of these tests to field_comparator_test.cc.
  35. #include <algorithm>
  36. #include <string>
  37. #include <vector>
  38. #include <google/protobuf/stubs/strutil.h>
  39. #include <google/protobuf/util/field_comparator.h>
  40. #include <google/protobuf/util/message_differencer.h>
  41. #include <google/protobuf/util/message_differencer_unittest.pb.h>
  42. #include <google/protobuf/text_format.h>
  43. #include <google/protobuf/wire_format.h>
  44. #include <google/protobuf/io/zero_copy_stream_impl.h>
  45. #include <google/protobuf/io/coded_stream.h>
  46. #include <google/protobuf/any_test.pb.h>
  47. #include <google/protobuf/map_unittest.pb.h>
  48. #include <google/protobuf/unittest.pb.h>
  49. #include <google/protobuf/map_test_util.h>
  50. #include <google/protobuf/test_util.h>
  51. #include <google/protobuf/stubs/logging.h>
  52. #include <google/protobuf/stubs/common.h>
  53. #include <google/protobuf/testing/googletest.h>
  54. #include <gtest/gtest.h>
  55. namespace google {
  56. namespace protobuf {
  57. namespace {
  58. const FieldDescriptor* GetFieldDescriptor(
  59. const Message& message, const string& field_name) {
  60. std::vector<string> field_path =
  61. Split(field_name, ".", true);
  62. const Descriptor* descriptor = message.GetDescriptor();
  63. const FieldDescriptor* field = NULL;
  64. for (int i = 0; i < field_path.size(); i++) {
  65. field = descriptor->FindFieldByName(field_path[i]);
  66. descriptor = field->message_type();
  67. }
  68. return field;
  69. }
  70. void ExpectEqualsWithDifferencer(util::MessageDifferencer* differencer,
  71. const Message& msg1,
  72. const Message& msg2) {
  73. differencer->set_scope(util::MessageDifferencer::FULL);
  74. EXPECT_TRUE(differencer->Compare(msg1, msg2));
  75. differencer->set_scope(util::MessageDifferencer::PARTIAL);
  76. EXPECT_TRUE(differencer->Compare(msg1, msg2));
  77. }
  78. TEST(MessageDifferencerTest, BasicEqualityTest) {
  79. // Create the testing protos
  80. unittest::TestAllTypes msg1;
  81. unittest::TestAllTypes msg2;
  82. TestUtil::SetAllFields(&msg1);
  83. TestUtil::SetAllFields(&msg2);
  84. // Compare
  85. EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
  86. }
  87. TEST(MessageDifferencerTest, BasicInequalityTest) {
  88. // Create the testing protos
  89. unittest::TestAllTypes msg1;
  90. unittest::TestAllTypes msg2;
  91. TestUtil::SetAllFields(&msg1);
  92. TestUtil::SetAllFields(&msg2);
  93. msg1.set_optional_int32(-1);
  94. // Compare
  95. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  96. }
  97. TEST(MessageDifferencerTest, RepeatedFieldInequalityTest) {
  98. // Create the testing protos
  99. unittest::TestAllTypes msg1;
  100. unittest::TestAllTypes msg2;
  101. TestUtil::SetAllFields(&msg1);
  102. TestUtil::SetAllFields(&msg2);
  103. msg1.add_repeated_int32(-1);
  104. // Compare
  105. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  106. }
  107. TEST(MessageDifferencerTest, RepeatedFieldSetOptimizationTest) {
  108. util::MessageDifferencer differencer;
  109. protobuf_unittest::TestDiffMessage msg1;
  110. protobuf_unittest::TestDiffMessage msg2;
  111. protobuf_unittest::TestDiffMessage::Item* item1 = msg1.add_item();
  112. protobuf_unittest::TestDiffMessage::Item* item2 = msg2.add_item();
  113. differencer.TreatAsSet(item1->GetDescriptor()->FindFieldByName("ra"));
  114. differencer.TreatAsSet(item2->GetDescriptor()->FindFieldByName("ra"));
  115. for (int i = 0; i < 1000; i++) {
  116. item1->add_ra(i);
  117. item2->add_ra(i);
  118. }
  119. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  120. item2->add_ra(1001);
  121. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  122. item1->add_ra(1001);
  123. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  124. item1->add_ra(1002);
  125. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  126. }
  127. TEST(MessageDifferencerTest, MapFieldEqualityTest) {
  128. // Create the testing protos
  129. unittest::TestMap msg1;
  130. unittest::TestMap msg2;
  131. MapReflectionTester tester(unittest::TestMap::descriptor());
  132. tester.SetMapFieldsViaReflection(&msg1);
  133. tester.SetMapFieldsViaReflection(&msg2);
  134. tester.SwapMapsViaReflection(&msg1);
  135. // Compare
  136. EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
  137. }
  138. TEST(MessageDifferencerTest, BasicPartialEqualityTest) {
  139. // Create the testing protos
  140. unittest::TestAllTypes msg1;
  141. unittest::TestAllTypes msg2;
  142. TestUtil::SetAllFields(&msg1);
  143. TestUtil::SetAllFields(&msg2);
  144. // Compare
  145. util::MessageDifferencer differencer;
  146. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  147. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  148. }
  149. TEST(MessageDifferencerTest, PartialEqualityTestExtraField) {
  150. // Create the testing protos
  151. unittest::TestAllTypes msg1;
  152. unittest::TestAllTypes msg2;
  153. TestUtil::SetAllFields(&msg1);
  154. TestUtil::SetAllFields(&msg2);
  155. msg1.clear_optional_int32();
  156. // Compare
  157. util::MessageDifferencer differencer;
  158. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  159. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  160. }
  161. TEST(MessageDifferencerTest, PartialEqualityTestSkipRequiredField) {
  162. // Create the testing protos
  163. unittest::TestRequired msg1;
  164. unittest::TestRequired msg2;
  165. msg1.set_a(401);
  166. msg2.set_a(401);
  167. msg2.set_b(402);
  168. // Compare
  169. util::MessageDifferencer differencer;
  170. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  171. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  172. }
  173. TEST(MessageDifferencerTest, BasicPartialInequalityTest) {
  174. // Create the testing protos
  175. unittest::TestAllTypes msg1;
  176. unittest::TestAllTypes msg2;
  177. TestUtil::SetAllFields(&msg1);
  178. TestUtil::SetAllFields(&msg2);
  179. msg1.set_optional_int32(-1);
  180. // Compare
  181. util::MessageDifferencer differencer;
  182. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  183. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  184. }
  185. TEST(MessageDifferencerTest, PartialInequalityMissingFieldTest) {
  186. // Create the testing protos
  187. unittest::TestAllTypes msg1;
  188. unittest::TestAllTypes msg2;
  189. TestUtil::SetAllFields(&msg1);
  190. TestUtil::SetAllFields(&msg2);
  191. msg2.clear_optional_int32();
  192. // Compare
  193. util::MessageDifferencer differencer;
  194. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  195. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  196. }
  197. TEST(MessageDifferencerTest, RepeatedFieldPartialInequalityTest) {
  198. // Create the testing protos
  199. unittest::TestAllTypes msg1;
  200. unittest::TestAllTypes msg2;
  201. TestUtil::SetAllFields(&msg1);
  202. TestUtil::SetAllFields(&msg2);
  203. msg1.add_repeated_int32(-1);
  204. // Compare
  205. util::MessageDifferencer differencer;
  206. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  207. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  208. }
  209. TEST(MessageDifferencerTest, BasicEquivalencyTest) {
  210. // Create the testing protos
  211. unittest::TestAllTypes msg1;
  212. unittest::TestAllTypes msg2;
  213. TestUtil::SetAllFields(&msg1);
  214. TestUtil::SetAllFields(&msg2);
  215. // Compare
  216. EXPECT_TRUE(util::MessageDifferencer::Equivalent(msg1, msg2));
  217. }
  218. TEST(MessageDifferencerTest, EquivalencyNotEqualTest) {
  219. // Create the testing protos
  220. unittest::TestAllTypes msg1;
  221. unittest::TestAllTypes msg2;
  222. TestUtil::SetAllFields(&msg1);
  223. TestUtil::SetAllFields(&msg2);
  224. msg1.clear_optional_int32();
  225. msg2.set_optional_int32(0);
  226. // Compare
  227. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  228. EXPECT_TRUE(util::MessageDifferencer::Equivalent(msg1, msg2));
  229. }
  230. TEST(MessageDifferencerTest, BasicInequivalencyTest) {
  231. // Create the testing protos
  232. unittest::TestAllTypes msg1;
  233. unittest::TestAllTypes msg2;
  234. TestUtil::SetAllFields(&msg1);
  235. TestUtil::SetAllFields(&msg2);
  236. msg1.set_optional_int32(-1);
  237. // Compare
  238. EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2));
  239. }
  240. TEST(MessageDifferencerTest, BasicEquivalencyNonSetTest) {
  241. // Create the testing protos
  242. unittest::TestAllTypes msg1;
  243. unittest::TestAllTypes msg2;
  244. // Compare
  245. EXPECT_TRUE(util::MessageDifferencer::Equivalent(msg1, msg2));
  246. }
  247. TEST(MessageDifferencerTest, BasicInequivalencyNonSetTest) {
  248. // Create the testing protos
  249. unittest::TestAllTypes msg1;
  250. unittest::TestAllTypes msg2;
  251. msg1.set_optional_int32(-1);
  252. // Compare
  253. EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2));
  254. }
  255. TEST(MessageDifferencerTest, BasicPartialEquivalencyTest) {
  256. // Create the testing protos
  257. unittest::TestAllTypes msg1;
  258. unittest::TestAllTypes msg2;
  259. TestUtil::SetAllFields(&msg1);
  260. TestUtil::SetAllFields(&msg2);
  261. // Compare
  262. util::MessageDifferencer differencer;
  263. differencer.set_message_field_comparison(
  264. util::MessageDifferencer::EQUIVALENT);
  265. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  266. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  267. }
  268. TEST(MessageDifferencerTest, PartialEquivalencyNotEqualTest) {
  269. // Create the testing protos
  270. unittest::TestAllTypes msg1;
  271. unittest::TestAllTypes msg2;
  272. TestUtil::SetAllFields(&msg1);
  273. TestUtil::SetAllFields(&msg2);
  274. msg1.set_optional_int32(0);
  275. msg2.clear_optional_int32();
  276. // Compare
  277. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  278. util::MessageDifferencer differencer;
  279. differencer.set_message_field_comparison(
  280. util::MessageDifferencer::EQUIVALENT);
  281. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  282. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  283. }
  284. TEST(MessageDifferencerTest, PartialEquivalencyTestExtraField) {
  285. // Create the testing protos
  286. unittest::TestAllTypes msg1;
  287. unittest::TestAllTypes msg2;
  288. TestUtil::SetAllFields(&msg1);
  289. TestUtil::SetAllFields(&msg2);
  290. msg1.clear_optional_int32();
  291. // Compare
  292. util::MessageDifferencer differencer;
  293. differencer.set_message_field_comparison(
  294. util::MessageDifferencer::EQUIVALENT);
  295. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  296. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  297. }
  298. TEST(MessageDifferencerTest, PartialEquivalencyTestSkipRequiredField) {
  299. // Create the testing protos
  300. unittest::TestRequired msg1;
  301. unittest::TestRequired msg2;
  302. msg1.set_a(401);
  303. msg2.set_a(401);
  304. msg2.set_b(402);
  305. // Compare
  306. util::MessageDifferencer differencer;
  307. differencer.set_message_field_comparison(
  308. util::MessageDifferencer::EQUIVALENT);
  309. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  310. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  311. }
  312. TEST(MessageDifferencerTest, BasicPartialInequivalencyTest) {
  313. // Create the testing protos
  314. unittest::TestAllTypes msg1;
  315. unittest::TestAllTypes msg2;
  316. TestUtil::SetAllFields(&msg1);
  317. TestUtil::SetAllFields(&msg2);
  318. msg1.set_optional_int32(-1);
  319. // Compare
  320. util::MessageDifferencer differencer;
  321. differencer.set_message_field_comparison(
  322. util::MessageDifferencer::EQUIVALENT);
  323. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  324. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  325. }
  326. TEST(MessageDifferencerTest, BasicPartialEquivalencyNonSetTest) {
  327. // Create the testing protos
  328. unittest::TestAllTypes msg1;
  329. unittest::TestAllTypes msg2;
  330. // Compare
  331. util::MessageDifferencer differencer;
  332. differencer.set_message_field_comparison(
  333. util::MessageDifferencer::EQUIVALENT);
  334. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  335. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  336. }
  337. TEST(MessageDifferencerTest, BasicPartialInequivalencyNonSetTest) {
  338. // Create the testing protos
  339. unittest::TestAllTypes msg1;
  340. unittest::TestAllTypes msg2;
  341. msg1.set_optional_int32(-1);
  342. // Compare
  343. util::MessageDifferencer differencer;
  344. differencer.set_message_field_comparison(
  345. util::MessageDifferencer::EQUIVALENT);
  346. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  347. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  348. }
  349. TEST(MessageDifferencerTest, ApproximateEqualityTest) {
  350. // Create the testing protos
  351. unittest::TestAllTypes msg1;
  352. unittest::TestAllTypes msg2;
  353. TestUtil::SetAllFields(&msg1);
  354. TestUtil::SetAllFields(&msg2);
  355. // Compare
  356. EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquals(msg1, msg2));
  357. }
  358. TEST(MessageDifferencerTest, ApproximateModifiedEqualityTest) {
  359. // Create the testing protos
  360. unittest::TestAllTypes msg1;
  361. unittest::TestAllTypes msg2;
  362. TestUtil::SetAllFields(&msg1);
  363. TestUtil::SetAllFields(&msg2);
  364. const float v1 = 2.300005f;
  365. const float v2 = 2.300006f;
  366. msg1.set_optional_float(v1);
  367. msg2.set_optional_float(v2);
  368. // Compare
  369. ASSERT_NE(v1, v2) << "Should not be the same: " << v1 << ", " << v2;
  370. ASSERT_FLOAT_EQ(v1, v2) << "Should be approx. equal: " << v1 << ", " << v2;
  371. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  372. EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquals(msg1, msg2));
  373. }
  374. TEST(MessageDifferencerTest, ApproximateEquivalencyTest) {
  375. // Create the testing protos
  376. unittest::TestAllTypes msg1;
  377. unittest::TestAllTypes msg2;
  378. TestUtil::SetAllFields(&msg1);
  379. TestUtil::SetAllFields(&msg2);
  380. // Compare
  381. EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
  382. msg2));
  383. }
  384. TEST(MessageDifferencerTest, ApproximateModifiedEquivalencyTest) {
  385. // Create the testing protos
  386. unittest::TestAllTypes msg1;
  387. unittest::TestAllTypes msg2;
  388. TestUtil::SetAllFields(&msg1);
  389. TestUtil::SetAllFields(&msg2);
  390. // Modify the approximateness requirement
  391. const float v1 = 2.300005f;
  392. const float v2 = 2.300006f;
  393. msg1.set_optional_float(v1);
  394. msg2.set_optional_float(v2);
  395. // Compare
  396. ASSERT_NE(v1, v2) << "Should not be the same: " << v1 << ", " << v2;
  397. ASSERT_FLOAT_EQ(v1, v2) << "Should be approx. equal: " << v1 << ", " << v2;
  398. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  399. EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
  400. msg2));
  401. // Modify the equivalency requirement too
  402. msg1.clear_optional_int32();
  403. msg2.set_optional_int32(0);
  404. // Compare. Now should only pass on ApproximatelyEquivalent
  405. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  406. EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2));
  407. EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquals(msg1, msg2));
  408. EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
  409. msg2));
  410. }
  411. TEST(MessageDifferencerTest, ApproximateInequivalencyTest) {
  412. // Create the testing protos
  413. unittest::TestAllTypes msg1;
  414. unittest::TestAllTypes msg2;
  415. TestUtil::SetAllFields(&msg1);
  416. TestUtil::SetAllFields(&msg2);
  417. // Should fail on equivalency
  418. msg1.set_optional_int32(-1);
  419. EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
  420. msg2));
  421. // Make these fields the same again.
  422. msg1.set_optional_int32(0);
  423. msg2.set_optional_int32(0);
  424. EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
  425. msg2));
  426. // Should fail on approximate equality check
  427. const float v1 = 2.3f;
  428. const float v2 = 9.3f;
  429. msg1.set_optional_float(v1);
  430. msg2.set_optional_float(v2);
  431. EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
  432. msg2));
  433. }
  434. TEST(MessageDifferencerTest, WithinFractionOrMarginFloatTest) {
  435. // Create the testing protos
  436. unittest::TestAllTypes msg1;
  437. unittest::TestAllTypes msg2;
  438. TestUtil::SetAllFields(&msg1);
  439. TestUtil::SetAllFields(&msg2);
  440. // Should fail on approximate equality check
  441. const float v1 = 100.0f;
  442. const float v2 = 109.9f;
  443. msg1.set_optional_float(v1);
  444. msg2.set_optional_float(v2);
  445. // Compare
  446. util::MessageDifferencer differencer;
  447. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  448. const FieldDescriptor* fd =
  449. msg1.GetDescriptor()->FindFieldByName("optional_float");
  450. // Set float comparison to exact, margin and fraction value should not matter.
  451. differencer.set_float_comparison(util::MessageDifferencer::EXACT);
  452. // Set margin for float comparison.
  453. differencer.SetFractionAndMargin(fd, 0.0, 10.0);
  454. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  455. // Margin and fraction float comparison is activated when float comparison is
  456. // set to approximate.
  457. differencer.set_float_comparison(util::MessageDifferencer::APPROXIMATE);
  458. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  459. // Test out float comparison with fraction.
  460. differencer.SetFractionAndMargin(fd, 0.2, 0.0);
  461. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  462. // Should fail since the fraction is smaller than error.
  463. differencer.SetFractionAndMargin(fd, 0.01, 0.0);
  464. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  465. // Should pass if either fraction or margin are satisfied.
  466. differencer.SetFractionAndMargin(fd, 0.01, 10.0);
  467. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  468. // Make sure that the margin and fraction only affects the field that it was
  469. // set for.
  470. msg1.set_default_float(v1);
  471. msg2.set_default_float(v2);
  472. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  473. msg1.set_default_float(v1);
  474. msg2.set_default_float(v1);
  475. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  476. }
  477. TEST(MessageDifferencerTest, WithinFractionOrMarginDoubleTest) {
  478. // Create the testing protos
  479. unittest::TestAllTypes msg1;
  480. unittest::TestAllTypes msg2;
  481. TestUtil::SetAllFields(&msg1);
  482. TestUtil::SetAllFields(&msg2);
  483. // Should fail on approximate equality check
  484. const double v1 = 100.0;
  485. const double v2 = 109.9;
  486. msg1.set_optional_double(v1);
  487. msg2.set_optional_double(v2);
  488. // Compare
  489. util::MessageDifferencer differencer;
  490. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  491. // Set comparison to exact, margin and fraction value should not matter.
  492. differencer.set_float_comparison(util::MessageDifferencer::EXACT);
  493. // Set margin for float comparison.
  494. const FieldDescriptor* fd =
  495. msg1.GetDescriptor()->FindFieldByName("optional_double");
  496. differencer.SetFractionAndMargin(fd, 0.0, 10.0);
  497. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  498. // Margin and fraction comparison is activated when float comparison is
  499. // set to approximate.
  500. differencer.set_float_comparison(util::MessageDifferencer::APPROXIMATE);
  501. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  502. // Test out comparison with fraction.
  503. differencer.SetFractionAndMargin(fd, 0.2, 0.0);
  504. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  505. // Should fail since the fraction is smaller than error.
  506. differencer.SetFractionAndMargin(fd, 0.01, 0.0);
  507. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  508. // Should pass if either fraction or margin are satisfied.
  509. differencer.SetFractionAndMargin(fd, 0.01, 10.0);
  510. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  511. // Make sure that the margin and fraction only affects the field that it was
  512. // set for.
  513. msg1.set_default_double(v1);
  514. msg2.set_default_double(v2);
  515. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  516. msg1.set_default_double(v1);
  517. msg2.set_default_double(v1);
  518. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  519. }
  520. TEST(MessageDifferencerTest, WithinDefaultFractionOrMarginDoubleTest) {
  521. // Create the testing protos
  522. unittest::TestAllTypes msg1;
  523. unittest::TestAllTypes msg2;
  524. TestUtil::SetAllFields(&msg1);
  525. TestUtil::SetAllFields(&msg2);
  526. // Should fail on approximate equality check
  527. const double v1 = 100.0;
  528. const double v2 = 109.9;
  529. msg1.set_optional_double(v1);
  530. msg2.set_optional_double(v2);
  531. util::MessageDifferencer differencer;
  532. // Compare
  533. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  534. // Set up a custom field comparitor, with a default fraction and margin for
  535. // float and double comparison.
  536. util::DefaultFieldComparator field_comparitor;
  537. field_comparitor.SetDefaultFractionAndMargin(0.0, 10.0);
  538. differencer.set_field_comparator(&field_comparitor);
  539. // Set comparison to exact, margin and fraction value should not matter.
  540. field_comparitor.set_float_comparison(util::DefaultFieldComparator::EXACT);
  541. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  542. // Margin and fraction comparison is activated when float comparison is
  543. // set to approximate.
  544. field_comparitor.set_float_comparison(
  545. util::DefaultFieldComparator::APPROXIMATE);
  546. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  547. // Test out comparison with fraction.
  548. field_comparitor.SetDefaultFractionAndMargin(0.2, 0.0);
  549. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  550. // Should fail since the fraction is smaller than error.
  551. field_comparitor.SetDefaultFractionAndMargin(0.01, 0.0);
  552. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  553. // Should pass if either fraction or margin are satisfied.
  554. field_comparitor.SetDefaultFractionAndMargin(0.01, 10.0);
  555. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  556. // Make sure that the default margin and fraction affects all fields
  557. msg1.set_default_double(v1);
  558. msg2.set_default_double(v2);
  559. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  560. }
  561. TEST(MessageDifferencerTest, BasicFieldOrderingsTest) {
  562. // Create the testing protos
  563. unittest::TestFieldOrderings msg1;
  564. unittest::TestFieldOrderings msg2;
  565. TestUtil::SetAllFieldsAndExtensions(&msg1);
  566. TestUtil::SetAllFieldsAndExtensions(&msg2);
  567. // Compare
  568. EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
  569. }
  570. TEST(MessageDifferencerTest, BasicFieldOrderingInequalityTest) {
  571. // Create the testing protos
  572. unittest::TestFieldOrderings msg1;
  573. unittest::TestFieldOrderings msg2;
  574. TestUtil::SetAllFieldsAndExtensions(&msg1);
  575. TestUtil::SetAllFieldsAndExtensions(&msg2);
  576. msg1.set_my_float(15.00);
  577. msg2.set_my_float(16.00);
  578. // Compare
  579. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  580. }
  581. TEST(MessageDifferencerTest, BasicExtensionTest) {
  582. // Create the testing protos
  583. unittest::TestAllExtensions msg1;
  584. unittest::TestAllExtensions msg2;
  585. TestUtil::SetAllExtensions(&msg1);
  586. TestUtil::SetAllExtensions(&msg2);
  587. // Compare
  588. EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
  589. }
  590. TEST(MessageDifferencerTest, BasicExtensionInequalityTest) {
  591. // Create the testing protos
  592. unittest::TestAllExtensions msg1;
  593. unittest::TestAllExtensions msg2;
  594. TestUtil::SetAllExtensions(&msg1);
  595. TestUtil::SetAllExtensions(&msg2);
  596. msg1.SetExtension(unittest::optional_int32_extension, 101);
  597. msg2.SetExtension(unittest::optional_int32_extension, 102);
  598. // Compare
  599. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  600. }
  601. TEST(MessageDifferencerTest, OneofTest) {
  602. // Create the testing protos
  603. unittest::TestOneof2 msg1;
  604. unittest::TestOneof2 msg2;
  605. TestUtil::SetOneof1(&msg1);
  606. TestUtil::SetOneof1(&msg2);
  607. // Compare
  608. EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
  609. }
  610. TEST(MessageDifferencerTest, OneofInequalityTest) {
  611. // Create the testing protos
  612. unittest::TestOneof2 msg1;
  613. unittest::TestOneof2 msg2;
  614. TestUtil::SetOneof1(&msg1);
  615. TestUtil::SetOneof2(&msg2);
  616. // Compare
  617. EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
  618. }
  619. TEST(MessageDifferencerTest, UnknownFieldPartialEqualTest) {
  620. unittest::TestEmptyMessage empty1;
  621. unittest::TestEmptyMessage empty2;
  622. UnknownFieldSet* unknown1 = empty1.mutable_unknown_fields();
  623. UnknownFieldSet* unknown2 = empty2.mutable_unknown_fields();
  624. unknown1->AddVarint(243, 122);
  625. unknown1->AddLengthDelimited(245, "abc");
  626. unknown1->AddGroup(246)->AddFixed32(248, 1);
  627. unknown1->mutable_field(2)->mutable_group()->AddFixed32(248, 2);
  628. unknown2->AddVarint(243, 122);
  629. unknown2->AddLengthDelimited(245, "abc");
  630. unknown2->AddGroup(246)->AddFixed32(248, 1);
  631. unknown2->mutable_field(2)->mutable_group()->AddFixed32(248, 2);
  632. util::MessageDifferencer differencer;
  633. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  634. EXPECT_TRUE(differencer.Compare(empty1, empty2));
  635. }
  636. TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllTest) {
  637. unittest::TestAllTypes msg1;
  638. unittest::TestAllTypes msg2;
  639. TestUtil::SetAllFields(&msg1);
  640. TestUtil::SetAllFields(&msg2);
  641. std::vector<const FieldDescriptor*> fields1;
  642. std::vector<const FieldDescriptor*> fields2;
  643. msg1.GetReflection()->ListFields(msg1, &fields1);
  644. msg2.GetReflection()->ListFields(msg2, &fields2);
  645. util::MessageDifferencer differencer;
  646. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
  647. }
  648. TEST(MessageDifferencerTest, SpecifiedFieldsInequalityAllTest) {
  649. unittest::TestAllTypes msg1;
  650. unittest::TestAllTypes msg2;
  651. TestUtil::SetAllFields(&msg1);
  652. std::vector<const FieldDescriptor*> fields1;
  653. std::vector<const FieldDescriptor*> fields2;
  654. msg1.GetReflection()->ListFields(msg1, &fields1);
  655. msg2.GetReflection()->ListFields(msg2, &fields2);
  656. util::MessageDifferencer differencer;
  657. EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
  658. }
  659. TEST(MessageDifferencerTest, SpecifiedFieldsEmptyListAlwaysSucceeds) {
  660. unittest::TestAllTypes msg1;
  661. unittest::TestAllTypes msg2;
  662. TestUtil::SetAllFields(&msg1);
  663. std::vector<const FieldDescriptor*> empty_fields;
  664. util::MessageDifferencer differencer;
  665. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
  666. empty_fields, empty_fields));
  667. TestUtil::SetAllFields(&msg2);
  668. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
  669. empty_fields, empty_fields));
  670. }
  671. TEST(MessageDifferencerTest, SpecifiedFieldsCompareWithSelf) {
  672. unittest::TestAllTypes msg1;
  673. TestUtil::SetAllFields(&msg1);
  674. std::vector<const FieldDescriptor*> fields;
  675. msg1.GetReflection()->ListFields(msg1, &fields);
  676. util::MessageDifferencer differencer;
  677. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, fields, fields));
  678. {
  679. // Compare with a subset of fields.
  680. std::vector<const FieldDescriptor*> compare_fields;
  681. for (int i = 0; i < fields.size(); ++i) {
  682. if (i % 2 == 0) {
  683. compare_fields.push_back(fields[i]);
  684. }
  685. }
  686. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1,
  687. compare_fields, compare_fields));
  688. }
  689. {
  690. // Specify a different set of fields to compare, even though we're using the
  691. // same message. This should fail, since we are explicitly saying that the
  692. // set of fields are different.
  693. std::vector<const FieldDescriptor*> compare_fields1;
  694. std::vector<const FieldDescriptor*> compare_fields2;
  695. for (int i = 0; i < fields.size(); ++i) {
  696. if (i % 2 == 0) {
  697. compare_fields1.push_back(fields[i]);
  698. } else {
  699. compare_fields2.push_back(fields[i]);
  700. }
  701. }
  702. EXPECT_FALSE(differencer.CompareWithFields(
  703. msg1, msg1, compare_fields1, compare_fields2));
  704. }
  705. }
  706. TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllShuffledTest) {
  707. // This is a public function, so make sure there are no assumptions about the
  708. // list of fields. Randomly shuffle them to make sure that they are properly
  709. // ordered for comparison.
  710. unittest::TestAllTypes msg1;
  711. unittest::TestAllTypes msg2;
  712. TestUtil::SetAllFields(&msg1);
  713. TestUtil::SetAllFields(&msg2);
  714. std::vector<const FieldDescriptor*> fields1;
  715. std::vector<const FieldDescriptor*> fields2;
  716. msg1.GetReflection()->ListFields(msg1, &fields1);
  717. msg2.GetReflection()->ListFields(msg2, &fields2);
  718. std::random_shuffle(fields1.begin(), fields1.end());
  719. std::random_shuffle(fields2.begin(), fields2.end());
  720. util::MessageDifferencer differencer;
  721. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
  722. }
  723. TEST(MessageDifferencerTest, SpecifiedFieldsSubsetEqualityTest) {
  724. // Specify a set of fields to compare. All the fields are equal.
  725. unittest::TestAllTypes msg1;
  726. unittest::TestAllTypes msg2;
  727. TestUtil::SetAllFields(&msg1);
  728. TestUtil::SetAllFields(&msg2);
  729. std::vector<const FieldDescriptor*> fields1;
  730. msg1.GetReflection()->ListFields(msg1, &fields1);
  731. std::vector<const FieldDescriptor*> compare_fields;
  732. // Only compare the field descriptors with even indices.
  733. for (int i = 0; i < fields1.size(); ++i) {
  734. if (i % 2 == 0) {
  735. compare_fields.push_back(fields1[i]);
  736. }
  737. }
  738. util::MessageDifferencer differencer;
  739. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
  740. compare_fields, compare_fields));
  741. }
  742. TEST(MessageDifferencerTest,
  743. SpecifiedFieldsSubsetIgnoresOtherFieldDifferencesTest) {
  744. // Specify a set of fields to compare, but clear all the other fields in one
  745. // of the messages. This should fail a regular compare, but CompareWithFields
  746. // should succeed.
  747. unittest::TestAllTypes msg1;
  748. unittest::TestAllTypes msg2;
  749. TestUtil::SetAllFields(&msg1);
  750. TestUtil::SetAllFields(&msg2);
  751. std::vector<const FieldDescriptor*> fields1;
  752. const Reflection* reflection = msg1.GetReflection();
  753. reflection->ListFields(msg1, &fields1);
  754. std::vector<const FieldDescriptor*> compare_fields;
  755. // Only compare the field descriptors with even indices.
  756. for (int i = 0; i < fields1.size(); ++i) {
  757. if (i % 2 == 0) {
  758. compare_fields.push_back(fields1[i]);
  759. } else {
  760. reflection->ClearField(&msg2, fields1[i]);
  761. }
  762. }
  763. util::MessageDifferencer differencer;
  764. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  765. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
  766. compare_fields, compare_fields));
  767. }
  768. TEST(MessageDifferencerTest, SpecifiedFieldsDetectsDifferencesTest) {
  769. // Change all of the repeated fields in one of the messages, and use only
  770. // those fields for comparison.
  771. unittest::TestAllTypes msg1;
  772. unittest::TestAllTypes msg2;
  773. TestUtil::SetAllFields(&msg1);
  774. TestUtil::SetAllFields(&msg2);
  775. TestUtil::ModifyRepeatedFields(&msg2);
  776. std::vector<const FieldDescriptor*> fields1;
  777. msg1.GetReflection()->ListFields(msg1, &fields1);
  778. std::vector<const FieldDescriptor*> compare_fields;
  779. // Only compare the repeated field descriptors.
  780. for (int i = 0; i < fields1.size(); ++i) {
  781. if (fields1[i]->is_repeated()) {
  782. compare_fields.push_back(fields1[i]);
  783. }
  784. }
  785. util::MessageDifferencer differencer;
  786. EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2,
  787. compare_fields, compare_fields));
  788. }
  789. TEST(MessageDifferencerTest, SpecifiedFieldsEquivalenceAllTest) {
  790. unittest::TestAllTypes msg1;
  791. unittest::TestAllTypes msg2;
  792. TestUtil::SetAllFields(&msg1);
  793. TestUtil::SetAllFields(&msg2);
  794. std::vector<const FieldDescriptor*> fields1;
  795. std::vector<const FieldDescriptor*> fields2;
  796. msg1.GetReflection()->ListFields(msg1, &fields1);
  797. msg2.GetReflection()->ListFields(msg2, &fields2);
  798. util::MessageDifferencer differencer;
  799. differencer.set_message_field_comparison(
  800. util::MessageDifferencer::EQUIVALENT);
  801. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
  802. }
  803. TEST(MessageDifferencerTest,
  804. SpecifiedFieldsEquivalenceIgnoresOtherFieldDifferencesTest) {
  805. unittest::TestAllTypes msg1;
  806. unittest::TestAllTypes msg2;
  807. const Descriptor* desc = msg1.GetDescriptor();
  808. const FieldDescriptor* optional_int32_desc =
  809. desc->FindFieldByName("optional_int32");
  810. const FieldDescriptor* optional_int64_desc =
  811. desc->FindFieldByName("optional_int64");
  812. const FieldDescriptor* default_int64_desc =
  813. desc->FindFieldByName("default_int64");
  814. ASSERT_TRUE(optional_int32_desc != NULL);
  815. ASSERT_TRUE(optional_int64_desc != NULL);
  816. ASSERT_TRUE(default_int64_desc != NULL);
  817. msg1.set_optional_int32(0);
  818. msg2.set_optional_int64(0);
  819. msg1.set_default_int64(default_int64_desc->default_value_int64());
  820. // Set a field to a non-default value so we know that field selection is
  821. // actually doing something.
  822. msg2.set_optional_uint64(23);
  823. std::vector<const FieldDescriptor*> fields1;
  824. std::vector<const FieldDescriptor*> fields2;
  825. fields1.push_back(optional_int32_desc);
  826. fields1.push_back(default_int64_desc);
  827. fields2.push_back(optional_int64_desc);
  828. util::MessageDifferencer differencer;
  829. EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
  830. differencer.set_message_field_comparison(
  831. util::MessageDifferencer::EQUIVALENT);
  832. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  833. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
  834. }
  835. TEST(MessageDifferencerTest, RepeatedFieldSetTest_SetOfSet) {
  836. // Create the testing protos
  837. protobuf_unittest::TestDiffMessage msg1;
  838. protobuf_unittest::TestDiffMessage msg2;
  839. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  840. item->add_ra(1); item->add_ra(2); item->add_ra(3);
  841. item = msg1.add_item();
  842. item->add_ra(5); item->add_ra(6);
  843. item = msg1.add_item();
  844. item->add_ra(1); item->add_ra(3);
  845. item = msg1.add_item();
  846. item->add_ra(6); item->add_ra(7); item->add_ra(8);
  847. item = msg2.add_item();
  848. item->add_ra(6); item->add_ra(5);
  849. item = msg2.add_item();
  850. item->add_ra(6); item->add_ra(8); item->add_ra(7);
  851. item = msg2.add_item();
  852. item->add_ra(1); item->add_ra(3);
  853. item = msg2.add_item();
  854. item->add_ra(3); item->add_ra(2); item->add_ra(1);
  855. // Compare
  856. util::MessageDifferencer differencer;
  857. differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
  858. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  859. }
  860. TEST(MessageDifferencerTest, RepeatedFieldSetTest_Combination) {
  861. // Create the testing protos
  862. protobuf_unittest::TestDiffMessage msg1;
  863. protobuf_unittest::TestDiffMessage msg2;
  864. // Treat "item" as Map, with key = "a"
  865. // Treat "item.ra" also as Set
  866. // Treat "rv" as Set
  867. // Treat "rw" as List
  868. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  869. item->set_a(3);
  870. item->add_ra(1); item->add_ra(2); item->add_ra(3);
  871. item = msg1.add_item();
  872. item->set_a(4);
  873. item->add_ra(5); item->add_ra(6);
  874. item = msg1.add_item();
  875. item->set_a(1);
  876. item->add_ra(1); item->add_ra(3);
  877. item = msg1.add_item();
  878. item->set_a(2);
  879. item->add_ra(6); item->add_ra(7); item->add_ra(8);
  880. item = msg2.add_item();
  881. item->set_a(4);
  882. item->add_ra(6); item->add_ra(5);
  883. item = msg2.add_item();
  884. item->set_a(2);
  885. item->add_ra(6); item->add_ra(8); item->add_ra(7);
  886. item = msg2.add_item();
  887. item->set_a(1);
  888. item->add_ra(1); item->add_ra(3);
  889. item = msg2.add_item();
  890. item->set_a(3);
  891. item->add_ra(3); item->add_ra(2); item->add_ra(1);
  892. msg1.add_rv(3);
  893. msg1.add_rv(4);
  894. msg1.add_rv(7);
  895. msg1.add_rv(0);
  896. msg2.add_rv(4);
  897. msg2.add_rv(3);
  898. msg2.add_rv(0);
  899. msg2.add_rv(7);
  900. msg1.add_rw("nothing"); msg2.add_rw("nothing");
  901. msg1.add_rw("should"); msg2.add_rw("should");
  902. msg1.add_rw("change"); msg2.add_rw("change");
  903. // Compare
  904. util::MessageDifferencer differencer1;
  905. differencer1.TreatAsMap(msg1.GetDescriptor()->FindFieldByName("item"),
  906. item->GetDescriptor()->FindFieldByName("a"));
  907. differencer1.TreatAsSet(msg1.GetDescriptor()->FindFieldByName("rv"));
  908. differencer1.TreatAsSet(item->GetDescriptor()->FindFieldByName("ra"));
  909. EXPECT_TRUE(differencer1.Compare(msg1, msg2));
  910. util::MessageDifferencer differencer2;
  911. differencer2.TreatAsMap(msg1.GetDescriptor()->FindFieldByName("item"),
  912. item->GetDescriptor()->FindFieldByName("a"));
  913. differencer2.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
  914. differencer2.TreatAsList(msg1.GetDescriptor()->FindFieldByName("rw"));
  915. EXPECT_TRUE(differencer2.Compare(msg1, msg2));
  916. }
  917. TEST(MessageDifferencerTest, RepeatedFieldMapTest_Partial) {
  918. protobuf_unittest::TestDiffMessage msg1;
  919. // message msg1 {
  920. // item { a: 1; b: "11" }
  921. // }
  922. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  923. item->set_a(1);
  924. item->set_b("11");
  925. protobuf_unittest::TestDiffMessage msg2;
  926. // message msg2 {
  927. // item { a: 2; b: "22" }
  928. // item { a: 1; b: "11" }
  929. // }
  930. item = msg2.add_item();
  931. item->set_a(2);
  932. item->set_b("22");
  933. item = msg2.add_item();
  934. item->set_a(1);
  935. item->set_b("11");
  936. // Compare
  937. util::MessageDifferencer differencer;
  938. differencer.TreatAsMap(GetFieldDescriptor(msg1, "item"),
  939. GetFieldDescriptor(msg1, "item.a"));
  940. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  941. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  942. }
  943. TEST(MessageDifferencerTest, RepeatedFieldSetTest_Duplicates) {
  944. protobuf_unittest::TestDiffMessage a, b, c;
  945. // message a: {
  946. // rv: 0
  947. // rv: 1
  948. // rv: 0
  949. // }
  950. a.add_rv(0);
  951. a.add_rv(1);
  952. a.add_rv(0);
  953. // message b: {
  954. // rv: 0
  955. // rv: 0
  956. // rv: 1
  957. // }
  958. b.add_rv(0);
  959. b.add_rv(0);
  960. b.add_rv(1);
  961. // message c: {
  962. // rv: 0
  963. // rv: 1
  964. // }
  965. c.add_rv(0);
  966. c.add_rv(1);
  967. util::MessageDifferencer differencer;
  968. differencer.TreatAsSet(GetFieldDescriptor(a, "rv"));
  969. EXPECT_TRUE(differencer.Compare(b, a));
  970. EXPECT_FALSE(differencer.Compare(c, a));
  971. util::MessageDifferencer differencer1;
  972. differencer1.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
  973. EXPECT_TRUE(differencer1.Compare(b, a));
  974. EXPECT_FALSE(differencer1.Compare(c, a));
  975. }
  976. TEST(MessageDifferencerTest, RepeatedFieldSetTest_PartialSimple) {
  977. protobuf_unittest::TestDiffMessage a, b, c;
  978. // message a: {
  979. // rm { c: 1 }
  980. // rm { c: 0 }
  981. // }
  982. a.add_rm()->set_c(1);
  983. a.add_rm()->set_c(0);
  984. // message b: {
  985. // rm { c: 1 }
  986. // rm {}
  987. // }
  988. b.add_rm()->set_c(1);
  989. b.add_rm();
  990. // message c: {
  991. // rm {}
  992. // rm { c: 1 }
  993. // }
  994. c.add_rm();
  995. c.add_rm()->set_c(1);
  996. util::MessageDifferencer differencer;
  997. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  998. differencer.TreatAsSet(GetFieldDescriptor(a, "rm"));
  999. EXPECT_TRUE(differencer.Compare(b, a));
  1000. EXPECT_TRUE(differencer.Compare(c, a));
  1001. }
  1002. TEST(MessageDifferencerTest, RepeatedFieldSetTest_Partial) {
  1003. protobuf_unittest::TestDiffMessage msg1, msg2;
  1004. // message msg1: {
  1005. // rm { a: 1 }
  1006. // rm { b: 2 }
  1007. // rm { c: 3 }
  1008. // }
  1009. msg1.add_rm()->set_a(1);
  1010. msg1.add_rm()->set_b(2);
  1011. msg1.add_rm()->set_c(3);
  1012. // message msg2: {
  1013. // rm { a: 1; c: 3 }
  1014. // rm { b: 2; c: 3 }
  1015. // rm { b: 2 }
  1016. // }
  1017. protobuf_unittest::TestField* field = msg2.add_rm();
  1018. field->set_a(1);
  1019. field->set_c(3);
  1020. field = msg2.add_rm();
  1021. field->set_b(2);
  1022. field->set_c(3);
  1023. field = msg2.add_rm();
  1024. field->set_b(2);
  1025. util::MessageDifferencer differencer;
  1026. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  1027. differencer.TreatAsSet(GetFieldDescriptor(msg1, "rm"));
  1028. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  1029. }
  1030. TEST(MessageDifferencerTest, RepeatedFieldMapTest_MultipleFieldsAsKey) {
  1031. protobuf_unittest::TestDiffMessage msg1;
  1032. protobuf_unittest::TestDiffMessage msg2;
  1033. // Treat "item" as Map, with key = ("a", "ra")
  1034. // Treat "item.ra" as Set
  1035. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1036. // key => value: (1, {2, 3}) => "a"
  1037. item->set_a(1);
  1038. item->add_ra(2);
  1039. item->add_ra(3);
  1040. item->set_b("a");
  1041. item = msg1.add_item();
  1042. // key => value: (2, {1, 3}) => "b"
  1043. item->set_a(2);
  1044. item->add_ra(1);
  1045. item->add_ra(3);
  1046. item->set_b("b");
  1047. item = msg1.add_item();
  1048. // key => value: (1, {1, 3}) => "c"
  1049. item->set_a(1);
  1050. item->add_ra(1);
  1051. item->add_ra(3);
  1052. item->set_b("c");
  1053. item = msg2.add_item();
  1054. // key => value: (1, {1, 3}) => "c"
  1055. item->set_a(1);
  1056. item->add_ra(3);
  1057. item->add_ra(1);
  1058. item->set_b("c");
  1059. item = msg2.add_item();
  1060. // key => value: (1, {2, 3}) => "a"
  1061. item->set_a(1);
  1062. item->add_ra(3);
  1063. item->add_ra(2);
  1064. item->set_b("a");
  1065. item = msg2.add_item();
  1066. // key => value: (2, {1, 3}) => "b"
  1067. item->set_a(2);
  1068. item->add_ra(3);
  1069. item->add_ra(1);
  1070. item->set_b("b");
  1071. // Compare
  1072. util::MessageDifferencer differencer;
  1073. differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.ra"));
  1074. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1075. std::vector<const FieldDescriptor*> key_fields;
  1076. key_fields.push_back(GetFieldDescriptor(msg1, "item.a"));
  1077. key_fields.push_back(GetFieldDescriptor(msg1, "item.ra"));
  1078. differencer.TreatAsMapWithMultipleFieldsAsKey(
  1079. GetFieldDescriptor(msg1, "item"), key_fields);
  1080. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  1081. // Introduce some differences.
  1082. msg1.clear_item();
  1083. msg2.clear_item();
  1084. item = msg1.add_item();
  1085. item->set_a(4);
  1086. item->add_ra(5);
  1087. item->add_ra(6);
  1088. item->set_b("hello");
  1089. item = msg2.add_item();
  1090. item->set_a(4);
  1091. item->add_ra(6);
  1092. item->add_ra(5);
  1093. item->set_b("world");
  1094. string output;
  1095. differencer.ReportDifferencesToString(&output);
  1096. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1097. EXPECT_EQ(
  1098. "moved: item[0].ra[0] -> item[0].ra[1] : 5\n"
  1099. "moved: item[0].ra[1] -> item[0].ra[0] : 6\n"
  1100. "modified: item[0].b: \"hello\" -> \"world\"\n",
  1101. output);
  1102. }
  1103. TEST(MessageDifferencerTest, RepeatedFieldMapTest_MultipleFieldPathsAsKey) {
  1104. protobuf_unittest::TestDiffMessage msg1;
  1105. protobuf_unittest::TestDiffMessage msg2;
  1106. // Treat "item" as Map, with key = ("m.a", "m.rc")
  1107. // Treat "item.m.rc" as Set
  1108. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1109. // key => value: (1, {2, 3}) => "a"
  1110. item->mutable_m()->set_a(1);
  1111. item->mutable_m()->add_rc(2);
  1112. item->mutable_m()->add_rc(3);
  1113. item->set_b("a");
  1114. item = msg1.add_item();
  1115. // key => value: (2, {1, 3}) => "b"
  1116. item->mutable_m()->set_a(2);
  1117. item->mutable_m()->add_rc(1);
  1118. item->mutable_m()->add_rc(3);
  1119. item->set_b("b");
  1120. item = msg1.add_item();
  1121. // key => value: (1, {1, 3}) => "c"
  1122. item->mutable_m()->set_a(1);
  1123. item->mutable_m()->add_rc(1);
  1124. item->mutable_m()->add_rc(3);
  1125. item->set_b("c");
  1126. item = msg2.add_item();
  1127. // key => value: (1, {1, 3}) => "c"
  1128. item->mutable_m()->set_a(1);
  1129. item->mutable_m()->add_rc(3);
  1130. item->mutable_m()->add_rc(1);
  1131. item->set_b("c");
  1132. item = msg2.add_item();
  1133. // key => value: (1, {2, 3}) => "a"
  1134. item->mutable_m()->set_a(1);
  1135. item->mutable_m()->add_rc(3);
  1136. item->mutable_m()->add_rc(2);
  1137. item->set_b("a");
  1138. item = msg2.add_item();
  1139. // key => value: (2, {1, 3}) => "b"
  1140. item->mutable_m()->set_a(2);
  1141. item->mutable_m()->add_rc(3);
  1142. item->mutable_m()->add_rc(1);
  1143. item->set_b("b");
  1144. // Compare
  1145. util::MessageDifferencer differencer;
  1146. differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.m.rc"));
  1147. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1148. std::vector<std::vector<const FieldDescriptor*> > key_field_paths;
  1149. std::vector<const FieldDescriptor*> key_field_path1;
  1150. key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m"));
  1151. key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m.a"));
  1152. std::vector<const FieldDescriptor*> key_field_path2;
  1153. key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m"));
  1154. key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m.rc"));
  1155. key_field_paths.push_back(key_field_path1);
  1156. key_field_paths.push_back(key_field_path2);
  1157. differencer.TreatAsMapWithMultipleFieldPathsAsKey(
  1158. GetFieldDescriptor(msg1, "item"), key_field_paths);
  1159. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  1160. // Introduce some differences.
  1161. msg1.clear_item();
  1162. msg2.clear_item();
  1163. item = msg1.add_item();
  1164. item->mutable_m()->set_a(4);
  1165. item->mutable_m()->add_rc(5);
  1166. item->mutable_m()->add_rc(6);
  1167. item->set_b("hello");
  1168. item = msg2.add_item();
  1169. item->mutable_m()->set_a(4);
  1170. item->mutable_m()->add_rc(6);
  1171. item->mutable_m()->add_rc(5);
  1172. item->set_b("world");
  1173. string output;
  1174. differencer.ReportDifferencesToString(&output);
  1175. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1176. EXPECT_EQ(
  1177. "modified: item[0].b: \"hello\" -> \"world\"\n"
  1178. "moved: item[0].m.rc[0] -> item[0].m.rc[1] : 5\n"
  1179. "moved: item[0].m.rc[1] -> item[0].m.rc[0] : 6\n",
  1180. output);
  1181. }
  1182. TEST(MessageDifferencerTest, RepeatedFieldMapTest_IgnoredKeyFields) {
  1183. protobuf_unittest::TestDiffMessage msg1;
  1184. protobuf_unittest::TestDiffMessage msg2;
  1185. // Treat "item" as Map, with key = ("a", "ra")
  1186. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1187. item->set_a(1);
  1188. item->add_ra(2);
  1189. item->set_b("hello");
  1190. item = msg2.add_item();
  1191. item->set_a(1);
  1192. item->add_ra(3);
  1193. item->set_b("world");
  1194. // Compare
  1195. util::MessageDifferencer differencer;
  1196. std::vector<const FieldDescriptor*> key_fields;
  1197. key_fields.push_back(GetFieldDescriptor(msg1, "item.a"));
  1198. key_fields.push_back(GetFieldDescriptor(msg1, "item.ra"));
  1199. differencer.TreatAsMapWithMultipleFieldsAsKey(
  1200. GetFieldDescriptor(msg1, "item"), key_fields);
  1201. string output;
  1202. differencer.ReportDifferencesToString(&output);
  1203. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1204. EXPECT_EQ(
  1205. "added: item[0]: { a: 1 ra: 3 b: \"world\" }\n"
  1206. "deleted: item[0]: { a: 1 ra: 2 b: \"hello\" }\n",
  1207. output);
  1208. // Ignored fields that are listed as parts of the key are still used
  1209. // in key comparison, but they're not used in value comparison.
  1210. differencer.IgnoreField(GetFieldDescriptor(msg1, "item.ra"));
  1211. output.clear();
  1212. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1213. EXPECT_EQ(
  1214. "added: item[0]: { a: 1 ra: 3 b: \"world\" }\n"
  1215. "deleted: item[0]: { a: 1 ra: 2 b: \"hello\" }\n",
  1216. output);
  1217. // Ignoring a field in the key is different from treating the left fields
  1218. // as key. That is:
  1219. // (key = ("a", "ra") && ignore "ra") != (key = ("a") && ignore "ra")
  1220. util::MessageDifferencer differencer2;
  1221. differencer2.TreatAsMap(GetFieldDescriptor(msg1, "item"),
  1222. GetFieldDescriptor(msg1, "item.a"));
  1223. differencer2.IgnoreField(GetFieldDescriptor(msg1, "item.ra"));
  1224. output.clear();
  1225. differencer2.ReportDifferencesToString(&output);
  1226. EXPECT_FALSE(differencer2.Compare(msg1, msg2));
  1227. EXPECT_EQ(
  1228. "ignored: item[0].ra\n"
  1229. "modified: item[0].b: \"hello\" -> \"world\"\n",
  1230. output);
  1231. }
  1232. static const char* const kIgnoredFields[] = {"rm.b", "rm.m.b"};
  1233. class TestIgnorer : public util::MessageDifferencer::IgnoreCriteria {
  1234. public:
  1235. virtual bool IsIgnored(
  1236. const Message& message1, const Message& message2,
  1237. const FieldDescriptor* field,
  1238. const std::vector<util::MessageDifferencer::SpecificField>&
  1239. parent_fields) {
  1240. string name = "";
  1241. for (int i = 0; i < parent_fields.size(); ++i) {
  1242. name += parent_fields[i].field->name() + ".";
  1243. }
  1244. name += field->name();
  1245. for (int i = 0; i < GOOGLE_ARRAYSIZE(kIgnoredFields); ++i) {
  1246. if (name.compare(kIgnoredFields[i]) == 0) {
  1247. return true;
  1248. }
  1249. }
  1250. return false;
  1251. }
  1252. };
  1253. TEST(MessageDifferencerTest, TreatRepeatedFieldAsSetWithIgnoredFields) {
  1254. protobuf_unittest::TestDiffMessage msg1;
  1255. protobuf_unittest::TestDiffMessage msg2;
  1256. TextFormat::MergeFromString("rm { a: 11\n b: 12 }", &msg1);
  1257. TextFormat::MergeFromString("rm { a: 11\n b: 13 }", &msg2);
  1258. util::MessageDifferencer differ;
  1259. differ.TreatAsSet(GetFieldDescriptor(msg1, "rm"));
  1260. differ.AddIgnoreCriteria(new TestIgnorer);
  1261. EXPECT_TRUE(differ.Compare(msg1, msg2));
  1262. }
  1263. TEST(MessageDifferencerTest, TreatRepeatedFieldAsMapWithIgnoredKeyFields) {
  1264. protobuf_unittest::TestDiffMessage msg1;
  1265. protobuf_unittest::TestDiffMessage msg2;
  1266. TextFormat::MergeFromString("rm { a: 11\n m { a: 12\n b: 13\n } }", &msg1);
  1267. TextFormat::MergeFromString("rm { a: 11\n m { a: 12\n b: 14\n } }", &msg2);
  1268. util::MessageDifferencer differ;
  1269. differ.TreatAsMap(GetFieldDescriptor(msg1, "rm"),
  1270. GetFieldDescriptor(msg1, "rm.m"));
  1271. differ.AddIgnoreCriteria(new TestIgnorer);
  1272. EXPECT_TRUE(differ.Compare(msg1, msg2));
  1273. }
  1274. // Takes the product of all elements of item.ra as the key for key comparison.
  1275. class ValueProductMapKeyComparator
  1276. : public util::MessageDifferencer::MapKeyComparator {
  1277. public:
  1278. typedef util::MessageDifferencer::SpecificField SpecificField;
  1279. virtual bool IsMatch(
  1280. const Message &message1, const Message &message2,
  1281. const std::vector<SpecificField>& parent_fields) const {
  1282. const Reflection* reflection1 = message1.GetReflection();
  1283. const Reflection* reflection2 = message2.GetReflection();
  1284. // FieldDescriptor for item.ra
  1285. const FieldDescriptor* ra_field =
  1286. message1.GetDescriptor()->FindFieldByName("ra");
  1287. // Get the product of all elements in item.ra
  1288. int result1 = 1, result2 = 1;
  1289. for (int i = 0; i < reflection1->FieldSize(message1, ra_field); ++i) {
  1290. result1 *= reflection1->GetRepeatedInt32(message1, ra_field, i);
  1291. }
  1292. for (int i = 0; i < reflection2->FieldSize(message2, ra_field); ++i) {
  1293. result2 *= reflection2->GetRepeatedInt32(message2, ra_field, i);
  1294. }
  1295. return result1 == result2;
  1296. }
  1297. };
  1298. TEST(MessageDifferencerTest, RepeatedFieldMapTest_CustomMapKeyComparator) {
  1299. protobuf_unittest::TestDiffMessage msg1;
  1300. protobuf_unittest::TestDiffMessage msg2;
  1301. // Treat "item" as Map, using custom key comparator to determine if two
  1302. // elements have the same key.
  1303. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1304. item->add_ra(6);
  1305. item->add_ra(35);
  1306. item->set_b("hello");
  1307. item = msg2.add_item();
  1308. item->add_ra(10);
  1309. item->add_ra(21);
  1310. item->set_b("hello");
  1311. util::MessageDifferencer differencer;
  1312. ValueProductMapKeyComparator key_comparator;
  1313. differencer.TreatAsMapUsingKeyComparator(
  1314. GetFieldDescriptor(msg1, "item"), &key_comparator);
  1315. string output;
  1316. differencer.ReportDifferencesToString(&output);
  1317. // Though the above two messages have different values for item.ra, they
  1318. // are regarded as having the same key because 6 * 35 == 10 * 21. That's
  1319. // how the key comparator determines if the two have the same key.
  1320. // However, in value comparison, all fields of the message are taken into
  1321. // consideration, so they are different because their item.ra fields have
  1322. // different values using normal value comparison.
  1323. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1324. EXPECT_EQ(
  1325. "modified: item[0].ra[0]: 6 -> 10\n"
  1326. "modified: item[0].ra[1]: 35 -> 21\n",
  1327. output);
  1328. differencer.IgnoreField(GetFieldDescriptor(msg1, "item.ra"));
  1329. output.clear();
  1330. // item.ra is ignored in value comparison, so the two messages equal.
  1331. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  1332. EXPECT_EQ("ignored: item[0].ra\n", output);
  1333. }
  1334. // Compares fields by their index offset by one, so index 0 matches with 1, etc.
  1335. class OffsetByOneMapKeyComparator
  1336. : public util::MessageDifferencer::MapKeyComparator {
  1337. public:
  1338. typedef util::MessageDifferencer::SpecificField SpecificField;
  1339. virtual bool IsMatch(const Message& message1, const Message& message2,
  1340. const std::vector<SpecificField>& parent_fields) const {
  1341. return parent_fields.back().index + 1 == parent_fields.back().new_index;
  1342. }
  1343. };
  1344. TEST(MessageDifferencerTest, RepeatedFieldMapTest_CustomIndexMapKeyComparator) {
  1345. protobuf_unittest::TestDiffMessage msg1;
  1346. protobuf_unittest::TestDiffMessage msg2;
  1347. // Treat "item" as Map, using custom key comparator to determine if two
  1348. // elements have the same key.
  1349. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1350. item->set_b("one");
  1351. item = msg2.add_item();
  1352. item->set_b("zero");
  1353. item = msg2.add_item();
  1354. item->set_b("one");
  1355. util::MessageDifferencer differencer;
  1356. OffsetByOneMapKeyComparator key_comparator;
  1357. differencer.TreatAsMapUsingKeyComparator(GetFieldDescriptor(msg1, "item"),
  1358. &key_comparator);
  1359. string output;
  1360. differencer.ReportDifferencesToString(&output);
  1361. // With the offset by one comparator msg1.item[0] should be compared to
  1362. // msg2.item[1] and thus be moved, msg2.item[0] should be marked as added.
  1363. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1364. EXPECT_EQ(
  1365. "moved: item[0] -> item[1] : { b: \"one\" }\n"
  1366. "added: item[0]: { b: \"zero\" }\n",
  1367. output);
  1368. }
  1369. TEST(MessageDifferencerTest, RepeatedFieldSetTest_Subset) {
  1370. protobuf_unittest::TestDiffMessage msg1;
  1371. protobuf_unittest::TestDiffMessage msg2;
  1372. msg1.add_rv(3);
  1373. msg1.add_rv(8);
  1374. msg1.add_rv(2);
  1375. msg2.add_rv(2);
  1376. msg2.add_rv(3);
  1377. msg2.add_rv(5);
  1378. msg2.add_rv(8);
  1379. util::MessageDifferencer differencer;
  1380. // Fail with only partial scope set.
  1381. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  1382. differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_LIST);
  1383. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1384. // Fail with only set-like comparison set.
  1385. differencer.set_scope(util::MessageDifferencer::FULL);
  1386. differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
  1387. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1388. // Succeed with scope and repeated field comparison set properly.
  1389. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  1390. differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
  1391. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  1392. }
  1393. TEST(MessageDifferencerTest, IgnoreField_Single) {
  1394. protobuf_unittest::TestField msg1;
  1395. protobuf_unittest::TestField msg2;
  1396. msg1.set_c(3);
  1397. msg1.add_rc(1);
  1398. msg2.set_c(5);
  1399. msg2.add_rc(1);
  1400. util::MessageDifferencer differencer;
  1401. differencer.IgnoreField(GetFieldDescriptor(msg1, "c"));
  1402. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1403. }
  1404. TEST(MessageDifferencerTest, IgnoreField_Repeated) {
  1405. protobuf_unittest::TestField msg1;
  1406. protobuf_unittest::TestField msg2;
  1407. msg1.set_c(3);
  1408. msg1.add_rc(1);
  1409. msg1.add_rc(2);
  1410. msg2.set_c(3);
  1411. msg2.add_rc(1);
  1412. msg2.add_rc(3);
  1413. util::MessageDifferencer differencer;
  1414. differencer.IgnoreField(GetFieldDescriptor(msg1, "rc"));
  1415. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1416. }
  1417. TEST(MessageDifferencerTest, IgnoreField_Message) {
  1418. protobuf_unittest::TestDiffMessage msg1;
  1419. protobuf_unittest::TestDiffMessage msg2;
  1420. protobuf_unittest::TestField* field;
  1421. field = msg1.add_rm();
  1422. field->set_c(3);
  1423. field = msg2.add_rm();
  1424. field->set_c(4);
  1425. util::MessageDifferencer differencer;
  1426. differencer.IgnoreField(GetFieldDescriptor(msg1, "rm"));
  1427. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1428. }
  1429. TEST(MessageDifferencerTest, IgnoreField_Group) {
  1430. protobuf_unittest::TestDiffMessage msg1;
  1431. protobuf_unittest::TestDiffMessage msg2;
  1432. protobuf_unittest::TestDiffMessage::Item* item;
  1433. item = msg1.add_item();
  1434. item->set_a(3);
  1435. item = msg2.add_item();
  1436. item->set_a(4);
  1437. util::MessageDifferencer differencer;
  1438. differencer.IgnoreField(GetFieldDescriptor(msg1, "item"));
  1439. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1440. }
  1441. TEST(MessageDifferencerTest, IgnoreField_Missing) {
  1442. protobuf_unittest::TestField msg1;
  1443. protobuf_unittest::TestField msg2;
  1444. msg1.set_c(3);
  1445. msg1.add_rc(1);
  1446. msg2.add_rc(1);
  1447. util::MessageDifferencer differencer;
  1448. differencer.IgnoreField(GetFieldDescriptor(msg1, "c"));
  1449. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1450. ExpectEqualsWithDifferencer(&differencer, msg2, msg1);
  1451. }
  1452. TEST(MessageDifferencerTest, IgnoreField_Multiple) {
  1453. protobuf_unittest::TestField msg1;
  1454. protobuf_unittest::TestField msg2;
  1455. msg1.set_c(3);
  1456. msg1.add_rc(1);
  1457. msg1.add_rc(2);
  1458. msg2.set_c(5);
  1459. msg2.add_rc(1);
  1460. msg2.add_rc(3);
  1461. const FieldDescriptor* c = GetFieldDescriptor(msg1, "c");
  1462. const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc");
  1463. { // Ignore c
  1464. util::MessageDifferencer differencer;
  1465. differencer.IgnoreField(c);
  1466. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1467. }
  1468. { // Ignore rc
  1469. util::MessageDifferencer differencer;
  1470. differencer.IgnoreField(rc);
  1471. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1472. }
  1473. { // Ignore both
  1474. util::MessageDifferencer differencer;
  1475. differencer.IgnoreField(c);
  1476. differencer.IgnoreField(rc);
  1477. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1478. }
  1479. }
  1480. TEST(MessageDifferencerTest, IgnoreField_NestedMessage) {
  1481. protobuf_unittest::TestDiffMessage msg1;
  1482. protobuf_unittest::TestDiffMessage msg2;
  1483. protobuf_unittest::TestField* field;
  1484. field = msg1.add_rm();
  1485. field->set_c(3);
  1486. field->add_rc(1);
  1487. field = msg2.add_rm();
  1488. field->set_c(4);
  1489. field->add_rc(1);
  1490. util::MessageDifferencer differencer;
  1491. differencer.IgnoreField(GetFieldDescriptor(msg1, "rm.c"));
  1492. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1493. }
  1494. TEST(MessageDifferencerTest, IgnoreField_NestedGroup) {
  1495. protobuf_unittest::TestDiffMessage msg1;
  1496. protobuf_unittest::TestDiffMessage msg2;
  1497. protobuf_unittest::TestDiffMessage::Item* item;
  1498. item = msg1.add_item();
  1499. item->set_a(3);
  1500. item->set_b("foo");
  1501. item = msg2.add_item();
  1502. item->set_a(4);
  1503. item->set_b("foo");
  1504. util::MessageDifferencer differencer;
  1505. differencer.IgnoreField(GetFieldDescriptor(msg1, "item.a"));
  1506. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1507. }
  1508. TEST(MessageDifferencerTest, IgnoreField_InsideSet) {
  1509. protobuf_unittest::TestDiffMessage msg1;
  1510. protobuf_unittest::TestDiffMessage msg2;
  1511. protobuf_unittest::TestDiffMessage::Item* item;
  1512. item = msg1.add_item();
  1513. item->set_a(1);
  1514. item->set_b("foo");
  1515. item->add_ra(1);
  1516. item = msg1.add_item();
  1517. item->set_a(2);
  1518. item->set_b("bar");
  1519. item->add_ra(2);
  1520. item = msg2.add_item();
  1521. item->set_a(2);
  1522. item->set_b("bar");
  1523. item->add_ra(2);
  1524. item = msg2.add_item();
  1525. item->set_a(1);
  1526. item->set_b("baz");
  1527. item->add_ra(1);
  1528. const FieldDescriptor* item_desc = GetFieldDescriptor(msg1, "item");
  1529. const FieldDescriptor* b = GetFieldDescriptor(msg1, "item.b");
  1530. util::MessageDifferencer differencer;
  1531. differencer.IgnoreField(b);
  1532. differencer.TreatAsSet(item_desc);
  1533. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1534. }
  1535. TEST(MessageDifferencerTest, IgnoreField_InsideMap) {
  1536. protobuf_unittest::TestDiffMessage msg1;
  1537. protobuf_unittest::TestDiffMessage msg2;
  1538. protobuf_unittest::TestDiffMessage::Item* item;
  1539. item = msg1.add_item();
  1540. item->set_a(1);
  1541. item->set_b("foo");
  1542. item->add_ra(1);
  1543. item = msg1.add_item();
  1544. item->set_a(2);
  1545. item->set_b("bar");
  1546. item->add_ra(2);
  1547. item = msg2.add_item();
  1548. item->set_a(2);
  1549. item->set_b("bar");
  1550. item->add_ra(2);
  1551. item = msg2.add_item();
  1552. item->set_a(1);
  1553. item->set_b("baz");
  1554. item->add_ra(1);
  1555. const FieldDescriptor* item_desc = GetFieldDescriptor(msg1, "item");
  1556. const FieldDescriptor* a = GetFieldDescriptor(msg1, "item.a");
  1557. const FieldDescriptor* b = GetFieldDescriptor(msg1, "item.b");
  1558. util::MessageDifferencer differencer;
  1559. differencer.IgnoreField(b);
  1560. differencer.TreatAsMap(item_desc, a);
  1561. ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
  1562. }
  1563. TEST(MessageDifferencerTest, IgnoreField_DoesNotIgnoreKey) {
  1564. protobuf_unittest::TestDiffMessage msg1;
  1565. protobuf_unittest::TestDiffMessage msg2;
  1566. protobuf_unittest::TestDiffMessage::Item* item;
  1567. item = msg1.add_item();
  1568. item->set_a(1);
  1569. item->set_b("foo");
  1570. item->add_ra(1);
  1571. item = msg2.add_item();
  1572. item->set_a(2);
  1573. item->set_b("foo");
  1574. item->add_ra(1);
  1575. const FieldDescriptor* item_desc = GetFieldDescriptor(msg1, "item");
  1576. const FieldDescriptor* a = GetFieldDescriptor(msg1, "item.a");
  1577. util::MessageDifferencer differencer;
  1578. differencer.IgnoreField(a);
  1579. differencer.TreatAsMap(item_desc, a);
  1580. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1581. }
  1582. TEST(MessageDifferencerTest, IgnoreField_TrumpsCompareWithFields) {
  1583. protobuf_unittest::TestField msg1;
  1584. protobuf_unittest::TestField msg2;
  1585. msg1.set_c(3);
  1586. msg1.add_rc(1);
  1587. msg1.add_rc(2);
  1588. msg2.set_c(3);
  1589. msg2.add_rc(1);
  1590. msg2.add_rc(3);
  1591. const FieldDescriptor* c = GetFieldDescriptor(msg1, "c");
  1592. const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc");
  1593. std::vector<const FieldDescriptor*> fields;
  1594. fields.push_back(c);
  1595. fields.push_back(rc);
  1596. util::MessageDifferencer differencer;
  1597. differencer.IgnoreField(rc);
  1598. differencer.set_scope(util::MessageDifferencer::FULL);
  1599. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields, fields));
  1600. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  1601. EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields, fields));
  1602. }
  1603. // Test class to save a copy of the last field_context.parent_fields() vector
  1604. // passed to the comparison function.
  1605. class ParentSavingFieldComparator : public util::FieldComparator {
  1606. public:
  1607. ParentSavingFieldComparator() {}
  1608. virtual ComparisonResult Compare(
  1609. const google::protobuf::Message& message_1,
  1610. const google::protobuf::Message& message_2,
  1611. const google::protobuf::FieldDescriptor* field,
  1612. int index_1, int index_2,
  1613. const google::protobuf::util::FieldContext* field_context) {
  1614. if (field_context)
  1615. parent_fields_ = *(field_context->parent_fields());
  1616. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  1617. return RECURSE;
  1618. } else {
  1619. return SAME;
  1620. }
  1621. }
  1622. std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_fields() {
  1623. return parent_fields_;
  1624. }
  1625. private:
  1626. std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_fields_;
  1627. };
  1628. // Tests if MessageDifferencer sends the parent fields in the FieldContext
  1629. // parameter.
  1630. TEST(MessageDifferencerTest, FieldContextParentFieldsTest) {
  1631. protobuf_unittest::TestDiffMessage msg1;
  1632. msg1.add_rm()->set_c(1);
  1633. protobuf_unittest::TestDiffMessage msg2;
  1634. msg2.add_rm()->set_c(1);
  1635. ParentSavingFieldComparator field_comparator;
  1636. util::MessageDifferencer differencer;
  1637. differencer.set_field_comparator(&field_comparator);
  1638. differencer.Compare(msg1, msg2);
  1639. // We want only one parent with the name "rm"
  1640. ASSERT_EQ(1, field_comparator.parent_fields().size());
  1641. EXPECT_EQ("rm", field_comparator.parent_fields()[0].field->name());
  1642. }
  1643. class ComparisonTest : public testing::Test {
  1644. protected:
  1645. ComparisonTest() : use_equivalency_(false), repeated_field_as_set_(false) {
  1646. // Setup the test.
  1647. TestUtil::SetAllFields(&proto1_);
  1648. TestUtil::SetAllFields(&proto2_);
  1649. TestUtil::SetAllExtensions(&proto1ex_);
  1650. TestUtil::SetAllExtensions(&proto2ex_);
  1651. TestUtil::SetAllFieldsAndExtensions(&orderings_proto1_);
  1652. TestUtil::SetAllFieldsAndExtensions(&orderings_proto2_);
  1653. unknown1_ = empty1_.mutable_unknown_fields();
  1654. unknown2_ = empty2_.mutable_unknown_fields();
  1655. }
  1656. ~ComparisonTest() { }
  1657. void SetSpecialFieldOption(const Message& message,
  1658. util::MessageDifferencer* d) {
  1659. if (!ignored_field_.empty()) {
  1660. d->IgnoreField(GetFieldDescriptor(message, ignored_field_));
  1661. }
  1662. if (repeated_field_as_set_) {
  1663. d->set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
  1664. }
  1665. if (!set_field_.empty()) {
  1666. d->TreatAsSet(GetFieldDescriptor(message, set_field_));
  1667. }
  1668. if (!map_field_.empty() && !map_key_.empty()) {
  1669. d->TreatAsMap(GetFieldDescriptor(message, map_field_),
  1670. GetFieldDescriptor(message, map_field_ + "." + map_key_));
  1671. }
  1672. }
  1673. string Run(const Message& msg1, const Message& msg2) {
  1674. string output;
  1675. // Setup the comparison.
  1676. util::MessageDifferencer differencer;
  1677. differencer.ReportDifferencesToString(&output);
  1678. if (use_equivalency_) {
  1679. differencer.set_message_field_comparison(
  1680. util::MessageDifferencer::EQUIVALENT);
  1681. }
  1682. SetSpecialFieldOption(msg1, &differencer);
  1683. // Conduct the comparison.
  1684. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  1685. return output;
  1686. }
  1687. string Run() {
  1688. return Run(proto1_, proto2_);
  1689. }
  1690. string RunOrder() {
  1691. return Run(orderings_proto1_, orderings_proto2_);
  1692. }
  1693. string RunEx() {
  1694. return Run(proto1ex_, proto2ex_);
  1695. }
  1696. string RunDiff() {
  1697. return Run(proto1diff_, proto2diff_);
  1698. }
  1699. string RunUn() {
  1700. return Run(empty1_, empty2_);
  1701. }
  1702. void use_equivalency() {
  1703. use_equivalency_ = true;
  1704. }
  1705. void repeated_field_as_set() {
  1706. repeated_field_as_set_ = true;
  1707. }
  1708. void field_as_set(const string& field) {
  1709. set_field_ = field;
  1710. }
  1711. void field_as_map(const string& field, const string& key) {
  1712. map_field_ = field;
  1713. map_key_ = key;
  1714. }
  1715. void ignore_field(const string& field) {
  1716. ignored_field_ = field;
  1717. }
  1718. unittest::TestAllTypes proto1_;
  1719. unittest::TestAllTypes proto2_;
  1720. unittest::TestFieldOrderings orderings_proto1_;
  1721. unittest::TestFieldOrderings orderings_proto2_;
  1722. unittest::TestAllExtensions proto1ex_;
  1723. unittest::TestAllExtensions proto2ex_;
  1724. unittest::TestDiffMessage proto1diff_;
  1725. unittest::TestDiffMessage proto2diff_;
  1726. unittest::TestEmptyMessage empty1_;
  1727. unittest::TestEmptyMessage empty2_;
  1728. unittest::TestMap map_proto1_;
  1729. unittest::TestMap map_proto2_;
  1730. UnknownFieldSet* unknown1_;
  1731. UnknownFieldSet* unknown2_;
  1732. bool use_equivalency_;
  1733. bool repeated_field_as_set_;
  1734. string set_field_;
  1735. string map_field_;
  1736. string map_key_;
  1737. string ignored_field_;
  1738. };
  1739. // Basic tests.
  1740. TEST_F(ComparisonTest, AdditionTest) {
  1741. proto1_.clear_optional_int32();
  1742. EXPECT_EQ("added: optional_int32: 101\n",
  1743. Run());
  1744. }
  1745. TEST_F(ComparisonTest, Addition_OrderTest) {
  1746. orderings_proto1_.clear_my_int();
  1747. EXPECT_EQ("added: my_int: 1\n",
  1748. RunOrder());
  1749. }
  1750. TEST_F(ComparisonTest, DeletionTest) {
  1751. proto2_.clear_optional_int32();
  1752. EXPECT_EQ("deleted: optional_int32: 101\n",
  1753. Run());
  1754. }
  1755. TEST_F(ComparisonTest, Deletion_OrderTest) {
  1756. orderings_proto2_.clear_my_string();
  1757. EXPECT_EQ("deleted: my_string: \"foo\"\n",
  1758. RunOrder());
  1759. }
  1760. TEST_F(ComparisonTest, RepeatedDeletionTest) {
  1761. proto2_.clear_repeated_int32();
  1762. EXPECT_EQ("deleted: repeated_int32[0]: 201\n"
  1763. "deleted: repeated_int32[1]: 301\n",
  1764. Run());
  1765. }
  1766. TEST_F(ComparisonTest, ModificationTest) {
  1767. proto1_.set_optional_int32(-1);
  1768. EXPECT_EQ("modified: optional_int32: -1 -> 101\n",
  1769. Run());
  1770. }
  1771. // Basic equivalency tests.
  1772. TEST_F(ComparisonTest, EquivalencyAdditionTest) {
  1773. use_equivalency();
  1774. proto1_.clear_optional_int32();
  1775. EXPECT_EQ("modified: optional_int32: 0 -> 101\n",
  1776. Run());
  1777. }
  1778. TEST_F(ComparisonTest, EquivalencyDeletionTest) {
  1779. use_equivalency();
  1780. proto2_.clear_optional_int32();
  1781. EXPECT_EQ("modified: optional_int32: 101 -> 0\n",
  1782. Run());
  1783. }
  1784. // Group tests.
  1785. TEST_F(ComparisonTest, GroupAdditionTest) {
  1786. proto1_.mutable_optionalgroup()->clear_a();
  1787. EXPECT_EQ("added: optionalgroup.a: 117\n",
  1788. Run());
  1789. }
  1790. TEST_F(ComparisonTest, GroupDeletionTest) {
  1791. proto2_.mutable_optionalgroup()->clear_a();
  1792. EXPECT_EQ("deleted: optionalgroup.a: 117\n",
  1793. Run());
  1794. }
  1795. TEST_F(ComparisonTest, GroupModificationTest) {
  1796. proto1_.mutable_optionalgroup()->set_a(2);
  1797. EXPECT_EQ("modified: optionalgroup.a: 2 -> 117\n",
  1798. Run());
  1799. }
  1800. TEST_F(ComparisonTest, GroupFullAdditionTest) {
  1801. proto1_.clear_optionalgroup();
  1802. // Note the difference in the output between this and GroupAdditionTest.
  1803. EXPECT_EQ("added: optionalgroup: { a: 117 }\n",
  1804. Run());
  1805. }
  1806. TEST_F(ComparisonTest, GroupFullDeletionTest) {
  1807. proto2_.clear_optionalgroup();
  1808. EXPECT_EQ("deleted: optionalgroup: { a: 117 }\n",
  1809. Run());
  1810. }
  1811. TEST_F(ComparisonTest, RepeatedSetOptionTest) {
  1812. repeated_field_as_set();
  1813. proto2_.clear_repeatedgroup();
  1814. proto1_.clear_repeatedgroup();
  1815. proto1_.add_repeatedgroup()->set_a(317);
  1816. proto2_.add_repeatedgroup()->set_a(909);
  1817. proto2_.add_repeatedgroup()->set_a(907);
  1818. proto1_.add_repeatedgroup()->set_a(904);
  1819. proto1_.add_repeatedgroup()->set_a(907);
  1820. proto1_.add_repeatedgroup()->set_a(909);
  1821. EXPECT_EQ("moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n"
  1822. "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n"
  1823. "deleted: repeatedgroup[0]: { a: 317 }\n"
  1824. "deleted: repeatedgroup[1]: { a: 904 }\n",
  1825. Run());
  1826. }
  1827. TEST_F(ComparisonTest, RepeatedSetOptionTest_Ex) {
  1828. repeated_field_as_set();
  1829. proto1ex_.ClearExtension(protobuf_unittest::repeated_nested_message_extension);
  1830. proto2ex_.ClearExtension(protobuf_unittest::repeated_nested_message_extension);
  1831. proto2ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
  1832. ->set_bb(909);
  1833. proto2ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
  1834. ->set_bb(907);
  1835. proto1ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
  1836. ->set_bb(904);
  1837. proto1ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
  1838. ->set_bb(907);
  1839. proto1ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
  1840. ->set_bb(909);
  1841. EXPECT_EQ("moved: (protobuf_unittest.repeated_nested_message_extension)[2] ->"
  1842. " (protobuf_unittest.repeated_nested_message_extension)[0] :"
  1843. " { bb: 909 }\n"
  1844. "deleted: (protobuf_unittest.repeated_nested_message_extension)[0]:"
  1845. " { bb: 904 }\n",
  1846. RunEx());
  1847. }
  1848. TEST_F(ComparisonTest, RepeatedMapFieldTest_Group) {
  1849. field_as_map("repeatedgroup", "a");
  1850. proto1_.clear_repeatedgroup();
  1851. proto2_.clear_repeatedgroup();
  1852. proto1_.add_repeatedgroup()->set_a(317); // deleted
  1853. proto1_.add_repeatedgroup()->set_a(904); // deleted
  1854. proto1_.add_repeatedgroup()->set_a(907); // moved from
  1855. proto1_.add_repeatedgroup()->set_a(909); // moved from
  1856. proto2_.add_repeatedgroup()->set_a(909); // moved to
  1857. proto2_.add_repeatedgroup()->set_a(318); // added
  1858. proto2_.add_repeatedgroup()->set_a(907); // moved to
  1859. EXPECT_EQ("moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n"
  1860. "added: repeatedgroup[1]: { a: 318 }\n"
  1861. "deleted: repeatedgroup[0]: { a: 317 }\n"
  1862. "deleted: repeatedgroup[1]: { a: 904 }\n",
  1863. Run());
  1864. }
  1865. TEST_F(ComparisonTest, RepeatedMapFieldTest_MessageKey) {
  1866. // Use m as key, but use b as value.
  1867. field_as_map("item", "m");
  1868. protobuf_unittest::TestDiffMessage msg1;
  1869. protobuf_unittest::TestDiffMessage msg2;
  1870. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1871. // The following code creates one deletion, one addition and two moved fields
  1872. // on the messages.
  1873. item->mutable_m()->set_c(0);
  1874. item->set_b("first");
  1875. item = msg1.add_item();
  1876. item->mutable_m()->set_c(2);
  1877. item->set_b("second");
  1878. item = msg1.add_item(); item->set_b("null"); // empty key moved
  1879. item = msg1.add_item();
  1880. item->mutable_m()->set_c(3);
  1881. item->set_b("third"); // deletion
  1882. item = msg1.add_item();
  1883. item->mutable_m()->set_c(2);
  1884. item->set_b("second"); // duplicated key ( deletion )
  1885. item = msg2.add_item();
  1886. item->mutable_m()->set_c(2);
  1887. item->set_b("second"); // modification
  1888. item = msg2.add_item();
  1889. item->mutable_m()->set_c(4);
  1890. item->set_b("fourth"); // addition
  1891. item = msg2.add_item();
  1892. item->mutable_m()->set_c(0);
  1893. item->set_b("fist"); // move with change
  1894. item = msg2.add_item(); item->set_b("null");
  1895. EXPECT_EQ(
  1896. "modified: item[0].b -> item[2].b: \"first\" -> \"fist\"\n"
  1897. "moved: item[1] -> item[0] : { b: \"second\" m { c: 2 } }\n"
  1898. "moved: item[2] -> item[3] : { b: \"null\" }\n"
  1899. "added: item[1]: { b: \"fourth\" m { c: 4 } }\n"
  1900. "deleted: item[3]: { b: \"third\" m { c: 3 } }\n"
  1901. "deleted: item[4]: { b: \"second\" m { c: 2 } }\n",
  1902. Run(msg1, msg2));
  1903. }
  1904. TEST_F(ComparisonTest, RepeatedFieldSetTest_SetOfSet) {
  1905. repeated_field_as_set();
  1906. // Create the testing protos
  1907. protobuf_unittest::TestDiffMessage msg1;
  1908. protobuf_unittest::TestDiffMessage msg2;
  1909. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1910. item->add_ra(1); item->add_ra(2); item->add_ra(3);
  1911. item = msg1.add_item();
  1912. item->add_ra(5); item->add_ra(6);
  1913. item = msg1.add_item();
  1914. item->add_ra(1); item->add_ra(3);
  1915. item = msg1.add_item();
  1916. item->add_ra(6); item->add_ra(7); item->add_ra(8);
  1917. item = msg2.add_item();
  1918. item->add_ra(6); item->add_ra(5);
  1919. item = msg2.add_item();
  1920. item->add_ra(6); item->add_ra(8);
  1921. item = msg2.add_item();
  1922. item->add_ra(1); item->add_ra(3);
  1923. item = msg2.add_item();
  1924. item->add_ra(3); item->add_ra(2); item->add_ra(1);
  1925. // Compare
  1926. EXPECT_EQ("moved: item[0].ra[0] -> item[3].ra[2] : 1\n"
  1927. "moved: item[0].ra[2] -> item[3].ra[0] : 3\n"
  1928. "moved: item[0] -> item[3] : { ra: 1 ra: 2 ra: 3 }\n"
  1929. "moved: item[1].ra[0] -> item[0].ra[1] : 5\n"
  1930. "moved: item[1].ra[1] -> item[0].ra[0] : 6\n"
  1931. "moved: item[1] -> item[0] : { ra: 5 ra: 6 }\n"
  1932. "added: item[1]: { ra: 6 ra: 8 }\n"
  1933. "deleted: item[3]: { ra: 6 ra: 7 ra: 8 }\n",
  1934. Run(msg1, msg2));
  1935. }
  1936. TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedKey) {
  1937. // used rb as a key, but b is the value.
  1938. repeated_field_as_set();
  1939. field_as_map("item", "rb");
  1940. protobuf_unittest::TestDiffMessage msg1;
  1941. protobuf_unittest::TestDiffMessage msg2;
  1942. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1943. item->add_rb("a");
  1944. item->add_rb("b");
  1945. item->set_b("first");
  1946. item = msg2.add_item();
  1947. item->add_rb("c");
  1948. item->set_b("second");
  1949. item = msg2.add_item();
  1950. item->add_rb("b");
  1951. item->add_rb("a");
  1952. item->set_b("fist");
  1953. EXPECT_EQ("modified: item[0].b -> item[1].b: \"first\" -> \"fist\"\n"
  1954. "moved: item[0].rb[0] -> item[1].rb[1] : \"a\"\n"
  1955. "moved: item[0].rb[1] -> item[1].rb[0] : \"b\"\n"
  1956. "added: item[0]: { b: \"second\" rb: \"c\" }\n",
  1957. Run(msg1, msg2));
  1958. }
  1959. TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedMessageKey) {
  1960. field_as_map("item", "rm");
  1961. protobuf_unittest::TestDiffMessage msg1;
  1962. protobuf_unittest::TestDiffMessage msg2;
  1963. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  1964. protobuf_unittest::TestField* key = item->add_rm();
  1965. key->set_c(2); key->add_rc(10); key->add_rc(10);
  1966. item = msg1.add_item(); key = item->add_rm();
  1967. key->set_c(0); key->add_rc(1); key->add_rc(2);
  1968. key = item->add_rm();
  1969. key->set_c(0);
  1970. item->add_rb("first");
  1971. item = msg2.add_item();
  1972. item->CopyFrom(msg1.item(1));
  1973. item->add_rb("second");
  1974. EXPECT_EQ("added: item[0].rb[1]: \"second\"\n"
  1975. "deleted: item[0]: { rm { c: 2 rc: 10 rc: 10 } }\n",
  1976. Run(msg1, msg2));
  1977. }
  1978. TEST_F(ComparisonTest, RepeatedSetOptionTest_Unknown) {
  1979. // Currently, as_set option doesn't have affects on unknown field.
  1980. // If needed, this feature will be added by request.
  1981. repeated_field_as_set();
  1982. unknown1_->AddGroup(245)->AddFixed32(248, 1);
  1983. unknown2_->AddGroup(245)->AddFixed32(248, 3);
  1984. unknown2_->AddGroup(245)->AddFixed32(248, 1);
  1985. // We expect it behaves the same as normal comparison.
  1986. EXPECT_EQ("modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n"
  1987. "added: 245[1]: { ... }\n",
  1988. RunUn());
  1989. }
  1990. TEST_F(ComparisonTest, Matching_Unknown) {
  1991. unknown1_->AddGroup(245)->AddFixed32(248, 1);
  1992. unknown2_->AddGroup(245)->AddFixed32(248, 1);
  1993. unknown1_->AddGroup(245)->AddFixed32(248, 3);
  1994. unknown2_->AddGroup(245)->AddFixed32(248, 3);
  1995. unknown2_->AddLengthDelimited(242, "cat");
  1996. unknown2_->AddGroup(246)->AddFixed32(248, 4);
  1997. // report_match is false so only added/modified fields are expected.
  1998. EXPECT_EQ("added: 242[0]: \"cat\"\n"
  1999. "added: 246[0]: { ... }\n",
  2000. RunUn());
  2001. }
  2002. TEST_F(ComparisonTest, RepeatedSetFieldTest) {
  2003. field_as_set("repeatedgroup");
  2004. proto1_.clear_repeatedgroup();
  2005. proto2_.clear_repeatedgroup();
  2006. proto2_.add_repeatedgroup()->set_a(909);
  2007. proto2_.add_repeatedgroup()->set_a(907);
  2008. proto1_.add_repeatedgroup()->set_a(317);
  2009. proto1_.add_repeatedgroup()->set_a(904);
  2010. proto1_.add_repeatedgroup()->set_a(907);
  2011. proto1_.add_repeatedgroup()->set_a(909);
  2012. EXPECT_EQ("moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n"
  2013. "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n"
  2014. "deleted: repeatedgroup[0]: { a: 317 }\n"
  2015. "deleted: repeatedgroup[1]: { a: 904 }\n",
  2016. Run());
  2017. }
  2018. // Embedded message tests.
  2019. TEST_F(ComparisonTest, EmbeddedAdditionTest) {
  2020. proto1_.mutable_optional_nested_message()->clear_bb();
  2021. EXPECT_EQ("added: optional_nested_message.bb: 118\n",
  2022. Run());
  2023. }
  2024. TEST_F(ComparisonTest, EmbeddedDeletionTest) {
  2025. proto2_.mutable_optional_nested_message()->clear_bb();
  2026. EXPECT_EQ("deleted: optional_nested_message.bb: 118\n",
  2027. Run());
  2028. }
  2029. TEST_F(ComparisonTest, EmbeddedModificationTest) {
  2030. proto1_.mutable_optional_nested_message()->set_bb(2);
  2031. EXPECT_EQ("modified: optional_nested_message.bb: 2 -> 118\n",
  2032. Run());
  2033. }
  2034. TEST_F(ComparisonTest, EmbeddedFullAdditionTest) {
  2035. proto1_.clear_optional_nested_message();
  2036. EXPECT_EQ("added: optional_nested_message: { bb: 118 }\n",
  2037. Run());
  2038. }
  2039. TEST_F(ComparisonTest, EmbeddedPartialAdditionTest) {
  2040. proto1_.clear_optional_nested_message();
  2041. proto2_.mutable_optional_nested_message()->clear_bb();
  2042. EXPECT_EQ("added: optional_nested_message: { }\n",
  2043. Run());
  2044. }
  2045. TEST_F(ComparisonTest, EmbeddedFullDeletionTest) {
  2046. proto2_.clear_optional_nested_message();
  2047. EXPECT_EQ("deleted: optional_nested_message: { bb: 118 }\n",
  2048. Run());
  2049. }
  2050. // Repeated element tests.
  2051. TEST_F(ComparisonTest, BasicRepeatedTest) {
  2052. proto1_.clear_repeated_int32();
  2053. proto2_.clear_repeated_int32();
  2054. proto1_.add_repeated_int32(500);
  2055. proto1_.add_repeated_int32(501);
  2056. proto1_.add_repeated_int32(502);
  2057. proto1_.add_repeated_int32(503);
  2058. proto1_.add_repeated_int32(500);
  2059. proto2_.add_repeated_int32(500);
  2060. proto2_.add_repeated_int32(509);
  2061. proto2_.add_repeated_int32(502);
  2062. proto2_.add_repeated_int32(504);
  2063. EXPECT_EQ("modified: repeated_int32[1]: 501 -> 509\n"
  2064. "modified: repeated_int32[3]: 503 -> 504\n"
  2065. "deleted: repeated_int32[4]: 500\n",
  2066. Run());
  2067. }
  2068. TEST_F(ComparisonTest, BasicRepeatedTest_SetOption) {
  2069. repeated_field_as_set();
  2070. proto1_.clear_repeated_int32();
  2071. proto2_.clear_repeated_int32();
  2072. proto1_.add_repeated_int32(501);
  2073. proto1_.add_repeated_int32(502);
  2074. proto1_.add_repeated_int32(503);
  2075. proto1_.add_repeated_int32(500);
  2076. proto1_.add_repeated_int32(500);
  2077. proto2_.add_repeated_int32(500);
  2078. proto2_.add_repeated_int32(509);
  2079. proto2_.add_repeated_int32(503);
  2080. proto2_.add_repeated_int32(502);
  2081. proto2_.add_repeated_int32(504);
  2082. EXPECT_EQ("moved: repeated_int32[1] -> repeated_int32[3] : 502\n"
  2083. "moved: repeated_int32[3] -> repeated_int32[0] : 500\n"
  2084. "added: repeated_int32[1]: 509\n"
  2085. "added: repeated_int32[4]: 504\n"
  2086. "deleted: repeated_int32[0]: 501\n"
  2087. "deleted: repeated_int32[4]: 500\n",
  2088. Run());
  2089. }
  2090. TEST_F(ComparisonTest, BasicRepeatedTest_SetField) {
  2091. field_as_set("repeated_int32");
  2092. proto1_.clear_repeated_int32();
  2093. proto2_.clear_repeated_int32();
  2094. proto1_.add_repeated_int32(501);
  2095. proto1_.add_repeated_int32(502);
  2096. proto1_.add_repeated_int32(503);
  2097. proto1_.add_repeated_int32(500);
  2098. proto1_.add_repeated_int32(500);
  2099. proto2_.add_repeated_int32(500);
  2100. proto2_.add_repeated_int32(509);
  2101. proto2_.add_repeated_int32(503);
  2102. proto2_.add_repeated_int32(502);
  2103. proto2_.add_repeated_int32(504);
  2104. EXPECT_EQ("moved: repeated_int32[1] -> repeated_int32[3] : 502\n"
  2105. "moved: repeated_int32[3] -> repeated_int32[0] : 500\n"
  2106. "added: repeated_int32[1]: 509\n"
  2107. "added: repeated_int32[4]: 504\n"
  2108. "deleted: repeated_int32[0]: 501\n"
  2109. "deleted: repeated_int32[4]: 500\n",
  2110. Run());
  2111. }
  2112. // Multiple action tests.
  2113. TEST_F(ComparisonTest, AddDeleteTest) {
  2114. proto1_.clear_optional_int32();
  2115. proto2_.clear_optional_int64();
  2116. EXPECT_EQ("added: optional_int32: 101\n"
  2117. "deleted: optional_int64: 102\n",
  2118. Run());
  2119. }
  2120. TEST_F(ComparisonTest, AddDelete_FieldOrderingTest) {
  2121. orderings_proto1_.ClearExtension(unittest::my_extension_string);
  2122. orderings_proto2_.clear_my_int();
  2123. EXPECT_EQ("deleted: my_int: 1\n"
  2124. "added: (protobuf_unittest.my_extension_string): \"bar\"\n",
  2125. RunOrder());
  2126. }
  2127. TEST_F(ComparisonTest, AllThreeTest) {
  2128. proto1_.clear_optional_int32();
  2129. proto2_.clear_optional_float();
  2130. proto2_.set_optional_string("hello world!");
  2131. EXPECT_EQ("added: optional_int32: 101\n"
  2132. "deleted: optional_float: 111\n"
  2133. "modified: optional_string: \"115\" -> \"hello world!\"\n",
  2134. Run());
  2135. }
  2136. TEST_F(ComparisonTest, SandwhichTest) {
  2137. proto1_.clear_optional_int64();
  2138. proto1_.clear_optional_uint32();
  2139. proto2_.clear_optional_uint64();
  2140. EXPECT_EQ("added: optional_int64: 102\n"
  2141. "added: optional_uint32: 103\n"
  2142. "deleted: optional_uint64: 104\n",
  2143. Run());
  2144. }
  2145. TEST_F(ComparisonTest, IgnoredNoChangeTest) {
  2146. proto1diff_.set_v(3);
  2147. proto2diff_.set_v(3);
  2148. proto2diff_.set_w("foo");
  2149. ignore_field("v");
  2150. EXPECT_EQ("ignored: v\n"
  2151. "added: w: \"foo\"\n",
  2152. RunDiff());
  2153. }
  2154. TEST_F(ComparisonTest, IgnoredAddTest) {
  2155. proto2diff_.set_v(3);
  2156. proto2diff_.set_w("foo");
  2157. ignore_field("v");
  2158. EXPECT_EQ("ignored: v\n"
  2159. "added: w: \"foo\"\n",
  2160. RunDiff());
  2161. }
  2162. TEST_F(ComparisonTest, IgnoredDeleteTest) {
  2163. proto1diff_.set_v(3);
  2164. proto2diff_.set_w("foo");
  2165. ignore_field("v");
  2166. EXPECT_EQ("ignored: v\n"
  2167. "added: w: \"foo\"\n",
  2168. RunDiff());
  2169. }
  2170. TEST_F(ComparisonTest, IgnoredModifyTest) {
  2171. proto1diff_.set_v(3);
  2172. proto2diff_.set_v(4);
  2173. proto2diff_.set_w("foo");
  2174. ignore_field("v");
  2175. EXPECT_EQ("ignored: v\n"
  2176. "added: w: \"foo\"\n",
  2177. RunDiff());
  2178. }
  2179. TEST_F(ComparisonTest, IgnoredRepeatedAddTest) {
  2180. proto1diff_.add_rv(3);
  2181. proto1diff_.add_rv(4);
  2182. proto2diff_.add_rv(3);
  2183. proto2diff_.add_rv(4);
  2184. proto2diff_.add_rv(5);
  2185. proto2diff_.set_w("foo");
  2186. ignore_field("rv");
  2187. EXPECT_EQ("ignored: rv\n"
  2188. "added: w: \"foo\"\n",
  2189. RunDiff());
  2190. }
  2191. TEST_F(ComparisonTest, IgnoredRepeatedDeleteTest) {
  2192. proto1diff_.add_rv(3);
  2193. proto1diff_.add_rv(4);
  2194. proto1diff_.add_rv(5);
  2195. proto2diff_.add_rv(3);
  2196. proto2diff_.add_rv(4);
  2197. proto2diff_.set_w("foo");
  2198. ignore_field("rv");
  2199. EXPECT_EQ("ignored: rv\n"
  2200. "added: w: \"foo\"\n",
  2201. RunDiff());
  2202. }
  2203. TEST_F(ComparisonTest, IgnoredRepeatedModifyTest) {
  2204. proto1diff_.add_rv(3);
  2205. proto1diff_.add_rv(4);
  2206. proto2diff_.add_rv(3);
  2207. proto2diff_.add_rv(5);
  2208. proto2diff_.set_w("foo");
  2209. ignore_field("rv");
  2210. EXPECT_EQ("ignored: rv\n"
  2211. "added: w: \"foo\"\n",
  2212. RunDiff());
  2213. }
  2214. TEST_F(ComparisonTest, IgnoredWholeNestedMessage) {
  2215. proto1diff_.mutable_m()->set_c(3);
  2216. proto2diff_.mutable_m()->set_c(4);
  2217. proto2diff_.set_w("foo");
  2218. ignore_field("m");
  2219. EXPECT_EQ("added: w: \"foo\"\n"
  2220. "ignored: m\n",
  2221. RunDiff());
  2222. }
  2223. TEST_F(ComparisonTest, IgnoredNestedField) {
  2224. proto1diff_.mutable_m()->set_c(3);
  2225. proto2diff_.mutable_m()->set_c(4);
  2226. proto2diff_.set_w("foo");
  2227. ignore_field("m.c");
  2228. EXPECT_EQ("added: w: \"foo\"\n"
  2229. "ignored: m.c\n",
  2230. RunDiff());
  2231. }
  2232. TEST_F(ComparisonTest, IgnoredRepeatedNested) {
  2233. proto1diff_.add_rm()->set_c(0);
  2234. proto1diff_.add_rm()->set_c(1);
  2235. proto2diff_.add_rm()->set_c(2);
  2236. proto2diff_.add_rm()->set_c(3);
  2237. proto2diff_.set_w("foo");
  2238. ignore_field("rm.c");
  2239. EXPECT_EQ("ignored: rm[0].c\n"
  2240. "ignored: rm[1].c\n"
  2241. "added: w: \"foo\"\n",
  2242. RunDiff());
  2243. }
  2244. TEST_F(ComparisonTest, IgnoredNestedRepeated) {
  2245. proto1diff_.mutable_m()->add_rc(23);
  2246. proto1diff_.mutable_m()->add_rc(24);
  2247. proto2diff_.mutable_m()->add_rc(25);
  2248. proto2diff_.set_w("foo");
  2249. ignore_field("m.rc");
  2250. EXPECT_EQ("added: w: \"foo\"\n"
  2251. "ignored: m.rc\n",
  2252. RunDiff());
  2253. }
  2254. TEST_F(ComparisonTest, ExtensionTest) {
  2255. proto1ex_.SetExtension(unittest::optional_int32_extension, 401);
  2256. proto2ex_.SetExtension(unittest::optional_int32_extension, 402);
  2257. proto1ex_.ClearExtension(unittest::optional_int64_extension);
  2258. proto2ex_.SetExtension(unittest::optional_int64_extension, 403);
  2259. EXPECT_EQ(
  2260. "modified: (protobuf_unittest.optional_int32_extension): 401 -> 402\n"
  2261. "added: (protobuf_unittest.optional_int64_extension): 403\n",
  2262. RunEx());
  2263. }
  2264. TEST_F(ComparisonTest, MatchedUnknownFieldTagTest) {
  2265. unknown1_->AddVarint(240, 122);
  2266. unknown2_->AddVarint(240, 121);
  2267. unknown1_->AddFixed32(241, 1);
  2268. unknown2_->AddFixed64(241, 2);
  2269. unknown1_->AddLengthDelimited(242, "cat");
  2270. unknown2_->AddLengthDelimited(242, "dog");
  2271. EXPECT_EQ(
  2272. "modified: 240[0]: 122 -> 121\n"
  2273. "deleted: 241[0]: 0x00000001\n"
  2274. "added: 241[0]: 0x0000000000000002\n"
  2275. "modified: 242[0]: \"cat\" -> \"dog\"\n",
  2276. RunUn());
  2277. }
  2278. TEST_F(ComparisonTest, UnmatchedUnknownFieldTagTest) {
  2279. unknown1_->AddFixed32(243, 1);
  2280. unknown2_->AddVarint(244, 2);
  2281. unknown2_->AddVarint(244, 4);
  2282. EXPECT_EQ(
  2283. "deleted: 243[0]: 0x00000001\n"
  2284. "added: 244[0]: 2\n"
  2285. "added: 244[1]: 4\n",
  2286. RunUn());
  2287. }
  2288. TEST_F(ComparisonTest, DifferentSizedUnknownFieldTest) {
  2289. unknown1_->AddVarint(240, 1);
  2290. unknown1_->AddVarint(240, 3);
  2291. unknown1_->AddVarint(240, 4);
  2292. unknown2_->AddVarint(240, 2);
  2293. unknown2_->AddVarint(240, 3);
  2294. unknown2_->AddVarint(240, 2);
  2295. unknown2_->AddVarint(240, 5);
  2296. EXPECT_EQ(
  2297. "modified: 240[0]: 1 -> 2\n"
  2298. "modified: 240[2]: 4 -> 2\n"
  2299. "added: 240[3]: 5\n",
  2300. RunUn());
  2301. }
  2302. TEST_F(ComparisonTest, UnknownFieldsAll) {
  2303. unknown1_->AddVarint(243, 122);
  2304. unknown1_->AddFixed64(244, 0x0172356);
  2305. unknown1_->AddFixed64(244, 0x098);
  2306. unknown1_->AddGroup(245)->AddFixed32(248, 1);
  2307. unknown1_->mutable_field(3)->mutable_group()->AddFixed32(248, 2);
  2308. unknown1_->AddGroup(249)->AddFixed64(250, 1);
  2309. unknown2_->AddVarint(243, 121);
  2310. unknown2_->AddLengthDelimited(73882, "test 123");
  2311. unknown2_->AddGroup(245)->AddFixed32(248, 3);
  2312. unknown2_->AddGroup(247);
  2313. EXPECT_EQ(
  2314. "modified: 243[0]: 122 -> 121\n"
  2315. "deleted: 244[0]: 0x0000000000172356\n"
  2316. "deleted: 244[1]: 0x0000000000000098\n"
  2317. "modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n"
  2318. "deleted: 245[0].248[1]: 0x00000002\n"
  2319. "added: 247[0]: { ... }\n"
  2320. "deleted: 249[0]: { ... }\n"
  2321. "added: 73882[0]: \"test 123\"\n",
  2322. RunUn());
  2323. }
  2324. TEST_F(ComparisonTest, EquivalentIgnoresUnknown) {
  2325. unittest::ForeignMessage message1, message2;
  2326. message1.set_c(5);
  2327. message1.mutable_unknown_fields()->AddVarint(123, 456);
  2328. message2.set_c(5);
  2329. message2.mutable_unknown_fields()->AddVarint(321, 654);
  2330. EXPECT_FALSE(util::MessageDifferencer::Equals(message1, message2));
  2331. EXPECT_TRUE(util::MessageDifferencer::Equivalent(message1, message2));
  2332. }
  2333. TEST_F(ComparisonTest, MapTest) {
  2334. Map<string, string>& map1 = *map_proto1_.mutable_map_string_string();
  2335. map1["key1"] = "1";
  2336. map1["key2"] = "2";
  2337. map1["key3"] = "3";
  2338. Map<string, string>& map2 = *map_proto2_.mutable_map_string_string();
  2339. map2["key3"] = "0";
  2340. map2["key2"] = "2";
  2341. map2["key1"] = "1";
  2342. EXPECT_EQ("modified: map_string_string.value: \"3\" -> \"0\"\n",
  2343. Run(map_proto1_, map_proto2_));
  2344. }
  2345. TEST_F(ComparisonTest, MapIgnoreKeyTest) {
  2346. Map<string, string>& map1 = *map_proto1_.mutable_map_string_string();
  2347. map1["key1"] = "1";
  2348. map1["key2"] = "2";
  2349. map1["key3"] = "3";
  2350. Map<string, string>& map2 = *map_proto2_.mutable_map_string_string();
  2351. map2["key4"] = "2";
  2352. map2["key5"] = "3";
  2353. map2["key6"] = "1";
  2354. util::MessageDifferencer differencer;
  2355. differencer.IgnoreField(
  2356. GetFieldDescriptor(map_proto1_, "map_string_string.key"));
  2357. EXPECT_TRUE(differencer.Compare(map_proto1_, map_proto2_));
  2358. }
  2359. TEST_F(ComparisonTest, MapRoundTripSyncTest) {
  2360. google::protobuf::TextFormat::Parser parser;
  2361. unittest::TestMap map_reflection1;
  2362. // By setting via reflection, data exists in repeated field.
  2363. ASSERT_TRUE(parser.ParseFromString(
  2364. "map_int32_foreign_message { key: 1 }", &map_reflection1));
  2365. // During copy, data is synced from repeated field to map.
  2366. unittest::TestMap map_reflection2 = map_reflection1;
  2367. // During comparison, data is synced from map to repeated field.
  2368. EXPECT_TRUE(
  2369. util::MessageDifferencer::Equals(map_reflection1, map_reflection2));
  2370. }
  2371. TEST_F(ComparisonTest, MapEntryPartialTest) {
  2372. google::protobuf::TextFormat::Parser parser;
  2373. unittest::TestMap map1;
  2374. unittest::TestMap map2;
  2375. string output;
  2376. util::MessageDifferencer differencer;
  2377. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  2378. differencer.ReportDifferencesToString(&output);
  2379. ASSERT_TRUE(parser.ParseFromString(
  2380. "map_int32_foreign_message { key: 1 value { c: 1 } }", &map1));
  2381. ASSERT_TRUE(parser.ParseFromString(
  2382. "map_int32_foreign_message { key: 1 value { c: 2 }}", &map2));
  2383. EXPECT_FALSE(differencer.Compare(map1, map2));
  2384. EXPECT_EQ("modified: map_int32_foreign_message.value.c: 1 -> 2\n", output);
  2385. ASSERT_TRUE(
  2386. parser.ParseFromString("map_int32_foreign_message { key: 1 }", &map1));
  2387. EXPECT_TRUE(differencer.Compare(map1, map2));
  2388. }
  2389. TEST_F(ComparisonTest, MapEntryPartialEmptyKeyTest) {
  2390. google::protobuf::TextFormat::Parser parser;
  2391. unittest::TestMap map1;
  2392. unittest::TestMap map2;
  2393. ASSERT_TRUE(parser.ParseFromString("map_int32_foreign_message {}", &map1));
  2394. ASSERT_TRUE(
  2395. parser.ParseFromString("map_int32_foreign_message { key: 1 }", &map2));
  2396. util::MessageDifferencer differencer;
  2397. differencer.set_scope(util::MessageDifferencer::PARTIAL);
  2398. EXPECT_TRUE(differencer.Compare(map1, map2));
  2399. }
  2400. // Considers strings keys as equal if they have equal lengths.
  2401. class LengthMapKeyComparator
  2402. : public util::MessageDifferencer::MapKeyComparator {
  2403. public:
  2404. typedef util::MessageDifferencer::SpecificField SpecificField;
  2405. virtual bool IsMatch(const Message& message1, const Message& message2,
  2406. const std::vector<SpecificField>& parent_fields) const {
  2407. const Reflection* reflection1 = message1.GetReflection();
  2408. const Reflection* reflection2 = message2.GetReflection();
  2409. const FieldDescriptor* key_field =
  2410. message1.GetDescriptor()->FindFieldByName("key");
  2411. return reflection1->GetString(message1, key_field).size() ==
  2412. reflection2->GetString(message2, key_field).size();
  2413. }
  2414. };
  2415. TEST_F(ComparisonTest, MapEntryCustomMapKeyComparator) {
  2416. google::protobuf::TextFormat::Parser parser;
  2417. protobuf_unittest::TestMap msg1;
  2418. protobuf_unittest::TestMap msg2;
  2419. ASSERT_TRUE(parser.ParseFromString(
  2420. "map_string_foreign_message { key: 'key1' value { c: 1 }}", &msg1));
  2421. ASSERT_TRUE(parser.ParseFromString(
  2422. "map_string_foreign_message { key: 'key2' value { c: 1 }}", &msg2));
  2423. util::MessageDifferencer differencer;
  2424. LengthMapKeyComparator key_comparator;
  2425. differencer.TreatAsMapUsingKeyComparator(
  2426. GetFieldDescriptor(msg1, "map_string_foreign_message"), &key_comparator);
  2427. string output;
  2428. differencer.ReportDifferencesToString(&output);
  2429. // Though the above two messages have different keys for their map entries,
  2430. // they are considered the same by key_comparator because their lengths are
  2431. // equal. However, in value comparison, all fields of the message are taken
  2432. // into consideration, so they are reported as different.
  2433. EXPECT_FALSE(differencer.Compare(msg1, msg2));
  2434. EXPECT_EQ("modified: map_string_foreign_message.key: \"key1\" -> \"key2\"\n",
  2435. output);
  2436. differencer.IgnoreField(
  2437. GetFieldDescriptor(msg1, "map_string_foreign_message.key"));
  2438. output.clear();
  2439. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  2440. EXPECT_EQ("ignored: map_string_foreign_message.key\n", output);
  2441. }
  2442. class MatchingTest : public testing::Test {
  2443. public:
  2444. typedef util::MessageDifferencer MessageDifferencer;
  2445. protected:
  2446. MatchingTest() {
  2447. }
  2448. ~MatchingTest() {
  2449. }
  2450. string RunWithResult(MessageDifferencer* differencer,
  2451. const Message& msg1, const Message& msg2,
  2452. bool result) {
  2453. string output;
  2454. {
  2455. // Before we return the "output" string, we must make sure the
  2456. // StreamReporter is destructored because its destructor will
  2457. // flush the stream.
  2458. io::StringOutputStream output_stream(&output);
  2459. MessageDifferencer::StreamReporter reporter(&output_stream);
  2460. reporter.set_report_modified_aggregates(true);
  2461. differencer->set_report_matches(true);
  2462. differencer->ReportDifferencesTo(&reporter);
  2463. if (result) {
  2464. EXPECT_TRUE(differencer->Compare(msg1, msg2));
  2465. } else {
  2466. EXPECT_FALSE(differencer->Compare(msg1, msg2));
  2467. }
  2468. }
  2469. return output;
  2470. }
  2471. private:
  2472. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MatchingTest);
  2473. };
  2474. TEST_F(MatchingTest, StreamReporterMatching) {
  2475. protobuf_unittest::TestField msg1, msg2;
  2476. msg1.set_c(72);
  2477. msg2.set_c(72);
  2478. msg1.add_rc(13);
  2479. msg2.add_rc(13);
  2480. msg1.add_rc(17);
  2481. msg2.add_rc(17);
  2482. string output;
  2483. MessageDifferencer differencer;
  2484. differencer.set_report_matches(true);
  2485. differencer.ReportDifferencesToString(&output);
  2486. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  2487. EXPECT_EQ(
  2488. "matched: c : 72\n"
  2489. "matched: rc[0] : 13\n"
  2490. "matched: rc[1] : 17\n",
  2491. output);
  2492. }
  2493. TEST_F(MatchingTest, DontReportMatchedWhenIgnoring) {
  2494. protobuf_unittest::TestField msg1, msg2;
  2495. msg1.set_c(72);
  2496. msg2.set_c(72);
  2497. msg1.add_rc(13);
  2498. msg2.add_rc(13);
  2499. msg1.add_rc(17);
  2500. msg2.add_rc(17);
  2501. string output;
  2502. MessageDifferencer differencer;
  2503. differencer.set_report_matches(true);
  2504. differencer.ReportDifferencesToString(&output);
  2505. differencer.IgnoreField(msg1.GetDescriptor()->FindFieldByName("c"));
  2506. EXPECT_TRUE(differencer.Compare(msg1, msg2));
  2507. EXPECT_EQ(
  2508. "ignored: c\n"
  2509. "matched: rc[0] : 13\n"
  2510. "matched: rc[1] : 17\n",
  2511. output);
  2512. }
  2513. TEST_F(MatchingTest, ReportMatchedForMovedFields) {
  2514. protobuf_unittest::TestDiffMessage msg1, msg2;
  2515. protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
  2516. item->set_a(53);
  2517. item->set_b("hello");
  2518. item = msg2.add_item();
  2519. item->set_a(27);
  2520. item = msg2.add_item();
  2521. item->set_a(53);
  2522. item->set_b("hello");
  2523. item = msg1.add_item();
  2524. item->set_a(27);
  2525. MessageDifferencer differencer;
  2526. const FieldDescriptor* desc;
  2527. desc = msg1.GetDescriptor()->FindFieldByName("item");
  2528. differencer.TreatAsSet(desc);
  2529. EXPECT_EQ(
  2530. "matched: item[0].a -> item[1].a : 53\n"
  2531. "matched: item[0].b -> item[1].b : \"hello\"\n"
  2532. "moved: item[0] -> item[1] : { a: 53 b: \"hello\" }\n"
  2533. "matched: item[1].a -> item[0].a : 27\n"
  2534. "moved: item[1] -> item[0] : { a: 27 }\n",
  2535. RunWithResult(&differencer, msg1, msg2, true));
  2536. }
  2537. TEST_F(MatchingTest, MatchesAppearInPostTraversalOrderForMovedFields) {
  2538. protobuf_unittest::TestDiffMessage msg1, msg2;
  2539. protobuf_unittest::TestDiffMessage::Item* item;
  2540. protobuf_unittest::TestField* field;
  2541. const FieldDescriptor* desc;
  2542. const FieldDescriptor* nested_desc;
  2543. const FieldDescriptor* double_nested_desc;
  2544. desc = msg1.GetDescriptor()->FindFieldByName("item");
  2545. nested_desc = desc->message_type()->FindFieldByName("rm");
  2546. double_nested_desc = nested_desc->message_type()->FindFieldByName("rc");
  2547. MessageDifferencer differencer;
  2548. differencer.TreatAsSet(desc);
  2549. differencer.TreatAsSet(nested_desc);
  2550. differencer.TreatAsSet(double_nested_desc);
  2551. item = msg1.add_item();
  2552. field = item->add_rm();
  2553. field->set_c(1);
  2554. field->add_rc(2);
  2555. field->add_rc(3);
  2556. field = item->add_rm();
  2557. field->set_c(4);
  2558. field->add_rc(5);
  2559. field->add_rc(6);
  2560. field->add_rc(7);
  2561. item = msg2.add_item();
  2562. field = item->add_rm();
  2563. field->set_c(4);
  2564. field->add_rc(7);
  2565. field->add_rc(6);
  2566. field->add_rc(5);
  2567. field = item->add_rm();
  2568. field->set_c(1);
  2569. field->add_rc(3);
  2570. field->add_rc(2);
  2571. item = msg1.add_item();
  2572. field = item->add_rm();
  2573. field->set_c(8);
  2574. field->add_rc(10);
  2575. field->add_rc(11);
  2576. field->add_rc(9);
  2577. item = msg2.add_item();
  2578. field = item->add_rm();
  2579. field->set_c(8);
  2580. field->add_rc(9);
  2581. field->add_rc(10);
  2582. field->add_rc(11);
  2583. EXPECT_EQ(
  2584. "matched: item[0].rm[0].c -> item[0].rm[1].c : 1\n"
  2585. "moved: item[0].rm[0].rc[0] -> item[0].rm[1].rc[1] : 2\n"
  2586. "moved: item[0].rm[0].rc[1] -> item[0].rm[1].rc[0] : 3\n"
  2587. "moved: item[0].rm[0] -> item[0].rm[1] : { c: 1 rc: 2 rc: 3 }\n"
  2588. "matched: item[0].rm[1].c -> item[0].rm[0].c : 4\n"
  2589. "moved: item[0].rm[1].rc[0] -> item[0].rm[0].rc[2] : 5\n"
  2590. "matched: item[0].rm[1].rc[1] -> item[0].rm[0].rc[1] : 6\n"
  2591. "moved: item[0].rm[1].rc[2] -> item[0].rm[0].rc[0] : 7\n"
  2592. "moved: item[0].rm[1] -> item[0].rm[0] : { c: 4 rc: 5 rc: 6 rc: 7 }\n"
  2593. "matched: item[0] : { rm { c: 1 rc: 2 rc: 3 }"
  2594. " rm { c: 4 rc: 5 rc: 6 rc: 7 } }\n"
  2595. "matched: item[1].rm[0].c : 8\n"
  2596. "moved: item[1].rm[0].rc[0] -> item[1].rm[0].rc[1] : 10\n"
  2597. "moved: item[1].rm[0].rc[1] -> item[1].rm[0].rc[2] : 11\n"
  2598. "moved: item[1].rm[0].rc[2] -> item[1].rm[0].rc[0] : 9\n"
  2599. "matched: item[1].rm[0] : { c: 8 rc: 10 rc: 11 rc: 9 }\n"
  2600. "matched: item[1] : { rm { c: 8 rc: 10 rc: 11 rc: 9 } }\n",
  2601. RunWithResult(&differencer, msg1, msg2, true));
  2602. }
  2603. TEST_F(MatchingTest, MatchAndModifiedInterleaveProperly) {
  2604. protobuf_unittest::TestDiffMessage msg1, msg2;
  2605. protobuf_unittest::TestDiffMessage::Item* item;
  2606. protobuf_unittest::TestField* field;
  2607. const FieldDescriptor* desc;
  2608. const FieldDescriptor* nested_key;
  2609. const FieldDescriptor* nested_desc;
  2610. const FieldDescriptor* double_nested_key;
  2611. const FieldDescriptor* double_nested_desc;
  2612. desc = msg1.GetDescriptor()->FindFieldByName("item");
  2613. nested_key = desc->message_type()->FindFieldByName("a");
  2614. nested_desc = desc->message_type()->FindFieldByName("rm");
  2615. double_nested_key = nested_desc->message_type()->FindFieldByName("c");
  2616. double_nested_desc = nested_desc->message_type()->FindFieldByName("rc");
  2617. MessageDifferencer differencer;
  2618. differencer.TreatAsMap(desc, nested_key);
  2619. differencer.TreatAsMap(nested_desc, double_nested_key);
  2620. differencer.TreatAsSet(double_nested_desc);
  2621. item = msg1.add_item();
  2622. item->set_a(1);
  2623. field = item->add_rm();
  2624. field->set_c(2);
  2625. field->add_rc(3);
  2626. field->add_rc(4);
  2627. field = item->add_rm();
  2628. field->set_c(5);
  2629. field->add_rc(6);
  2630. field->add_rc(7);
  2631. field->add_rc(8);
  2632. item = msg1.add_item();
  2633. item->set_a(9);
  2634. field = item->add_rm();
  2635. field->set_c(10);
  2636. field->add_rc(11);
  2637. field->add_rc(12);
  2638. field = item->add_rm();
  2639. field->set_c(13);
  2640. item = msg2.add_item();
  2641. item->set_a(1);
  2642. field = item->add_rm();
  2643. field->set_c(5);
  2644. field->add_rc(8);
  2645. field->add_rc(8);
  2646. field->add_rc(6);
  2647. field = item->add_rm();
  2648. field->set_c(3);
  2649. field->add_rc(2);
  2650. field->add_rc(4);
  2651. item = msg2.add_item();
  2652. item->set_a(9);
  2653. field = item->add_rm();
  2654. field->set_c(10);
  2655. field->add_rc(12);
  2656. field->add_rc(11);
  2657. field = item->add_rm();
  2658. field->set_c(13);
  2659. EXPECT_EQ(
  2660. "matched: item[0].a : 1\n"
  2661. "matched: item[0].rm[1].c -> item[0].rm[0].c : 5\n"
  2662. "moved: item[0].rm[1].rc[0] -> item[0].rm[0].rc[2] : 6\n"
  2663. "moved: item[0].rm[1].rc[2] -> item[0].rm[0].rc[0] : 8\n"
  2664. "added: item[0].rm[0].rc[1]: 8\n"
  2665. "deleted: item[0].rm[1].rc[1]: 7\n"
  2666. "modified: item[0].rm[1] -> item[0].rm[0]: { c: 5 rc: 6 rc: 7 rc: 8 } ->"
  2667. " { c: 5 rc: 8 rc: 8 rc: 6 }\n"
  2668. "added: item[0].rm[1]: { c: 3 rc: 2 rc: 4 }\n"
  2669. "deleted: item[0].rm[0]: { c: 2 rc: 3 rc: 4 }\n"
  2670. "modified: item[0]: { a: 1 rm { c: 2 rc: 3 rc: 4 }"
  2671. " rm { c: 5 rc: 6 rc: 7 rc: 8 } } ->"
  2672. " { a: 1 rm { c: 5 rc: 8 rc: 8 rc: 6 }"
  2673. " rm { c: 3 rc: 2 rc: 4 } }\n"
  2674. "matched: item[1].a : 9\n"
  2675. "matched: item[1].rm[0].c : 10\n"
  2676. "moved: item[1].rm[0].rc[0] -> item[1].rm[0].rc[1] : 11\n"
  2677. "moved: item[1].rm[0].rc[1] -> item[1].rm[0].rc[0] : 12\n"
  2678. "matched: item[1].rm[0] : { c: 10 rc: 11 rc: 12 }\n"
  2679. "matched: item[1].rm[1].c : 13\n"
  2680. "matched: item[1].rm[1] : { c: 13 }\n"
  2681. "matched: item[1] : { a: 9 rm { c: 10 rc: 11 rc: 12 } rm { c: 13 } }\n",
  2682. RunWithResult(&differencer, msg1, msg2, false));
  2683. }
  2684. TEST_F(MatchingTest, MatchingWorksWithExtensions) {
  2685. protobuf_unittest::TestAllExtensions msg1, msg2;
  2686. protobuf_unittest::TestAllTypes::NestedMessage* nested;
  2687. using protobuf_unittest::repeated_nested_message_extension;
  2688. const FileDescriptor* descriptor;
  2689. const FieldDescriptor* desc;
  2690. const FieldDescriptor* nested_key;
  2691. descriptor = msg1.GetDescriptor()->file();
  2692. desc = descriptor->FindExtensionByName("repeated_nested_message_extension");
  2693. ASSERT_FALSE(desc == NULL);
  2694. nested_key = desc->message_type()->FindFieldByName("bb");
  2695. MessageDifferencer differencer;
  2696. differencer.TreatAsMap(desc, nested_key);
  2697. nested = msg1.AddExtension(repeated_nested_message_extension);
  2698. nested->set_bb(7);
  2699. nested = msg1.AddExtension(repeated_nested_message_extension);
  2700. nested->set_bb(13);
  2701. nested = msg1.AddExtension(repeated_nested_message_extension);
  2702. nested->set_bb(11);
  2703. nested = msg2.AddExtension(repeated_nested_message_extension);
  2704. nested->set_bb(11);
  2705. nested = msg2.AddExtension(repeated_nested_message_extension);
  2706. nested->set_bb(13);
  2707. nested = msg2.AddExtension(repeated_nested_message_extension);
  2708. nested->set_bb(7);
  2709. EXPECT_EQ(
  2710. "matched: (protobuf_unittest.repeated_nested_message_extension)[0].bb ->"
  2711. " (protobuf_unittest.repeated_nested_message_extension)[2].bb : 7\n"
  2712. "moved: (protobuf_unittest.repeated_nested_message_extension)[0] ->"
  2713. " (protobuf_unittest.repeated_nested_message_extension)[2] :"
  2714. " { bb: 7 }\n"
  2715. "matched: (protobuf_unittest.repeated_nested_message_extension)[1].bb :"
  2716. " 13\n"
  2717. "matched: (protobuf_unittest.repeated_nested_message_extension)[1] :"
  2718. " { bb: 13 }\n"
  2719. "matched: (protobuf_unittest.repeated_nested_message_extension)[2].bb ->"
  2720. " (protobuf_unittest.repeated_nested_message_extension)[0].bb :"
  2721. " 11\n"
  2722. "moved: (protobuf_unittest.repeated_nested_message_extension)[2] ->"
  2723. " (protobuf_unittest.repeated_nested_message_extension)[0] :"
  2724. " { bb: 11 }\n",
  2725. RunWithResult(&differencer, msg1, msg2, true));
  2726. }
  2727. TEST(AnyTest, Simple) {
  2728. protobuf_unittest::TestField value1, value2;
  2729. value1.set_a(20);
  2730. value2.set_a(21);
  2731. protobuf_unittest::TestAny m1, m2;
  2732. m1.mutable_any_value()->PackFrom(value1);
  2733. m2.mutable_any_value()->PackFrom(value2);
  2734. util::MessageDifferencer message_differencer;
  2735. string difference_string;
  2736. message_differencer.ReportDifferencesToString(&difference_string);
  2737. EXPECT_FALSE(message_differencer.Compare(m1, m2));
  2738. EXPECT_EQ("modified: any_value.a: 20 -> 21\n", difference_string);
  2739. }
  2740. TEST(Anytest, TreatAsSet) {
  2741. protobuf_unittest::TestField value1, value2;
  2742. value1.set_a(20);
  2743. value1.set_b(30);
  2744. value2.set_a(20);
  2745. value2.set_b(31);
  2746. protobuf_unittest::TestAny m1, m2;
  2747. m1.add_repeated_any_value()->PackFrom(value1);
  2748. m1.add_repeated_any_value()->PackFrom(value2);
  2749. m2.add_repeated_any_value()->PackFrom(value2);
  2750. m2.add_repeated_any_value()->PackFrom(value1);
  2751. util::MessageDifferencer message_differencer;
  2752. message_differencer.TreatAsSet(GetFieldDescriptor(m1, "repeated_any_value"));
  2753. EXPECT_TRUE(message_differencer.Compare(m1, m2));
  2754. }
  2755. TEST(Anytest, TreatAsSet_DifferentType) {
  2756. protobuf_unittest::TestField value1;
  2757. value1.set_a(20);
  2758. value1.set_b(30);
  2759. protobuf_unittest::TestDiffMessage value2;
  2760. value2.add_rv(40);
  2761. protobuf_unittest::TestAny m1, m2;
  2762. m1.add_repeated_any_value()->PackFrom(value1);
  2763. m1.add_repeated_any_value()->PackFrom(value2);
  2764. m2.add_repeated_any_value()->PackFrom(value2);
  2765. m2.add_repeated_any_value()->PackFrom(value1);
  2766. util::MessageDifferencer message_differencer;
  2767. message_differencer.TreatAsSet(GetFieldDescriptor(m1, "repeated_any_value"));
  2768. EXPECT_TRUE(message_differencer.Compare(m1, m2));
  2769. }
  2770. } // namespace
  2771. } // namespace protobuf
  2772. } // namespace google