message_differencer_unittest.cc 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358
  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