| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359 |
- // Protocol Buffers - Google's data interchange format
- // Copyright 2008 Google Inc. All rights reserved.
- // https://developers.google.com/protocol-buffers/
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Google Inc. nor the names of its
- // contributors may be used to endorse or promote products derived from
- // this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- // Author: jschorr@google.com (Joseph Schorr)
- // Based on original Protocol Buffers design by
- // Sanjay Ghemawat, Jeff Dean, and others.
- //
- // TODO(ksroka): Move some of these tests to field_comparator_test.cc.
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <google/protobuf/stubs/strutil.h>
- #include <google/protobuf/util/field_comparator.h>
- #include <google/protobuf/util/message_differencer.h>
- #include <google/protobuf/util/message_differencer_unittest.pb.h>
- #include <google/protobuf/text_format.h>
- #include <google/protobuf/wire_format.h>
- #include <google/protobuf/io/zero_copy_stream_impl.h>
- #include <google/protobuf/io/coded_stream.h>
- #include <google/protobuf/any_test.pb.h>
- #include <google/protobuf/map_unittest.pb.h>
- #include <google/protobuf/unittest.pb.h>
- #include <google/protobuf/map_test_util.h>
- #include <google/protobuf/test_util.h>
- #include <google/protobuf/stubs/logging.h>
- #include <google/protobuf/stubs/common.h>
- #include <google/protobuf/testing/googletest.h>
- #include <gtest/gtest.h>
- namespace google {
- namespace protobuf {
- namespace {
- const FieldDescriptor* GetFieldDescriptor(
- const Message& message, const string& field_name) {
- std::vector<string> field_path =
- Split(field_name, ".", true);
- const Descriptor* descriptor = message.GetDescriptor();
- const FieldDescriptor* field = NULL;
- for (int i = 0; i < field_path.size(); i++) {
- field = descriptor->FindFieldByName(field_path[i]);
- descriptor = field->message_type();
- }
- return field;
- }
- void ExpectEqualsWithDifferencer(util::MessageDifferencer* differencer,
- const Message& msg1,
- const Message& msg2) {
- differencer->set_scope(util::MessageDifferencer::FULL);
- EXPECT_TRUE(differencer->Compare(msg1, msg2));
- differencer->set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer->Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicEqualityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicInequalityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.set_optional_int32(-1);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldInequalityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.add_repeated_int32(-1);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldSetOptimizationTest) {
- util::MessageDifferencer differencer;
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item1 = msg1.add_item();
- protobuf_unittest::TestDiffMessage::Item* item2 = msg2.add_item();
- differencer.TreatAsSet(item1->GetDescriptor()->FindFieldByName("ra"));
- differencer.TreatAsSet(item2->GetDescriptor()->FindFieldByName("ra"));
- for (int i = 0; i < 1000; i++) {
- item1->add_ra(i);
- item2->add_ra(i);
- }
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- item2->add_ra(1001);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- item1->add_ra(1001);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- item1->add_ra(1002);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, MapFieldEqualityTest) {
- // Create the testing protos
- unittest::TestMap msg1;
- unittest::TestMap msg2;
- MapReflectionTester tester(unittest::TestMap::descriptor());
- tester.SetMapFieldsViaReflection(&msg1);
- tester.SetMapFieldsViaReflection(&msg2);
- tester.SwapMapsViaReflection(&msg1);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicPartialEqualityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, PartialEqualityTestExtraField) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.clear_optional_int32();
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, PartialEqualityTestSkipRequiredField) {
- // Create the testing protos
- unittest::TestRequired msg1;
- unittest::TestRequired msg2;
- msg1.set_a(401);
- msg2.set_a(401);
- msg2.set_b(402);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicPartialInequalityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.set_optional_int32(-1);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, PartialInequalityMissingFieldTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg2.clear_optional_int32();
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldPartialInequalityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.add_repeated_int32(-1);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicEquivalencyTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::Equivalent(msg1, msg2));
- }
- TEST(MessageDifferencerTest, EquivalencyNotEqualTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.clear_optional_int32();
- msg2.set_optional_int32(0);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- EXPECT_TRUE(util::MessageDifferencer::Equivalent(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicInequivalencyTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.set_optional_int32(-1);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicEquivalencyNonSetTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::Equivalent(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicInequivalencyNonSetTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- msg1.set_optional_int32(-1);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicPartialEquivalencyTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, PartialEquivalencyNotEqualTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.set_optional_int32(0);
- msg2.clear_optional_int32();
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, PartialEquivalencyTestExtraField) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.clear_optional_int32();
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, PartialEquivalencyTestSkipRequiredField) {
- // Create the testing protos
- unittest::TestRequired msg1;
- unittest::TestRequired msg2;
- msg1.set_a(401);
- msg2.set_a(401);
- msg2.set_b(402);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicPartialInequivalencyTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- msg1.set_optional_int32(-1);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicPartialEquivalencyNonSetTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicPartialInequivalencyNonSetTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- msg1.set_optional_int32(-1);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, ApproximateEqualityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, ApproximateModifiedEqualityTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- const float v1 = 2.300005f;
- const float v2 = 2.300006f;
- msg1.set_optional_float(v1);
- msg2.set_optional_float(v2);
- // Compare
- ASSERT_NE(v1, v2) << "Should not be the same: " << v1 << ", " << v2;
- ASSERT_FLOAT_EQ(v1, v2) << "Should be approx. equal: " << v1 << ", " << v2;
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, ApproximateEquivalencyTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
- msg2));
- }
- TEST(MessageDifferencerTest, ApproximateModifiedEquivalencyTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Modify the approximateness requirement
- const float v1 = 2.300005f;
- const float v2 = 2.300006f;
- msg1.set_optional_float(v1);
- msg2.set_optional_float(v2);
- // Compare
- ASSERT_NE(v1, v2) << "Should not be the same: " << v1 << ", " << v2;
- ASSERT_FLOAT_EQ(v1, v2) << "Should be approx. equal: " << v1 << ", " << v2;
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
- msg2));
- // Modify the equivalency requirement too
- msg1.clear_optional_int32();
- msg2.set_optional_int32(0);
- // Compare. Now should only pass on ApproximatelyEquivalent
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- EXPECT_FALSE(util::MessageDifferencer::Equivalent(msg1, msg2));
- EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquals(msg1, msg2));
- EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
- msg2));
- }
- TEST(MessageDifferencerTest, ApproximateInequivalencyTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Should fail on equivalency
- msg1.set_optional_int32(-1);
- EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
- msg2));
- // Make these fields the same again.
- msg1.set_optional_int32(0);
- msg2.set_optional_int32(0);
- EXPECT_TRUE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
- msg2));
- // Should fail on approximate equality check
- const float v1 = 2.3f;
- const float v2 = 9.3f;
- msg1.set_optional_float(v1);
- msg2.set_optional_float(v2);
- EXPECT_FALSE(util::MessageDifferencer::ApproximatelyEquivalent(msg1,
- msg2));
- }
- TEST(MessageDifferencerTest, WithinFractionOrMarginFloatTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Should fail on approximate equality check
- const float v1 = 100.0f;
- const float v2 = 109.9f;
- msg1.set_optional_float(v1);
- msg2.set_optional_float(v2);
- // Compare
- util::MessageDifferencer differencer;
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- const FieldDescriptor* fd =
- msg1.GetDescriptor()->FindFieldByName("optional_float");
- // Set float comparison to exact, margin and fraction value should not matter.
- differencer.set_float_comparison(util::MessageDifferencer::EXACT);
- // Set margin for float comparison.
- differencer.SetFractionAndMargin(fd, 0.0, 10.0);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Margin and fraction float comparison is activated when float comparison is
- // set to approximate.
- differencer.set_float_comparison(util::MessageDifferencer::APPROXIMATE);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Test out float comparison with fraction.
- differencer.SetFractionAndMargin(fd, 0.2, 0.0);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Should fail since the fraction is smaller than error.
- differencer.SetFractionAndMargin(fd, 0.01, 0.0);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Should pass if either fraction or margin are satisfied.
- differencer.SetFractionAndMargin(fd, 0.01, 10.0);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Make sure that the margin and fraction only affects the field that it was
- // set for.
- msg1.set_default_float(v1);
- msg2.set_default_float(v2);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- msg1.set_default_float(v1);
- msg2.set_default_float(v1);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, WithinFractionOrMarginDoubleTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Should fail on approximate equality check
- const double v1 = 100.0;
- const double v2 = 109.9;
- msg1.set_optional_double(v1);
- msg2.set_optional_double(v2);
- // Compare
- util::MessageDifferencer differencer;
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Set comparison to exact, margin and fraction value should not matter.
- differencer.set_float_comparison(util::MessageDifferencer::EXACT);
- // Set margin for float comparison.
- const FieldDescriptor* fd =
- msg1.GetDescriptor()->FindFieldByName("optional_double");
- differencer.SetFractionAndMargin(fd, 0.0, 10.0);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Margin and fraction comparison is activated when float comparison is
- // set to approximate.
- differencer.set_float_comparison(util::MessageDifferencer::APPROXIMATE);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Test out comparison with fraction.
- differencer.SetFractionAndMargin(fd, 0.2, 0.0);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Should fail since the fraction is smaller than error.
- differencer.SetFractionAndMargin(fd, 0.01, 0.0);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Should pass if either fraction or margin are satisfied.
- differencer.SetFractionAndMargin(fd, 0.01, 10.0);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Make sure that the margin and fraction only affects the field that it was
- // set for.
- msg1.set_default_double(v1);
- msg2.set_default_double(v2);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- msg1.set_default_double(v1);
- msg2.set_default_double(v1);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, WithinDefaultFractionOrMarginDoubleTest) {
- // Create the testing protos
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- // Should fail on approximate equality check
- const double v1 = 100.0;
- const double v2 = 109.9;
- msg1.set_optional_double(v1);
- msg2.set_optional_double(v2);
- util::MessageDifferencer differencer;
- // Compare
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Set up a custom field comparitor, with a default fraction and margin for
- // float and double comparison.
- util::DefaultFieldComparator field_comparitor;
- field_comparitor.SetDefaultFractionAndMargin(0.0, 10.0);
- differencer.set_field_comparator(&field_comparitor);
- // Set comparison to exact, margin and fraction value should not matter.
- field_comparitor.set_float_comparison(util::DefaultFieldComparator::EXACT);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Margin and fraction comparison is activated when float comparison is
- // set to approximate.
- field_comparitor.set_float_comparison(
- util::DefaultFieldComparator::APPROXIMATE);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Test out comparison with fraction.
- field_comparitor.SetDefaultFractionAndMargin(0.2, 0.0);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Should fail since the fraction is smaller than error.
- field_comparitor.SetDefaultFractionAndMargin(0.01, 0.0);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Should pass if either fraction or margin are satisfied.
- field_comparitor.SetDefaultFractionAndMargin(0.01, 10.0);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Make sure that the default margin and fraction affects all fields
- msg1.set_default_double(v1);
- msg2.set_default_double(v2);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicFieldOrderingsTest) {
- // Create the testing protos
- unittest::TestFieldOrderings msg1;
- unittest::TestFieldOrderings msg2;
- TestUtil::SetAllFieldsAndExtensions(&msg1);
- TestUtil::SetAllFieldsAndExtensions(&msg2);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicFieldOrderingInequalityTest) {
- // Create the testing protos
- unittest::TestFieldOrderings msg1;
- unittest::TestFieldOrderings msg2;
- TestUtil::SetAllFieldsAndExtensions(&msg1);
- TestUtil::SetAllFieldsAndExtensions(&msg2);
- msg1.set_my_float(15.00);
- msg2.set_my_float(16.00);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicExtensionTest) {
- // Create the testing protos
- unittest::TestAllExtensions msg1;
- unittest::TestAllExtensions msg2;
- TestUtil::SetAllExtensions(&msg1);
- TestUtil::SetAllExtensions(&msg2);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, BasicExtensionInequalityTest) {
- // Create the testing protos
- unittest::TestAllExtensions msg1;
- unittest::TestAllExtensions msg2;
- TestUtil::SetAllExtensions(&msg1);
- TestUtil::SetAllExtensions(&msg2);
- msg1.SetExtension(unittest::optional_int32_extension, 101);
- msg2.SetExtension(unittest::optional_int32_extension, 102);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, OneofTest) {
- // Create the testing protos
- unittest::TestOneof2 msg1;
- unittest::TestOneof2 msg2;
- TestUtil::SetOneof1(&msg1);
- TestUtil::SetOneof1(&msg2);
- // Compare
- EXPECT_TRUE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, OneofInequalityTest) {
- // Create the testing protos
- unittest::TestOneof2 msg1;
- unittest::TestOneof2 msg2;
- TestUtil::SetOneof1(&msg1);
- TestUtil::SetOneof2(&msg2);
- // Compare
- EXPECT_FALSE(util::MessageDifferencer::Equals(msg1, msg2));
- }
- TEST(MessageDifferencerTest, UnknownFieldPartialEqualTest) {
- unittest::TestEmptyMessage empty1;
- unittest::TestEmptyMessage empty2;
- UnknownFieldSet* unknown1 = empty1.mutable_unknown_fields();
- UnknownFieldSet* unknown2 = empty2.mutable_unknown_fields();
- unknown1->AddVarint(243, 122);
- unknown1->AddLengthDelimited(245, "abc");
- unknown1->AddGroup(246)->AddFixed32(248, 1);
- unknown1->mutable_field(2)->mutable_group()->AddFixed32(248, 2);
- unknown2->AddVarint(243, 122);
- unknown2->AddLengthDelimited(245, "abc");
- unknown2->AddGroup(246)->AddFixed32(248, 1);
- unknown2->mutable_field(2)->mutable_group()->AddFixed32(248, 2);
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(empty1, empty2));
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllTest) {
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- std::vector<const FieldDescriptor*> fields1;
- std::vector<const FieldDescriptor*> fields2;
- msg1.GetReflection()->ListFields(msg1, &fields1);
- msg2.GetReflection()->ListFields(msg2, &fields2);
- util::MessageDifferencer differencer;
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsInequalityAllTest) {
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- std::vector<const FieldDescriptor*> fields1;
- std::vector<const FieldDescriptor*> fields2;
- msg1.GetReflection()->ListFields(msg1, &fields1);
- msg2.GetReflection()->ListFields(msg2, &fields2);
- util::MessageDifferencer differencer;
- EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsEmptyListAlwaysSucceeds) {
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- std::vector<const FieldDescriptor*> empty_fields;
- util::MessageDifferencer differencer;
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
- empty_fields, empty_fields));
- TestUtil::SetAllFields(&msg2);
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
- empty_fields, empty_fields));
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsCompareWithSelf) {
- unittest::TestAllTypes msg1;
- TestUtil::SetAllFields(&msg1);
- std::vector<const FieldDescriptor*> fields;
- msg1.GetReflection()->ListFields(msg1, &fields);
- util::MessageDifferencer differencer;
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1, fields, fields));
- {
- // Compare with a subset of fields.
- std::vector<const FieldDescriptor*> compare_fields;
- for (int i = 0; i < fields.size(); ++i) {
- if (i % 2 == 0) {
- compare_fields.push_back(fields[i]);
- }
- }
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg1,
- compare_fields, compare_fields));
- }
- {
- // Specify a different set of fields to compare, even though we're using the
- // same message. This should fail, since we are explicitly saying that the
- // set of fields are different.
- std::vector<const FieldDescriptor*> compare_fields1;
- std::vector<const FieldDescriptor*> compare_fields2;
- for (int i = 0; i < fields.size(); ++i) {
- if (i % 2 == 0) {
- compare_fields1.push_back(fields[i]);
- } else {
- compare_fields2.push_back(fields[i]);
- }
- }
- EXPECT_FALSE(differencer.CompareWithFields(
- msg1, msg1, compare_fields1, compare_fields2));
- }
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsEqualityAllShuffledTest) {
- // This is a public function, so make sure there are no assumptions about the
- // list of fields. Randomly shuffle them to make sure that they are properly
- // ordered for comparison.
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- std::vector<const FieldDescriptor*> fields1;
- std::vector<const FieldDescriptor*> fields2;
- msg1.GetReflection()->ListFields(msg1, &fields1);
- msg2.GetReflection()->ListFields(msg2, &fields2);
- std::random_shuffle(fields1.begin(), fields1.end());
- std::random_shuffle(fields2.begin(), fields2.end());
- util::MessageDifferencer differencer;
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsSubsetEqualityTest) {
- // Specify a set of fields to compare. All the fields are equal.
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- std::vector<const FieldDescriptor*> fields1;
- msg1.GetReflection()->ListFields(msg1, &fields1);
- std::vector<const FieldDescriptor*> compare_fields;
- // Only compare the field descriptors with even indices.
- for (int i = 0; i < fields1.size(); ++i) {
- if (i % 2 == 0) {
- compare_fields.push_back(fields1[i]);
- }
- }
- util::MessageDifferencer differencer;
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
- compare_fields, compare_fields));
- }
- TEST(MessageDifferencerTest,
- SpecifiedFieldsSubsetIgnoresOtherFieldDifferencesTest) {
- // Specify a set of fields to compare, but clear all the other fields in one
- // of the messages. This should fail a regular compare, but CompareWithFields
- // should succeed.
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- std::vector<const FieldDescriptor*> fields1;
- const Reflection* reflection = msg1.GetReflection();
- reflection->ListFields(msg1, &fields1);
- std::vector<const FieldDescriptor*> compare_fields;
- // Only compare the field descriptors with even indices.
- for (int i = 0; i < fields1.size(); ++i) {
- if (i % 2 == 0) {
- compare_fields.push_back(fields1[i]);
- } else {
- reflection->ClearField(&msg2, fields1[i]);
- }
- }
- util::MessageDifferencer differencer;
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2,
- compare_fields, compare_fields));
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsDetectsDifferencesTest) {
- // Change all of the repeated fields in one of the messages, and use only
- // those fields for comparison.
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- TestUtil::ModifyRepeatedFields(&msg2);
- std::vector<const FieldDescriptor*> fields1;
- msg1.GetReflection()->ListFields(msg1, &fields1);
- std::vector<const FieldDescriptor*> compare_fields;
- // Only compare the repeated field descriptors.
- for (int i = 0; i < fields1.size(); ++i) {
- if (fields1[i]->is_repeated()) {
- compare_fields.push_back(fields1[i]);
- }
- }
- util::MessageDifferencer differencer;
- EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2,
- compare_fields, compare_fields));
- }
- TEST(MessageDifferencerTest, SpecifiedFieldsEquivalenceAllTest) {
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- TestUtil::SetAllFields(&msg1);
- TestUtil::SetAllFields(&msg2);
- std::vector<const FieldDescriptor*> fields1;
- std::vector<const FieldDescriptor*> fields2;
- msg1.GetReflection()->ListFields(msg1, &fields1);
- msg2.GetReflection()->ListFields(msg2, &fields2);
- util::MessageDifferencer differencer;
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
- }
- TEST(MessageDifferencerTest,
- SpecifiedFieldsEquivalenceIgnoresOtherFieldDifferencesTest) {
- unittest::TestAllTypes msg1;
- unittest::TestAllTypes msg2;
- const Descriptor* desc = msg1.GetDescriptor();
- const FieldDescriptor* optional_int32_desc =
- desc->FindFieldByName("optional_int32");
- const FieldDescriptor* optional_int64_desc =
- desc->FindFieldByName("optional_int64");
- const FieldDescriptor* default_int64_desc =
- desc->FindFieldByName("default_int64");
- ASSERT_TRUE(optional_int32_desc != NULL);
- ASSERT_TRUE(optional_int64_desc != NULL);
- ASSERT_TRUE(default_int64_desc != NULL);
- msg1.set_optional_int32(0);
- msg2.set_optional_int64(0);
- msg1.set_default_int64(default_int64_desc->default_value_int64());
- // Set a field to a non-default value so we know that field selection is
- // actually doing something.
- msg2.set_optional_uint64(23);
- std::vector<const FieldDescriptor*> fields1;
- std::vector<const FieldDescriptor*> fields2;
- fields1.push_back(optional_int32_desc);
- fields1.push_back(default_int64_desc);
- fields2.push_back(optional_int64_desc);
- util::MessageDifferencer differencer;
- EXPECT_FALSE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields1, fields2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldSetTest_SetOfSet) {
- // Create the testing protos
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->add_ra(1); item->add_ra(2); item->add_ra(3);
- item = msg1.add_item();
- item->add_ra(5); item->add_ra(6);
- item = msg1.add_item();
- item->add_ra(1); item->add_ra(3);
- item = msg1.add_item();
- item->add_ra(6); item->add_ra(7); item->add_ra(8);
- item = msg2.add_item();
- item->add_ra(6); item->add_ra(5);
- item = msg2.add_item();
- item->add_ra(6); item->add_ra(8); item->add_ra(7);
- item = msg2.add_item();
- item->add_ra(1); item->add_ra(3);
- item = msg2.add_item();
- item->add_ra(3); item->add_ra(2); item->add_ra(1);
- // Compare
- util::MessageDifferencer differencer;
- differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldSetTest_Combination) {
- // Create the testing protos
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- // Treat "item" as Map, with key = "a"
- // Treat "item.ra" also as Set
- // Treat "rv" as Set
- // Treat "rw" as List
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->set_a(3);
- item->add_ra(1); item->add_ra(2); item->add_ra(3);
- item = msg1.add_item();
- item->set_a(4);
- item->add_ra(5); item->add_ra(6);
- item = msg1.add_item();
- item->set_a(1);
- item->add_ra(1); item->add_ra(3);
- item = msg1.add_item();
- item->set_a(2);
- item->add_ra(6); item->add_ra(7); item->add_ra(8);
- item = msg2.add_item();
- item->set_a(4);
- item->add_ra(6); item->add_ra(5);
- item = msg2.add_item();
- item->set_a(2);
- item->add_ra(6); item->add_ra(8); item->add_ra(7);
- item = msg2.add_item();
- item->set_a(1);
- item->add_ra(1); item->add_ra(3);
- item = msg2.add_item();
- item->set_a(3);
- item->add_ra(3); item->add_ra(2); item->add_ra(1);
- msg1.add_rv(3);
- msg1.add_rv(4);
- msg1.add_rv(7);
- msg1.add_rv(0);
- msg2.add_rv(4);
- msg2.add_rv(3);
- msg2.add_rv(0);
- msg2.add_rv(7);
- msg1.add_rw("nothing"); msg2.add_rw("nothing");
- msg1.add_rw("should"); msg2.add_rw("should");
- msg1.add_rw("change"); msg2.add_rw("change");
- // Compare
- util::MessageDifferencer differencer1;
- differencer1.TreatAsMap(msg1.GetDescriptor()->FindFieldByName("item"),
- item->GetDescriptor()->FindFieldByName("a"));
- differencer1.TreatAsSet(msg1.GetDescriptor()->FindFieldByName("rv"));
- differencer1.TreatAsSet(item->GetDescriptor()->FindFieldByName("ra"));
- EXPECT_TRUE(differencer1.Compare(msg1, msg2));
- util::MessageDifferencer differencer2;
- differencer2.TreatAsMap(msg1.GetDescriptor()->FindFieldByName("item"),
- item->GetDescriptor()->FindFieldByName("a"));
- differencer2.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
- differencer2.TreatAsList(msg1.GetDescriptor()->FindFieldByName("rw"));
- EXPECT_TRUE(differencer2.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldMapTest_Partial) {
- protobuf_unittest::TestDiffMessage msg1;
- // message msg1 {
- // item { a: 1; b: "11" }
- // }
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->set_a(1);
- item->set_b("11");
- protobuf_unittest::TestDiffMessage msg2;
- // message msg2 {
- // item { a: 2; b: "22" }
- // item { a: 1; b: "11" }
- // }
- item = msg2.add_item();
- item->set_a(2);
- item->set_b("22");
- item = msg2.add_item();
- item->set_a(1);
- item->set_b("11");
- // Compare
- util::MessageDifferencer differencer;
- differencer.TreatAsMap(GetFieldDescriptor(msg1, "item"),
- GetFieldDescriptor(msg1, "item.a"));
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldSetTest_Duplicates) {
- protobuf_unittest::TestDiffMessage a, b, c;
- // message a: {
- // rv: 0
- // rv: 1
- // rv: 0
- // }
- a.add_rv(0);
- a.add_rv(1);
- a.add_rv(0);
- // message b: {
- // rv: 0
- // rv: 0
- // rv: 1
- // }
- b.add_rv(0);
- b.add_rv(0);
- b.add_rv(1);
- // message c: {
- // rv: 0
- // rv: 1
- // }
- c.add_rv(0);
- c.add_rv(1);
- util::MessageDifferencer differencer;
- differencer.TreatAsSet(GetFieldDescriptor(a, "rv"));
- EXPECT_TRUE(differencer.Compare(b, a));
- EXPECT_FALSE(differencer.Compare(c, a));
- util::MessageDifferencer differencer1;
- differencer1.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
- EXPECT_TRUE(differencer1.Compare(b, a));
- EXPECT_FALSE(differencer1.Compare(c, a));
- }
- TEST(MessageDifferencerTest, RepeatedFieldSetTest_PartialSimple) {
- protobuf_unittest::TestDiffMessage a, b, c;
- // message a: {
- // rm { c: 1 }
- // rm { c: 0 }
- // }
- a.add_rm()->set_c(1);
- a.add_rm()->set_c(0);
- // message b: {
- // rm { c: 1 }
- // rm {}
- // }
- b.add_rm()->set_c(1);
- b.add_rm();
- // message c: {
- // rm {}
- // rm { c: 1 }
- // }
- c.add_rm();
- c.add_rm()->set_c(1);
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- differencer.TreatAsSet(GetFieldDescriptor(a, "rm"));
- EXPECT_TRUE(differencer.Compare(b, a));
- EXPECT_TRUE(differencer.Compare(c, a));
- }
- TEST(MessageDifferencerTest, RepeatedFieldSetTest_Partial) {
- protobuf_unittest::TestDiffMessage msg1, msg2;
- // message msg1: {
- // rm { a: 1 }
- // rm { b: 2 }
- // rm { c: 3 }
- // }
- msg1.add_rm()->set_a(1);
- msg1.add_rm()->set_b(2);
- msg1.add_rm()->set_c(3);
- // message msg2: {
- // rm { a: 1; c: 3 }
- // rm { b: 2; c: 3 }
- // rm { b: 2 }
- // }
- protobuf_unittest::TestField* field = msg2.add_rm();
- field->set_a(1);
- field->set_c(3);
- field = msg2.add_rm();
- field->set_b(2);
- field->set_c(3);
- field = msg2.add_rm();
- field->set_b(2);
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- differencer.TreatAsSet(GetFieldDescriptor(msg1, "rm"));
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, RepeatedFieldMapTest_MultipleFieldsAsKey) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- // Treat "item" as Map, with key = ("a", "ra")
- // Treat "item.ra" as Set
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- // key => value: (1, {2, 3}) => "a"
- item->set_a(1);
- item->add_ra(2);
- item->add_ra(3);
- item->set_b("a");
- item = msg1.add_item();
- // key => value: (2, {1, 3}) => "b"
- item->set_a(2);
- item->add_ra(1);
- item->add_ra(3);
- item->set_b("b");
- item = msg1.add_item();
- // key => value: (1, {1, 3}) => "c"
- item->set_a(1);
- item->add_ra(1);
- item->add_ra(3);
- item->set_b("c");
- item = msg2.add_item();
- // key => value: (1, {1, 3}) => "c"
- item->set_a(1);
- item->add_ra(3);
- item->add_ra(1);
- item->set_b("c");
- item = msg2.add_item();
- // key => value: (1, {2, 3}) => "a"
- item->set_a(1);
- item->add_ra(3);
- item->add_ra(2);
- item->set_b("a");
- item = msg2.add_item();
- // key => value: (2, {1, 3}) => "b"
- item->set_a(2);
- item->add_ra(3);
- item->add_ra(1);
- item->set_b("b");
- // Compare
- util::MessageDifferencer differencer;
- differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.ra"));
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- std::vector<const FieldDescriptor*> key_fields;
- key_fields.push_back(GetFieldDescriptor(msg1, "item.a"));
- key_fields.push_back(GetFieldDescriptor(msg1, "item.ra"));
- differencer.TreatAsMapWithMultipleFieldsAsKey(
- GetFieldDescriptor(msg1, "item"), key_fields);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Introduce some differences.
- msg1.clear_item();
- msg2.clear_item();
- item = msg1.add_item();
- item->set_a(4);
- item->add_ra(5);
- item->add_ra(6);
- item->set_b("hello");
- item = msg2.add_item();
- item->set_a(4);
- item->add_ra(6);
- item->add_ra(5);
- item->set_b("world");
- string output;
- differencer.ReportDifferencesToString(&output);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "moved: item[0].ra[0] -> item[0].ra[1] : 5\n"
- "moved: item[0].ra[1] -> item[0].ra[0] : 6\n"
- "modified: item[0].b: \"hello\" -> \"world\"\n",
- output);
- }
- TEST(MessageDifferencerTest, RepeatedFieldMapTest_MultipleFieldPathsAsKey) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- // Treat "item" as Map, with key = ("m.a", "m.rc")
- // Treat "item.m.rc" as Set
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- // key => value: (1, {2, 3}) => "a"
- item->mutable_m()->set_a(1);
- item->mutable_m()->add_rc(2);
- item->mutable_m()->add_rc(3);
- item->set_b("a");
- item = msg1.add_item();
- // key => value: (2, {1, 3}) => "b"
- item->mutable_m()->set_a(2);
- item->mutable_m()->add_rc(1);
- item->mutable_m()->add_rc(3);
- item->set_b("b");
- item = msg1.add_item();
- // key => value: (1, {1, 3}) => "c"
- item->mutable_m()->set_a(1);
- item->mutable_m()->add_rc(1);
- item->mutable_m()->add_rc(3);
- item->set_b("c");
- item = msg2.add_item();
- // key => value: (1, {1, 3}) => "c"
- item->mutable_m()->set_a(1);
- item->mutable_m()->add_rc(3);
- item->mutable_m()->add_rc(1);
- item->set_b("c");
- item = msg2.add_item();
- // key => value: (1, {2, 3}) => "a"
- item->mutable_m()->set_a(1);
- item->mutable_m()->add_rc(3);
- item->mutable_m()->add_rc(2);
- item->set_b("a");
- item = msg2.add_item();
- // key => value: (2, {1, 3}) => "b"
- item->mutable_m()->set_a(2);
- item->mutable_m()->add_rc(3);
- item->mutable_m()->add_rc(1);
- item->set_b("b");
- // Compare
- util::MessageDifferencer differencer;
- differencer.TreatAsSet(GetFieldDescriptor(msg1, "item.m.rc"));
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- std::vector<std::vector<const FieldDescriptor*> > key_field_paths;
- std::vector<const FieldDescriptor*> key_field_path1;
- key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m"));
- key_field_path1.push_back(GetFieldDescriptor(msg1, "item.m.a"));
- std::vector<const FieldDescriptor*> key_field_path2;
- key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m"));
- key_field_path2.push_back(GetFieldDescriptor(msg1, "item.m.rc"));
- key_field_paths.push_back(key_field_path1);
- key_field_paths.push_back(key_field_path2);
- differencer.TreatAsMapWithMultipleFieldPathsAsKey(
- GetFieldDescriptor(msg1, "item"), key_field_paths);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- // Introduce some differences.
- msg1.clear_item();
- msg2.clear_item();
- item = msg1.add_item();
- item->mutable_m()->set_a(4);
- item->mutable_m()->add_rc(5);
- item->mutable_m()->add_rc(6);
- item->set_b("hello");
- item = msg2.add_item();
- item->mutable_m()->set_a(4);
- item->mutable_m()->add_rc(6);
- item->mutable_m()->add_rc(5);
- item->set_b("world");
- string output;
- differencer.ReportDifferencesToString(&output);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "modified: item[0].b: \"hello\" -> \"world\"\n"
- "moved: item[0].m.rc[0] -> item[0].m.rc[1] : 5\n"
- "moved: item[0].m.rc[1] -> item[0].m.rc[0] : 6\n",
- output);
- }
- TEST(MessageDifferencerTest, RepeatedFieldMapTest_IgnoredKeyFields) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- // Treat "item" as Map, with key = ("a", "ra")
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->set_a(1);
- item->add_ra(2);
- item->set_b("hello");
- item = msg2.add_item();
- item->set_a(1);
- item->add_ra(3);
- item->set_b("world");
- // Compare
- util::MessageDifferencer differencer;
- std::vector<const FieldDescriptor*> key_fields;
- key_fields.push_back(GetFieldDescriptor(msg1, "item.a"));
- key_fields.push_back(GetFieldDescriptor(msg1, "item.ra"));
- differencer.TreatAsMapWithMultipleFieldsAsKey(
- GetFieldDescriptor(msg1, "item"), key_fields);
- string output;
- differencer.ReportDifferencesToString(&output);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "added: item[0]: { a: 1 ra: 3 b: \"world\" }\n"
- "deleted: item[0]: { a: 1 ra: 2 b: \"hello\" }\n",
- output);
- // Ignored fields that are listed as parts of the key are still used
- // in key comparison, but they're not used in value comparison.
- differencer.IgnoreField(GetFieldDescriptor(msg1, "item.ra"));
- output.clear();
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "added: item[0]: { a: 1 ra: 3 b: \"world\" }\n"
- "deleted: item[0]: { a: 1 ra: 2 b: \"hello\" }\n",
- output);
- // Ignoring a field in the key is different from treating the left fields
- // as key. That is:
- // (key = ("a", "ra") && ignore "ra") != (key = ("a") && ignore "ra")
- util::MessageDifferencer differencer2;
- differencer2.TreatAsMap(GetFieldDescriptor(msg1, "item"),
- GetFieldDescriptor(msg1, "item.a"));
- differencer2.IgnoreField(GetFieldDescriptor(msg1, "item.ra"));
- output.clear();
- differencer2.ReportDifferencesToString(&output);
- EXPECT_FALSE(differencer2.Compare(msg1, msg2));
- EXPECT_EQ(
- "ignored: item[0].ra\n"
- "modified: item[0].b: \"hello\" -> \"world\"\n",
- output);
- }
- static const char* const kIgnoredFields[] = {"rm.b", "rm.m.b"};
- class TestIgnorer : public util::MessageDifferencer::IgnoreCriteria {
- public:
- virtual bool IsIgnored(
- const Message& message1, const Message& message2,
- const FieldDescriptor* field,
- const std::vector<util::MessageDifferencer::SpecificField>&
- parent_fields) {
- string name = "";
- for (int i = 0; i < parent_fields.size(); ++i) {
- name += parent_fields[i].field->name() + ".";
- }
- name += field->name();
- for (int i = 0; i < GOOGLE_ARRAYSIZE(kIgnoredFields); ++i) {
- if (name.compare(kIgnoredFields[i]) == 0) {
- return true;
- }
- }
- return false;
- }
- };
- TEST(MessageDifferencerTest, TreatRepeatedFieldAsSetWithIgnoredFields) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- TextFormat::MergeFromString("rm { a: 11\n b: 12 }", &msg1);
- TextFormat::MergeFromString("rm { a: 11\n b: 13 }", &msg2);
- util::MessageDifferencer differ;
- differ.TreatAsSet(GetFieldDescriptor(msg1, "rm"));
- differ.AddIgnoreCriteria(new TestIgnorer);
- EXPECT_TRUE(differ.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, TreatRepeatedFieldAsMapWithIgnoredKeyFields) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- TextFormat::MergeFromString("rm { a: 11\n m { a: 12\n b: 13\n } }", &msg1);
- TextFormat::MergeFromString("rm { a: 11\n m { a: 12\n b: 14\n } }", &msg2);
- util::MessageDifferencer differ;
- differ.TreatAsMap(GetFieldDescriptor(msg1, "rm"),
- GetFieldDescriptor(msg1, "rm.m"));
- differ.AddIgnoreCriteria(new TestIgnorer);
- EXPECT_TRUE(differ.Compare(msg1, msg2));
- }
- // Takes the product of all elements of item.ra as the key for key comparison.
- class ValueProductMapKeyComparator
- : public util::MessageDifferencer::MapKeyComparator {
- public:
- typedef util::MessageDifferencer::SpecificField SpecificField;
- virtual bool IsMatch(
- const Message &message1, const Message &message2,
- const std::vector<SpecificField>& parent_fields) const {
- const Reflection* reflection1 = message1.GetReflection();
- const Reflection* reflection2 = message2.GetReflection();
- // FieldDescriptor for item.ra
- const FieldDescriptor* ra_field =
- message1.GetDescriptor()->FindFieldByName("ra");
- // Get the product of all elements in item.ra
- int result1 = 1, result2 = 1;
- for (int i = 0; i < reflection1->FieldSize(message1, ra_field); ++i) {
- result1 *= reflection1->GetRepeatedInt32(message1, ra_field, i);
- }
- for (int i = 0; i < reflection2->FieldSize(message2, ra_field); ++i) {
- result2 *= reflection2->GetRepeatedInt32(message2, ra_field, i);
- }
- return result1 == result2;
- }
- };
- TEST(MessageDifferencerTest, RepeatedFieldMapTest_CustomMapKeyComparator) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- // Treat "item" as Map, using custom key comparator to determine if two
- // elements have the same key.
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->add_ra(6);
- item->add_ra(35);
- item->set_b("hello");
- item = msg2.add_item();
- item->add_ra(10);
- item->add_ra(21);
- item->set_b("hello");
- util::MessageDifferencer differencer;
- ValueProductMapKeyComparator key_comparator;
- differencer.TreatAsMapUsingKeyComparator(
- GetFieldDescriptor(msg1, "item"), &key_comparator);
- string output;
- differencer.ReportDifferencesToString(&output);
- // Though the above two messages have different values for item.ra, they
- // are regarded as having the same key because 6 * 35 == 10 * 21. That's
- // how the key comparator determines if the two have the same key.
- // However, in value comparison, all fields of the message are taken into
- // consideration, so they are different because their item.ra fields have
- // different values using normal value comparison.
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "modified: item[0].ra[0]: 6 -> 10\n"
- "modified: item[0].ra[1]: 35 -> 21\n",
- output);
- differencer.IgnoreField(GetFieldDescriptor(msg1, "item.ra"));
- output.clear();
- // item.ra is ignored in value comparison, so the two messages equal.
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- EXPECT_EQ("ignored: item[0].ra\n", output);
- }
- // Compares fields by their index offset by one, so index 0 matches with 1, etc.
- class OffsetByOneMapKeyComparator
- : public util::MessageDifferencer::MapKeyComparator {
- public:
- typedef util::MessageDifferencer::SpecificField SpecificField;
- virtual bool IsMatch(const Message& message1, const Message& message2,
- const std::vector<SpecificField>& parent_fields) const {
- return parent_fields.back().index + 1 == parent_fields.back().new_index;
- }
- };
- TEST(MessageDifferencerTest, RepeatedFieldMapTest_CustomIndexMapKeyComparator) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- // Treat "item" as Map, using custom key comparator to determine if two
- // elements have the same key.
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->set_b("one");
- item = msg2.add_item();
- item->set_b("zero");
- item = msg2.add_item();
- item->set_b("one");
- util::MessageDifferencer differencer;
- OffsetByOneMapKeyComparator key_comparator;
- differencer.TreatAsMapUsingKeyComparator(GetFieldDescriptor(msg1, "item"),
- &key_comparator);
- string output;
- differencer.ReportDifferencesToString(&output);
- // With the offset by one comparator msg1.item[0] should be compared to
- // msg2.item[1] and thus be moved, msg2.item[0] should be marked as added.
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "moved: item[0] -> item[1] : { b: \"one\" }\n"
- "added: item[0]: { b: \"zero\" }\n",
- output);
- }
- TEST(MessageDifferencerTest, RepeatedFieldSetTest_Subset) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- msg1.add_rv(3);
- msg1.add_rv(8);
- msg1.add_rv(2);
- msg2.add_rv(2);
- msg2.add_rv(3);
- msg2.add_rv(5);
- msg2.add_rv(8);
- util::MessageDifferencer differencer;
- // Fail with only partial scope set.
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_LIST);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Fail with only set-like comparison set.
- differencer.set_scope(util::MessageDifferencer::FULL);
- differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- // Succeed with scope and repeated field comparison set properly.
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- differencer.set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, IgnoreField_Single) {
- protobuf_unittest::TestField msg1;
- protobuf_unittest::TestField msg2;
- msg1.set_c(3);
- msg1.add_rc(1);
- msg2.set_c(5);
- msg2.add_rc(1);
- util::MessageDifferencer differencer;
- differencer.IgnoreField(GetFieldDescriptor(msg1, "c"));
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_Repeated) {
- protobuf_unittest::TestField msg1;
- protobuf_unittest::TestField msg2;
- msg1.set_c(3);
- msg1.add_rc(1);
- msg1.add_rc(2);
- msg2.set_c(3);
- msg2.add_rc(1);
- msg2.add_rc(3);
- util::MessageDifferencer differencer;
- differencer.IgnoreField(GetFieldDescriptor(msg1, "rc"));
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_Message) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestField* field;
- field = msg1.add_rm();
- field->set_c(3);
- field = msg2.add_rm();
- field->set_c(4);
- util::MessageDifferencer differencer;
- differencer.IgnoreField(GetFieldDescriptor(msg1, "rm"));
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_Group) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item;
- item = msg1.add_item();
- item->set_a(3);
- item = msg2.add_item();
- item->set_a(4);
- util::MessageDifferencer differencer;
- differencer.IgnoreField(GetFieldDescriptor(msg1, "item"));
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_Missing) {
- protobuf_unittest::TestField msg1;
- protobuf_unittest::TestField msg2;
- msg1.set_c(3);
- msg1.add_rc(1);
- msg2.add_rc(1);
- util::MessageDifferencer differencer;
- differencer.IgnoreField(GetFieldDescriptor(msg1, "c"));
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- ExpectEqualsWithDifferencer(&differencer, msg2, msg1);
- }
- TEST(MessageDifferencerTest, IgnoreField_Multiple) {
- protobuf_unittest::TestField msg1;
- protobuf_unittest::TestField msg2;
- msg1.set_c(3);
- msg1.add_rc(1);
- msg1.add_rc(2);
- msg2.set_c(5);
- msg2.add_rc(1);
- msg2.add_rc(3);
- const FieldDescriptor* c = GetFieldDescriptor(msg1, "c");
- const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc");
- { // Ignore c
- util::MessageDifferencer differencer;
- differencer.IgnoreField(c);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- { // Ignore rc
- util::MessageDifferencer differencer;
- differencer.IgnoreField(rc);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- { // Ignore both
- util::MessageDifferencer differencer;
- differencer.IgnoreField(c);
- differencer.IgnoreField(rc);
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- }
- TEST(MessageDifferencerTest, IgnoreField_NestedMessage) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestField* field;
- field = msg1.add_rm();
- field->set_c(3);
- field->add_rc(1);
- field = msg2.add_rm();
- field->set_c(4);
- field->add_rc(1);
- util::MessageDifferencer differencer;
- differencer.IgnoreField(GetFieldDescriptor(msg1, "rm.c"));
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_NestedGroup) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item;
- item = msg1.add_item();
- item->set_a(3);
- item->set_b("foo");
- item = msg2.add_item();
- item->set_a(4);
- item->set_b("foo");
- util::MessageDifferencer differencer;
- differencer.IgnoreField(GetFieldDescriptor(msg1, "item.a"));
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_InsideSet) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item;
- item = msg1.add_item();
- item->set_a(1);
- item->set_b("foo");
- item->add_ra(1);
- item = msg1.add_item();
- item->set_a(2);
- item->set_b("bar");
- item->add_ra(2);
- item = msg2.add_item();
- item->set_a(2);
- item->set_b("bar");
- item->add_ra(2);
- item = msg2.add_item();
- item->set_a(1);
- item->set_b("baz");
- item->add_ra(1);
- const FieldDescriptor* item_desc = GetFieldDescriptor(msg1, "item");
- const FieldDescriptor* b = GetFieldDescriptor(msg1, "item.b");
- util::MessageDifferencer differencer;
- differencer.IgnoreField(b);
- differencer.TreatAsSet(item_desc);
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_InsideMap) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item;
- item = msg1.add_item();
- item->set_a(1);
- item->set_b("foo");
- item->add_ra(1);
- item = msg1.add_item();
- item->set_a(2);
- item->set_b("bar");
- item->add_ra(2);
- item = msg2.add_item();
- item->set_a(2);
- item->set_b("bar");
- item->add_ra(2);
- item = msg2.add_item();
- item->set_a(1);
- item->set_b("baz");
- item->add_ra(1);
- const FieldDescriptor* item_desc = GetFieldDescriptor(msg1, "item");
- const FieldDescriptor* a = GetFieldDescriptor(msg1, "item.a");
- const FieldDescriptor* b = GetFieldDescriptor(msg1, "item.b");
- util::MessageDifferencer differencer;
- differencer.IgnoreField(b);
- differencer.TreatAsMap(item_desc, a);
- ExpectEqualsWithDifferencer(&differencer, msg1, msg2);
- }
- TEST(MessageDifferencerTest, IgnoreField_DoesNotIgnoreKey) {
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item;
- item = msg1.add_item();
- item->set_a(1);
- item->set_b("foo");
- item->add_ra(1);
- item = msg2.add_item();
- item->set_a(2);
- item->set_b("foo");
- item->add_ra(1);
- const FieldDescriptor* item_desc = GetFieldDescriptor(msg1, "item");
- const FieldDescriptor* a = GetFieldDescriptor(msg1, "item.a");
- util::MessageDifferencer differencer;
- differencer.IgnoreField(a);
- differencer.TreatAsMap(item_desc, a);
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- }
- TEST(MessageDifferencerTest, IgnoreField_TrumpsCompareWithFields) {
- protobuf_unittest::TestField msg1;
- protobuf_unittest::TestField msg2;
- msg1.set_c(3);
- msg1.add_rc(1);
- msg1.add_rc(2);
- msg2.set_c(3);
- msg2.add_rc(1);
- msg2.add_rc(3);
- const FieldDescriptor* c = GetFieldDescriptor(msg1, "c");
- const FieldDescriptor* rc = GetFieldDescriptor(msg1, "rc");
- std::vector<const FieldDescriptor*> fields;
- fields.push_back(c);
- fields.push_back(rc);
- util::MessageDifferencer differencer;
- differencer.IgnoreField(rc);
- differencer.set_scope(util::MessageDifferencer::FULL);
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields, fields));
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.CompareWithFields(msg1, msg2, fields, fields));
- }
- // Test class to save a copy of the last field_context.parent_fields() vector
- // passed to the comparison function.
- class ParentSavingFieldComparator : public util::FieldComparator {
- public:
- ParentSavingFieldComparator() {}
- virtual ComparisonResult Compare(
- const google::protobuf::Message& message_1,
- const google::protobuf::Message& message_2,
- const google::protobuf::FieldDescriptor* field,
- int index_1, int index_2,
- const google::protobuf::util::FieldContext* field_context) {
- if (field_context)
- parent_fields_ = *(field_context->parent_fields());
- if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
- return RECURSE;
- } else {
- return SAME;
- }
- }
- std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_fields() {
- return parent_fields_;
- }
- private:
- std::vector<google::protobuf::util::MessageDifferencer::SpecificField> parent_fields_;
- };
- // Tests if MessageDifferencer sends the parent fields in the FieldContext
- // parameter.
- TEST(MessageDifferencerTest, FieldContextParentFieldsTest) {
- protobuf_unittest::TestDiffMessage msg1;
- msg1.add_rm()->set_c(1);
- protobuf_unittest::TestDiffMessage msg2;
- msg2.add_rm()->set_c(1);
- ParentSavingFieldComparator field_comparator;
- util::MessageDifferencer differencer;
- differencer.set_field_comparator(&field_comparator);
- differencer.Compare(msg1, msg2);
- // We want only one parent with the name "rm"
- ASSERT_EQ(1, field_comparator.parent_fields().size());
- EXPECT_EQ("rm", field_comparator.parent_fields()[0].field->name());
- }
- class ComparisonTest : public testing::Test {
- protected:
- ComparisonTest() : use_equivalency_(false), repeated_field_as_set_(false) {
- // Setup the test.
- TestUtil::SetAllFields(&proto1_);
- TestUtil::SetAllFields(&proto2_);
- TestUtil::SetAllExtensions(&proto1ex_);
- TestUtil::SetAllExtensions(&proto2ex_);
- TestUtil::SetAllFieldsAndExtensions(&orderings_proto1_);
- TestUtil::SetAllFieldsAndExtensions(&orderings_proto2_);
- unknown1_ = empty1_.mutable_unknown_fields();
- unknown2_ = empty2_.mutable_unknown_fields();
- }
- ~ComparisonTest() { }
- void SetSpecialFieldOption(const Message& message,
- util::MessageDifferencer* d) {
- if (!ignored_field_.empty()) {
- d->IgnoreField(GetFieldDescriptor(message, ignored_field_));
- }
- if (repeated_field_as_set_) {
- d->set_repeated_field_comparison(util::MessageDifferencer::AS_SET);
- }
- if (!set_field_.empty()) {
- d->TreatAsSet(GetFieldDescriptor(message, set_field_));
- }
- if (!map_field_.empty() && !map_key_.empty()) {
- d->TreatAsMap(GetFieldDescriptor(message, map_field_),
- GetFieldDescriptor(message, map_field_ + "." + map_key_));
- }
- }
- string Run(const Message& msg1, const Message& msg2) {
- string output;
- // Setup the comparison.
- util::MessageDifferencer differencer;
- differencer.ReportDifferencesToString(&output);
- if (use_equivalency_) {
- differencer.set_message_field_comparison(
- util::MessageDifferencer::EQUIVALENT);
- }
- SetSpecialFieldOption(msg1, &differencer);
- // Conduct the comparison.
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- return output;
- }
- string Run() {
- return Run(proto1_, proto2_);
- }
- string RunOrder() {
- return Run(orderings_proto1_, orderings_proto2_);
- }
- string RunEx() {
- return Run(proto1ex_, proto2ex_);
- }
- string RunDiff() {
- return Run(proto1diff_, proto2diff_);
- }
- string RunUn() {
- return Run(empty1_, empty2_);
- }
- void use_equivalency() {
- use_equivalency_ = true;
- }
- void repeated_field_as_set() {
- repeated_field_as_set_ = true;
- }
- void field_as_set(const string& field) {
- set_field_ = field;
- }
- void field_as_map(const string& field, const string& key) {
- map_field_ = field;
- map_key_ = key;
- }
- void ignore_field(const string& field) {
- ignored_field_ = field;
- }
- unittest::TestAllTypes proto1_;
- unittest::TestAllTypes proto2_;
- unittest::TestFieldOrderings orderings_proto1_;
- unittest::TestFieldOrderings orderings_proto2_;
- unittest::TestAllExtensions proto1ex_;
- unittest::TestAllExtensions proto2ex_;
- unittest::TestDiffMessage proto1diff_;
- unittest::TestDiffMessage proto2diff_;
- unittest::TestEmptyMessage empty1_;
- unittest::TestEmptyMessage empty2_;
- unittest::TestMap map_proto1_;
- unittest::TestMap map_proto2_;
- UnknownFieldSet* unknown1_;
- UnknownFieldSet* unknown2_;
- bool use_equivalency_;
- bool repeated_field_as_set_;
- string set_field_;
- string map_field_;
- string map_key_;
- string ignored_field_;
- };
- // Basic tests.
- TEST_F(ComparisonTest, AdditionTest) {
- proto1_.clear_optional_int32();
- EXPECT_EQ("added: optional_int32: 101\n",
- Run());
- }
- TEST_F(ComparisonTest, Addition_OrderTest) {
- orderings_proto1_.clear_my_int();
- EXPECT_EQ("added: my_int: 1\n",
- RunOrder());
- }
- TEST_F(ComparisonTest, DeletionTest) {
- proto2_.clear_optional_int32();
- EXPECT_EQ("deleted: optional_int32: 101\n",
- Run());
- }
- TEST_F(ComparisonTest, Deletion_OrderTest) {
- orderings_proto2_.clear_my_string();
- EXPECT_EQ("deleted: my_string: \"foo\"\n",
- RunOrder());
- }
- TEST_F(ComparisonTest, RepeatedDeletionTest) {
- proto2_.clear_repeated_int32();
- EXPECT_EQ("deleted: repeated_int32[0]: 201\n"
- "deleted: repeated_int32[1]: 301\n",
- Run());
- }
- TEST_F(ComparisonTest, ModificationTest) {
- proto1_.set_optional_int32(-1);
- EXPECT_EQ("modified: optional_int32: -1 -> 101\n",
- Run());
- }
- // Basic equivalency tests.
- TEST_F(ComparisonTest, EquivalencyAdditionTest) {
- use_equivalency();
- proto1_.clear_optional_int32();
- EXPECT_EQ("modified: optional_int32: 0 -> 101\n",
- Run());
- }
- TEST_F(ComparisonTest, EquivalencyDeletionTest) {
- use_equivalency();
- proto2_.clear_optional_int32();
- EXPECT_EQ("modified: optional_int32: 101 -> 0\n",
- Run());
- }
- // Group tests.
- TEST_F(ComparisonTest, GroupAdditionTest) {
- proto1_.mutable_optionalgroup()->clear_a();
- EXPECT_EQ("added: optionalgroup.a: 117\n",
- Run());
- }
- TEST_F(ComparisonTest, GroupDeletionTest) {
- proto2_.mutable_optionalgroup()->clear_a();
- EXPECT_EQ("deleted: optionalgroup.a: 117\n",
- Run());
- }
- TEST_F(ComparisonTest, GroupModificationTest) {
- proto1_.mutable_optionalgroup()->set_a(2);
- EXPECT_EQ("modified: optionalgroup.a: 2 -> 117\n",
- Run());
- }
- TEST_F(ComparisonTest, GroupFullAdditionTest) {
- proto1_.clear_optionalgroup();
- // Note the difference in the output between this and GroupAdditionTest.
- EXPECT_EQ("added: optionalgroup: { a: 117 }\n",
- Run());
- }
- TEST_F(ComparisonTest, GroupFullDeletionTest) {
- proto2_.clear_optionalgroup();
- EXPECT_EQ("deleted: optionalgroup: { a: 117 }\n",
- Run());
- }
- TEST_F(ComparisonTest, RepeatedSetOptionTest) {
- repeated_field_as_set();
- proto2_.clear_repeatedgroup();
- proto1_.clear_repeatedgroup();
- proto1_.add_repeatedgroup()->set_a(317);
- proto2_.add_repeatedgroup()->set_a(909);
- proto2_.add_repeatedgroup()->set_a(907);
- proto1_.add_repeatedgroup()->set_a(904);
- proto1_.add_repeatedgroup()->set_a(907);
- proto1_.add_repeatedgroup()->set_a(909);
- EXPECT_EQ("moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n"
- "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n"
- "deleted: repeatedgroup[0]: { a: 317 }\n"
- "deleted: repeatedgroup[1]: { a: 904 }\n",
- Run());
- }
- TEST_F(ComparisonTest, RepeatedSetOptionTest_Ex) {
- repeated_field_as_set();
- proto1ex_.ClearExtension(protobuf_unittest::repeated_nested_message_extension);
- proto2ex_.ClearExtension(protobuf_unittest::repeated_nested_message_extension);
- proto2ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
- ->set_bb(909);
- proto2ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
- ->set_bb(907);
- proto1ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
- ->set_bb(904);
- proto1ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
- ->set_bb(907);
- proto1ex_.AddExtension(protobuf_unittest::repeated_nested_message_extension)
- ->set_bb(909);
- EXPECT_EQ("moved: (protobuf_unittest.repeated_nested_message_extension)[2] ->"
- " (protobuf_unittest.repeated_nested_message_extension)[0] :"
- " { bb: 909 }\n"
- "deleted: (protobuf_unittest.repeated_nested_message_extension)[0]:"
- " { bb: 904 }\n",
- RunEx());
- }
- TEST_F(ComparisonTest, RepeatedMapFieldTest_Group) {
- field_as_map("repeatedgroup", "a");
- proto1_.clear_repeatedgroup();
- proto2_.clear_repeatedgroup();
- proto1_.add_repeatedgroup()->set_a(317); // deleted
- proto1_.add_repeatedgroup()->set_a(904); // deleted
- proto1_.add_repeatedgroup()->set_a(907); // moved from
- proto1_.add_repeatedgroup()->set_a(909); // moved from
- proto2_.add_repeatedgroup()->set_a(909); // moved to
- proto2_.add_repeatedgroup()->set_a(318); // added
- proto2_.add_repeatedgroup()->set_a(907); // moved to
- EXPECT_EQ("moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n"
- "added: repeatedgroup[1]: { a: 318 }\n"
- "deleted: repeatedgroup[0]: { a: 317 }\n"
- "deleted: repeatedgroup[1]: { a: 904 }\n",
- Run());
- }
- TEST_F(ComparisonTest, RepeatedMapFieldTest_MessageKey) {
- // Use m as key, but use b as value.
- field_as_map("item", "m");
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- // The following code creates one deletion, one addition and two moved fields
- // on the messages.
- item->mutable_m()->set_c(0);
- item->set_b("first");
- item = msg1.add_item();
- item->mutable_m()->set_c(2);
- item->set_b("second");
- item = msg1.add_item(); item->set_b("null"); // empty key moved
- item = msg1.add_item();
- item->mutable_m()->set_c(3);
- item->set_b("third"); // deletion
- item = msg1.add_item();
- item->mutable_m()->set_c(2);
- item->set_b("second"); // duplicated key ( deletion )
- item = msg2.add_item();
- item->mutable_m()->set_c(2);
- item->set_b("second"); // modification
- item = msg2.add_item();
- item->mutable_m()->set_c(4);
- item->set_b("fourth"); // addition
- item = msg2.add_item();
- item->mutable_m()->set_c(0);
- item->set_b("fist"); // move with change
- item = msg2.add_item(); item->set_b("null");
- EXPECT_EQ(
- "modified: item[0].b -> item[2].b: \"first\" -> \"fist\"\n"
- "moved: item[1] -> item[0] : { b: \"second\" m { c: 2 } }\n"
- "moved: item[2] -> item[3] : { b: \"null\" }\n"
- "added: item[1]: { b: \"fourth\" m { c: 4 } }\n"
- "deleted: item[3]: { b: \"third\" m { c: 3 } }\n"
- "deleted: item[4]: { b: \"second\" m { c: 2 } }\n",
- Run(msg1, msg2));
- }
- TEST_F(ComparisonTest, RepeatedFieldSetTest_SetOfSet) {
- repeated_field_as_set();
- // Create the testing protos
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->add_ra(1); item->add_ra(2); item->add_ra(3);
- item = msg1.add_item();
- item->add_ra(5); item->add_ra(6);
- item = msg1.add_item();
- item->add_ra(1); item->add_ra(3);
- item = msg1.add_item();
- item->add_ra(6); item->add_ra(7); item->add_ra(8);
- item = msg2.add_item();
- item->add_ra(6); item->add_ra(5);
- item = msg2.add_item();
- item->add_ra(6); item->add_ra(8);
- item = msg2.add_item();
- item->add_ra(1); item->add_ra(3);
- item = msg2.add_item();
- item->add_ra(3); item->add_ra(2); item->add_ra(1);
- // Compare
- EXPECT_EQ("moved: item[0].ra[0] -> item[3].ra[2] : 1\n"
- "moved: item[0].ra[2] -> item[3].ra[0] : 3\n"
- "moved: item[0] -> item[3] : { ra: 1 ra: 2 ra: 3 }\n"
- "moved: item[1].ra[0] -> item[0].ra[1] : 5\n"
- "moved: item[1].ra[1] -> item[0].ra[0] : 6\n"
- "moved: item[1] -> item[0] : { ra: 5 ra: 6 }\n"
- "added: item[1]: { ra: 6 ra: 8 }\n"
- "deleted: item[3]: { ra: 6 ra: 7 ra: 8 }\n",
- Run(msg1, msg2));
- }
- TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedKey) {
- // used rb as a key, but b is the value.
- repeated_field_as_set();
- field_as_map("item", "rb");
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->add_rb("a");
- item->add_rb("b");
- item->set_b("first");
- item = msg2.add_item();
- item->add_rb("c");
- item->set_b("second");
- item = msg2.add_item();
- item->add_rb("b");
- item->add_rb("a");
- item->set_b("fist");
- EXPECT_EQ("modified: item[0].b -> item[1].b: \"first\" -> \"fist\"\n"
- "moved: item[0].rb[0] -> item[1].rb[1] : \"a\"\n"
- "moved: item[0].rb[1] -> item[1].rb[0] : \"b\"\n"
- "added: item[0]: { b: \"second\" rb: \"c\" }\n",
- Run(msg1, msg2));
- }
- TEST_F(ComparisonTest, RepeatedMapFieldTest_RepeatedMessageKey) {
- field_as_map("item", "rm");
- protobuf_unittest::TestDiffMessage msg1;
- protobuf_unittest::TestDiffMessage msg2;
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- protobuf_unittest::TestField* key = item->add_rm();
- key->set_c(2); key->add_rc(10); key->add_rc(10);
- item = msg1.add_item(); key = item->add_rm();
- key->set_c(0); key->add_rc(1); key->add_rc(2);
- key = item->add_rm();
- key->set_c(0);
- item->add_rb("first");
- item = msg2.add_item();
- item->CopyFrom(msg1.item(1));
- item->add_rb("second");
- EXPECT_EQ("added: item[0].rb[1]: \"second\"\n"
- "deleted: item[0]: { rm { c: 2 rc: 10 rc: 10 } }\n",
- Run(msg1, msg2));
- }
- TEST_F(ComparisonTest, RepeatedSetOptionTest_Unknown) {
- // Currently, as_set option doesn't have affects on unknown field.
- // If needed, this feature will be added by request.
- repeated_field_as_set();
- unknown1_->AddGroup(245)->AddFixed32(248, 1);
- unknown2_->AddGroup(245)->AddFixed32(248, 3);
- unknown2_->AddGroup(245)->AddFixed32(248, 1);
- // We expect it behaves the same as normal comparison.
- EXPECT_EQ("modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n"
- "added: 245[1]: { ... }\n",
- RunUn());
- }
- TEST_F(ComparisonTest, Matching_Unknown) {
- unknown1_->AddGroup(245)->AddFixed32(248, 1);
- unknown2_->AddGroup(245)->AddFixed32(248, 1);
- unknown1_->AddGroup(245)->AddFixed32(248, 3);
- unknown2_->AddGroup(245)->AddFixed32(248, 3);
- unknown2_->AddLengthDelimited(242, "cat");
- unknown2_->AddGroup(246)->AddFixed32(248, 4);
- // report_match is false so only added/modified fields are expected.
- EXPECT_EQ("added: 242[0]: \"cat\"\n"
- "added: 246[0]: { ... }\n",
- RunUn());
- }
- TEST_F(ComparisonTest, RepeatedSetFieldTest) {
- field_as_set("repeatedgroup");
- proto1_.clear_repeatedgroup();
- proto2_.clear_repeatedgroup();
- proto2_.add_repeatedgroup()->set_a(909);
- proto2_.add_repeatedgroup()->set_a(907);
- proto1_.add_repeatedgroup()->set_a(317);
- proto1_.add_repeatedgroup()->set_a(904);
- proto1_.add_repeatedgroup()->set_a(907);
- proto1_.add_repeatedgroup()->set_a(909);
- EXPECT_EQ("moved: repeatedgroup[2] -> repeatedgroup[1] : { a: 907 }\n"
- "moved: repeatedgroup[3] -> repeatedgroup[0] : { a: 909 }\n"
- "deleted: repeatedgroup[0]: { a: 317 }\n"
- "deleted: repeatedgroup[1]: { a: 904 }\n",
- Run());
- }
- // Embedded message tests.
- TEST_F(ComparisonTest, EmbeddedAdditionTest) {
- proto1_.mutable_optional_nested_message()->clear_bb();
- EXPECT_EQ("added: optional_nested_message.bb: 118\n",
- Run());
- }
- TEST_F(ComparisonTest, EmbeddedDeletionTest) {
- proto2_.mutable_optional_nested_message()->clear_bb();
- EXPECT_EQ("deleted: optional_nested_message.bb: 118\n",
- Run());
- }
- TEST_F(ComparisonTest, EmbeddedModificationTest) {
- proto1_.mutable_optional_nested_message()->set_bb(2);
- EXPECT_EQ("modified: optional_nested_message.bb: 2 -> 118\n",
- Run());
- }
- TEST_F(ComparisonTest, EmbeddedFullAdditionTest) {
- proto1_.clear_optional_nested_message();
- EXPECT_EQ("added: optional_nested_message: { bb: 118 }\n",
- Run());
- }
- TEST_F(ComparisonTest, EmbeddedPartialAdditionTest) {
- proto1_.clear_optional_nested_message();
- proto2_.mutable_optional_nested_message()->clear_bb();
- EXPECT_EQ("added: optional_nested_message: { }\n",
- Run());
- }
- TEST_F(ComparisonTest, EmbeddedFullDeletionTest) {
- proto2_.clear_optional_nested_message();
- EXPECT_EQ("deleted: optional_nested_message: { bb: 118 }\n",
- Run());
- }
- // Repeated element tests.
- TEST_F(ComparisonTest, BasicRepeatedTest) {
- proto1_.clear_repeated_int32();
- proto2_.clear_repeated_int32();
- proto1_.add_repeated_int32(500);
- proto1_.add_repeated_int32(501);
- proto1_.add_repeated_int32(502);
- proto1_.add_repeated_int32(503);
- proto1_.add_repeated_int32(500);
- proto2_.add_repeated_int32(500);
- proto2_.add_repeated_int32(509);
- proto2_.add_repeated_int32(502);
- proto2_.add_repeated_int32(504);
- EXPECT_EQ("modified: repeated_int32[1]: 501 -> 509\n"
- "modified: repeated_int32[3]: 503 -> 504\n"
- "deleted: repeated_int32[4]: 500\n",
- Run());
- }
- TEST_F(ComparisonTest, BasicRepeatedTest_SetOption) {
- repeated_field_as_set();
- proto1_.clear_repeated_int32();
- proto2_.clear_repeated_int32();
- proto1_.add_repeated_int32(501);
- proto1_.add_repeated_int32(502);
- proto1_.add_repeated_int32(503);
- proto1_.add_repeated_int32(500);
- proto1_.add_repeated_int32(500);
- proto2_.add_repeated_int32(500);
- proto2_.add_repeated_int32(509);
- proto2_.add_repeated_int32(503);
- proto2_.add_repeated_int32(502);
- proto2_.add_repeated_int32(504);
- EXPECT_EQ("moved: repeated_int32[1] -> repeated_int32[3] : 502\n"
- "moved: repeated_int32[3] -> repeated_int32[0] : 500\n"
- "added: repeated_int32[1]: 509\n"
- "added: repeated_int32[4]: 504\n"
- "deleted: repeated_int32[0]: 501\n"
- "deleted: repeated_int32[4]: 500\n",
- Run());
- }
- TEST_F(ComparisonTest, BasicRepeatedTest_SetField) {
- field_as_set("repeated_int32");
- proto1_.clear_repeated_int32();
- proto2_.clear_repeated_int32();
- proto1_.add_repeated_int32(501);
- proto1_.add_repeated_int32(502);
- proto1_.add_repeated_int32(503);
- proto1_.add_repeated_int32(500);
- proto1_.add_repeated_int32(500);
- proto2_.add_repeated_int32(500);
- proto2_.add_repeated_int32(509);
- proto2_.add_repeated_int32(503);
- proto2_.add_repeated_int32(502);
- proto2_.add_repeated_int32(504);
- EXPECT_EQ("moved: repeated_int32[1] -> repeated_int32[3] : 502\n"
- "moved: repeated_int32[3] -> repeated_int32[0] : 500\n"
- "added: repeated_int32[1]: 509\n"
- "added: repeated_int32[4]: 504\n"
- "deleted: repeated_int32[0]: 501\n"
- "deleted: repeated_int32[4]: 500\n",
- Run());
- }
- // Multiple action tests.
- TEST_F(ComparisonTest, AddDeleteTest) {
- proto1_.clear_optional_int32();
- proto2_.clear_optional_int64();
- EXPECT_EQ("added: optional_int32: 101\n"
- "deleted: optional_int64: 102\n",
- Run());
- }
- TEST_F(ComparisonTest, AddDelete_FieldOrderingTest) {
- orderings_proto1_.ClearExtension(unittest::my_extension_string);
- orderings_proto2_.clear_my_int();
- EXPECT_EQ("deleted: my_int: 1\n"
- "added: (protobuf_unittest.my_extension_string): \"bar\"\n",
- RunOrder());
- }
- TEST_F(ComparisonTest, AllThreeTest) {
- proto1_.clear_optional_int32();
- proto2_.clear_optional_float();
- proto2_.set_optional_string("hello world!");
- EXPECT_EQ("added: optional_int32: 101\n"
- "deleted: optional_float: 111\n"
- "modified: optional_string: \"115\" -> \"hello world!\"\n",
- Run());
- }
- TEST_F(ComparisonTest, SandwhichTest) {
- proto1_.clear_optional_int64();
- proto1_.clear_optional_uint32();
- proto2_.clear_optional_uint64();
- EXPECT_EQ("added: optional_int64: 102\n"
- "added: optional_uint32: 103\n"
- "deleted: optional_uint64: 104\n",
- Run());
- }
- TEST_F(ComparisonTest, IgnoredNoChangeTest) {
- proto1diff_.set_v(3);
- proto2diff_.set_v(3);
- proto2diff_.set_w("foo");
- ignore_field("v");
- EXPECT_EQ("ignored: v\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredAddTest) {
- proto2diff_.set_v(3);
- proto2diff_.set_w("foo");
- ignore_field("v");
- EXPECT_EQ("ignored: v\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredDeleteTest) {
- proto1diff_.set_v(3);
- proto2diff_.set_w("foo");
- ignore_field("v");
- EXPECT_EQ("ignored: v\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredModifyTest) {
- proto1diff_.set_v(3);
- proto2diff_.set_v(4);
- proto2diff_.set_w("foo");
- ignore_field("v");
- EXPECT_EQ("ignored: v\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredRepeatedAddTest) {
- proto1diff_.add_rv(3);
- proto1diff_.add_rv(4);
- proto2diff_.add_rv(3);
- proto2diff_.add_rv(4);
- proto2diff_.add_rv(5);
- proto2diff_.set_w("foo");
- ignore_field("rv");
- EXPECT_EQ("ignored: rv\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredRepeatedDeleteTest) {
- proto1diff_.add_rv(3);
- proto1diff_.add_rv(4);
- proto1diff_.add_rv(5);
- proto2diff_.add_rv(3);
- proto2diff_.add_rv(4);
- proto2diff_.set_w("foo");
- ignore_field("rv");
- EXPECT_EQ("ignored: rv\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredRepeatedModifyTest) {
- proto1diff_.add_rv(3);
- proto1diff_.add_rv(4);
- proto2diff_.add_rv(3);
- proto2diff_.add_rv(5);
- proto2diff_.set_w("foo");
- ignore_field("rv");
- EXPECT_EQ("ignored: rv\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredWholeNestedMessage) {
- proto1diff_.mutable_m()->set_c(3);
- proto2diff_.mutable_m()->set_c(4);
- proto2diff_.set_w("foo");
- ignore_field("m");
- EXPECT_EQ("added: w: \"foo\"\n"
- "ignored: m\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredNestedField) {
- proto1diff_.mutable_m()->set_c(3);
- proto2diff_.mutable_m()->set_c(4);
- proto2diff_.set_w("foo");
- ignore_field("m.c");
- EXPECT_EQ("added: w: \"foo\"\n"
- "ignored: m.c\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredRepeatedNested) {
- proto1diff_.add_rm()->set_c(0);
- proto1diff_.add_rm()->set_c(1);
- proto2diff_.add_rm()->set_c(2);
- proto2diff_.add_rm()->set_c(3);
- proto2diff_.set_w("foo");
- ignore_field("rm.c");
- EXPECT_EQ("ignored: rm[0].c\n"
- "ignored: rm[1].c\n"
- "added: w: \"foo\"\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, IgnoredNestedRepeated) {
- proto1diff_.mutable_m()->add_rc(23);
- proto1diff_.mutable_m()->add_rc(24);
- proto2diff_.mutable_m()->add_rc(25);
- proto2diff_.set_w("foo");
- ignore_field("m.rc");
- EXPECT_EQ("added: w: \"foo\"\n"
- "ignored: m.rc\n",
- RunDiff());
- }
- TEST_F(ComparisonTest, ExtensionTest) {
- proto1ex_.SetExtension(unittest::optional_int32_extension, 401);
- proto2ex_.SetExtension(unittest::optional_int32_extension, 402);
- proto1ex_.ClearExtension(unittest::optional_int64_extension);
- proto2ex_.SetExtension(unittest::optional_int64_extension, 403);
- EXPECT_EQ(
- "modified: (protobuf_unittest.optional_int32_extension): 401 -> 402\n"
- "added: (protobuf_unittest.optional_int64_extension): 403\n",
- RunEx());
- }
- TEST_F(ComparisonTest, MatchedUnknownFieldTagTest) {
- unknown1_->AddVarint(240, 122);
- unknown2_->AddVarint(240, 121);
- unknown1_->AddFixed32(241, 1);
- unknown2_->AddFixed64(241, 2);
- unknown1_->AddLengthDelimited(242, "cat");
- unknown2_->AddLengthDelimited(242, "dog");
- EXPECT_EQ(
- "modified: 240[0]: 122 -> 121\n"
- "deleted: 241[0]: 0x00000001\n"
- "added: 241[0]: 0x0000000000000002\n"
- "modified: 242[0]: \"cat\" -> \"dog\"\n",
- RunUn());
- }
- TEST_F(ComparisonTest, UnmatchedUnknownFieldTagTest) {
- unknown1_->AddFixed32(243, 1);
- unknown2_->AddVarint(244, 2);
- unknown2_->AddVarint(244, 4);
- EXPECT_EQ(
- "deleted: 243[0]: 0x00000001\n"
- "added: 244[0]: 2\n"
- "added: 244[1]: 4\n",
- RunUn());
- }
- TEST_F(ComparisonTest, DifferentSizedUnknownFieldTest) {
- unknown1_->AddVarint(240, 1);
- unknown1_->AddVarint(240, 3);
- unknown1_->AddVarint(240, 4);
- unknown2_->AddVarint(240, 2);
- unknown2_->AddVarint(240, 3);
- unknown2_->AddVarint(240, 2);
- unknown2_->AddVarint(240, 5);
- EXPECT_EQ(
- "modified: 240[0]: 1 -> 2\n"
- "modified: 240[2]: 4 -> 2\n"
- "added: 240[3]: 5\n",
- RunUn());
- }
- TEST_F(ComparisonTest, UnknownFieldsAll) {
- unknown1_->AddVarint(243, 122);
- unknown1_->AddFixed64(244, 0x0172356);
- unknown1_->AddFixed64(244, 0x098);
- unknown1_->AddGroup(245)->AddFixed32(248, 1);
- unknown1_->mutable_field(3)->mutable_group()->AddFixed32(248, 2);
- unknown1_->AddGroup(249)->AddFixed64(250, 1);
- unknown2_->AddVarint(243, 121);
- unknown2_->AddLengthDelimited(73882, "test 123");
- unknown2_->AddGroup(245)->AddFixed32(248, 3);
- unknown2_->AddGroup(247);
- EXPECT_EQ(
- "modified: 243[0]: 122 -> 121\n"
- "deleted: 244[0]: 0x0000000000172356\n"
- "deleted: 244[1]: 0x0000000000000098\n"
- "modified: 245[0].248[0]: 0x00000001 -> 0x00000003\n"
- "deleted: 245[0].248[1]: 0x00000002\n"
- "added: 247[0]: { ... }\n"
- "deleted: 249[0]: { ... }\n"
- "added: 73882[0]: \"test 123\"\n",
- RunUn());
- }
- TEST_F(ComparisonTest, EquivalentIgnoresUnknown) {
- unittest::ForeignMessage message1, message2;
- message1.set_c(5);
- message1.mutable_unknown_fields()->AddVarint(123, 456);
- message2.set_c(5);
- message2.mutable_unknown_fields()->AddVarint(321, 654);
- EXPECT_FALSE(util::MessageDifferencer::Equals(message1, message2));
- EXPECT_TRUE(util::MessageDifferencer::Equivalent(message1, message2));
- }
- TEST_F(ComparisonTest, MapTest) {
- Map<string, string>& map1 = *map_proto1_.mutable_map_string_string();
- map1["key1"] = "1";
- map1["key2"] = "2";
- map1["key3"] = "3";
- Map<string, string>& map2 = *map_proto2_.mutable_map_string_string();
- map2["key3"] = "0";
- map2["key2"] = "2";
- map2["key1"] = "1";
- EXPECT_EQ("modified: map_string_string.value: \"3\" -> \"0\"\n",
- Run(map_proto1_, map_proto2_));
- }
- TEST_F(ComparisonTest, MapIgnoreKeyTest) {
- Map<string, string>& map1 = *map_proto1_.mutable_map_string_string();
- map1["key1"] = "1";
- map1["key2"] = "2";
- map1["key3"] = "3";
- Map<string, string>& map2 = *map_proto2_.mutable_map_string_string();
- map2["key4"] = "2";
- map2["key5"] = "3";
- map2["key6"] = "1";
- util::MessageDifferencer differencer;
- differencer.IgnoreField(
- GetFieldDescriptor(map_proto1_, "map_string_string.key"));
- EXPECT_TRUE(differencer.Compare(map_proto1_, map_proto2_));
- }
- TEST_F(ComparisonTest, MapRoundTripSyncTest) {
- google::protobuf::TextFormat::Parser parser;
- unittest::TestMap map_reflection1;
- // By setting via reflection, data exists in repeated field.
- ASSERT_TRUE(parser.ParseFromString(
- "map_int32_foreign_message { key: 1 }", &map_reflection1));
- // During copy, data is synced from repeated field to map.
- unittest::TestMap map_reflection2 = map_reflection1;
- // During comparison, data is synced from map to repeated field.
- EXPECT_TRUE(
- util::MessageDifferencer::Equals(map_reflection1, map_reflection2));
- }
- TEST_F(ComparisonTest, MapEntryPartialTest) {
- google::protobuf::TextFormat::Parser parser;
- unittest::TestMap map1;
- unittest::TestMap map2;
- string output;
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- differencer.ReportDifferencesToString(&output);
- ASSERT_TRUE(parser.ParseFromString(
- "map_int32_foreign_message { key: 1 value { c: 1 } }", &map1));
- ASSERT_TRUE(parser.ParseFromString(
- "map_int32_foreign_message { key: 1 value { c: 2 }}", &map2));
- EXPECT_FALSE(differencer.Compare(map1, map2));
- EXPECT_EQ("modified: map_int32_foreign_message.value.c: 1 -> 2\n", output);
- ASSERT_TRUE(
- parser.ParseFromString("map_int32_foreign_message { key: 1 }", &map1));
- EXPECT_TRUE(differencer.Compare(map1, map2));
- }
- TEST_F(ComparisonTest, MapEntryPartialEmptyKeyTest) {
- google::protobuf::TextFormat::Parser parser;
- unittest::TestMap map1;
- unittest::TestMap map2;
- ASSERT_TRUE(parser.ParseFromString("map_int32_foreign_message {}", &map1));
- ASSERT_TRUE(
- parser.ParseFromString("map_int32_foreign_message { key: 1 }", &map2));
- util::MessageDifferencer differencer;
- differencer.set_scope(util::MessageDifferencer::PARTIAL);
- EXPECT_TRUE(differencer.Compare(map1, map2));
- }
- // Considers strings keys as equal if they have equal lengths.
- class LengthMapKeyComparator
- : public util::MessageDifferencer::MapKeyComparator {
- public:
- typedef util::MessageDifferencer::SpecificField SpecificField;
- virtual bool IsMatch(const Message& message1, const Message& message2,
- const std::vector<SpecificField>& parent_fields) const {
- const Reflection* reflection1 = message1.GetReflection();
- const Reflection* reflection2 = message2.GetReflection();
- const FieldDescriptor* key_field =
- message1.GetDescriptor()->FindFieldByName("key");
- return reflection1->GetString(message1, key_field).size() ==
- reflection2->GetString(message2, key_field).size();
- }
- };
- TEST_F(ComparisonTest, MapEntryCustomMapKeyComparator) {
- google::protobuf::TextFormat::Parser parser;
- protobuf_unittest::TestMap msg1;
- protobuf_unittest::TestMap msg2;
- ASSERT_TRUE(parser.ParseFromString(
- "map_string_foreign_message { key: 'key1' value { c: 1 }}", &msg1));
- ASSERT_TRUE(parser.ParseFromString(
- "map_string_foreign_message { key: 'key2' value { c: 1 }}", &msg2));
- util::MessageDifferencer differencer;
- LengthMapKeyComparator key_comparator;
- differencer.TreatAsMapUsingKeyComparator(
- GetFieldDescriptor(msg1, "map_string_foreign_message"), &key_comparator);
- string output;
- differencer.ReportDifferencesToString(&output);
- // Though the above two messages have different keys for their map entries,
- // they are considered the same by key_comparator because their lengths are
- // equal. However, in value comparison, all fields of the message are taken
- // into consideration, so they are reported as different.
- EXPECT_FALSE(differencer.Compare(msg1, msg2));
- EXPECT_EQ("modified: map_string_foreign_message.key: \"key1\" -> \"key2\"\n",
- output);
- differencer.IgnoreField(
- GetFieldDescriptor(msg1, "map_string_foreign_message.key"));
- output.clear();
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- EXPECT_EQ("ignored: map_string_foreign_message.key\n", output);
- }
- class MatchingTest : public testing::Test {
- public:
- typedef util::MessageDifferencer MessageDifferencer;
- protected:
- MatchingTest() {
- }
- ~MatchingTest() {
- }
- string RunWithResult(MessageDifferencer* differencer,
- const Message& msg1, const Message& msg2,
- bool result) {
- string output;
- {
- // Before we return the "output" string, we must make sure the
- // StreamReporter is destructored because its destructor will
- // flush the stream.
- io::StringOutputStream output_stream(&output);
- MessageDifferencer::StreamReporter reporter(&output_stream);
- reporter.set_report_modified_aggregates(true);
- differencer->set_report_matches(true);
- differencer->ReportDifferencesTo(&reporter);
- if (result) {
- EXPECT_TRUE(differencer->Compare(msg1, msg2));
- } else {
- EXPECT_FALSE(differencer->Compare(msg1, msg2));
- }
- }
- return output;
- }
- private:
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MatchingTest);
- };
- TEST_F(MatchingTest, StreamReporterMatching) {
- protobuf_unittest::TestField msg1, msg2;
- msg1.set_c(72);
- msg2.set_c(72);
- msg1.add_rc(13);
- msg2.add_rc(13);
- msg1.add_rc(17);
- msg2.add_rc(17);
- string output;
- MessageDifferencer differencer;
- differencer.set_report_matches(true);
- differencer.ReportDifferencesToString(&output);
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "matched: c : 72\n"
- "matched: rc[0] : 13\n"
- "matched: rc[1] : 17\n",
- output);
- }
- TEST_F(MatchingTest, DontReportMatchedWhenIgnoring) {
- protobuf_unittest::TestField msg1, msg2;
- msg1.set_c(72);
- msg2.set_c(72);
- msg1.add_rc(13);
- msg2.add_rc(13);
- msg1.add_rc(17);
- msg2.add_rc(17);
- string output;
- MessageDifferencer differencer;
- differencer.set_report_matches(true);
- differencer.ReportDifferencesToString(&output);
- differencer.IgnoreField(msg1.GetDescriptor()->FindFieldByName("c"));
- EXPECT_TRUE(differencer.Compare(msg1, msg2));
- EXPECT_EQ(
- "ignored: c\n"
- "matched: rc[0] : 13\n"
- "matched: rc[1] : 17\n",
- output);
- }
- TEST_F(MatchingTest, ReportMatchedForMovedFields) {
- protobuf_unittest::TestDiffMessage msg1, msg2;
- protobuf_unittest::TestDiffMessage::Item* item = msg1.add_item();
- item->set_a(53);
- item->set_b("hello");
- item = msg2.add_item();
- item->set_a(27);
- item = msg2.add_item();
- item->set_a(53);
- item->set_b("hello");
- item = msg1.add_item();
- item->set_a(27);
- MessageDifferencer differencer;
- const FieldDescriptor* desc;
- desc = msg1.GetDescriptor()->FindFieldByName("item");
- differencer.TreatAsSet(desc);
- EXPECT_EQ(
- "matched: item[0].a -> item[1].a : 53\n"
- "matched: item[0].b -> item[1].b : \"hello\"\n"
- "moved: item[0] -> item[1] : { a: 53 b: \"hello\" }\n"
- "matched: item[1].a -> item[0].a : 27\n"
- "moved: item[1] -> item[0] : { a: 27 }\n",
- RunWithResult(&differencer, msg1, msg2, true));
- }
- TEST_F(MatchingTest, MatchesAppearInPostTraversalOrderForMovedFields) {
- protobuf_unittest::TestDiffMessage msg1, msg2;
- protobuf_unittest::TestDiffMessage::Item* item;
- protobuf_unittest::TestField* field;
- const FieldDescriptor* desc;
- const FieldDescriptor* nested_desc;
- const FieldDescriptor* double_nested_desc;
- desc = msg1.GetDescriptor()->FindFieldByName("item");
- nested_desc = desc->message_type()->FindFieldByName("rm");
- double_nested_desc = nested_desc->message_type()->FindFieldByName("rc");
- MessageDifferencer differencer;
- differencer.TreatAsSet(desc);
- differencer.TreatAsSet(nested_desc);
- differencer.TreatAsSet(double_nested_desc);
- item = msg1.add_item();
- field = item->add_rm();
- field->set_c(1);
- field->add_rc(2);
- field->add_rc(3);
- field = item->add_rm();
- field->set_c(4);
- field->add_rc(5);
- field->add_rc(6);
- field->add_rc(7);
- item = msg2.add_item();
- field = item->add_rm();
- field->set_c(4);
- field->add_rc(7);
- field->add_rc(6);
- field->add_rc(5);
- field = item->add_rm();
- field->set_c(1);
- field->add_rc(3);
- field->add_rc(2);
- item = msg1.add_item();
- field = item->add_rm();
- field->set_c(8);
- field->add_rc(10);
- field->add_rc(11);
- field->add_rc(9);
- item = msg2.add_item();
- field = item->add_rm();
- field->set_c(8);
- field->add_rc(9);
- field->add_rc(10);
- field->add_rc(11);
- EXPECT_EQ(
- "matched: item[0].rm[0].c -> item[0].rm[1].c : 1\n"
- "moved: item[0].rm[0].rc[0] -> item[0].rm[1].rc[1] : 2\n"
- "moved: item[0].rm[0].rc[1] -> item[0].rm[1].rc[0] : 3\n"
- "moved: item[0].rm[0] -> item[0].rm[1] : { c: 1 rc: 2 rc: 3 }\n"
- "matched: item[0].rm[1].c -> item[0].rm[0].c : 4\n"
- "moved: item[0].rm[1].rc[0] -> item[0].rm[0].rc[2] : 5\n"
- "matched: item[0].rm[1].rc[1] -> item[0].rm[0].rc[1] : 6\n"
- "moved: item[0].rm[1].rc[2] -> item[0].rm[0].rc[0] : 7\n"
- "moved: item[0].rm[1] -> item[0].rm[0] : { c: 4 rc: 5 rc: 6 rc: 7 }\n"
- "matched: item[0] : { rm { c: 1 rc: 2 rc: 3 }"
- " rm { c: 4 rc: 5 rc: 6 rc: 7 } }\n"
- "matched: item[1].rm[0].c : 8\n"
- "moved: item[1].rm[0].rc[0] -> item[1].rm[0].rc[1] : 10\n"
- "moved: item[1].rm[0].rc[1] -> item[1].rm[0].rc[2] : 11\n"
- "moved: item[1].rm[0].rc[2] -> item[1].rm[0].rc[0] : 9\n"
- "matched: item[1].rm[0] : { c: 8 rc: 10 rc: 11 rc: 9 }\n"
- "matched: item[1] : { rm { c: 8 rc: 10 rc: 11 rc: 9 } }\n",
- RunWithResult(&differencer, msg1, msg2, true));
- }
- TEST_F(MatchingTest, MatchAndModifiedInterleaveProperly) {
- protobuf_unittest::TestDiffMessage msg1, msg2;
- protobuf_unittest::TestDiffMessage::Item* item;
- protobuf_unittest::TestField* field;
- const FieldDescriptor* desc;
- const FieldDescriptor* nested_key;
- const FieldDescriptor* nested_desc;
- const FieldDescriptor* double_nested_key;
- const FieldDescriptor* double_nested_desc;
- desc = msg1.GetDescriptor()->FindFieldByName("item");
- nested_key = desc->message_type()->FindFieldByName("a");
- nested_desc = desc->message_type()->FindFieldByName("rm");
- double_nested_key = nested_desc->message_type()->FindFieldByName("c");
- double_nested_desc = nested_desc->message_type()->FindFieldByName("rc");
- MessageDifferencer differencer;
- differencer.TreatAsMap(desc, nested_key);
- differencer.TreatAsMap(nested_desc, double_nested_key);
- differencer.TreatAsSet(double_nested_desc);
- item = msg1.add_item();
- item->set_a(1);
- field = item->add_rm();
- field->set_c(2);
- field->add_rc(3);
- field->add_rc(4);
- field = item->add_rm();
- field->set_c(5);
- field->add_rc(6);
- field->add_rc(7);
- field->add_rc(8);
- item = msg1.add_item();
- item->set_a(9);
- field = item->add_rm();
- field->set_c(10);
- field->add_rc(11);
- field->add_rc(12);
- field = item->add_rm();
- field->set_c(13);
- item = msg2.add_item();
- item->set_a(1);
- field = item->add_rm();
- field->set_c(5);
- field->add_rc(8);
- field->add_rc(8);
- field->add_rc(6);
- field = item->add_rm();
- field->set_c(3);
- field->add_rc(2);
- field->add_rc(4);
- item = msg2.add_item();
- item->set_a(9);
- field = item->add_rm();
- field->set_c(10);
- field->add_rc(12);
- field->add_rc(11);
- field = item->add_rm();
- field->set_c(13);
- EXPECT_EQ(
- "matched: item[0].a : 1\n"
- "matched: item[0].rm[1].c -> item[0].rm[0].c : 5\n"
- "moved: item[0].rm[1].rc[0] -> item[0].rm[0].rc[2] : 6\n"
- "moved: item[0].rm[1].rc[2] -> item[0].rm[0].rc[0] : 8\n"
- "added: item[0].rm[0].rc[1]: 8\n"
- "deleted: item[0].rm[1].rc[1]: 7\n"
- "modified: item[0].rm[1] -> item[0].rm[0]: { c: 5 rc: 6 rc: 7 rc: 8 } ->"
- " { c: 5 rc: 8 rc: 8 rc: 6 }\n"
- "added: item[0].rm[1]: { c: 3 rc: 2 rc: 4 }\n"
- "deleted: item[0].rm[0]: { c: 2 rc: 3 rc: 4 }\n"
- "modified: item[0]: { a: 1 rm { c: 2 rc: 3 rc: 4 }"
- " rm { c: 5 rc: 6 rc: 7 rc: 8 } } ->"
- " { a: 1 rm { c: 5 rc: 8 rc: 8 rc: 6 }"
- " rm { c: 3 rc: 2 rc: 4 } }\n"
- "matched: item[1].a : 9\n"
- "matched: item[1].rm[0].c : 10\n"
- "moved: item[1].rm[0].rc[0] -> item[1].rm[0].rc[1] : 11\n"
- "moved: item[1].rm[0].rc[1] -> item[1].rm[0].rc[0] : 12\n"
- "matched: item[1].rm[0] : { c: 10 rc: 11 rc: 12 }\n"
- "matched: item[1].rm[1].c : 13\n"
- "matched: item[1].rm[1] : { c: 13 }\n"
- "matched: item[1] : { a: 9 rm { c: 10 rc: 11 rc: 12 } rm { c: 13 } }\n",
- RunWithResult(&differencer, msg1, msg2, false));
- }
- TEST_F(MatchingTest, MatchingWorksWithExtensions) {
- protobuf_unittest::TestAllExtensions msg1, msg2;
- protobuf_unittest::TestAllTypes::NestedMessage* nested;
- using protobuf_unittest::repeated_nested_message_extension;
- const FileDescriptor* descriptor;
- const FieldDescriptor* desc;
- const FieldDescriptor* nested_key;
- descriptor = msg1.GetDescriptor()->file();
- desc = descriptor->FindExtensionByName("repeated_nested_message_extension");
- ASSERT_FALSE(desc == NULL);
- nested_key = desc->message_type()->FindFieldByName("bb");
- MessageDifferencer differencer;
- differencer.TreatAsMap(desc, nested_key);
- nested = msg1.AddExtension(repeated_nested_message_extension);
- nested->set_bb(7);
- nested = msg1.AddExtension(repeated_nested_message_extension);
- nested->set_bb(13);
- nested = msg1.AddExtension(repeated_nested_message_extension);
- nested->set_bb(11);
- nested = msg2.AddExtension(repeated_nested_message_extension);
- nested->set_bb(11);
- nested = msg2.AddExtension(repeated_nested_message_extension);
- nested->set_bb(13);
- nested = msg2.AddExtension(repeated_nested_message_extension);
- nested->set_bb(7);
- EXPECT_EQ(
- "matched: (protobuf_unittest.repeated_nested_message_extension)[0].bb ->"
- " (protobuf_unittest.repeated_nested_message_extension)[2].bb : 7\n"
- "moved: (protobuf_unittest.repeated_nested_message_extension)[0] ->"
- " (protobuf_unittest.repeated_nested_message_extension)[2] :"
- " { bb: 7 }\n"
- "matched: (protobuf_unittest.repeated_nested_message_extension)[1].bb :"
- " 13\n"
- "matched: (protobuf_unittest.repeated_nested_message_extension)[1] :"
- " { bb: 13 }\n"
- "matched: (protobuf_unittest.repeated_nested_message_extension)[2].bb ->"
- " (protobuf_unittest.repeated_nested_message_extension)[0].bb :"
- " 11\n"
- "moved: (protobuf_unittest.repeated_nested_message_extension)[2] ->"
- " (protobuf_unittest.repeated_nested_message_extension)[0] :"
- " { bb: 11 }\n",
- RunWithResult(&differencer, msg1, msg2, true));
- }
- TEST(AnyTest, Simple) {
- protobuf_unittest::TestField value1, value2;
- value1.set_a(20);
- value2.set_a(21);
- protobuf_unittest::TestAny m1, m2;
- m1.mutable_any_value()->PackFrom(value1);
- m2.mutable_any_value()->PackFrom(value2);
- util::MessageDifferencer message_differencer;
- string difference_string;
- message_differencer.ReportDifferencesToString(&difference_string);
- EXPECT_FALSE(message_differencer.Compare(m1, m2));
- EXPECT_EQ("modified: any_value.a: 20 -> 21\n", difference_string);
- }
- TEST(Anytest, TreatAsSet) {
- protobuf_unittest::TestField value1, value2;
- value1.set_a(20);
- value1.set_b(30);
- value2.set_a(20);
- value2.set_b(31);
- protobuf_unittest::TestAny m1, m2;
- m1.add_repeated_any_value()->PackFrom(value1);
- m1.add_repeated_any_value()->PackFrom(value2);
- m2.add_repeated_any_value()->PackFrom(value2);
- m2.add_repeated_any_value()->PackFrom(value1);
- util::MessageDifferencer message_differencer;
- message_differencer.TreatAsSet(GetFieldDescriptor(m1, "repeated_any_value"));
- EXPECT_TRUE(message_differencer.Compare(m1, m2));
- }
- TEST(Anytest, TreatAsSet_DifferentType) {
- protobuf_unittest::TestField value1;
- value1.set_a(20);
- value1.set_b(30);
- protobuf_unittest::TestDiffMessage value2;
- value2.add_rv(40);
- protobuf_unittest::TestAny m1, m2;
- m1.add_repeated_any_value()->PackFrom(value1);
- m1.add_repeated_any_value()->PackFrom(value2);
- m2.add_repeated_any_value()->PackFrom(value2);
- m2.add_repeated_any_value()->PackFrom(value1);
- util::MessageDifferencer message_differencer;
- message_differencer.TreatAsSet(GetFieldDescriptor(m1, "repeated_any_value"));
- EXPECT_TRUE(message_differencer.Compare(m1, m2));
- }
- } // namespace
- } // namespace protobuf
- } // namespace google
|