| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446 |
- // Copyright 2007, Google Inc.
- // All rights reserved.
- //
- // 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.
- // Google Mock - a framework for writing C++ mock classes.
- //
- // This file tests some commonly used argument matchers.
- #include <algorithm>
- #include <array>
- #include <cstddef>
- #include <deque>
- #include <forward_list>
- #include <iterator>
- #include <list>
- #include <memory>
- #include <ostream>
- #include <string>
- #include <tuple>
- #include <vector>
- #include "gmock/gmock.h"
- #include "test/gmock-matchers_test.h"
- #include "gtest/gtest.h"
- // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
- // possible loss of data and C4100, unreferenced local parameter
- GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
- namespace testing {
- namespace gmock_matchers_test {
- namespace {
- std::vector<std::unique_ptr<int>> MakeUniquePtrs(const std::vector<int>& ints) {
- std::vector<std::unique_ptr<int>> pointers;
- for (int i : ints) pointers.emplace_back(new int(i));
- return pointers;
- }
- std::string OfType(const std::string& type_name) {
- #if GTEST_HAS_RTTI
- return IsReadableTypeName(type_name) ? " (of type " + type_name + ")" : "";
- #else
- return "";
- #endif
- }
- TEST(ContainsTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(Contains(Pointee(2))));
- helper.Call(MakeUniquePtrs({1, 2}));
- }
- INSTANTIATE_GTEST_MATCHER_TEST_P(ElementsAreTest);
- // Tests the variadic version of the ElementsAreMatcher
- TEST(ElementsAreTest, HugeMatcher) {
- vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
- EXPECT_THAT(test_vector,
- ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
- Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
- }
- // Tests the variadic version of the UnorderedElementsAreMatcher
- TEST(ElementsAreTest, HugeMatcherStr) {
- vector<std::string> test_vector{
- "literal_string", "", "", "", "", "", "", "", "", "", "", ""};
- EXPECT_THAT(test_vector, UnorderedElementsAre("literal_string", _, _, _, _, _,
- _, _, _, _, _, _));
- }
- // Tests the variadic version of the UnorderedElementsAreMatcher
- TEST(ElementsAreTest, HugeMatcherUnordered) {
- vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
- EXPECT_THAT(test_vector, UnorderedElementsAre(
- Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
- Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
- }
- // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
- // matches the matcher.
- TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
- ASSERT_THAT(5, Ge(2)) << "This should succeed.";
- ASSERT_THAT("Foo", EndsWith("oo"));
- EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
- EXPECT_THAT("Hello", StartsWith("Hell"));
- }
- // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
- // doesn't match the matcher.
- TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
- // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
- // which cannot reference auto variables.
- static unsigned short n; // NOLINT
- n = 5;
- EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)),
- "Value of: n\n"
- "Expected: is > 10\n"
- " Actual: 5" +
- OfType("unsigned short"));
- n = 0;
- EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))),
- "Value of: n\n"
- "Expected: (is <= 7) and (is >= 5)\n"
- " Actual: 0" +
- OfType("unsigned short"));
- }
- // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
- // has a reference type.
- TEST(MatcherAssertionTest, WorksForByRefArguments) {
- // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
- // reference auto variables.
- static int n;
- n = 0;
- EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
- EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
- "Value of: n\n"
- "Expected: does not reference the variable @");
- // Tests the "Actual" part.
- EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))),
- "Actual: 0" + OfType("int") + ", which is located @");
- }
- // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
- // monomorphic.
- TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
- Matcher<const char*> starts_with_he = StartsWith("he");
- ASSERT_THAT("hello", starts_with_he);
- Matcher<const std::string&> ends_with_ok = EndsWith("ok");
- ASSERT_THAT("book", ends_with_ok);
- const std::string bad = "bad";
- EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
- "Value of: bad\n"
- "Expected: ends with \"ok\"\n"
- " Actual: \"bad\"");
- Matcher<int> is_greater_than_5 = Gt(5);
- EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
- "Value of: 5\n"
- "Expected: is > 5\n"
- " Actual: 5" +
- OfType("int"));
- }
- TEST(PointeeTest, RawPointer) {
- const Matcher<int*> m = Pointee(Ge(0));
- int n = 1;
- EXPECT_TRUE(m.Matches(&n));
- n = -1;
- EXPECT_FALSE(m.Matches(&n));
- EXPECT_FALSE(m.Matches(nullptr));
- }
- TEST(PointeeTest, RawPointerToConst) {
- const Matcher<const double*> m = Pointee(Ge(0));
- double x = 1;
- EXPECT_TRUE(m.Matches(&x));
- x = -1;
- EXPECT_FALSE(m.Matches(&x));
- EXPECT_FALSE(m.Matches(nullptr));
- }
- TEST(PointeeTest, ReferenceToConstRawPointer) {
- const Matcher<int* const&> m = Pointee(Ge(0));
- int n = 1;
- EXPECT_TRUE(m.Matches(&n));
- n = -1;
- EXPECT_FALSE(m.Matches(&n));
- EXPECT_FALSE(m.Matches(nullptr));
- }
- TEST(PointeeTest, ReferenceToNonConstRawPointer) {
- const Matcher<double*&> m = Pointee(Ge(0));
- double x = 1.0;
- double* p = &x;
- EXPECT_TRUE(m.Matches(p));
- x = -1;
- EXPECT_FALSE(m.Matches(p));
- p = nullptr;
- EXPECT_FALSE(m.Matches(p));
- }
- TEST(PointeeTest, SmartPointer) {
- const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
- std::unique_ptr<int> n(new int(1));
- EXPECT_TRUE(m.Matches(n));
- }
- TEST(PointeeTest, SmartPointerToConst) {
- const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0));
- // There's no implicit conversion from unique_ptr<int> to const
- // unique_ptr<const int>, so we must pass a unique_ptr<const int> into the
- // matcher.
- std::unique_ptr<const int> n(new int(1));
- EXPECT_TRUE(m.Matches(n));
- }
- TEST(PointerTest, RawPointer) {
- int n = 1;
- const Matcher<int*> m = Pointer(Eq(&n));
- EXPECT_TRUE(m.Matches(&n));
- int* p = nullptr;
- EXPECT_FALSE(m.Matches(p));
- EXPECT_FALSE(m.Matches(nullptr));
- }
- TEST(PointerTest, RawPointerToConst) {
- int n = 1;
- const Matcher<const int*> m = Pointer(Eq(&n));
- EXPECT_TRUE(m.Matches(&n));
- int* p = nullptr;
- EXPECT_FALSE(m.Matches(p));
- EXPECT_FALSE(m.Matches(nullptr));
- }
- TEST(PointerTest, SmartPointer) {
- std::unique_ptr<int> n(new int(10));
- int* raw_n = n.get();
- const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
- EXPECT_TRUE(m.Matches(n));
- }
- TEST(PointerTest, SmartPointerToConst) {
- std::unique_ptr<const int> n(new int(10));
- const int* raw_n = n.get();
- const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n));
- // There's no implicit conversion from unique_ptr<int> to const
- // unique_ptr<const int>, so we must pass a unique_ptr<const int> into the
- // matcher.
- std::unique_ptr<const int> p(new int(10));
- EXPECT_FALSE(m.Matches(p));
- }
- // Minimal const-propagating pointer.
- template <typename T>
- class ConstPropagatingPtr {
- public:
- typedef T element_type;
- ConstPropagatingPtr() : val_() {}
- explicit ConstPropagatingPtr(T* t) : val_(t) {}
- ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
- T* get() { return val_; }
- T& operator*() { return *val_; }
- // Most smart pointers return non-const T* and T& from the next methods.
- const T* get() const { return val_; }
- const T& operator*() const { return *val_; }
- private:
- T* val_;
- };
- INSTANTIATE_GTEST_MATCHER_TEST_P(PointeeTest);
- TEST(PointeeTest, WorksWithConstPropagatingPointers) {
- const Matcher<ConstPropagatingPtr<int>> m = Pointee(Lt(5));
- int three = 3;
- const ConstPropagatingPtr<int> co(&three);
- ConstPropagatingPtr<int> o(&three);
- EXPECT_TRUE(m.Matches(o));
- EXPECT_TRUE(m.Matches(co));
- *o = 6;
- EXPECT_FALSE(m.Matches(o));
- EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
- }
- TEST(PointeeTest, NeverMatchesNull) {
- const Matcher<const char*> m = Pointee(_);
- EXPECT_FALSE(m.Matches(nullptr));
- }
- // Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
- TEST(PointeeTest, MatchesAgainstAValue) {
- const Matcher<int*> m = Pointee(5);
- int n = 5;
- EXPECT_TRUE(m.Matches(&n));
- n = -1;
- EXPECT_FALSE(m.Matches(&n));
- EXPECT_FALSE(m.Matches(nullptr));
- }
- TEST(PointeeTest, CanDescribeSelf) {
- const Matcher<int*> m = Pointee(Gt(3));
- EXPECT_EQ("points to a value that is > 3", Describe(m));
- EXPECT_EQ("does not point to a value that is > 3", DescribeNegation(m));
- }
- TEST_P(PointeeTestP, CanExplainMatchResult) {
- const Matcher<const std::string*> m = Pointee(StartsWith("Hi"));
- EXPECT_EQ("", Explain(m, static_cast<const std::string*>(nullptr)));
- const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
- long n = 3; // NOLINT
- EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
- Explain(m2, &n));
- }
- TEST(PointeeTest, AlwaysExplainsPointee) {
- const Matcher<int*> m = Pointee(0);
- int n = 42;
- EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
- }
- // An uncopyable class.
- class Uncopyable {
- public:
- Uncopyable() : value_(-1) {}
- explicit Uncopyable(int a_value) : value_(a_value) {}
- int value() const { return value_; }
- void set_value(int i) { value_ = i; }
- private:
- int value_;
- Uncopyable(const Uncopyable&) = delete;
- Uncopyable& operator=(const Uncopyable&) = delete;
- };
- // Returns true if and only if x.value() is positive.
- bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
- MATCHER_P(UncopyableIs, inner_matcher, "") {
- return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
- }
- // A user-defined struct for testing Field().
- struct AStruct {
- AStruct() : x(0), y(1.0), z(5), p(nullptr) {}
- AStruct(const AStruct& rhs)
- : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
- int x; // A non-const field.
- const double y; // A const field.
- Uncopyable z; // An uncopyable field.
- const char* p; // A pointer field.
- };
- // A derived struct for testing Field().
- struct DerivedStruct : public AStruct {
- char ch;
- };
- INSTANTIATE_GTEST_MATCHER_TEST_P(FieldTest);
- // Tests that Field(&Foo::field, ...) works when field is non-const.
- TEST(FieldTest, WorksForNonConstField) {
- Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
- Matcher<AStruct> m_with_name = Field("x", &AStruct::x, Ge(0));
- AStruct a;
- EXPECT_TRUE(m.Matches(a));
- EXPECT_TRUE(m_with_name.Matches(a));
- a.x = -1;
- EXPECT_FALSE(m.Matches(a));
- EXPECT_FALSE(m_with_name.Matches(a));
- }
- // Tests that Field(&Foo::field, ...) works when field is const.
- TEST(FieldTest, WorksForConstField) {
- AStruct a;
- Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
- Matcher<AStruct> m_with_name = Field("y", &AStruct::y, Ge(0.0));
- EXPECT_TRUE(m.Matches(a));
- EXPECT_TRUE(m_with_name.Matches(a));
- m = Field(&AStruct::y, Le(0.0));
- m_with_name = Field("y", &AStruct::y, Le(0.0));
- EXPECT_FALSE(m.Matches(a));
- EXPECT_FALSE(m_with_name.Matches(a));
- }
- // Tests that Field(&Foo::field, ...) works when field is not copyable.
- TEST(FieldTest, WorksForUncopyableField) {
- AStruct a;
- Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
- EXPECT_TRUE(m.Matches(a));
- m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
- EXPECT_FALSE(m.Matches(a));
- }
- // Tests that Field(&Foo::field, ...) works when field is a pointer.
- TEST(FieldTest, WorksForPointerField) {
- // Matching against NULL.
- Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(nullptr));
- AStruct a;
- EXPECT_TRUE(m.Matches(a));
- a.p = "hi";
- EXPECT_FALSE(m.Matches(a));
- // Matching a pointer that is not NULL.
- m = Field(&AStruct::p, StartsWith("hi"));
- a.p = "hill";
- EXPECT_TRUE(m.Matches(a));
- a.p = "hole";
- EXPECT_FALSE(m.Matches(a));
- }
- // Tests that Field() works when the object is passed by reference.
- TEST(FieldTest, WorksForByRefArgument) {
- Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
- AStruct a;
- EXPECT_TRUE(m.Matches(a));
- a.x = -1;
- EXPECT_FALSE(m.Matches(a));
- }
- // Tests that Field(&Foo::field, ...) works when the argument's type
- // is a sub-type of Foo.
- TEST(FieldTest, WorksForArgumentOfSubType) {
- // Note that the matcher expects DerivedStruct but we say AStruct
- // inside Field().
- Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
- DerivedStruct d;
- EXPECT_TRUE(m.Matches(d));
- d.x = -1;
- EXPECT_FALSE(m.Matches(d));
- }
- // Tests that Field(&Foo::field, m) works when field's type and m's
- // argument type are compatible but not the same.
- TEST(FieldTest, WorksForCompatibleMatcherType) {
- // The field is an int, but the inner matcher expects a signed char.
- Matcher<const AStruct&> m = Field(&AStruct::x, Matcher<signed char>(Ge(0)));
- AStruct a;
- EXPECT_TRUE(m.Matches(a));
- a.x = -1;
- EXPECT_FALSE(m.Matches(a));
- }
- // Tests that Field() can describe itself.
- TEST(FieldTest, CanDescribeSelf) {
- Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
- EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
- EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
- }
- TEST(FieldTest, CanDescribeSelfWithFieldName) {
- Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0));
- EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m));
- EXPECT_EQ("is an object whose field `field_name` isn't >= 0",
- DescribeNegation(m));
- }
- // Tests that Field() can explain the match result.
- TEST_P(FieldTestP, CanExplainMatchResult) {
- Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
- AStruct a;
- a.x = 1;
- EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
- m = Field(&AStruct::x, GreaterThan(0));
- EXPECT_EQ(
- "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
- Explain(m, a));
- }
- TEST_P(FieldTestP, CanExplainMatchResultWithFieldName) {
- Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0));
- AStruct a;
- a.x = 1;
- EXPECT_EQ("whose field `field_name` is 1" + OfType("int"), Explain(m, a));
- m = Field("field_name", &AStruct::x, GreaterThan(0));
- EXPECT_EQ("whose field `field_name` is 1" + OfType("int") +
- ", which is 1 more than 0",
- Explain(m, a));
- }
- INSTANTIATE_GTEST_MATCHER_TEST_P(FieldForPointerTest);
- // Tests that Field() works when the argument is a pointer to const.
- TEST(FieldForPointerTest, WorksForPointerToConst) {
- Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
- AStruct a;
- EXPECT_TRUE(m.Matches(&a));
- a.x = -1;
- EXPECT_FALSE(m.Matches(&a));
- }
- // Tests that Field() works when the argument is a pointer to non-const.
- TEST(FieldForPointerTest, WorksForPointerToNonConst) {
- Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
- AStruct a;
- EXPECT_TRUE(m.Matches(&a));
- a.x = -1;
- EXPECT_FALSE(m.Matches(&a));
- }
- // Tests that Field() works when the argument is a reference to a const pointer.
- TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
- Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
- AStruct a;
- EXPECT_TRUE(m.Matches(&a));
- a.x = -1;
- EXPECT_FALSE(m.Matches(&a));
- }
- // Tests that Field() does not match the NULL pointer.
- TEST(FieldForPointerTest, DoesNotMatchNull) {
- Matcher<const AStruct*> m = Field(&AStruct::x, _);
- EXPECT_FALSE(m.Matches(nullptr));
- }
- // Tests that Field(&Foo::field, ...) works when the argument's type
- // is a sub-type of const Foo*.
- TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
- // Note that the matcher expects DerivedStruct but we say AStruct
- // inside Field().
- Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
- DerivedStruct d;
- EXPECT_TRUE(m.Matches(&d));
- d.x = -1;
- EXPECT_FALSE(m.Matches(&d));
- }
- // Tests that Field() can describe itself when used to match a pointer.
- TEST(FieldForPointerTest, CanDescribeSelf) {
- Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
- EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
- EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
- }
- TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
- Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0));
- EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m));
- EXPECT_EQ("is an object whose field `field_name` isn't >= 0",
- DescribeNegation(m));
- }
- // Tests that Field() can explain the result of matching a pointer.
- TEST_P(FieldForPointerTestP, CanExplainMatchResult) {
- Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
- AStruct a;
- a.x = 1;
- EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr)));
- EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
- Explain(m, &a));
- m = Field(&AStruct::x, GreaterThan(0));
- EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
- ", which is 1 more than 0",
- Explain(m, &a));
- }
- TEST_P(FieldForPointerTestP, CanExplainMatchResultWithFieldName) {
- Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0));
- AStruct a;
- a.x = 1;
- EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr)));
- EXPECT_EQ(
- "which points to an object whose field `field_name` is 1" + OfType("int"),
- Explain(m, &a));
- m = Field("field_name", &AStruct::x, GreaterThan(0));
- EXPECT_EQ("which points to an object whose field `field_name` is 1" +
- OfType("int") + ", which is 1 more than 0",
- Explain(m, &a));
- }
- // A user-defined class for testing Property().
- class AClass {
- public:
- AClass() : n_(0) {}
- // A getter that returns a non-reference.
- int n() const { return n_; }
- void set_n(int new_n) { n_ = new_n; }
- // A getter that returns a reference to const.
- const std::string& s() const { return s_; }
- const std::string& s_ref() const& { return s_; }
- void set_s(const std::string& new_s) { s_ = new_s; }
- // A getter that returns a reference to non-const.
- double& x() const { return x_; }
- private:
- int n_;
- std::string s_;
- static double x_;
- };
- double AClass::x_ = 0.0;
- // A derived class for testing Property().
- class DerivedClass : public AClass {
- public:
- int k() const { return k_; }
- private:
- int k_;
- };
- INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyTest);
- // Tests that Property(&Foo::property, ...) works when property()
- // returns a non-reference.
- TEST(PropertyTest, WorksForNonReferenceProperty) {
- Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
- Matcher<const AClass&> m_with_name = Property("n", &AClass::n, Ge(0));
- AClass a;
- a.set_n(1);
- EXPECT_TRUE(m.Matches(a));
- EXPECT_TRUE(m_with_name.Matches(a));
- a.set_n(-1);
- EXPECT_FALSE(m.Matches(a));
- EXPECT_FALSE(m_with_name.Matches(a));
- }
- // Tests that Property(&Foo::property, ...) works when property()
- // returns a reference to const.
- TEST(PropertyTest, WorksForReferenceToConstProperty) {
- Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
- Matcher<const AClass&> m_with_name =
- Property("s", &AClass::s, StartsWith("hi"));
- AClass a;
- a.set_s("hill");
- EXPECT_TRUE(m.Matches(a));
- EXPECT_TRUE(m_with_name.Matches(a));
- a.set_s("hole");
- EXPECT_FALSE(m.Matches(a));
- EXPECT_FALSE(m_with_name.Matches(a));
- }
- // Tests that Property(&Foo::property, ...) works when property() is
- // ref-qualified.
- TEST(PropertyTest, WorksForRefQualifiedProperty) {
- Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith("hi"));
- Matcher<const AClass&> m_with_name =
- Property("s", &AClass::s_ref, StartsWith("hi"));
- AClass a;
- a.set_s("hill");
- EXPECT_TRUE(m.Matches(a));
- EXPECT_TRUE(m_with_name.Matches(a));
- a.set_s("hole");
- EXPECT_FALSE(m.Matches(a));
- EXPECT_FALSE(m_with_name.Matches(a));
- }
- // Tests that Property(&Foo::property, ...) works when property()
- // returns a reference to non-const.
- TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
- double x = 0.0;
- AClass a;
- Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
- EXPECT_FALSE(m.Matches(a));
- m = Property(&AClass::x, Not(Ref(x)));
- EXPECT_TRUE(m.Matches(a));
- }
- // Tests that Property(&Foo::property, ...) works when the argument is
- // passed by value.
- TEST(PropertyTest, WorksForByValueArgument) {
- Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
- AClass a;
- a.set_s("hill");
- EXPECT_TRUE(m.Matches(a));
- a.set_s("hole");
- EXPECT_FALSE(m.Matches(a));
- }
- // Tests that Property(&Foo::property, ...) works when the argument's
- // type is a sub-type of Foo.
- TEST(PropertyTest, WorksForArgumentOfSubType) {
- // The matcher expects a DerivedClass, but inside the Property() we
- // say AClass.
- Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
- DerivedClass d;
- d.set_n(1);
- EXPECT_TRUE(m.Matches(d));
- d.set_n(-1);
- EXPECT_FALSE(m.Matches(d));
- }
- // Tests that Property(&Foo::property, m) works when property()'s type
- // and m's argument type are compatible but different.
- TEST(PropertyTest, WorksForCompatibleMatcherType) {
- // n() returns an int but the inner matcher expects a signed char.
- Matcher<const AClass&> m = Property(&AClass::n, Matcher<signed char>(Ge(0)));
- Matcher<const AClass&> m_with_name =
- Property("n", &AClass::n, Matcher<signed char>(Ge(0)));
- AClass a;
- EXPECT_TRUE(m.Matches(a));
- EXPECT_TRUE(m_with_name.Matches(a));
- a.set_n(-1);
- EXPECT_FALSE(m.Matches(a));
- EXPECT_FALSE(m_with_name.Matches(a));
- }
- // Tests that Property() can describe itself.
- TEST(PropertyTest, CanDescribeSelf) {
- Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
- EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
- EXPECT_EQ("is an object whose given property isn't >= 0",
- DescribeNegation(m));
- }
- TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
- Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0));
- EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m));
- EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0",
- DescribeNegation(m));
- }
- // Tests that Property() can explain the match result.
- TEST_P(PropertyTestP, CanExplainMatchResult) {
- Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
- AClass a;
- a.set_n(1);
- EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
- m = Property(&AClass::n, GreaterThan(0));
- EXPECT_EQ(
- "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
- Explain(m, a));
- }
- TEST_P(PropertyTestP, CanExplainMatchResultWithPropertyName) {
- Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0));
- AClass a;
- a.set_n(1);
- EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int"), Explain(m, a));
- m = Property("fancy_name", &AClass::n, GreaterThan(0));
- EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int") +
- ", which is 1 more than 0",
- Explain(m, a));
- }
- INSTANTIATE_GTEST_MATCHER_TEST_P(PropertyForPointerTest);
- // Tests that Property() works when the argument is a pointer to const.
- TEST(PropertyForPointerTest, WorksForPointerToConst) {
- Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
- AClass a;
- a.set_n(1);
- EXPECT_TRUE(m.Matches(&a));
- a.set_n(-1);
- EXPECT_FALSE(m.Matches(&a));
- }
- // Tests that Property() works when the argument is a pointer to non-const.
- TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
- Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
- AClass a;
- a.set_s("hill");
- EXPECT_TRUE(m.Matches(&a));
- a.set_s("hole");
- EXPECT_FALSE(m.Matches(&a));
- }
- // Tests that Property() works when the argument is a reference to a
- // const pointer.
- TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
- Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
- AClass a;
- a.set_s("hill");
- EXPECT_TRUE(m.Matches(&a));
- a.set_s("hole");
- EXPECT_FALSE(m.Matches(&a));
- }
- // Tests that Property() does not match the NULL pointer.
- TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
- Matcher<const AClass*> m = Property(&AClass::x, _);
- EXPECT_FALSE(m.Matches(nullptr));
- }
- // Tests that Property(&Foo::property, ...) works when the argument's
- // type is a sub-type of const Foo*.
- TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
- // The matcher expects a DerivedClass, but inside the Property() we
- // say AClass.
- Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
- DerivedClass d;
- d.set_n(1);
- EXPECT_TRUE(m.Matches(&d));
- d.set_n(-1);
- EXPECT_FALSE(m.Matches(&d));
- }
- // Tests that Property() can describe itself when used to match a pointer.
- TEST(PropertyForPointerTest, CanDescribeSelf) {
- Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
- EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
- EXPECT_EQ("is an object whose given property isn't >= 0",
- DescribeNegation(m));
- }
- TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
- Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0));
- EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m));
- EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0",
- DescribeNegation(m));
- }
- // Tests that Property() can explain the result of matching a pointer.
- TEST_P(PropertyForPointerTestP, CanExplainMatchResult) {
- Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
- AClass a;
- a.set_n(1);
- EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr)));
- EXPECT_EQ(
- "which points to an object whose given property is 1" + OfType("int"),
- Explain(m, &a));
- m = Property(&AClass::n, GreaterThan(0));
- EXPECT_EQ("which points to an object whose given property is 1" +
- OfType("int") + ", which is 1 more than 0",
- Explain(m, &a));
- }
- TEST_P(PropertyForPointerTestP, CanExplainMatchResultWithPropertyName) {
- Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0));
- AClass a;
- a.set_n(1);
- EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr)));
- EXPECT_EQ("which points to an object whose property `fancy_name` is 1" +
- OfType("int"),
- Explain(m, &a));
- m = Property("fancy_name", &AClass::n, GreaterThan(0));
- EXPECT_EQ("which points to an object whose property `fancy_name` is 1" +
- OfType("int") + ", which is 1 more than 0",
- Explain(m, &a));
- }
- // Tests ResultOf.
- // Tests that ResultOf(f, ...) compiles and works as expected when f is a
- // function pointer.
- std::string IntToStringFunction(int input) {
- return input == 1 ? "foo" : "bar";
- }
- INSTANTIATE_GTEST_MATCHER_TEST_P(ResultOfTest);
- TEST(ResultOfTest, WorksForFunctionPointers) {
- Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string("foo")));
- EXPECT_TRUE(matcher.Matches(1));
- EXPECT_FALSE(matcher.Matches(2));
- }
- // Tests that ResultOf() can describe itself.
- TEST(ResultOfTest, CanDescribeItself) {
- Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
- EXPECT_EQ(
- "is mapped by the given callable to a value that "
- "is equal to \"foo\"",
- Describe(matcher));
- EXPECT_EQ(
- "is mapped by the given callable to a value that "
- "isn't equal to \"foo\"",
- DescribeNegation(matcher));
- }
- // Tests that ResultOf() can describe itself when provided a result description.
- TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {
- Matcher<int> matcher =
- ResultOf("string conversion", &IntToStringFunction, StrEq("foo"));
- EXPECT_EQ("whose string conversion is equal to \"foo\"", Describe(matcher));
- EXPECT_EQ("whose string conversion isn't equal to \"foo\"",
- DescribeNegation(matcher));
- }
- // Tests that ResultOf() can explain the match result.
- int IntFunction(int input) { return input == 42 ? 80 : 90; }
- TEST_P(ResultOfTestP, CanExplainMatchResult) {
- Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
- EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
- Explain(matcher, 36));
- matcher = ResultOf(&IntFunction, GreaterThan(85));
- EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
- ", which is 5 more than 85",
- Explain(matcher, 36));
- }
- TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) {
- Matcher<int> matcher = ResultOf("magic int conversion", &IntFunction, Ge(85));
- EXPECT_EQ("whose magic int conversion is 90" + OfType("int"),
- Explain(matcher, 36));
- matcher = ResultOf("magic int conversion", &IntFunction, GreaterThan(85));
- EXPECT_EQ("whose magic int conversion is 90" + OfType("int") +
- ", which is 5 more than 85",
- Explain(matcher, 36));
- }
- // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
- // returns a non-reference.
- TEST(ResultOfTest, WorksForNonReferenceResults) {
- Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
- EXPECT_TRUE(matcher.Matches(42));
- EXPECT_FALSE(matcher.Matches(36));
- }
- // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
- // returns a reference to non-const.
- double& DoubleFunction(double& input) { return input; } // NOLINT
- Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
- return obj;
- }
- TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
- double x = 3.14;
- double x2 = x;
- Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
- EXPECT_TRUE(matcher.Matches(x));
- EXPECT_FALSE(matcher.Matches(x2));
- // Test that ResultOf works with uncopyable objects
- Uncopyable obj(0);
- Uncopyable obj2(0);
- Matcher<Uncopyable&> matcher2 = ResultOf(&RefUncopyableFunction, Ref(obj));
- EXPECT_TRUE(matcher2.Matches(obj));
- EXPECT_FALSE(matcher2.Matches(obj2));
- }
- // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
- // returns a reference to const.
- const std::string& StringFunction(const std::string& input) { return input; }
- TEST(ResultOfTest, WorksForReferenceToConstResults) {
- std::string s = "foo";
- std::string s2 = s;
- Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
- EXPECT_TRUE(matcher.Matches(s));
- EXPECT_FALSE(matcher.Matches(s2));
- }
- // Tests that ResultOf(f, m) works when f(x) and m's
- // argument types are compatible but different.
- TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
- // IntFunction() returns int but the inner matcher expects a signed char.
- Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
- EXPECT_TRUE(matcher.Matches(36));
- EXPECT_FALSE(matcher.Matches(42));
- }
- // Tests that the program aborts when ResultOf is passed
- // a NULL function pointer.
- TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
- EXPECT_DEATH_IF_SUPPORTED(
- ResultOf(static_cast<std::string (*)(int dummy)>(nullptr),
- Eq(std::string("foo"))),
- "NULL function pointer is passed into ResultOf\\(\\)\\.");
- }
- // Tests that ResultOf(f, ...) compiles and works as expected when f is a
- // function reference.
- TEST(ResultOfTest, WorksForFunctionReferences) {
- Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
- EXPECT_TRUE(matcher.Matches(1));
- EXPECT_FALSE(matcher.Matches(2));
- }
- // Tests that ResultOf(f, ...) compiles and works as expected when f is a
- // function object.
- struct Functor {
- std::string operator()(int input) const { return IntToStringFunction(input); }
- };
- TEST(ResultOfTest, WorksForFunctors) {
- Matcher<int> matcher = ResultOf(Functor(), Eq(std::string("foo")));
- EXPECT_TRUE(matcher.Matches(1));
- EXPECT_FALSE(matcher.Matches(2));
- }
- // Tests that ResultOf(f, ...) compiles and works as expected when f is a
- // functor with more than one operator() defined. ResultOf() must work
- // for each defined operator().
- struct PolymorphicFunctor {
- typedef int result_type;
- int operator()(int n) { return n; }
- int operator()(const char* s) { return static_cast<int>(strlen(s)); }
- std::string operator()(int* p) { return p ? "good ptr" : "null"; }
- };
- TEST(ResultOfTest, WorksForPolymorphicFunctors) {
- Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
- EXPECT_TRUE(matcher_int.Matches(10));
- EXPECT_FALSE(matcher_int.Matches(2));
- Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
- EXPECT_TRUE(matcher_string.Matches("long string"));
- EXPECT_FALSE(matcher_string.Matches("shrt"));
- }
- TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
- Matcher<int*> matcher = ResultOf(PolymorphicFunctor(), "good ptr");
- int n = 0;
- EXPECT_TRUE(matcher.Matches(&n));
- EXPECT_FALSE(matcher.Matches(nullptr));
- }
- TEST(ResultOfTest, WorksForLambdas) {
- Matcher<int> matcher = ResultOf(
- [](int str_len) {
- return std::string(static_cast<size_t>(str_len), 'x');
- },
- "xxx");
- EXPECT_TRUE(matcher.Matches(3));
- EXPECT_FALSE(matcher.Matches(1));
- }
- TEST(ResultOfTest, WorksForNonCopyableArguments) {
- Matcher<std::unique_ptr<int>> matcher = ResultOf(
- [](const std::unique_ptr<int>& str_len) {
- return std::string(static_cast<size_t>(*str_len), 'x');
- },
- "xxx");
- EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(new int(3))));
- EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(new int(1))));
- }
- const int* ReferencingFunction(const int& n) { return &n; }
- struct ReferencingFunctor {
- typedef const int* result_type;
- result_type operator()(const int& n) { return &n; }
- };
- TEST(ResultOfTest, WorksForReferencingCallables) {
- const int n = 1;
- const int n2 = 1;
- Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
- EXPECT_TRUE(matcher2.Matches(n));
- EXPECT_FALSE(matcher2.Matches(n2));
- Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
- EXPECT_TRUE(matcher3.Matches(n));
- EXPECT_FALSE(matcher3.Matches(n2));
- }
- TEST(SizeIsTest, ImplementsSizeIs) {
- vector<int> container;
- EXPECT_THAT(container, SizeIs(0));
- EXPECT_THAT(container, Not(SizeIs(1)));
- container.push_back(0);
- EXPECT_THAT(container, Not(SizeIs(0)));
- EXPECT_THAT(container, SizeIs(1));
- container.push_back(0);
- EXPECT_THAT(container, Not(SizeIs(0)));
- EXPECT_THAT(container, SizeIs(2));
- }
- TEST(SizeIsTest, WorksWithMap) {
- map<std::string, int> container;
- EXPECT_THAT(container, SizeIs(0));
- EXPECT_THAT(container, Not(SizeIs(1)));
- container.insert(make_pair("foo", 1));
- EXPECT_THAT(container, Not(SizeIs(0)));
- EXPECT_THAT(container, SizeIs(1));
- container.insert(make_pair("bar", 2));
- EXPECT_THAT(container, Not(SizeIs(0)));
- EXPECT_THAT(container, SizeIs(2));
- }
- TEST(SizeIsTest, WorksWithReferences) {
- vector<int> container;
- Matcher<const vector<int>&> m = SizeIs(1);
- EXPECT_THAT(container, Not(m));
- container.push_back(0);
- EXPECT_THAT(container, m);
- }
- TEST(SizeIsTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(SizeIs(3)));
- helper.Call(MakeUniquePtrs({1, 2, 3}));
- }
- // SizeIs should work for any type that provides a size() member function.
- // For example, a size_type member type should not need to be provided.
- struct MinimalistCustomType {
- int size() const { return 1; }
- };
- TEST(SizeIsTest, WorksWithMinimalistCustomType) {
- MinimalistCustomType container;
- EXPECT_THAT(container, SizeIs(1));
- EXPECT_THAT(container, Not(SizeIs(0)));
- }
- TEST(SizeIsTest, CanDescribeSelf) {
- Matcher<vector<int>> m = SizeIs(2);
- EXPECT_EQ("has a size that is equal to 2", Describe(m));
- EXPECT_EQ("has a size that isn't equal to 2", DescribeNegation(m));
- }
- TEST(SizeIsTest, ExplainsResult) {
- Matcher<vector<int>> m1 = SizeIs(2);
- Matcher<vector<int>> m2 = SizeIs(Lt(2u));
- Matcher<vector<int>> m3 = SizeIs(AnyOf(0, 3));
- Matcher<vector<int>> m4 = SizeIs(Gt(1u));
- vector<int> container;
- EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
- EXPECT_EQ("whose size 0 matches", Explain(m2, container));
- EXPECT_EQ("whose size 0 matches, which matches (is equal to 0)",
- Explain(m3, container));
- EXPECT_EQ("whose size 0 doesn't match", Explain(m4, container));
- container.push_back(0);
- container.push_back(0);
- EXPECT_EQ("whose size 2 matches", Explain(m1, container));
- EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
- EXPECT_EQ(
- "whose size 2 doesn't match, isn't equal to 0, and isn't equal to 3",
- Explain(m3, container));
- EXPECT_EQ("whose size 2 matches", Explain(m4, container));
- }
- TEST(WhenSortedByTest, WorksForEmptyContainer) {
- const vector<int> numbers;
- EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
- EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
- }
- TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
- vector<unsigned> numbers;
- numbers.push_back(3);
- numbers.push_back(1);
- numbers.push_back(2);
- numbers.push_back(2);
- EXPECT_THAT(numbers,
- WhenSortedBy(greater<unsigned>(), ElementsAre(3, 2, 2, 1)));
- EXPECT_THAT(numbers,
- Not(WhenSortedBy(greater<unsigned>(), ElementsAre(1, 2, 2, 3))));
- }
- TEST(WhenSortedByTest, WorksForNonVectorContainer) {
- list<std::string> words;
- words.push_back("say");
- words.push_back("hello");
- words.push_back("world");
- EXPECT_THAT(words, WhenSortedBy(less<std::string>(),
- ElementsAre("hello", "say", "world")));
- EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),
- ElementsAre("say", "hello", "world"))));
- }
- TEST(WhenSortedByTest, WorksForNativeArray) {
- const int numbers[] = {1, 3, 2, 4};
- const int sorted_numbers[] = {1, 2, 3, 4};
- EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
- EXPECT_THAT(numbers,
- WhenSortedBy(less<int>(), ElementsAreArray(sorted_numbers)));
- EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
- }
- TEST(WhenSortedByTest, CanDescribeSelf) {
- const Matcher<vector<int>> m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
- EXPECT_EQ(
- "(when sorted) has 2 elements where\n"
- "element #0 is equal to 1,\n"
- "element #1 is equal to 2",
- Describe(m));
- EXPECT_EQ(
- "(when sorted) doesn't have 2 elements, or\n"
- "element #0 isn't equal to 1, or\n"
- "element #1 isn't equal to 2",
- DescribeNegation(m));
- }
- TEST(WhenSortedByTest, ExplainsMatchResult) {
- const int a[] = {2, 1};
- EXPECT_EQ(
- Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a),
- "which is { 1, 2 } when sorted, whose element #0 (1) isn't equal to 2");
- EXPECT_EQ(Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a),
- "which is { 1, 2 } when sorted");
- }
- // WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
- // need to test it as exhaustively as we test the latter.
- TEST(WhenSortedTest, WorksForEmptyContainer) {
- const vector<int> numbers;
- EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
- EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
- }
- TEST(WhenSortedTest, WorksForNonEmptyContainer) {
- list<std::string> words;
- words.push_back("3");
- words.push_back("1");
- words.push_back("2");
- words.push_back("2");
- EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
- EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
- }
- TEST(WhenSortedTest, WorksForMapTypes) {
- map<std::string, int> word_counts;
- word_counts["and"] = 1;
- word_counts["the"] = 1;
- word_counts["buffalo"] = 2;
- EXPECT_THAT(word_counts,
- WhenSorted(ElementsAre(Pair("and", 1), Pair("buffalo", 2),
- Pair("the", 1))));
- EXPECT_THAT(word_counts,
- Not(WhenSorted(ElementsAre(Pair("and", 1), Pair("the", 1),
- Pair("buffalo", 2)))));
- }
- TEST(WhenSortedTest, WorksForMultiMapTypes) {
- multimap<int, int> ifib;
- ifib.insert(make_pair(8, 6));
- ifib.insert(make_pair(2, 3));
- ifib.insert(make_pair(1, 1));
- ifib.insert(make_pair(3, 4));
- ifib.insert(make_pair(1, 2));
- ifib.insert(make_pair(5, 5));
- EXPECT_THAT(ifib,
- WhenSorted(ElementsAre(Pair(1, 1), Pair(1, 2), Pair(2, 3),
- Pair(3, 4), Pair(5, 5), Pair(8, 6))));
- EXPECT_THAT(ifib,
- Not(WhenSorted(ElementsAre(Pair(8, 6), Pair(2, 3), Pair(1, 1),
- Pair(3, 4), Pair(1, 2), Pair(5, 5)))));
- }
- TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
- std::deque<int> d;
- d.push_back(2);
- d.push_back(1);
- EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));
- EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
- }
- TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
- std::deque<int> d;
- d.push_back(2);
- d.push_back(1);
- Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
- EXPECT_THAT(d, WhenSorted(vector_match));
- Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
- EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
- }
- // Deliberately bare pseudo-container.
- // Offers only begin() and end() accessors, yielding InputIterator.
- template <typename T>
- class Streamlike {
- private:
- class ConstIter;
- public:
- typedef ConstIter const_iterator;
- typedef T value_type;
- template <typename InIter>
- Streamlike(InIter first, InIter last) : remainder_(first, last) {}
- const_iterator begin() const {
- return const_iterator(this, remainder_.begin());
- }
- const_iterator end() const { return const_iterator(this, remainder_.end()); }
- private:
- class ConstIter {
- public:
- using iterator_category = std::input_iterator_tag;
- using value_type = T;
- using difference_type = ptrdiff_t;
- using pointer = const value_type*;
- using reference = const value_type&;
- ConstIter(const Streamlike* s, typename std::list<value_type>::iterator pos)
- : s_(s), pos_(pos) {}
- const value_type& operator*() const { return *pos_; }
- const value_type* operator->() const { return &*pos_; }
- ConstIter& operator++() {
- s_->remainder_.erase(pos_++);
- return *this;
- }
- // *iter++ is required to work (see std::istreambuf_iterator).
- // (void)iter++ is also required to work.
- class PostIncrProxy {
- public:
- explicit PostIncrProxy(const value_type& value) : value_(value) {}
- value_type operator*() const { return value_; }
- private:
- value_type value_;
- };
- PostIncrProxy operator++(int) {
- PostIncrProxy proxy(**this);
- ++(*this);
- return proxy;
- }
- friend bool operator==(const ConstIter& a, const ConstIter& b) {
- return a.s_ == b.s_ && a.pos_ == b.pos_;
- }
- friend bool operator!=(const ConstIter& a, const ConstIter& b) {
- return !(a == b);
- }
- private:
- const Streamlike* s_;
- typename std::list<value_type>::iterator pos_;
- };
- friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) {
- os << "[";
- typedef typename std::list<value_type>::const_iterator Iter;
- const char* sep = "";
- for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
- os << sep << *it;
- sep = ",";
- }
- os << "]";
- return os;
- }
- mutable std::list<value_type> remainder_; // modified by iteration
- };
- TEST(StreamlikeTest, Iteration) {
- const int a[5] = {2, 1, 4, 5, 3};
- Streamlike<int> s(a, a + 5);
- Streamlike<int>::const_iterator it = s.begin();
- const int* ip = a;
- while (it != s.end()) {
- SCOPED_TRACE(ip - a);
- EXPECT_EQ(*ip++, *it++);
- }
- }
- INSTANTIATE_GTEST_MATCHER_TEST_P(BeginEndDistanceIsTest);
- TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
- std::forward_list<int> container;
- EXPECT_THAT(container, BeginEndDistanceIs(0));
- EXPECT_THAT(container, Not(BeginEndDistanceIs(1)));
- container.push_front(0);
- EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
- EXPECT_THAT(container, BeginEndDistanceIs(1));
- container.push_front(0);
- EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
- EXPECT_THAT(container, BeginEndDistanceIs(2));
- }
- TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
- const int a[5] = {1, 2, 3, 4, 5};
- Streamlike<int> s(a, a + 5);
- EXPECT_THAT(s, BeginEndDistanceIs(5));
- }
- TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
- Matcher<vector<int>> m = BeginEndDistanceIs(2);
- EXPECT_EQ("distance between begin() and end() is equal to 2", Describe(m));
- EXPECT_EQ("distance between begin() and end() isn't equal to 2",
- DescribeNegation(m));
- }
- TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(BeginEndDistanceIs(2)));
- helper.Call(MakeUniquePtrs({1, 2}));
- }
- TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
- Matcher<vector<int>> m1 = BeginEndDistanceIs(2);
- Matcher<vector<int>> m2 = BeginEndDistanceIs(Lt(2));
- Matcher<vector<int>> m3 = BeginEndDistanceIs(AnyOf(0, 3));
- Matcher<vector<int>> m4 = BeginEndDistanceIs(GreaterThan(1));
- vector<int> container;
- EXPECT_EQ("whose distance between begin() and end() 0 doesn't match",
- Explain(m1, container));
- EXPECT_EQ("whose distance between begin() and end() 0 matches",
- Explain(m2, container));
- EXPECT_EQ(
- "whose distance between begin() and end() 0 matches, which matches (is "
- "equal to 0)",
- Explain(m3, container));
- EXPECT_EQ(
- "whose distance between begin() and end() 0 doesn't match, which is 1 "
- "less than 1",
- Explain(m4, container));
- container.push_back(0);
- container.push_back(0);
- EXPECT_EQ("whose distance between begin() and end() 2 matches",
- Explain(m1, container));
- EXPECT_EQ("whose distance between begin() and end() 2 doesn't match",
- Explain(m2, container));
- EXPECT_EQ(
- "whose distance between begin() and end() 2 doesn't match, isn't equal "
- "to 0, and isn't equal to 3",
- Explain(m3, container));
- EXPECT_EQ(
- "whose distance between begin() and end() 2 matches, which is 1 more "
- "than 1",
- Explain(m4, container));
- }
- TEST(WhenSortedTest, WorksForStreamlike) {
- // Streamlike 'container' provides only minimal iterator support.
- // Its iterators are tagged with input_iterator_tag.
- const int a[5] = {2, 1, 4, 5, 3};
- Streamlike<int> s(std::begin(a), std::end(a));
- EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
- EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
- }
- TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
- const int a[] = {2, 1, 4, 5, 3};
- Streamlike<int> s(std::begin(a), std::end(a));
- Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
- EXPECT_THAT(s, WhenSorted(vector_match));
- EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
- }
- TEST(IsSupersetOfTest, WorksForNativeArray) {
- const int subset[] = {1, 4};
- const int superset[] = {1, 2, 4};
- const int disjoint[] = {1, 0, 3};
- EXPECT_THAT(subset, IsSupersetOf(subset));
- EXPECT_THAT(subset, Not(IsSupersetOf(superset)));
- EXPECT_THAT(superset, IsSupersetOf(subset));
- EXPECT_THAT(subset, Not(IsSupersetOf(disjoint)));
- EXPECT_THAT(disjoint, Not(IsSupersetOf(subset)));
- }
- TEST(IsSupersetOfTest, WorksWithDuplicates) {
- const int not_enough[] = {1, 2};
- const int enough[] = {1, 1, 2};
- const int expected[] = {1, 1};
- EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));
- EXPECT_THAT(enough, IsSupersetOf(expected));
- }
- TEST(IsSupersetOfTest, WorksForEmpty) {
- vector<int> numbers;
- vector<int> expected;
- EXPECT_THAT(numbers, IsSupersetOf(expected));
- expected.push_back(1);
- EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
- expected.clear();
- numbers.push_back(1);
- numbers.push_back(2);
- EXPECT_THAT(numbers, IsSupersetOf(expected));
- expected.push_back(1);
- EXPECT_THAT(numbers, IsSupersetOf(expected));
- expected.push_back(2);
- EXPECT_THAT(numbers, IsSupersetOf(expected));
- expected.push_back(3);
- EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
- }
- TEST(IsSupersetOfTest, WorksForStreamlike) {
- const int a[5] = {1, 2, 3, 4, 5};
- Streamlike<int> s(std::begin(a), std::end(a));
- vector<int> expected;
- expected.push_back(1);
- expected.push_back(2);
- expected.push_back(5);
- EXPECT_THAT(s, IsSupersetOf(expected));
- expected.push_back(0);
- EXPECT_THAT(s, Not(IsSupersetOf(expected)));
- }
- TEST(IsSupersetOfTest, TakesStlContainer) {
- const int actual[] = {3, 1, 2};
- ::std::list<int> expected;
- expected.push_back(1);
- expected.push_back(3);
- EXPECT_THAT(actual, IsSupersetOf(expected));
- expected.push_back(4);
- EXPECT_THAT(actual, Not(IsSupersetOf(expected)));
- }
- TEST(IsSupersetOfTest, Describe) {
- typedef std::vector<int> IntVec;
- IntVec expected;
- expected.push_back(111);
- expected.push_back(222);
- expected.push_back(333);
- EXPECT_THAT(
- Describe<IntVec>(IsSupersetOf(expected)),
- Eq("a surjection from elements to requirements exists such that:\n"
- " - an element is equal to 111\n"
- " - an element is equal to 222\n"
- " - an element is equal to 333"));
- }
- TEST(IsSupersetOfTest, DescribeNegation) {
- typedef std::vector<int> IntVec;
- IntVec expected;
- expected.push_back(111);
- expected.push_back(222);
- expected.push_back(333);
- EXPECT_THAT(
- DescribeNegation<IntVec>(IsSupersetOf(expected)),
- Eq("no surjection from elements to requirements exists such that:\n"
- " - an element is equal to 111\n"
- " - an element is equal to 222\n"
- " - an element is equal to 333"));
- }
- TEST(IsSupersetOfTest, MatchAndExplain) {
- std::vector<int> v;
- v.push_back(2);
- v.push_back(3);
- std::vector<int> expected;
- expected.push_back(1);
- expected.push_back(2);
- StringMatchResultListener listener;
- ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(),
- Eq("where the following matchers don't match any elements:\n"
- "matcher #0: is equal to 1"));
- v.push_back(1);
- listener.Clear();
- ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(), Eq("where:\n"
- " - element #0 is matched by matcher #1,\n"
- " - element #2 is matched by matcher #0"));
- }
- TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
- const int numbers[] = {1, 3, 6, 2, 4, 5};
- EXPECT_THAT(numbers, IsSupersetOf({1, 2}));
- EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0})));
- }
- TEST(IsSupersetOfTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
- helper.Call(MakeUniquePtrs({1, 2}));
- EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
- helper.Call(MakeUniquePtrs({2}));
- }
- TEST(IsSubsetOfTest, WorksForNativeArray) {
- const int subset[] = {1, 4};
- const int superset[] = {1, 2, 4};
- const int disjoint[] = {1, 0, 3};
- EXPECT_THAT(subset, IsSubsetOf(subset));
- EXPECT_THAT(subset, IsSubsetOf(superset));
- EXPECT_THAT(superset, Not(IsSubsetOf(subset)));
- EXPECT_THAT(subset, Not(IsSubsetOf(disjoint)));
- EXPECT_THAT(disjoint, Not(IsSubsetOf(subset)));
- }
- TEST(IsSubsetOfTest, WorksWithDuplicates) {
- const int not_enough[] = {1, 2};
- const int enough[] = {1, 1, 2};
- const int actual[] = {1, 1};
- EXPECT_THAT(actual, Not(IsSubsetOf(not_enough)));
- EXPECT_THAT(actual, IsSubsetOf(enough));
- }
- TEST(IsSubsetOfTest, WorksForEmpty) {
- vector<int> numbers;
- vector<int> expected;
- EXPECT_THAT(numbers, IsSubsetOf(expected));
- expected.push_back(1);
- EXPECT_THAT(numbers, IsSubsetOf(expected));
- expected.clear();
- numbers.push_back(1);
- numbers.push_back(2);
- EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));
- expected.push_back(1);
- EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));
- expected.push_back(2);
- EXPECT_THAT(numbers, IsSubsetOf(expected));
- expected.push_back(3);
- EXPECT_THAT(numbers, IsSubsetOf(expected));
- }
- TEST(IsSubsetOfTest, WorksForStreamlike) {
- const int a[5] = {1, 2};
- Streamlike<int> s(std::begin(a), std::end(a));
- vector<int> expected;
- expected.push_back(1);
- EXPECT_THAT(s, Not(IsSubsetOf(expected)));
- expected.push_back(2);
- expected.push_back(5);
- EXPECT_THAT(s, IsSubsetOf(expected));
- }
- TEST(IsSubsetOfTest, TakesStlContainer) {
- const int actual[] = {3, 1, 2};
- ::std::list<int> expected;
- expected.push_back(1);
- expected.push_back(3);
- EXPECT_THAT(actual, Not(IsSubsetOf(expected)));
- expected.push_back(2);
- expected.push_back(4);
- EXPECT_THAT(actual, IsSubsetOf(expected));
- }
- TEST(IsSubsetOfTest, Describe) {
- typedef std::vector<int> IntVec;
- IntVec expected;
- expected.push_back(111);
- expected.push_back(222);
- expected.push_back(333);
- EXPECT_THAT(
- Describe<IntVec>(IsSubsetOf(expected)),
- Eq("an injection from elements to requirements exists such that:\n"
- " - an element is equal to 111\n"
- " - an element is equal to 222\n"
- " - an element is equal to 333"));
- }
- TEST(IsSubsetOfTest, DescribeNegation) {
- typedef std::vector<int> IntVec;
- IntVec expected;
- expected.push_back(111);
- expected.push_back(222);
- expected.push_back(333);
- EXPECT_THAT(
- DescribeNegation<IntVec>(IsSubsetOf(expected)),
- Eq("no injection from elements to requirements exists such that:\n"
- " - an element is equal to 111\n"
- " - an element is equal to 222\n"
- " - an element is equal to 333"));
- }
- TEST(IsSubsetOfTest, MatchAndExplain) {
- std::vector<int> v;
- v.push_back(2);
- v.push_back(3);
- std::vector<int> expected;
- expected.push_back(1);
- expected.push_back(2);
- StringMatchResultListener listener;
- ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(),
- Eq("where the following elements don't match any matchers:\n"
- "element #1: 3"));
- expected.push_back(3);
- listener.Clear();
- ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(), Eq("where:\n"
- " - element #0 is matched by matcher #1,\n"
- " - element #1 is matched by matcher #2"));
- }
- TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
- const int numbers[] = {1, 2, 3};
- EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4}));
- EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2})));
- }
- TEST(IsSubsetOfTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
- helper.Call(MakeUniquePtrs({1}));
- EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
- helper.Call(MakeUniquePtrs({2}));
- }
- // A container whose iterator returns a temporary. This can iterate over the
- // characters in a string.
- class CharString {
- public:
- using value_type = char;
- class const_iterator {
- public:
- using iterator_category = std::input_iterator_tag;
- using value_type = char;
- using difference_type = std::ptrdiff_t;
- using pointer = const char*;
- using reference = const char&;
- // Create an iterator that points to the given character.
- explicit const_iterator(const char* ptr) : ptr_(ptr) {}
- // Returns the current character. IMPORTANT: this must return a temporary,
- // not a reference, to test that ElementsAre() works with containers whose
- // iterators return temporaries.
- char operator*() const { return *ptr_; }
- // Advances to the next character.
- const_iterator& operator++() {
- ++ptr_;
- return *this;
- }
- // Compares two iterators.
- bool operator==(const const_iterator& other) const {
- return ptr_ == other.ptr_;
- }
- bool operator!=(const const_iterator& other) const {
- return ptr_ != other.ptr_;
- }
- private:
- const char* ptr_ = nullptr;
- };
- // Creates a CharString that contains the given string.
- explicit CharString(const std::string& s) : s_(s) {}
- // Returns an iterator pointing to the first character in the string.
- const_iterator begin() const { return const_iterator(s_.c_str()); }
- // Returns an iterator pointing past the last character in the string.
- const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
- private:
- std::string s_;
- };
- // Tests using ElementsAre() with a container whose iterator returns a
- // temporary.
- TEST(ElementsAreTest, WorksWithContainerThatReturnsTempInIterator) {
- CharString s("abc");
- EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
- EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
- }
- // Tests using ElementsAreArray() with a container whose iterator returns a
- // temporary.
- TEST(ElementsAreArrayTest, WorksWithContainerThatReturnsTempInIterator) {
- CharString s("abc");
- EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
- EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
- }
- // A container whose iterator returns a temporary and is not copy-assignable.
- // This simulates the behavior of the proxy object returned by absl::StrSplit().
- class CharString2 {
- public:
- using value_type = char;
- class const_iterator {
- public:
- using iterator_category = std::input_iterator_tag;
- using value_type = char;
- using difference_type = std::ptrdiff_t;
- using pointer = const char*;
- using reference = const char&;
- // Make const_iterator copy-constructible but not copy-assignable,
- // simulating the behavior of the proxy object returned by absl::StrSplit().
- const_iterator(const const_iterator&) = default;
- const_iterator& operator=(const const_iterator&) = delete;
- // Create an iterator that points to the given character.
- explicit const_iterator(const char* ptr) : ptr_(ptr) {}
- // Returns the current character. IMPORTANT: this must return a temporary,
- // not a reference, to test that ElementsAre() works with containers whose
- // iterators return temporaries.
- char operator*() const { return *ptr_; }
- // Advances to the next character.
- const_iterator& operator++() {
- ++ptr_;
- return *this;
- }
- // Compares two iterators.
- bool operator==(const const_iterator& other) const {
- return ptr_ == other.ptr_;
- }
- bool operator!=(const const_iterator& other) const {
- return ptr_ != other.ptr_;
- }
- private:
- const char* ptr_ = nullptr;
- };
- // Creates a CharString that contains the given string.
- explicit CharString2(const std::string& s) : s_(s) {}
- // Returns an iterator pointing to the first character in the string.
- const_iterator begin() const { return const_iterator(s_.c_str()); }
- // Returns an iterator pointing past the last character in the string.
- const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
- private:
- std::string s_;
- };
- // Tests using ElementsAre() with a container whose iterator returns a
- // temporary and is not copy-assignable.
- TEST(ElementsAreTest, WorksWithContainerThatReturnsTempInUnassignableIterator) {
- CharString2 s("abc");
- EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
- EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
- }
- // Tests using ElementsAreArray() with a container whose iterator returns a
- // temporary and is not copy-assignable.
- TEST(ElementsAreArrayTest,
- WorksWithContainerThatReturnsTempInUnassignableIterator) {
- CharString2 s("abc");
- EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
- EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
- }
- // A container whose iterator returns a temporary and is neither
- // copy-constructible nor copy-assignable.
- class CharString3 {
- public:
- using value_type = char;
- class const_iterator {
- public:
- using iterator_category = std::input_iterator_tag;
- using value_type = char;
- using difference_type = std::ptrdiff_t;
- using pointer = const char*;
- using reference = const char&;
- // Make const_iterator neither copy-constructible nor copy-assignable.
- const_iterator(const const_iterator&) = delete;
- const_iterator& operator=(const const_iterator&) = delete;
- // Create an iterator that points to the given character.
- explicit const_iterator(const char* ptr) : ptr_(ptr) {}
- // Returns the current character. IMPORTANT: this must return a temporary,
- // not a reference, to test that ElementsAre() works with containers whose
- // iterators return temporaries.
- char operator*() const { return *ptr_; }
- // Advances to the next character.
- const_iterator& operator++() {
- ++ptr_;
- return *this;
- }
- // Compares two iterators.
- bool operator==(const const_iterator& other) const {
- return ptr_ == other.ptr_;
- }
- bool operator!=(const const_iterator& other) const {
- return ptr_ != other.ptr_;
- }
- private:
- const char* ptr_ = nullptr;
- };
- // Creates a CharString that contains the given string.
- explicit CharString3(const std::string& s) : s_(s) {}
- // Returns an iterator pointing to the first character in the string.
- const_iterator begin() const { return const_iterator(s_.c_str()); }
- // Returns an iterator pointing past the last character in the string.
- const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
- private:
- std::string s_;
- };
- // Tests using ElementsAre() with a container whose iterator returns a
- // temporary and is neither copy-constructible nor copy-assignable.
- TEST(ElementsAreTest, WorksWithContainerThatReturnsTempInUncopyableIterator) {
- CharString3 s("abc");
- EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
- EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
- }
- // Tests using ElementsAreArray() with a container whose iterator returns a
- // temporary and is neither copy-constructible nor copy-assignable.
- TEST(ElementsAreArrayTest,
- WorksWithContainerThatReturnsTempInUncopyableIterator) {
- CharString3 s("abc");
- EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
- EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
- }
- // A container whose iterator returns a temporary, is neither
- // copy-constructible nor copy-assignable, and has no member types.
- class CharString4 {
- public:
- using value_type = char;
- class const_iterator {
- public:
- // Do not define difference_type, etc.
- // Make const_iterator neither copy-constructible nor copy-assignable.
- const_iterator(const const_iterator&) = delete;
- const_iterator& operator=(const const_iterator&) = delete;
- // Create an iterator that points to the given character.
- explicit const_iterator(const char* ptr) : ptr_(ptr) {}
- // Returns the current character. IMPORTANT: this must return a temporary,
- // not a reference, to test that ElementsAre() works with containers whose
- // iterators return temporaries.
- char operator*() const { return *ptr_; }
- // Advances to the next character.
- const_iterator& operator++() {
- ++ptr_;
- return *this;
- }
- // Compares two iterators.
- bool operator==(const const_iterator& other) const {
- return ptr_ == other.ptr_;
- }
- bool operator!=(const const_iterator& other) const {
- return ptr_ != other.ptr_;
- }
- private:
- const char* ptr_ = nullptr;
- };
- // Creates a CharString that contains the given string.
- explicit CharString4(const std::string& s) : s_(s) {}
- // Returns an iterator pointing to the first character in the string.
- const_iterator begin() const { return const_iterator(s_.c_str()); }
- // Returns an iterator pointing past the last character in the string.
- const_iterator end() const { return const_iterator(s_.c_str() + s_.size()); }
- private:
- std::string s_;
- };
- // Tests using ElementsAre() with a container whose iterator returns a
- // temporary, is neither copy-constructible nor copy-assignable, and has no
- // member types.
- TEST(ElementsAreTest, WorksWithContainerWithIteratorWithNoMemberTypes) {
- CharString4 s("abc");
- EXPECT_THAT(s, ElementsAre('a', 'b', 'c'));
- EXPECT_THAT(s, Not(ElementsAre('a', 'b', 'd')));
- }
- // Tests using ElementsAreArray() with a container whose iterator returns a
- // temporary, is neither copy-constructible nor copy-assignable, and has no
- // member types.
- TEST(ElementsAreArrayTest, WorksWithContainerWithIteratorWithNoMemberTypes) {
- CharString4 s("abc");
- EXPECT_THAT(s, ElementsAreArray({'a', 'b', 'c'}));
- EXPECT_THAT(s, Not(ElementsAreArray({'a', 'b', 'd'})));
- }
- // Tests using ElementsAre() and ElementsAreArray() with stream-like
- // "containers".
- TEST(ElemensAreStreamTest, WorksForStreamlike) {
- const int a[5] = {1, 2, 3, 4, 5};
- Streamlike<int> s(std::begin(a), std::end(a));
- EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5));
- EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3)));
- }
- TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
- const int a[5] = {1, 2, 3, 4, 5};
- Streamlike<int> s(std::begin(a), std::end(a));
- vector<int> expected;
- expected.push_back(1);
- expected.push_back(2);
- expected.push_back(3);
- expected.push_back(4);
- expected.push_back(5);
- EXPECT_THAT(s, ElementsAreArray(expected));
- expected[3] = 0;
- EXPECT_THAT(s, Not(ElementsAreArray(expected)));
- }
- TEST(ElementsAreTest, WorksWithUncopyable) {
- Uncopyable objs[2];
- objs[0].set_value(-3);
- objs[1].set_value(1);
- EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
- }
- TEST(ElementsAreTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
- helper.Call(MakeUniquePtrs({1, 2}));
- EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
- helper.Call(MakeUniquePtrs({3, 4}));
- }
- TEST(ElementsAreTest, TakesStlContainer) {
- const int actual[] = {3, 1, 2};
- ::std::list<int> expected;
- expected.push_back(3);
- expected.push_back(1);
- expected.push_back(2);
- EXPECT_THAT(actual, ElementsAreArray(expected));
- expected.push_back(4);
- EXPECT_THAT(actual, Not(ElementsAreArray(expected)));
- }
- // Tests for UnorderedElementsAreArray()
- TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
- const int a[] = {0, 1, 2, 3, 4};
- std::vector<int> s(std::begin(a), std::end(a));
- do {
- StringMatchResultListener listener;
- EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), s, &listener))
- << listener.str();
- } while (std::next_permutation(s.begin(), s.end()));
- }
- TEST(UnorderedElementsAreArrayTest, VectorBool) {
- const bool a[] = {false, true, false, true, true};
- const bool b[] = {true, false, true, true, false};
- std::vector<bool> expected(std::begin(a), std::end(a));
- std::vector<bool> actual(std::begin(b), std::end(b));
- StringMatchResultListener listener;
- EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), actual,
- &listener))
- << listener.str();
- }
- TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
- // Streamlike 'container' provides only minimal iterator support.
- // Its iterators are tagged with input_iterator_tag, and it has no
- // size() or empty() methods.
- const int a[5] = {2, 1, 4, 5, 3};
- Streamlike<int> s(std::begin(a), std::end(a));
- ::std::vector<int> expected;
- expected.push_back(1);
- expected.push_back(2);
- expected.push_back(3);
- expected.push_back(4);
- expected.push_back(5);
- EXPECT_THAT(s, UnorderedElementsAreArray(expected));
- expected.push_back(6);
- EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
- }
- TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
- const int actual[] = {3, 1, 2};
- ::std::list<int> expected;
- expected.push_back(1);
- expected.push_back(2);
- expected.push_back(3);
- EXPECT_THAT(actual, UnorderedElementsAreArray(expected));
- expected.push_back(4);
- EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
- }
- TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
- const int a[5] = {2, 1, 4, 5, 3};
- EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
- EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
- }
- TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
- const std::string a[5] = {"a", "b", "c", "d", "e"};
- EXPECT_THAT(a, UnorderedElementsAreArray({"a", "b", "c", "d", "e"}));
- EXPECT_THAT(a, Not(UnorderedElementsAreArray({"a", "b", "c", "d", "ef"})));
- }
- TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
- const int a[5] = {2, 1, 4, 5, 3};
- EXPECT_THAT(a,
- UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
- EXPECT_THAT(
- a, Not(UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
- }
- TEST(UnorderedElementsAreArrayTest,
- TakesInitializerListOfDifferentTypedMatchers) {
- const int a[5] = {2, 1, 4, 5, 3};
- // The compiler cannot infer the type of the initializer list if its
- // elements have different types. We must explicitly specify the
- // unified element type in this case.
- EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int>>(
- {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
- EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int>>(
- {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
- }
- TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper,
- Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
- helper.Call(MakeUniquePtrs({2, 1}));
- }
- class UnorderedElementsAreTest : public testing::Test {
- protected:
- typedef std::vector<int> IntVec;
- };
- TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
- Uncopyable objs[2];
- objs[0].set_value(-3);
- objs[1].set_value(1);
- EXPECT_THAT(objs,
- UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
- }
- TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
- const int a[] = {1, 2, 3};
- std::vector<int> s(std::begin(a), std::end(a));
- do {
- StringMatchResultListener listener;
- EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), s, &listener))
- << listener.str();
- } while (std::next_permutation(s.begin(), s.end()));
- }
- TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
- const int a[] = {1, 2, 3};
- std::vector<int> s(std::begin(a), std::end(a));
- std::vector<Matcher<int>> mv;
- mv.push_back(1);
- mv.push_back(2);
- mv.push_back(2);
- // The element with value '3' matches nothing: fail fast.
- StringMatchResultListener listener;
- EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
- << listener.str();
- }
- TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
- // Streamlike 'container' provides only minimal iterator support.
- // Its iterators are tagged with input_iterator_tag, and it has no
- // size() or empty() methods.
- const int a[5] = {2, 1, 4, 5, 3};
- Streamlike<int> s(std::begin(a), std::end(a));
- EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
- EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
- }
- TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
- helper.Call(MakeUniquePtrs({2, 1}));
- }
- // One naive implementation of the matcher runs in O(N!) time, which is too
- // slow for many real-world inputs. This test shows that our matcher can match
- // 100 inputs very quickly (a few milliseconds). An O(100!) is 10^158
- // iterations and obviously effectively incomputable.
- // [ RUN ] UnorderedElementsAreTest.Performance
- // [ OK ] UnorderedElementsAreTest.Performance (4 ms)
- TEST_F(UnorderedElementsAreTest, Performance) {
- std::vector<int> s;
- std::vector<Matcher<int>> mv;
- for (int i = 0; i < 100; ++i) {
- s.push_back(i);
- mv.push_back(_);
- }
- mv[50] = Eq(0);
- StringMatchResultListener listener;
- EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
- << listener.str();
- }
- // Another variant of 'Performance' with similar expectations.
- // [ RUN ] UnorderedElementsAreTest.PerformanceHalfStrict
- // [ OK ] UnorderedElementsAreTest.PerformanceHalfStrict (4 ms)
- TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
- std::vector<int> s;
- std::vector<Matcher<int>> mv;
- for (int i = 0; i < 100; ++i) {
- s.push_back(i);
- if (i & 1) {
- mv.push_back(_);
- } else {
- mv.push_back(i);
- }
- }
- StringMatchResultListener listener;
- EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
- << listener.str();
- }
- TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
- std::vector<int> v;
- v.push_back(4);
- StringMatchResultListener listener;
- EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(),
- Eq("which has 1 element\n"
- "where the following matchers don't match any elements:\n"
- "matcher #0: is equal to 1,\n"
- "matcher #1: is equal to 2,\n"
- "matcher #2: is equal to 3\n"
- "and where the following elements don't match any matchers:\n"
- "element #0: 4"));
- }
- TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
- std::vector<int> v;
- StringMatchResultListener listener;
- EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(),
- Eq("where the following matchers don't match any elements:\n"
- "matcher #0: is equal to 1,\n"
- "matcher #1: is equal to 2,\n"
- "matcher #2: is equal to 3"));
- }
- TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
- std::vector<int> v;
- v.push_back(1);
- v.push_back(1);
- StringMatchResultListener listener;
- EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(),
- Eq("where the following matchers don't match any elements:\n"
- "matcher #1: is equal to 2"));
- }
- TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
- std::vector<int> v;
- v.push_back(1);
- v.push_back(2);
- StringMatchResultListener listener;
- EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(),
- Eq("where the following elements don't match any matchers:\n"
- "element #1: 2"));
- }
- TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
- std::vector<int> v;
- v.push_back(2);
- v.push_back(3);
- StringMatchResultListener listener;
- EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
- << listener.str();
- EXPECT_THAT(listener.str(),
- Eq("where"
- " the following matchers don't match any elements:\n"
- "matcher #0: is equal to 1\n"
- "and"
- " where"
- " the following elements don't match any matchers:\n"
- "element #1: 3"));
- }
- // Test helper for formatting element, matcher index pairs in expectations.
- static std::string EMString(int element, int matcher) {
- stringstream ss;
- ss << "(element #" << element << ", matcher #" << matcher << ")";
- return ss.str();
- }
- TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
- // A situation where all elements and matchers have a match
- // associated with them, but the max matching is not perfect.
- std::vector<std::string> v;
- v.push_back("a");
- v.push_back("b");
- v.push_back("c");
- StringMatchResultListener listener;
- EXPECT_FALSE(ExplainMatchResult(
- UnorderedElementsAre("a", "a", AnyOf("b", "c")), v, &listener))
- << listener.str();
- std::string prefix =
- "where no permutation of the elements can satisfy all matchers, "
- "and the closest match is 2 of 3 matchers with the "
- "pairings:\n";
- // We have to be a bit loose here, because there are 4 valid max matches.
- EXPECT_THAT(
- listener.str(),
- AnyOf(
- prefix + "{\n " + EMString(0, 0) + ",\n " + EMString(1, 2) + "\n}",
- prefix + "{\n " + EMString(0, 1) + ",\n " + EMString(1, 2) + "\n}",
- prefix + "{\n " + EMString(0, 0) + ",\n " + EMString(2, 2) + "\n}",
- prefix + "{\n " + EMString(0, 1) + ",\n " + EMString(2, 2) +
- "\n}"));
- }
- TEST_F(UnorderedElementsAreTest, Describe) {
- EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()), Eq("is empty"));
- EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(345)),
- Eq("has 1 element and that element is equal to 345"));
- EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
- Eq("has 3 elements and there exists some permutation "
- "of elements such that:\n"
- " - element #0 is equal to 111, and\n"
- " - element #1 is equal to 222, and\n"
- " - element #2 is equal to 333"));
- }
- TEST_F(UnorderedElementsAreTest, DescribeNegation) {
- EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
- Eq("isn't empty"));
- EXPECT_THAT(
- DescribeNegation<IntVec>(UnorderedElementsAre(345)),
- Eq("doesn't have 1 element, or has 1 element that isn't equal to 345"));
- EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
- Eq("doesn't have 3 elements, or there exists no permutation "
- "of elements such that:\n"
- " - element #0 is equal to 123, and\n"
- " - element #1 is equal to 234, and\n"
- " - element #2 is equal to 345"));
- }
- // Tests Each().
- INSTANTIATE_GTEST_MATCHER_TEST_P(EachTest);
- TEST_P(EachTestP, ExplainsMatchResultCorrectly) {
- set<int> a; // empty
- Matcher<set<int>> m = Each(2);
- EXPECT_EQ("", Explain(m, a));
- Matcher<const int (&)[1]> n = Each(1); // NOLINT
- const int b[1] = {1};
- EXPECT_EQ("", Explain(n, b));
- n = Each(3);
- EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
- a.insert(1);
- a.insert(2);
- a.insert(3);
- m = Each(GreaterThan(0));
- EXPECT_EQ("", Explain(m, a));
- m = Each(GreaterThan(10));
- EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
- Explain(m, a));
- }
- TEST(EachTest, DescribesItselfCorrectly) {
- Matcher<vector<int>> m = Each(1);
- EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
- Matcher<vector<int>> m2 = Not(m);
- EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
- }
- TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
- vector<int> some_vector;
- EXPECT_THAT(some_vector, Each(1));
- some_vector.push_back(3);
- EXPECT_THAT(some_vector, Not(Each(1)));
- EXPECT_THAT(some_vector, Each(3));
- some_vector.push_back(1);
- some_vector.push_back(2);
- EXPECT_THAT(some_vector, Not(Each(3)));
- EXPECT_THAT(some_vector, Each(Lt(3.5)));
- vector<std::string> another_vector;
- another_vector.push_back("fee");
- EXPECT_THAT(another_vector, Each(std::string("fee")));
- another_vector.push_back("fie");
- another_vector.push_back("foe");
- another_vector.push_back("fum");
- EXPECT_THAT(another_vector, Not(Each(std::string("fee"))));
- }
- TEST(EachTest, MatchesMapWhenAllElementsMatch) {
- map<const char*, int> my_map;
- const char* bar = "a string";
- my_map[bar] = 2;
- EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
- map<std::string, int> another_map;
- EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1)));
- another_map["fee"] = 1;
- EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1)));
- another_map["fie"] = 2;
- another_map["foe"] = 3;
- another_map["fum"] = 4;
- EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fee"), 1))));
- EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fum"), 1))));
- EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
- }
- TEST(EachTest, AcceptsMatcher) {
- const int a[] = {1, 2, 3};
- EXPECT_THAT(a, Each(Gt(0)));
- EXPECT_THAT(a, Not(Each(Gt(1))));
- }
- TEST(EachTest, WorksForNativeArrayAsTuple) {
- const int a[] = {1, 2};
- const int* const pointer = a;
- EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));
- EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
- }
- TEST(EachTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(Each(Pointee(Gt(0)))));
- helper.Call(MakeUniquePtrs({1, 2}));
- }
- // For testing Pointwise().
- class IsHalfOfMatcher {
- public:
- template <typename T1, typename T2>
- bool MatchAndExplain(const std::tuple<T1, T2>& a_pair,
- MatchResultListener* listener) const {
- if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
- *listener << "where the second is " << std::get<1>(a_pair);
- return true;
- } else {
- *listener << "where the second/2 is " << std::get<1>(a_pair) / 2;
- return false;
- }
- }
- void DescribeTo(ostream* os) const {
- *os << "are a pair where the first is half of the second";
- }
- void DescribeNegationTo(ostream* os) const {
- *os << "are a pair where the first isn't half of the second";
- }
- };
- PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
- return MakePolymorphicMatcher(IsHalfOfMatcher());
- }
- TEST(PointwiseTest, DescribesSelf) {
- vector<int> rhs;
- rhs.push_back(1);
- rhs.push_back(2);
- rhs.push_back(3);
- const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
- EXPECT_EQ(
- "contains 3 values, where each value and its corresponding value "
- "in { 1, 2, 3 } are a pair where the first is half of the second",
- Describe(m));
- EXPECT_EQ(
- "doesn't contain exactly 3 values, or contains a value x at some "
- "index i where x and the i-th value of { 1, 2, 3 } are a pair "
- "where the first isn't half of the second",
- DescribeNegation(m));
- }
- TEST(PointwiseTest, MakesCopyOfRhs) {
- list<signed char> rhs;
- rhs.push_back(2);
- rhs.push_back(4);
- int lhs[] = {1, 2};
- const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
- EXPECT_THAT(lhs, m);
- // Changing rhs now shouldn't affect m, which made a copy of rhs.
- rhs.push_back(6);
- EXPECT_THAT(lhs, m);
- }
- TEST(PointwiseTest, WorksForLhsNativeArray) {
- const int lhs[] = {1, 2, 3};
- vector<int> rhs;
- rhs.push_back(2);
- rhs.push_back(4);
- rhs.push_back(6);
- EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
- EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
- }
- TEST(PointwiseTest, WorksForRhsNativeArray) {
- const int rhs[] = {1, 2, 3};
- vector<int> lhs;
- lhs.push_back(2);
- lhs.push_back(4);
- lhs.push_back(6);
- EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
- EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
- }
- // Test is effective only with sanitizers.
- TEST(PointwiseTest, WorksForVectorOfBool) {
- vector<bool> rhs(3, false);
- rhs[1] = true;
- vector<bool> lhs = rhs;
- EXPECT_THAT(lhs, Pointwise(Eq(), rhs));
- rhs[0] = true;
- EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs)));
- }
- TEST(PointwiseTest, WorksForRhsInitializerList) {
- const vector<int> lhs{2, 4, 6};
- EXPECT_THAT(lhs, Pointwise(Gt(), {1, 2, 3}));
- EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
- }
- TEST(PointwiseTest, RejectsWrongSize) {
- const double lhs[2] = {1, 2};
- const int rhs[1] = {0};
- EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
- EXPECT_EQ("which contains 2 values", Explain(Pointwise(Gt(), rhs), lhs));
- const int rhs2[3] = {0, 1, 2};
- EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
- }
- TEST(PointwiseTest, RejectsWrongContent) {
- const double lhs[3] = {1, 2, 3};
- const int rhs[3] = {2, 6, 4};
- EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
- EXPECT_EQ(
- "where the value pair (2, 6) at index #1 don't match, "
- "where the second/2 is 3",
- Explain(Pointwise(IsHalfOf(), rhs), lhs));
- }
- TEST(PointwiseTest, AcceptsCorrectContent) {
- const double lhs[3] = {1, 2, 3};
- const int rhs[3] = {2, 4, 6};
- EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
- EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
- }
- TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
- const double lhs[3] = {1, 2, 3};
- const int rhs[3] = {2, 4, 6};
- const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
- EXPECT_THAT(lhs, Pointwise(m1, rhs));
- EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
- // This type works as a std::tuple<const double&, const int&> can be
- // implicitly cast to std::tuple<double, int>.
- const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
- EXPECT_THAT(lhs, Pointwise(m2, rhs));
- EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
- }
- MATCHER(PointeeEquals, "Points to an equal value") {
- return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
- ::testing::get<0>(arg), result_listener);
- }
- TEST(PointwiseTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
- helper.Call(MakeUniquePtrs({1, 2}));
- }
- TEST(UnorderedPointwiseTest, DescribesSelf) {
- vector<int> rhs;
- rhs.push_back(1);
- rhs.push_back(2);
- rhs.push_back(3);
- const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);
- EXPECT_EQ(
- "has 3 elements and there exists some permutation of elements such "
- "that:\n"
- " - element #0 and 1 are a pair where the first is half of the second, "
- "and\n"
- " - element #1 and 2 are a pair where the first is half of the second, "
- "and\n"
- " - element #2 and 3 are a pair where the first is half of the second",
- Describe(m));
- EXPECT_EQ(
- "doesn't have 3 elements, or there exists no permutation of elements "
- "such that:\n"
- " - element #0 and 1 are a pair where the first is half of the second, "
- "and\n"
- " - element #1 and 2 are a pair where the first is half of the second, "
- "and\n"
- " - element #2 and 3 are a pair where the first is half of the second",
- DescribeNegation(m));
- }
- TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
- list<signed char> rhs;
- rhs.push_back(2);
- rhs.push_back(4);
- int lhs[] = {2, 1};
- const Matcher<const int (&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
- EXPECT_THAT(lhs, m);
- // Changing rhs now shouldn't affect m, which made a copy of rhs.
- rhs.push_back(6);
- EXPECT_THAT(lhs, m);
- }
- TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
- const int lhs[] = {1, 2, 3};
- vector<int> rhs;
- rhs.push_back(4);
- rhs.push_back(6);
- rhs.push_back(2);
- EXPECT_THAT(lhs, UnorderedPointwise(Lt(), rhs));
- EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
- }
- TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
- const int rhs[] = {1, 2, 3};
- vector<int> lhs;
- lhs.push_back(4);
- lhs.push_back(2);
- lhs.push_back(6);
- EXPECT_THAT(lhs, UnorderedPointwise(Gt(), rhs));
- EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
- }
- TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
- const vector<int> lhs{2, 4, 6};
- EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
- EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
- }
- TEST(UnorderedPointwiseTest, RejectsWrongSize) {
- const double lhs[2] = {1, 2};
- const int rhs[1] = {0};
- EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
- EXPECT_EQ("which has 2 elements\n",
- Explain(UnorderedPointwise(Gt(), rhs), lhs));
- const int rhs2[3] = {0, 1, 2};
- EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
- }
- TEST(UnorderedPointwiseTest, RejectsWrongContent) {
- const double lhs[3] = {1, 2, 3};
- const int rhs[3] = {2, 6, 6};
- EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
- EXPECT_EQ(
- "where the following elements don't match any matchers:\n"
- "element #1: 2",
- Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
- }
- TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
- const double lhs[3] = {1, 2, 3};
- const int rhs[3] = {2, 4, 6};
- EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
- }
- TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
- const double lhs[3] = {1, 2, 3};
- const int rhs[3] = {6, 4, 2};
- EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
- }
- TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
- const double lhs[3] = {1, 2, 3};
- const int rhs[3] = {4, 6, 2};
- const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
- EXPECT_THAT(lhs, UnorderedPointwise(m1, rhs));
- // This type works as a std::tuple<const double&, const int&> can be
- // implicitly cast to std::tuple<double, int>.
- const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
- EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs));
- }
- TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
- ContainerHelper helper;
- EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
- std::vector<int>{1, 2})));
- helper.Call(MakeUniquePtrs({2, 1}));
- }
- TEST(PointeeTest, WorksOnMoveOnlyType) {
- std::unique_ptr<int> p(new int(3));
- EXPECT_THAT(p, Pointee(Eq(3)));
- EXPECT_THAT(p, Not(Pointee(Eq(2))));
- }
- class PredicateFormatterFromMatcherTest : public ::testing::Test {
- protected:
- enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
- // A matcher that can return different results when used multiple times on the
- // same input. No real matcher should do this; but this lets us test that we
- // detect such behavior and fail appropriately.
- class MockMatcher : public MatcherInterface<Behavior> {
- public:
- bool MatchAndExplain(Behavior behavior,
- MatchResultListener* listener) const override {
- *listener << "[MatchAndExplain]";
- switch (behavior) {
- case kInitialSuccess:
- // The first call to MatchAndExplain should use a "not interested"
- // listener; so this is expected to return |true|. There should be no
- // subsequent calls.
- return !listener->IsInterested();
- case kAlwaysFail:
- return false;
- case kFlaky:
- // The first call to MatchAndExplain should use a "not interested"
- // listener; so this will return |false|. Subsequent calls should have
- // an "interested" listener; so this will return |true|, thus
- // simulating a flaky matcher.
- return listener->IsInterested();
- }
- GTEST_LOG_(FATAL) << "This should never be reached";
- return false;
- }
- void DescribeTo(ostream* os) const override { *os << "[DescribeTo]"; }
- void DescribeNegationTo(ostream* os) const override {
- *os << "[DescribeNegationTo]";
- }
- };
- AssertionResult RunPredicateFormatter(Behavior behavior) {
- auto matcher = MakeMatcher(new MockMatcher);
- PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
- matcher);
- return predicate_formatter("dummy-name", behavior);
- }
- };
- TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
- AssertionResult result = RunPredicateFormatter(kInitialSuccess);
- EXPECT_TRUE(result); // Implicit cast to bool.
- std::string expect;
- EXPECT_EQ(expect, result.message());
- }
- TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
- AssertionResult result = RunPredicateFormatter(kAlwaysFail);
- EXPECT_FALSE(result); // Implicit cast to bool.
- std::string expect =
- "Value of: dummy-name\nExpected: [DescribeTo]\n"
- " Actual: 1" +
- OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]";
- EXPECT_EQ(expect, result.message());
- }
- TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
- AssertionResult result = RunPredicateFormatter(kFlaky);
- EXPECT_FALSE(result); // Implicit cast to bool.
- std::string expect =
- "Value of: dummy-name\nExpected: [DescribeTo]\n"
- " The matcher failed on the initial attempt; but passed when rerun to "
- "generate the explanation.\n"
- " Actual: 2" +
- OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]";
- EXPECT_EQ(expect, result.message());
- }
- // Tests for ElementsAre().
- TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
- Matcher<const vector<int>&> m = ElementsAre();
- EXPECT_EQ("is empty", Describe(m));
- }
- TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
- Matcher<vector<int>> m = ElementsAre(Gt(5));
- EXPECT_EQ("has 1 element that is > 5", Describe(m));
- }
- TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
- Matcher<list<std::string>> m = ElementsAre(StrEq("one"), "two");
- EXPECT_EQ(
- "has 2 elements where\n"
- "element #0 is equal to \"one\",\n"
- "element #1 is equal to \"two\"",
- Describe(m));
- }
- TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
- Matcher<vector<int>> m = ElementsAre();
- EXPECT_EQ("isn't empty", DescribeNegation(m));
- }
- TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) {
- Matcher<const list<int>&> m = ElementsAre(Gt(5));
- EXPECT_EQ(
- "doesn't have 1 element, or\n"
- "element #0 isn't > 5",
- DescribeNegation(m));
- }
- TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
- Matcher<const list<std::string>&> m = ElementsAre("one", "two");
- EXPECT_EQ(
- "doesn't have 2 elements, or\n"
- "element #0 isn't equal to \"one\", or\n"
- "element #1 isn't equal to \"two\"",
- DescribeNegation(m));
- }
- TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
- Matcher<const list<int>&> m = ElementsAre(1, Ne(2));
- list<int> test_list;
- test_list.push_back(1);
- test_list.push_back(3);
- EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
- }
- TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) {
- Matcher<const vector<int>&> m =
- ElementsAre(GreaterThan(1), 0, GreaterThan(2));
- const int a[] = {10, 0, 100};
- vector<int> test_vector(std::begin(a), std::end(a));
- EXPECT_EQ(
- "whose element #0 matches, which is 9 more than 1,\n"
- "and whose element #2 matches, which is 98 more than 2",
- Explain(m, test_vector));
- }
- TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
- Matcher<const list<int>&> m = ElementsAre(1, 3);
- list<int> test_list;
- // No need to explain when the container is empty.
- EXPECT_EQ("", Explain(m, test_list));
- test_list.push_back(1);
- EXPECT_EQ("which has 1 element", Explain(m, test_list));
- }
- TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {
- Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
- vector<int> v;
- v.push_back(2);
- v.push_back(1);
- EXPECT_EQ(Explain(m, v), "whose element #0 (2) isn't equal to 1");
- v[0] = 1;
- EXPECT_EQ(Explain(m, v),
- "whose element #1 (1) is <= 5, which is 4 less than 5");
- }
- TEST(ElementsAreTest, MatchesOneElementVector) {
- vector<std::string> test_vector;
- test_vector.push_back("test string");
- EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
- }
- TEST(ElementsAreTest, MatchesOneElementList) {
- list<std::string> test_list;
- test_list.push_back("test string");
- EXPECT_THAT(test_list, ElementsAre("test string"));
- }
- TEST(ElementsAreTest, MatchesThreeElementVector) {
- vector<std::string> test_vector;
- test_vector.push_back("one");
- test_vector.push_back("two");
- test_vector.push_back("three");
- EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
- }
- TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
- vector<int> test_vector;
- test_vector.push_back(4);
- EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
- }
- TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
- vector<int> test_vector;
- test_vector.push_back(4);
- EXPECT_THAT(test_vector, ElementsAre(_));
- }
- TEST(ElementsAreTest, MatchesOneElementValue) {
- vector<int> test_vector;
- test_vector.push_back(4);
- EXPECT_THAT(test_vector, ElementsAre(4));
- }
- TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
- vector<int> test_vector;
- test_vector.push_back(1);
- test_vector.push_back(2);
- test_vector.push_back(3);
- EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
- }
- TEST(ElementsAreTest, MatchesTenElementVector) {
- const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- vector<int> test_vector(std::begin(a), std::end(a));
- EXPECT_THAT(test_vector,
- // The element list can contain values and/or matchers
- // of different types.
- ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
- }
- TEST(ElementsAreTest, DoesNotMatchWrongSize) {
- vector<std::string> test_vector;
- test_vector.push_back("test string");
- test_vector.push_back("test string");
- Matcher<vector<std::string>> m = ElementsAre(StrEq("test string"));
- EXPECT_FALSE(m.Matches(test_vector));
- }
- TEST(ElementsAreTest, DoesNotMatchWrongValue) {
- vector<std::string> test_vector;
- test_vector.push_back("other string");
- Matcher<vector<std::string>> m = ElementsAre(StrEq("test string"));
- EXPECT_FALSE(m.Matches(test_vector));
- }
- TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
- vector<std::string> test_vector;
- test_vector.push_back("one");
- test_vector.push_back("three");
- test_vector.push_back("two");
- Matcher<vector<std::string>> m =
- ElementsAre(StrEq("one"), StrEq("two"), StrEq("three"));
- EXPECT_FALSE(m.Matches(test_vector));
- }
- TEST(ElementsAreTest, WorksForNestedContainer) {
- constexpr std::array<const char*, 2> strings = {{"Hi", "world"}};
- vector<list<char>> nested;
- for (const auto& s : strings) {
- nested.emplace_back(s, s + strlen(s));
- }
- EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
- ElementsAre('w', 'o', _, _, 'd')));
- EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
- ElementsAre('w', 'o', _, _, 'd'))));
- }
- TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
- int a[] = {0, 1, 2};
- vector<int> v(std::begin(a), std::end(a));
- EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
- EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
- }
- TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
- int a[] = {0, 1, 2};
- vector<int> v(std::begin(a), std::end(a));
- EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
- EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
- }
- TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
- int array[] = {0, 1, 2};
- EXPECT_THAT(array, ElementsAre(0, 1, _));
- EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
- EXPECT_THAT(array, Not(ElementsAre(0, _)));
- }
- class NativeArrayPassedAsPointerAndSize {
- public:
- NativeArrayPassedAsPointerAndSize() = default;
- MOCK_METHOD(void, Helper, (int* array, int size));
- private:
- NativeArrayPassedAsPointerAndSize(const NativeArrayPassedAsPointerAndSize&) =
- delete;
- NativeArrayPassedAsPointerAndSize& operator=(
- const NativeArrayPassedAsPointerAndSize&) = delete;
- };
- TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
- int array[] = {0, 1};
- ::std::tuple<int*, size_t> array_as_tuple(array, 2);
- EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
- EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
- NativeArrayPassedAsPointerAndSize helper;
- EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1));
- helper.Helper(array, 2);
- }
- TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
- const char a2[][3] = {"hi", "lo"};
- EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
- ElementsAre('l', 'o', '\0')));
- EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
- EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
- ElementsAre('l', 'o', '\0')));
- }
- TEST(ElementsAreTest, AcceptsStringLiteral) {
- std::string array[] = {"hi", "one", "two"};
- EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
- EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
- }
- // Declared here with the size unknown. Defined AFTER the following test.
- extern const char kHi[];
- TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
- // The size of kHi is not known in this test, but ElementsAre() should
- // still accept it.
- std::string array1[] = {"hi"};
- EXPECT_THAT(array1, ElementsAre(kHi));
- std::string array2[] = {"ho"};
- EXPECT_THAT(array2, Not(ElementsAre(kHi)));
- }
- const char kHi[] = "hi";
- TEST(ElementsAreTest, MakesCopyOfArguments) {
- int x = 1;
- int y = 2;
- // This should make a copy of x and y.
- ::testing::internal::ElementsAreMatcher<std::tuple<int, int>>
- polymorphic_matcher = ElementsAre(x, y);
- // Changing x and y now shouldn't affect the meaning of the above matcher.
- x = y = 0;
- const int array1[] = {1, 2};
- EXPECT_THAT(array1, polymorphic_matcher);
- const int array2[] = {0, 0};
- EXPECT_THAT(array2, Not(polymorphic_matcher));
- }
- // Tests for ElementsAreArray(). Since ElementsAreArray() shares most
- // of the implementation with ElementsAre(), we don't test it as
- // thoroughly here.
- TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
- const int a[] = {1, 2, 3};
- vector<int> test_vector(std::begin(a), std::end(a));
- EXPECT_THAT(test_vector, ElementsAreArray(a));
- test_vector[2] = 0;
- EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
- }
- TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
- std::array<const char*, 3> a = {{"one", "two", "three"}};
- vector<std::string> test_vector(std::begin(a), std::end(a));
- EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size()));
- const char** p = a.data();
- test_vector[0] = "1";
- EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size())));
- }
- TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
- const char* a[] = {"one", "two", "three"};
- vector<std::string> test_vector(std::begin(a), std::end(a));
- EXPECT_THAT(test_vector, ElementsAreArray(a));
- test_vector[0] = "1";
- EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
- }
- TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
- const Matcher<std::string> kMatcherArray[] = {StrEq("one"), StrEq("two"),
- StrEq("three")};
- vector<std::string> test_vector;
- test_vector.push_back("one");
- test_vector.push_back("two");
- test_vector.push_back("three");
- EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
- test_vector.push_back("three");
- EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
- }
- TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
- const int a[] = {1, 2, 3};
- vector<int> test_vector(std::begin(a), std::end(a));
- const vector<int> expected(std::begin(a), std::end(a));
- EXPECT_THAT(test_vector, ElementsAreArray(expected));
- test_vector.push_back(4);
- EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
- }
- TEST(ElementsAreArrayTest, TakesInitializerList) {
- const int a[5] = {1, 2, 3, 4, 5};
- EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5}));
- EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4})));
- EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6})));
- }
- TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
- const std::string a[5] = {"a", "b", "c", "d", "e"};
- EXPECT_THAT(a, ElementsAreArray({"a", "b", "c", "d", "e"}));
- EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "e", "d"})));
- EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "d", "ef"})));
- }
- TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
- const int a[5] = {1, 2, 3, 4, 5};
- EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
- EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
- }
- TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
- const int a[5] = {1, 2, 3, 4, 5};
- // The compiler cannot infer the type of the initializer list if its
- // elements have different types. We must explicitly specify the
- // unified element type in this case.
- EXPECT_THAT(
- a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
- EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>(
- {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
- }
- TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
- const int a[] = {1, 2, 3};
- const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};
- vector<int> test_vector(std::begin(a), std::end(a));
- const vector<Matcher<int>> expected(std::begin(kMatchers),
- std::end(kMatchers));
- EXPECT_THAT(test_vector, ElementsAreArray(expected));
- test_vector.push_back(4);
- EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
- }
- TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
- const int a[] = {1, 2, 3};
- const vector<int> test_vector(std::begin(a), std::end(a));
- const vector<int> expected(std::begin(a), std::end(a));
- EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
- // Pointers are iterators, too.
- EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a)));
- // The empty range of NULL pointers should also be okay.
- int* const null_int = nullptr;
- EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
- EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
- }
- // Since ElementsAre() and ElementsAreArray() share much of the
- // implementation, we only do a test for native arrays here.
- TEST(ElementsAreArrayTest, WorksWithNativeArray) {
- ::std::string a[] = {"hi", "ho"};
- ::std::string b[] = {"hi", "ho"};
- EXPECT_THAT(a, ElementsAreArray(b));
- EXPECT_THAT(a, ElementsAreArray(b, 2));
- EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
- }
- TEST(ElementsAreArrayTest, SourceLifeSpan) {
- const int a[] = {1, 2, 3};
- vector<int> test_vector(std::begin(a), std::end(a));
- vector<int> expect(std::begin(a), std::end(a));
- ElementsAreArrayMatcher<int> matcher_maker =
- ElementsAreArray(expect.begin(), expect.end());
- EXPECT_THAT(test_vector, matcher_maker);
- // Changing in place the values that initialized matcher_maker should not
- // affect matcher_maker anymore. It should have made its own copy of them.
- for (int& i : expect) {
- i += 10;
- }
- EXPECT_THAT(test_vector, matcher_maker);
- test_vector.push_back(3);
- EXPECT_THAT(test_vector, Not(matcher_maker));
- }
- // Tests Contains().
- INSTANTIATE_GTEST_MATCHER_TEST_P(ContainsTest);
- TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
- list<int> some_list;
- some_list.push_back(3);
- some_list.push_back(1);
- some_list.push_back(2);
- some_list.push_back(3);
- EXPECT_THAT(some_list, Contains(1));
- EXPECT_THAT(some_list, Contains(Gt(2.5)));
- EXPECT_THAT(some_list, Contains(Eq(2.0f)));
- list<std::string> another_list;
- another_list.push_back("fee");
- another_list.push_back("fie");
- another_list.push_back("foe");
- another_list.push_back("fum");
- EXPECT_THAT(another_list, Contains(std::string("fee")));
- }
- TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
- list<int> some_list;
- some_list.push_back(3);
- some_list.push_back(1);
- EXPECT_THAT(some_list, Not(Contains(4)));
- }
- TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
- set<int> some_set;
- some_set.insert(3);
- some_set.insert(1);
- some_set.insert(2);
- EXPECT_THAT(some_set, Contains(Eq(1.0)));
- EXPECT_THAT(some_set, Contains(Eq(3.0f)));
- EXPECT_THAT(some_set, Contains(2));
- set<std::string> another_set;
- another_set.insert("fee");
- another_set.insert("fie");
- another_set.insert("foe");
- another_set.insert("fum");
- EXPECT_THAT(another_set, Contains(Eq(std::string("fum"))));
- }
- TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
- set<int> some_set;
- some_set.insert(3);
- some_set.insert(1);
- EXPECT_THAT(some_set, Not(Contains(4)));
- set<std::string> c_string_set;
- c_string_set.insert("hello");
- EXPECT_THAT(c_string_set, Not(Contains(std::string("goodbye"))));
- }
- TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {
- const int a[2] = {1, 2};
- Matcher<const int (&)[2]> m = Contains(2);
- EXPECT_EQ("whose element #1 matches", Explain(m, a));
- m = Contains(3);
- EXPECT_EQ("", Explain(m, a));
- m = Contains(GreaterThan(0));
- EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
- m = Contains(GreaterThan(10));
- EXPECT_EQ("", Explain(m, a));
- }
- TEST(ContainsTest, DescribesItselfCorrectly) {
- Matcher<vector<int>> m = Contains(1);
- EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
- Matcher<vector<int>> m2 = Not(m);
- EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
- }
- TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
- map<std::string, int> my_map;
- const char* bar = "a string";
- my_map[bar] = 2;
- EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
- map<std::string, int> another_map;
- another_map["fee"] = 1;
- another_map["fie"] = 2;
- another_map["foe"] = 3;
- another_map["fum"] = 4;
- EXPECT_THAT(another_map,
- Contains(pair<const std::string, int>(std::string("fee"), 1)));
- EXPECT_THAT(another_map, Contains(pair<const std::string, int>("fie", 2)));
- }
- TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
- map<int, int> some_map;
- some_map[1] = 11;
- some_map[2] = 22;
- EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
- }
- TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
- const char* string_array[] = {"fee", "fie", "foe", "fum"};
- EXPECT_THAT(string_array, Contains(Eq(std::string("fum"))));
- }
- TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
- int int_array[] = {1, 2, 3, 4};
- EXPECT_THAT(int_array, Not(Contains(5)));
- }
- TEST(ContainsTest, AcceptsMatcher) {
- const int a[] = {1, 2, 3};
- EXPECT_THAT(a, Contains(Gt(2)));
- EXPECT_THAT(a, Not(Contains(Gt(4))));
- }
- TEST(ContainsTest, WorksForNativeArrayAsTuple) {
- const int a[] = {1, 2};
- const int* const pointer = a;
- EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
- EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
- }
- TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
- int a[][3] = {{1, 2, 3}, {4, 5, 6}};
- EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
- EXPECT_THAT(a, Contains(Contains(5)));
- EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
- EXPECT_THAT(a, Contains(Not(Contains(5))));
- }
- } // namespace
- } // namespace gmock_matchers_test
- } // namespace testing
- GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100
|