1*28f6c2f2SEnji Cooper // Copyright 2007, Google Inc.
2*28f6c2f2SEnji Cooper // All rights reserved.
3*28f6c2f2SEnji Cooper //
4*28f6c2f2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5*28f6c2f2SEnji Cooper // modification, are permitted provided that the following conditions are
6*28f6c2f2SEnji Cooper // met:
7*28f6c2f2SEnji Cooper //
8*28f6c2f2SEnji Cooper // * Redistributions of source code must retain the above copyright
9*28f6c2f2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10*28f6c2f2SEnji Cooper // * Redistributions in binary form must reproduce the above
11*28f6c2f2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12*28f6c2f2SEnji Cooper // in the documentation and/or other materials provided with the
13*28f6c2f2SEnji Cooper // distribution.
14*28f6c2f2SEnji Cooper // * Neither the name of Google Inc. nor the names of its
15*28f6c2f2SEnji Cooper // contributors may be used to endorse or promote products derived from
16*28f6c2f2SEnji Cooper // this software without specific prior written permission.
17*28f6c2f2SEnji Cooper //
18*28f6c2f2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*28f6c2f2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*28f6c2f2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*28f6c2f2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*28f6c2f2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*28f6c2f2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*28f6c2f2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*28f6c2f2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*28f6c2f2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*28f6c2f2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*28f6c2f2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*28f6c2f2SEnji Cooper
30*28f6c2f2SEnji Cooper // Google Mock - a framework for writing C++ mock classes.
31*28f6c2f2SEnji Cooper //
32*28f6c2f2SEnji Cooper // This file tests some commonly used argument matchers.
33*28f6c2f2SEnji Cooper
34*28f6c2f2SEnji Cooper #include <array>
35*28f6c2f2SEnji Cooper #include <memory>
36*28f6c2f2SEnji Cooper #include <ostream>
37*28f6c2f2SEnji Cooper #include <string>
38*28f6c2f2SEnji Cooper #include <tuple>
39*28f6c2f2SEnji Cooper #include <utility>
40*28f6c2f2SEnji Cooper #include <vector>
41*28f6c2f2SEnji Cooper
42*28f6c2f2SEnji Cooper #include "gtest/gtest.h"
43*28f6c2f2SEnji Cooper
44*28f6c2f2SEnji Cooper // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
45*28f6c2f2SEnji Cooper // possible loss of data and C4100, unreferenced local parameter
46*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
47*28f6c2f2SEnji Cooper
48*28f6c2f2SEnji Cooper #include "test/gmock-matchers_test.h"
49*28f6c2f2SEnji Cooper
50*28f6c2f2SEnji Cooper namespace testing {
51*28f6c2f2SEnji Cooper namespace gmock_matchers_test {
52*28f6c2f2SEnji Cooper namespace {
53*28f6c2f2SEnji Cooper
TEST(AddressTest,NonConst)54*28f6c2f2SEnji Cooper TEST(AddressTest, NonConst) {
55*28f6c2f2SEnji Cooper int n = 1;
56*28f6c2f2SEnji Cooper const Matcher<int> m = Address(Eq(&n));
57*28f6c2f2SEnji Cooper
58*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n));
59*28f6c2f2SEnji Cooper
60*28f6c2f2SEnji Cooper int other = 5;
61*28f6c2f2SEnji Cooper
62*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(other));
63*28f6c2f2SEnji Cooper
64*28f6c2f2SEnji Cooper int& n_ref = n;
65*28f6c2f2SEnji Cooper
66*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n_ref));
67*28f6c2f2SEnji Cooper }
68*28f6c2f2SEnji Cooper
TEST(AddressTest,Const)69*28f6c2f2SEnji Cooper TEST(AddressTest, Const) {
70*28f6c2f2SEnji Cooper const int n = 1;
71*28f6c2f2SEnji Cooper const Matcher<int> m = Address(Eq(&n));
72*28f6c2f2SEnji Cooper
73*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n));
74*28f6c2f2SEnji Cooper
75*28f6c2f2SEnji Cooper int other = 5;
76*28f6c2f2SEnji Cooper
77*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(other));
78*28f6c2f2SEnji Cooper }
79*28f6c2f2SEnji Cooper
TEST(AddressTest,MatcherDoesntCopy)80*28f6c2f2SEnji Cooper TEST(AddressTest, MatcherDoesntCopy) {
81*28f6c2f2SEnji Cooper std::unique_ptr<int> n(new int(1));
82*28f6c2f2SEnji Cooper const Matcher<std::unique_ptr<int>> m = Address(Eq(&n));
83*28f6c2f2SEnji Cooper
84*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n));
85*28f6c2f2SEnji Cooper }
86*28f6c2f2SEnji Cooper
TEST(AddressTest,Describe)87*28f6c2f2SEnji Cooper TEST(AddressTest, Describe) {
88*28f6c2f2SEnji Cooper Matcher<int> matcher = Address(_);
89*28f6c2f2SEnji Cooper EXPECT_EQ("has address that is anything", Describe(matcher));
90*28f6c2f2SEnji Cooper EXPECT_EQ("does not have address that is anything",
91*28f6c2f2SEnji Cooper DescribeNegation(matcher));
92*28f6c2f2SEnji Cooper }
93*28f6c2f2SEnji Cooper
94*28f6c2f2SEnji Cooper // The following two tests verify that values without a public copy
95*28f6c2f2SEnji Cooper // ctor can be used as arguments to matchers like Eq(), Ge(), and etc
96*28f6c2f2SEnji Cooper // with the help of ByRef().
97*28f6c2f2SEnji Cooper
98*28f6c2f2SEnji Cooper class NotCopyable {
99*28f6c2f2SEnji Cooper public:
NotCopyable(int a_value)100*28f6c2f2SEnji Cooper explicit NotCopyable(int a_value) : value_(a_value) {}
101*28f6c2f2SEnji Cooper
value() const102*28f6c2f2SEnji Cooper int value() const { return value_; }
103*28f6c2f2SEnji Cooper
operator ==(const NotCopyable & rhs) const104*28f6c2f2SEnji Cooper bool operator==(const NotCopyable& rhs) const {
105*28f6c2f2SEnji Cooper return value() == rhs.value();
106*28f6c2f2SEnji Cooper }
107*28f6c2f2SEnji Cooper
operator >=(const NotCopyable & rhs) const108*28f6c2f2SEnji Cooper bool operator>=(const NotCopyable& rhs) const {
109*28f6c2f2SEnji Cooper return value() >= rhs.value();
110*28f6c2f2SEnji Cooper }
111*28f6c2f2SEnji Cooper
112*28f6c2f2SEnji Cooper private:
113*28f6c2f2SEnji Cooper int value_;
114*28f6c2f2SEnji Cooper
115*28f6c2f2SEnji Cooper NotCopyable(const NotCopyable&) = delete;
116*28f6c2f2SEnji Cooper NotCopyable& operator=(const NotCopyable&) = delete;
117*28f6c2f2SEnji Cooper };
118*28f6c2f2SEnji Cooper
TEST(ByRefTest,AllowsNotCopyableConstValueInMatchers)119*28f6c2f2SEnji Cooper TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
120*28f6c2f2SEnji Cooper const NotCopyable const_value1(1);
121*28f6c2f2SEnji Cooper const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
122*28f6c2f2SEnji Cooper
123*28f6c2f2SEnji Cooper const NotCopyable n1(1), n2(2);
124*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n1));
125*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(n2));
126*28f6c2f2SEnji Cooper }
127*28f6c2f2SEnji Cooper
TEST(ByRefTest,AllowsNotCopyableValueInMatchers)128*28f6c2f2SEnji Cooper TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
129*28f6c2f2SEnji Cooper NotCopyable value2(2);
130*28f6c2f2SEnji Cooper const Matcher<NotCopyable&> m = Ge(ByRef(value2));
131*28f6c2f2SEnji Cooper
132*28f6c2f2SEnji Cooper NotCopyable n1(1), n2(2);
133*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(n1));
134*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n2));
135*28f6c2f2SEnji Cooper }
136*28f6c2f2SEnji Cooper
TEST(IsEmptyTest,ImplementsIsEmpty)137*28f6c2f2SEnji Cooper TEST(IsEmptyTest, ImplementsIsEmpty) {
138*28f6c2f2SEnji Cooper vector<int> container;
139*28f6c2f2SEnji Cooper EXPECT_THAT(container, IsEmpty());
140*28f6c2f2SEnji Cooper container.push_back(0);
141*28f6c2f2SEnji Cooper EXPECT_THAT(container, Not(IsEmpty()));
142*28f6c2f2SEnji Cooper container.push_back(1);
143*28f6c2f2SEnji Cooper EXPECT_THAT(container, Not(IsEmpty()));
144*28f6c2f2SEnji Cooper }
145*28f6c2f2SEnji Cooper
TEST(IsEmptyTest,WorksWithString)146*28f6c2f2SEnji Cooper TEST(IsEmptyTest, WorksWithString) {
147*28f6c2f2SEnji Cooper std::string text;
148*28f6c2f2SEnji Cooper EXPECT_THAT(text, IsEmpty());
149*28f6c2f2SEnji Cooper text = "foo";
150*28f6c2f2SEnji Cooper EXPECT_THAT(text, Not(IsEmpty()));
151*28f6c2f2SEnji Cooper text = std::string("\0", 1);
152*28f6c2f2SEnji Cooper EXPECT_THAT(text, Not(IsEmpty()));
153*28f6c2f2SEnji Cooper }
154*28f6c2f2SEnji Cooper
TEST(IsEmptyTest,CanDescribeSelf)155*28f6c2f2SEnji Cooper TEST(IsEmptyTest, CanDescribeSelf) {
156*28f6c2f2SEnji Cooper Matcher<vector<int>> m = IsEmpty();
157*28f6c2f2SEnji Cooper EXPECT_EQ("is empty", Describe(m));
158*28f6c2f2SEnji Cooper EXPECT_EQ("isn't empty", DescribeNegation(m));
159*28f6c2f2SEnji Cooper }
160*28f6c2f2SEnji Cooper
TEST(IsEmptyTest,ExplainsResult)161*28f6c2f2SEnji Cooper TEST(IsEmptyTest, ExplainsResult) {
162*28f6c2f2SEnji Cooper Matcher<vector<int>> m = IsEmpty();
163*28f6c2f2SEnji Cooper vector<int> container;
164*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, container));
165*28f6c2f2SEnji Cooper container.push_back(0);
166*28f6c2f2SEnji Cooper EXPECT_EQ("whose size is 1", Explain(m, container));
167*28f6c2f2SEnji Cooper }
168*28f6c2f2SEnji Cooper
TEST(IsEmptyTest,WorksWithMoveOnly)169*28f6c2f2SEnji Cooper TEST(IsEmptyTest, WorksWithMoveOnly) {
170*28f6c2f2SEnji Cooper ContainerHelper helper;
171*28f6c2f2SEnji Cooper EXPECT_CALL(helper, Call(IsEmpty()));
172*28f6c2f2SEnji Cooper helper.Call({});
173*28f6c2f2SEnji Cooper }
174*28f6c2f2SEnji Cooper
TEST(IsTrueTest,IsTrueIsFalse)175*28f6c2f2SEnji Cooper TEST(IsTrueTest, IsTrueIsFalse) {
176*28f6c2f2SEnji Cooper EXPECT_THAT(true, IsTrue());
177*28f6c2f2SEnji Cooper EXPECT_THAT(false, IsFalse());
178*28f6c2f2SEnji Cooper EXPECT_THAT(true, Not(IsFalse()));
179*28f6c2f2SEnji Cooper EXPECT_THAT(false, Not(IsTrue()));
180*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(IsTrue()));
181*28f6c2f2SEnji Cooper EXPECT_THAT(0, IsFalse());
182*28f6c2f2SEnji Cooper EXPECT_THAT(nullptr, Not(IsTrue()));
183*28f6c2f2SEnji Cooper EXPECT_THAT(nullptr, IsFalse());
184*28f6c2f2SEnji Cooper EXPECT_THAT(-1, IsTrue());
185*28f6c2f2SEnji Cooper EXPECT_THAT(-1, Not(IsFalse()));
186*28f6c2f2SEnji Cooper EXPECT_THAT(1, IsTrue());
187*28f6c2f2SEnji Cooper EXPECT_THAT(1, Not(IsFalse()));
188*28f6c2f2SEnji Cooper EXPECT_THAT(2, IsTrue());
189*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(IsFalse()));
190*28f6c2f2SEnji Cooper int a = 42;
191*28f6c2f2SEnji Cooper EXPECT_THAT(a, IsTrue());
192*28f6c2f2SEnji Cooper EXPECT_THAT(a, Not(IsFalse()));
193*28f6c2f2SEnji Cooper EXPECT_THAT(&a, IsTrue());
194*28f6c2f2SEnji Cooper EXPECT_THAT(&a, Not(IsFalse()));
195*28f6c2f2SEnji Cooper EXPECT_THAT(false, Not(IsTrue()));
196*28f6c2f2SEnji Cooper EXPECT_THAT(true, Not(IsFalse()));
197*28f6c2f2SEnji Cooper EXPECT_THAT(std::true_type(), IsTrue());
198*28f6c2f2SEnji Cooper EXPECT_THAT(std::true_type(), Not(IsFalse()));
199*28f6c2f2SEnji Cooper EXPECT_THAT(std::false_type(), IsFalse());
200*28f6c2f2SEnji Cooper EXPECT_THAT(std::false_type(), Not(IsTrue()));
201*28f6c2f2SEnji Cooper EXPECT_THAT(nullptr, Not(IsTrue()));
202*28f6c2f2SEnji Cooper EXPECT_THAT(nullptr, IsFalse());
203*28f6c2f2SEnji Cooper std::unique_ptr<int> null_unique;
204*28f6c2f2SEnji Cooper std::unique_ptr<int> nonnull_unique(new int(0));
205*28f6c2f2SEnji Cooper EXPECT_THAT(null_unique, Not(IsTrue()));
206*28f6c2f2SEnji Cooper EXPECT_THAT(null_unique, IsFalse());
207*28f6c2f2SEnji Cooper EXPECT_THAT(nonnull_unique, IsTrue());
208*28f6c2f2SEnji Cooper EXPECT_THAT(nonnull_unique, Not(IsFalse()));
209*28f6c2f2SEnji Cooper }
210*28f6c2f2SEnji Cooper
211*28f6c2f2SEnji Cooper #ifdef GTEST_HAS_TYPED_TEST
212*28f6c2f2SEnji Cooper // Tests ContainerEq with different container types, and
213*28f6c2f2SEnji Cooper // different element types.
214*28f6c2f2SEnji Cooper
215*28f6c2f2SEnji Cooper template <typename T>
216*28f6c2f2SEnji Cooper class ContainerEqTest : public testing::Test {};
217*28f6c2f2SEnji Cooper
218*28f6c2f2SEnji Cooper typedef testing::Types<set<int>, vector<size_t>, multiset<size_t>, list<int>>
219*28f6c2f2SEnji Cooper ContainerEqTestTypes;
220*28f6c2f2SEnji Cooper
221*28f6c2f2SEnji Cooper TYPED_TEST_SUITE(ContainerEqTest, ContainerEqTestTypes);
222*28f6c2f2SEnji Cooper
223*28f6c2f2SEnji Cooper // Tests that the filled container is equal to itself.
TYPED_TEST(ContainerEqTest,EqualsSelf)224*28f6c2f2SEnji Cooper TYPED_TEST(ContainerEqTest, EqualsSelf) {
225*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
226*28f6c2f2SEnji Cooper TypeParam my_set(vals, vals + 6);
227*28f6c2f2SEnji Cooper const Matcher<TypeParam> m = ContainerEq(my_set);
228*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(my_set));
229*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, my_set));
230*28f6c2f2SEnji Cooper }
231*28f6c2f2SEnji Cooper
232*28f6c2f2SEnji Cooper // Tests that missing values are reported.
TYPED_TEST(ContainerEqTest,ValueMissing)233*28f6c2f2SEnji Cooper TYPED_TEST(ContainerEqTest, ValueMissing) {
234*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
235*28f6c2f2SEnji Cooper static const int test_vals[] = {2, 1, 8, 5};
236*28f6c2f2SEnji Cooper TypeParam my_set(vals, vals + 6);
237*28f6c2f2SEnji Cooper TypeParam test_set(test_vals, test_vals + 4);
238*28f6c2f2SEnji Cooper const Matcher<TypeParam> m = ContainerEq(my_set);
239*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_set));
240*28f6c2f2SEnji Cooper EXPECT_EQ("which doesn't have these expected elements: 3",
241*28f6c2f2SEnji Cooper Explain(m, test_set));
242*28f6c2f2SEnji Cooper }
243*28f6c2f2SEnji Cooper
244*28f6c2f2SEnji Cooper // Tests that added values are reported.
TYPED_TEST(ContainerEqTest,ValueAdded)245*28f6c2f2SEnji Cooper TYPED_TEST(ContainerEqTest, ValueAdded) {
246*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
247*28f6c2f2SEnji Cooper static const int test_vals[] = {1, 2, 3, 5, 8, 46};
248*28f6c2f2SEnji Cooper TypeParam my_set(vals, vals + 6);
249*28f6c2f2SEnji Cooper TypeParam test_set(test_vals, test_vals + 6);
250*28f6c2f2SEnji Cooper const Matcher<const TypeParam&> m = ContainerEq(my_set);
251*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_set));
252*28f6c2f2SEnji Cooper EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
253*28f6c2f2SEnji Cooper }
254*28f6c2f2SEnji Cooper
255*28f6c2f2SEnji Cooper // Tests that added and missing values are reported together.
TYPED_TEST(ContainerEqTest,ValueAddedAndRemoved)256*28f6c2f2SEnji Cooper TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
257*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
258*28f6c2f2SEnji Cooper static const int test_vals[] = {1, 2, 3, 8, 46};
259*28f6c2f2SEnji Cooper TypeParam my_set(vals, vals + 6);
260*28f6c2f2SEnji Cooper TypeParam test_set(test_vals, test_vals + 5);
261*28f6c2f2SEnji Cooper const Matcher<TypeParam> m = ContainerEq(my_set);
262*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_set));
263*28f6c2f2SEnji Cooper EXPECT_EQ(
264*28f6c2f2SEnji Cooper "which has these unexpected elements: 46,\n"
265*28f6c2f2SEnji Cooper "and doesn't have these expected elements: 5",
266*28f6c2f2SEnji Cooper Explain(m, test_set));
267*28f6c2f2SEnji Cooper }
268*28f6c2f2SEnji Cooper
269*28f6c2f2SEnji Cooper // Tests duplicated value -- expect no explanation.
TYPED_TEST(ContainerEqTest,DuplicateDifference)270*28f6c2f2SEnji Cooper TYPED_TEST(ContainerEqTest, DuplicateDifference) {
271*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
272*28f6c2f2SEnji Cooper static const int test_vals[] = {1, 2, 3, 5, 8};
273*28f6c2f2SEnji Cooper TypeParam my_set(vals, vals + 6);
274*28f6c2f2SEnji Cooper TypeParam test_set(test_vals, test_vals + 5);
275*28f6c2f2SEnji Cooper const Matcher<const TypeParam&> m = ContainerEq(my_set);
276*28f6c2f2SEnji Cooper // Depending on the container, match may be true or false
277*28f6c2f2SEnji Cooper // But in any case there should be no explanation.
278*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, test_set));
279*28f6c2f2SEnji Cooper }
280*28f6c2f2SEnji Cooper #endif // GTEST_HAS_TYPED_TEST
281*28f6c2f2SEnji Cooper
282*28f6c2f2SEnji Cooper // Tests that multiple missing values are reported.
283*28f6c2f2SEnji Cooper // Using just vector here, so order is predictable.
TEST(ContainerEqExtraTest,MultipleValuesMissing)284*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, MultipleValuesMissing) {
285*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
286*28f6c2f2SEnji Cooper static const int test_vals[] = {2, 1, 5};
287*28f6c2f2SEnji Cooper vector<int> my_set(vals, vals + 6);
288*28f6c2f2SEnji Cooper vector<int> test_set(test_vals, test_vals + 3);
289*28f6c2f2SEnji Cooper const Matcher<vector<int>> m = ContainerEq(my_set);
290*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_set));
291*28f6c2f2SEnji Cooper EXPECT_EQ("which doesn't have these expected elements: 3, 8",
292*28f6c2f2SEnji Cooper Explain(m, test_set));
293*28f6c2f2SEnji Cooper }
294*28f6c2f2SEnji Cooper
295*28f6c2f2SEnji Cooper // Tests that added values are reported.
296*28f6c2f2SEnji Cooper // Using just vector here, so order is predictable.
TEST(ContainerEqExtraTest,MultipleValuesAdded)297*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, MultipleValuesAdded) {
298*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
299*28f6c2f2SEnji Cooper static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
300*28f6c2f2SEnji Cooper list<size_t> my_set(vals, vals + 6);
301*28f6c2f2SEnji Cooper list<size_t> test_set(test_vals, test_vals + 7);
302*28f6c2f2SEnji Cooper const Matcher<const list<size_t>&> m = ContainerEq(my_set);
303*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_set));
304*28f6c2f2SEnji Cooper EXPECT_EQ("which has these unexpected elements: 92, 46",
305*28f6c2f2SEnji Cooper Explain(m, test_set));
306*28f6c2f2SEnji Cooper }
307*28f6c2f2SEnji Cooper
308*28f6c2f2SEnji Cooper // Tests that added and missing values are reported together.
TEST(ContainerEqExtraTest,MultipleValuesAddedAndRemoved)309*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
310*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
311*28f6c2f2SEnji Cooper static const int test_vals[] = {1, 2, 3, 92, 46};
312*28f6c2f2SEnji Cooper list<size_t> my_set(vals, vals + 6);
313*28f6c2f2SEnji Cooper list<size_t> test_set(test_vals, test_vals + 5);
314*28f6c2f2SEnji Cooper const Matcher<const list<size_t>> m = ContainerEq(my_set);
315*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_set));
316*28f6c2f2SEnji Cooper EXPECT_EQ(
317*28f6c2f2SEnji Cooper "which has these unexpected elements: 92, 46,\n"
318*28f6c2f2SEnji Cooper "and doesn't have these expected elements: 5, 8",
319*28f6c2f2SEnji Cooper Explain(m, test_set));
320*28f6c2f2SEnji Cooper }
321*28f6c2f2SEnji Cooper
322*28f6c2f2SEnji Cooper // Tests to see that duplicate elements are detected,
323*28f6c2f2SEnji Cooper // but (as above) not reported in the explanation.
TEST(ContainerEqExtraTest,MultiSetOfIntDuplicateDifference)324*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
325*28f6c2f2SEnji Cooper static const int vals[] = {1, 1, 2, 3, 5, 8};
326*28f6c2f2SEnji Cooper static const int test_vals[] = {1, 2, 3, 5, 8};
327*28f6c2f2SEnji Cooper vector<int> my_set(vals, vals + 6);
328*28f6c2f2SEnji Cooper vector<int> test_set(test_vals, test_vals + 5);
329*28f6c2f2SEnji Cooper const Matcher<vector<int>> m = ContainerEq(my_set);
330*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(my_set));
331*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_set));
332*28f6c2f2SEnji Cooper // There is nothing to report when both sets contain all the same values.
333*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, test_set));
334*28f6c2f2SEnji Cooper }
335*28f6c2f2SEnji Cooper
336*28f6c2f2SEnji Cooper // Tests that ContainerEq works for non-trivial associative containers,
337*28f6c2f2SEnji Cooper // like maps.
TEST(ContainerEqExtraTest,WorksForMaps)338*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, WorksForMaps) {
339*28f6c2f2SEnji Cooper map<int, std::string> my_map;
340*28f6c2f2SEnji Cooper my_map[0] = "a";
341*28f6c2f2SEnji Cooper my_map[1] = "b";
342*28f6c2f2SEnji Cooper
343*28f6c2f2SEnji Cooper map<int, std::string> test_map;
344*28f6c2f2SEnji Cooper test_map[0] = "aa";
345*28f6c2f2SEnji Cooper test_map[1] = "b";
346*28f6c2f2SEnji Cooper
347*28f6c2f2SEnji Cooper const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
348*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(my_map));
349*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(test_map));
350*28f6c2f2SEnji Cooper
351*28f6c2f2SEnji Cooper EXPECT_EQ(
352*28f6c2f2SEnji Cooper "which has these unexpected elements: (0, \"aa\"),\n"
353*28f6c2f2SEnji Cooper "and doesn't have these expected elements: (0, \"a\")",
354*28f6c2f2SEnji Cooper Explain(m, test_map));
355*28f6c2f2SEnji Cooper }
356*28f6c2f2SEnji Cooper
TEST(ContainerEqExtraTest,WorksForNativeArray)357*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, WorksForNativeArray) {
358*28f6c2f2SEnji Cooper int a1[] = {1, 2, 3};
359*28f6c2f2SEnji Cooper int a2[] = {1, 2, 3};
360*28f6c2f2SEnji Cooper int b[] = {1, 2, 4};
361*28f6c2f2SEnji Cooper
362*28f6c2f2SEnji Cooper EXPECT_THAT(a1, ContainerEq(a2));
363*28f6c2f2SEnji Cooper EXPECT_THAT(a1, Not(ContainerEq(b)));
364*28f6c2f2SEnji Cooper }
365*28f6c2f2SEnji Cooper
TEST(ContainerEqExtraTest,WorksForTwoDimensionalNativeArray)366*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
367*28f6c2f2SEnji Cooper const char a1[][3] = {"hi", "lo"};
368*28f6c2f2SEnji Cooper const char a2[][3] = {"hi", "lo"};
369*28f6c2f2SEnji Cooper const char b[][3] = {"lo", "hi"};
370*28f6c2f2SEnji Cooper
371*28f6c2f2SEnji Cooper // Tests using ContainerEq() in the first dimension.
372*28f6c2f2SEnji Cooper EXPECT_THAT(a1, ContainerEq(a2));
373*28f6c2f2SEnji Cooper EXPECT_THAT(a1, Not(ContainerEq(b)));
374*28f6c2f2SEnji Cooper
375*28f6c2f2SEnji Cooper // Tests using ContainerEq() in the second dimension.
376*28f6c2f2SEnji Cooper EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
377*28f6c2f2SEnji Cooper EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
378*28f6c2f2SEnji Cooper }
379*28f6c2f2SEnji Cooper
TEST(ContainerEqExtraTest,WorksForNativeArrayAsTuple)380*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
381*28f6c2f2SEnji Cooper const int a1[] = {1, 2, 3};
382*28f6c2f2SEnji Cooper const int a2[] = {1, 2, 3};
383*28f6c2f2SEnji Cooper const int b[] = {1, 2, 3, 4};
384*28f6c2f2SEnji Cooper
385*28f6c2f2SEnji Cooper const int* const p1 = a1;
386*28f6c2f2SEnji Cooper EXPECT_THAT(std::make_tuple(p1, 3), ContainerEq(a2));
387*28f6c2f2SEnji Cooper EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(b)));
388*28f6c2f2SEnji Cooper
389*28f6c2f2SEnji Cooper const int c[] = {1, 3, 2};
390*28f6c2f2SEnji Cooper EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(c)));
391*28f6c2f2SEnji Cooper }
392*28f6c2f2SEnji Cooper
TEST(ContainerEqExtraTest,CopiesNativeArrayParameter)393*28f6c2f2SEnji Cooper TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
394*28f6c2f2SEnji Cooper std::string a1[][3] = {{"hi", "hello", "ciao"}, {"bye", "see you", "ciao"}};
395*28f6c2f2SEnji Cooper
396*28f6c2f2SEnji Cooper std::string a2[][3] = {{"hi", "hello", "ciao"}, {"bye", "see you", "ciao"}};
397*28f6c2f2SEnji Cooper
398*28f6c2f2SEnji Cooper const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
399*28f6c2f2SEnji Cooper EXPECT_THAT(a1, m);
400*28f6c2f2SEnji Cooper
401*28f6c2f2SEnji Cooper a2[0][0] = "ha";
402*28f6c2f2SEnji Cooper EXPECT_THAT(a1, m);
403*28f6c2f2SEnji Cooper }
404*28f6c2f2SEnji Cooper
405*28f6c2f2SEnji Cooper namespace {
406*28f6c2f2SEnji Cooper
407*28f6c2f2SEnji Cooper // Used as a check on the more complex max flow method used in the
408*28f6c2f2SEnji Cooper // real testing::internal::FindMaxBipartiteMatching. This method is
409*28f6c2f2SEnji Cooper // compatible but runs in worst-case factorial time, so we only
410*28f6c2f2SEnji Cooper // use it in testing for small problem sizes.
411*28f6c2f2SEnji Cooper template <typename Graph>
412*28f6c2f2SEnji Cooper class BacktrackingMaxBPMState {
413*28f6c2f2SEnji Cooper public:
414*28f6c2f2SEnji Cooper // Does not take ownership of 'g'.
BacktrackingMaxBPMState(const Graph * g)415*28f6c2f2SEnji Cooper explicit BacktrackingMaxBPMState(const Graph* g) : graph_(g) {}
416*28f6c2f2SEnji Cooper
Compute()417*28f6c2f2SEnji Cooper ElementMatcherPairs Compute() {
418*28f6c2f2SEnji Cooper if (graph_->LhsSize() == 0 || graph_->RhsSize() == 0) {
419*28f6c2f2SEnji Cooper return best_so_far_;
420*28f6c2f2SEnji Cooper }
421*28f6c2f2SEnji Cooper lhs_used_.assign(graph_->LhsSize(), kUnused);
422*28f6c2f2SEnji Cooper rhs_used_.assign(graph_->RhsSize(), kUnused);
423*28f6c2f2SEnji Cooper for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) {
424*28f6c2f2SEnji Cooper matches_.clear();
425*28f6c2f2SEnji Cooper RecurseInto(irhs);
426*28f6c2f2SEnji Cooper if (best_so_far_.size() == graph_->RhsSize()) break;
427*28f6c2f2SEnji Cooper }
428*28f6c2f2SEnji Cooper return best_so_far_;
429*28f6c2f2SEnji Cooper }
430*28f6c2f2SEnji Cooper
431*28f6c2f2SEnji Cooper private:
432*28f6c2f2SEnji Cooper static const size_t kUnused = static_cast<size_t>(-1);
433*28f6c2f2SEnji Cooper
PushMatch(size_t lhs,size_t rhs)434*28f6c2f2SEnji Cooper void PushMatch(size_t lhs, size_t rhs) {
435*28f6c2f2SEnji Cooper matches_.push_back(ElementMatcherPair(lhs, rhs));
436*28f6c2f2SEnji Cooper lhs_used_[lhs] = rhs;
437*28f6c2f2SEnji Cooper rhs_used_[rhs] = lhs;
438*28f6c2f2SEnji Cooper if (matches_.size() > best_so_far_.size()) {
439*28f6c2f2SEnji Cooper best_so_far_ = matches_;
440*28f6c2f2SEnji Cooper }
441*28f6c2f2SEnji Cooper }
442*28f6c2f2SEnji Cooper
PopMatch()443*28f6c2f2SEnji Cooper void PopMatch() {
444*28f6c2f2SEnji Cooper const ElementMatcherPair& back = matches_.back();
445*28f6c2f2SEnji Cooper lhs_used_[back.first] = kUnused;
446*28f6c2f2SEnji Cooper rhs_used_[back.second] = kUnused;
447*28f6c2f2SEnji Cooper matches_.pop_back();
448*28f6c2f2SEnji Cooper }
449*28f6c2f2SEnji Cooper
RecurseInto(size_t irhs)450*28f6c2f2SEnji Cooper bool RecurseInto(size_t irhs) {
451*28f6c2f2SEnji Cooper if (rhs_used_[irhs] != kUnused) {
452*28f6c2f2SEnji Cooper return true;
453*28f6c2f2SEnji Cooper }
454*28f6c2f2SEnji Cooper for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) {
455*28f6c2f2SEnji Cooper if (lhs_used_[ilhs] != kUnused) {
456*28f6c2f2SEnji Cooper continue;
457*28f6c2f2SEnji Cooper }
458*28f6c2f2SEnji Cooper if (!graph_->HasEdge(ilhs, irhs)) {
459*28f6c2f2SEnji Cooper continue;
460*28f6c2f2SEnji Cooper }
461*28f6c2f2SEnji Cooper PushMatch(ilhs, irhs);
462*28f6c2f2SEnji Cooper if (best_so_far_.size() == graph_->RhsSize()) {
463*28f6c2f2SEnji Cooper return false;
464*28f6c2f2SEnji Cooper }
465*28f6c2f2SEnji Cooper for (size_t mi = irhs + 1; mi < graph_->RhsSize(); ++mi) {
466*28f6c2f2SEnji Cooper if (!RecurseInto(mi)) return false;
467*28f6c2f2SEnji Cooper }
468*28f6c2f2SEnji Cooper PopMatch();
469*28f6c2f2SEnji Cooper }
470*28f6c2f2SEnji Cooper return true;
471*28f6c2f2SEnji Cooper }
472*28f6c2f2SEnji Cooper
473*28f6c2f2SEnji Cooper const Graph* graph_; // not owned
474*28f6c2f2SEnji Cooper std::vector<size_t> lhs_used_;
475*28f6c2f2SEnji Cooper std::vector<size_t> rhs_used_;
476*28f6c2f2SEnji Cooper ElementMatcherPairs matches_;
477*28f6c2f2SEnji Cooper ElementMatcherPairs best_so_far_;
478*28f6c2f2SEnji Cooper };
479*28f6c2f2SEnji Cooper
480*28f6c2f2SEnji Cooper template <typename Graph>
481*28f6c2f2SEnji Cooper const size_t BacktrackingMaxBPMState<Graph>::kUnused;
482*28f6c2f2SEnji Cooper
483*28f6c2f2SEnji Cooper } // namespace
484*28f6c2f2SEnji Cooper
485*28f6c2f2SEnji Cooper // Implement a simple backtracking algorithm to determine if it is possible
486*28f6c2f2SEnji Cooper // to find one element per matcher, without reusing elements.
487*28f6c2f2SEnji Cooper template <typename Graph>
FindBacktrackingMaxBPM(const Graph & g)488*28f6c2f2SEnji Cooper ElementMatcherPairs FindBacktrackingMaxBPM(const Graph& g) {
489*28f6c2f2SEnji Cooper return BacktrackingMaxBPMState<Graph>(&g).Compute();
490*28f6c2f2SEnji Cooper }
491*28f6c2f2SEnji Cooper
492*28f6c2f2SEnji Cooper class BacktrackingBPMTest : public ::testing::Test {};
493*28f6c2f2SEnji Cooper
494*28f6c2f2SEnji Cooper // Tests the MaxBipartiteMatching algorithm with square matrices.
495*28f6c2f2SEnji Cooper // The single int param is the # of nodes on each of the left and right sides.
496*28f6c2f2SEnji Cooper class BipartiteTest : public ::testing::TestWithParam<size_t> {};
497*28f6c2f2SEnji Cooper
498*28f6c2f2SEnji Cooper // Verify all match graphs up to some moderate number of edges.
TEST_P(BipartiteTest,Exhaustive)499*28f6c2f2SEnji Cooper TEST_P(BipartiteTest, Exhaustive) {
500*28f6c2f2SEnji Cooper size_t nodes = GetParam();
501*28f6c2f2SEnji Cooper MatchMatrix graph(nodes, nodes);
502*28f6c2f2SEnji Cooper do {
503*28f6c2f2SEnji Cooper ElementMatcherPairs matches = internal::FindMaxBipartiteMatching(graph);
504*28f6c2f2SEnji Cooper EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), matches.size())
505*28f6c2f2SEnji Cooper << "graph: " << graph.DebugString();
506*28f6c2f2SEnji Cooper // Check that all elements of matches are in the graph.
507*28f6c2f2SEnji Cooper // Check that elements of first and second are unique.
508*28f6c2f2SEnji Cooper std::vector<bool> seen_element(graph.LhsSize());
509*28f6c2f2SEnji Cooper std::vector<bool> seen_matcher(graph.RhsSize());
510*28f6c2f2SEnji Cooper SCOPED_TRACE(PrintToString(matches));
511*28f6c2f2SEnji Cooper for (size_t i = 0; i < matches.size(); ++i) {
512*28f6c2f2SEnji Cooper size_t ilhs = matches[i].first;
513*28f6c2f2SEnji Cooper size_t irhs = matches[i].second;
514*28f6c2f2SEnji Cooper EXPECT_TRUE(graph.HasEdge(ilhs, irhs));
515*28f6c2f2SEnji Cooper EXPECT_FALSE(seen_element[ilhs]);
516*28f6c2f2SEnji Cooper EXPECT_FALSE(seen_matcher[irhs]);
517*28f6c2f2SEnji Cooper seen_element[ilhs] = true;
518*28f6c2f2SEnji Cooper seen_matcher[irhs] = true;
519*28f6c2f2SEnji Cooper }
520*28f6c2f2SEnji Cooper } while (graph.NextGraph());
521*28f6c2f2SEnji Cooper }
522*28f6c2f2SEnji Cooper
523*28f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(AllGraphs, BipartiteTest,
524*28f6c2f2SEnji Cooper ::testing::Range(size_t{0}, size_t{5}));
525*28f6c2f2SEnji Cooper
526*28f6c2f2SEnji Cooper // Parameterized by a pair interpreted as (LhsSize, RhsSize).
527*28f6c2f2SEnji Cooper class BipartiteNonSquareTest
528*28f6c2f2SEnji Cooper : public ::testing::TestWithParam<std::pair<size_t, size_t>> {};
529*28f6c2f2SEnji Cooper
TEST_F(BipartiteNonSquareTest,SimpleBacktracking)530*28f6c2f2SEnji Cooper TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
531*28f6c2f2SEnji Cooper // .......
532*28f6c2f2SEnji Cooper // 0:-----\ :
533*28f6c2f2SEnji Cooper // 1:---\ | :
534*28f6c2f2SEnji Cooper // 2:---\ | :
535*28f6c2f2SEnji Cooper // 3:-\ | | :
536*28f6c2f2SEnji Cooper // :.......:
537*28f6c2f2SEnji Cooper // 0 1 2
538*28f6c2f2SEnji Cooper MatchMatrix g(4, 3);
539*28f6c2f2SEnji Cooper constexpr std::array<std::array<size_t, 2>, 4> kEdges = {
540*28f6c2f2SEnji Cooper {{{0, 2}}, {{1, 1}}, {{2, 1}}, {{3, 0}}}};
541*28f6c2f2SEnji Cooper for (size_t i = 0; i < kEdges.size(); ++i) {
542*28f6c2f2SEnji Cooper g.SetEdge(kEdges[i][0], kEdges[i][1], true);
543*28f6c2f2SEnji Cooper }
544*28f6c2f2SEnji Cooper EXPECT_THAT(FindBacktrackingMaxBPM(g),
545*28f6c2f2SEnji Cooper ElementsAre(Pair(3, 0), Pair(AnyOf(1, 2), 1), Pair(0, 2)))
546*28f6c2f2SEnji Cooper << g.DebugString();
547*28f6c2f2SEnji Cooper }
548*28f6c2f2SEnji Cooper
549*28f6c2f2SEnji Cooper // Verify a few nonsquare matrices.
TEST_P(BipartiteNonSquareTest,Exhaustive)550*28f6c2f2SEnji Cooper TEST_P(BipartiteNonSquareTest, Exhaustive) {
551*28f6c2f2SEnji Cooper size_t nlhs = GetParam().first;
552*28f6c2f2SEnji Cooper size_t nrhs = GetParam().second;
553*28f6c2f2SEnji Cooper MatchMatrix graph(nlhs, nrhs);
554*28f6c2f2SEnji Cooper do {
555*28f6c2f2SEnji Cooper EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
556*28f6c2f2SEnji Cooper internal::FindMaxBipartiteMatching(graph).size())
557*28f6c2f2SEnji Cooper << "graph: " << graph.DebugString()
558*28f6c2f2SEnji Cooper << "\nbacktracking: " << PrintToString(FindBacktrackingMaxBPM(graph))
559*28f6c2f2SEnji Cooper << "\nmax flow: "
560*28f6c2f2SEnji Cooper << PrintToString(internal::FindMaxBipartiteMatching(graph));
561*28f6c2f2SEnji Cooper } while (graph.NextGraph());
562*28f6c2f2SEnji Cooper }
563*28f6c2f2SEnji Cooper
564*28f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(
565*28f6c2f2SEnji Cooper AllGraphs, BipartiteNonSquareTest,
566*28f6c2f2SEnji Cooper testing::Values(std::make_pair(1, 2), std::make_pair(2, 1),
567*28f6c2f2SEnji Cooper std::make_pair(3, 2), std::make_pair(2, 3),
568*28f6c2f2SEnji Cooper std::make_pair(4, 1), std::make_pair(1, 4),
569*28f6c2f2SEnji Cooper std::make_pair(4, 3), std::make_pair(3, 4)));
570*28f6c2f2SEnji Cooper
571*28f6c2f2SEnji Cooper class BipartiteRandomTest
572*28f6c2f2SEnji Cooper : public ::testing::TestWithParam<std::pair<int, int>> {};
573*28f6c2f2SEnji Cooper
574*28f6c2f2SEnji Cooper // Verifies a large sample of larger graphs.
TEST_P(BipartiteRandomTest,LargerNets)575*28f6c2f2SEnji Cooper TEST_P(BipartiteRandomTest, LargerNets) {
576*28f6c2f2SEnji Cooper int nodes = GetParam().first;
577*28f6c2f2SEnji Cooper int iters = GetParam().second;
578*28f6c2f2SEnji Cooper MatchMatrix graph(static_cast<size_t>(nodes), static_cast<size_t>(nodes));
579*28f6c2f2SEnji Cooper
580*28f6c2f2SEnji Cooper auto seed = static_cast<uint32_t>(GTEST_FLAG_GET(random_seed));
581*28f6c2f2SEnji Cooper if (seed == 0) {
582*28f6c2f2SEnji Cooper seed = static_cast<uint32_t>(time(nullptr));
583*28f6c2f2SEnji Cooper }
584*28f6c2f2SEnji Cooper
585*28f6c2f2SEnji Cooper for (; iters > 0; --iters, ++seed) {
586*28f6c2f2SEnji Cooper srand(static_cast<unsigned int>(seed));
587*28f6c2f2SEnji Cooper graph.Randomize();
588*28f6c2f2SEnji Cooper EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
589*28f6c2f2SEnji Cooper internal::FindMaxBipartiteMatching(graph).size())
590*28f6c2f2SEnji Cooper << " graph: " << graph.DebugString()
591*28f6c2f2SEnji Cooper << "\nTo reproduce the failure, rerun the test with the flag"
592*28f6c2f2SEnji Cooper " --"
593*28f6c2f2SEnji Cooper << GTEST_FLAG_PREFIX_ << "random_seed=" << seed;
594*28f6c2f2SEnji Cooper }
595*28f6c2f2SEnji Cooper }
596*28f6c2f2SEnji Cooper
597*28f6c2f2SEnji Cooper // Test argument is a std::pair<int, int> representing (nodes, iters).
598*28f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(Samples, BipartiteRandomTest,
599*28f6c2f2SEnji Cooper testing::Values(std::make_pair(5, 10000),
600*28f6c2f2SEnji Cooper std::make_pair(6, 5000),
601*28f6c2f2SEnji Cooper std::make_pair(7, 2000),
602*28f6c2f2SEnji Cooper std::make_pair(8, 500),
603*28f6c2f2SEnji Cooper std::make_pair(9, 100)));
604*28f6c2f2SEnji Cooper
605*28f6c2f2SEnji Cooper // Tests IsReadableTypeName().
606*28f6c2f2SEnji Cooper
TEST(IsReadableTypeNameTest,ReturnsTrueForShortNames)607*28f6c2f2SEnji Cooper TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
608*28f6c2f2SEnji Cooper EXPECT_TRUE(IsReadableTypeName("int"));
609*28f6c2f2SEnji Cooper EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
610*28f6c2f2SEnji Cooper EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
611*28f6c2f2SEnji Cooper EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
612*28f6c2f2SEnji Cooper }
613*28f6c2f2SEnji Cooper
TEST(IsReadableTypeNameTest,ReturnsTrueForLongNonTemplateNonFunctionNames)614*28f6c2f2SEnji Cooper TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
615*28f6c2f2SEnji Cooper EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
616*28f6c2f2SEnji Cooper EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
617*28f6c2f2SEnji Cooper EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
618*28f6c2f2SEnji Cooper }
619*28f6c2f2SEnji Cooper
TEST(IsReadableTypeNameTest,ReturnsFalseForLongTemplateNames)620*28f6c2f2SEnji Cooper TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
621*28f6c2f2SEnji Cooper EXPECT_FALSE(
622*28f6c2f2SEnji Cooper IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
623*28f6c2f2SEnji Cooper EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
624*28f6c2f2SEnji Cooper }
625*28f6c2f2SEnji Cooper
TEST(IsReadableTypeNameTest,ReturnsFalseForLongFunctionTypeNames)626*28f6c2f2SEnji Cooper TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
627*28f6c2f2SEnji Cooper EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
628*28f6c2f2SEnji Cooper }
629*28f6c2f2SEnji Cooper
630*28f6c2f2SEnji Cooper // Tests FormatMatcherDescription().
631*28f6c2f2SEnji Cooper
TEST(FormatMatcherDescriptionTest,WorksForEmptyDescription)632*28f6c2f2SEnji Cooper TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
633*28f6c2f2SEnji Cooper EXPECT_EQ("is even",
634*28f6c2f2SEnji Cooper FormatMatcherDescription(false, "IsEven", {}, Strings()));
635*28f6c2f2SEnji Cooper EXPECT_EQ("not (is even)",
636*28f6c2f2SEnji Cooper FormatMatcherDescription(true, "IsEven", {}, Strings()));
637*28f6c2f2SEnji Cooper
638*28f6c2f2SEnji Cooper EXPECT_EQ("equals (a: 5)",
639*28f6c2f2SEnji Cooper FormatMatcherDescription(false, "Equals", {"a"}, {"5"}));
640*28f6c2f2SEnji Cooper
641*28f6c2f2SEnji Cooper EXPECT_EQ(
642*28f6c2f2SEnji Cooper "is in range (a: 5, b: 8)",
643*28f6c2f2SEnji Cooper FormatMatcherDescription(false, "IsInRange", {"a", "b"}, {"5", "8"}));
644*28f6c2f2SEnji Cooper }
645*28f6c2f2SEnji Cooper
646*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTupleTest);
647*28f6c2f2SEnji Cooper
TEST_P(MatcherTupleTestP,ExplainsMatchFailure)648*28f6c2f2SEnji Cooper TEST_P(MatcherTupleTestP, ExplainsMatchFailure) {
649*28f6c2f2SEnji Cooper stringstream ss1;
650*28f6c2f2SEnji Cooper ExplainMatchFailureTupleTo(
651*28f6c2f2SEnji Cooper std::make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
652*28f6c2f2SEnji Cooper std::make_tuple('a', 10), &ss1);
653*28f6c2f2SEnji Cooper EXPECT_EQ("", ss1.str()); // Successful match.
654*28f6c2f2SEnji Cooper
655*28f6c2f2SEnji Cooper stringstream ss2;
656*28f6c2f2SEnji Cooper ExplainMatchFailureTupleTo(
657*28f6c2f2SEnji Cooper std::make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
658*28f6c2f2SEnji Cooper std::make_tuple(2, 'b'), &ss2);
659*28f6c2f2SEnji Cooper EXPECT_EQ(
660*28f6c2f2SEnji Cooper " Expected arg #0: is > 5\n"
661*28f6c2f2SEnji Cooper " Actual: 2, which is 3 less than 5\n"
662*28f6c2f2SEnji Cooper " Expected arg #1: is equal to 'a' (97, 0x61)\n"
663*28f6c2f2SEnji Cooper " Actual: 'b' (98, 0x62)\n",
664*28f6c2f2SEnji Cooper ss2.str()); // Failed match where both arguments need explanation.
665*28f6c2f2SEnji Cooper
666*28f6c2f2SEnji Cooper stringstream ss3;
667*28f6c2f2SEnji Cooper ExplainMatchFailureTupleTo(
668*28f6c2f2SEnji Cooper std::make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
669*28f6c2f2SEnji Cooper std::make_tuple(2, 'a'), &ss3);
670*28f6c2f2SEnji Cooper EXPECT_EQ(
671*28f6c2f2SEnji Cooper " Expected arg #0: is > 5\n"
672*28f6c2f2SEnji Cooper " Actual: 2, which is 3 less than 5\n",
673*28f6c2f2SEnji Cooper ss3.str()); // Failed match where only one argument needs
674*28f6c2f2SEnji Cooper // explanation.
675*28f6c2f2SEnji Cooper }
676*28f6c2f2SEnji Cooper
677*28f6c2f2SEnji Cooper // Sample optional type implementation with minimal requirements for use with
678*28f6c2f2SEnji Cooper // Optional matcher.
679*28f6c2f2SEnji Cooper template <typename T>
680*28f6c2f2SEnji Cooper class SampleOptional {
681*28f6c2f2SEnji Cooper public:
682*28f6c2f2SEnji Cooper using value_type = T;
SampleOptional(T value)683*28f6c2f2SEnji Cooper explicit SampleOptional(T value)
684*28f6c2f2SEnji Cooper : value_(std::move(value)), has_value_(true) {}
SampleOptional()685*28f6c2f2SEnji Cooper SampleOptional() : value_(), has_value_(false) {}
operator bool() const686*28f6c2f2SEnji Cooper operator bool() const { return has_value_; }
operator *() const687*28f6c2f2SEnji Cooper const T& operator*() const { return value_; }
688*28f6c2f2SEnji Cooper
689*28f6c2f2SEnji Cooper private:
690*28f6c2f2SEnji Cooper T value_;
691*28f6c2f2SEnji Cooper bool has_value_;
692*28f6c2f2SEnji Cooper };
693*28f6c2f2SEnji Cooper
TEST(OptionalTest,DescribesSelf)694*28f6c2f2SEnji Cooper TEST(OptionalTest, DescribesSelf) {
695*28f6c2f2SEnji Cooper const Matcher<SampleOptional<int>> m = Optional(Eq(1));
696*28f6c2f2SEnji Cooper EXPECT_EQ("value is equal to 1", Describe(m));
697*28f6c2f2SEnji Cooper }
698*28f6c2f2SEnji Cooper
TEST(OptionalTest,ExplainsSelf)699*28f6c2f2SEnji Cooper TEST(OptionalTest, ExplainsSelf) {
700*28f6c2f2SEnji Cooper const Matcher<SampleOptional<int>> m = Optional(Eq(1));
701*28f6c2f2SEnji Cooper EXPECT_EQ("whose value 1 matches", Explain(m, SampleOptional<int>(1)));
702*28f6c2f2SEnji Cooper EXPECT_EQ("whose value 2 doesn't match", Explain(m, SampleOptional<int>(2)));
703*28f6c2f2SEnji Cooper }
704*28f6c2f2SEnji Cooper
TEST(OptionalTest,MatchesNonEmptyOptional)705*28f6c2f2SEnji Cooper TEST(OptionalTest, MatchesNonEmptyOptional) {
706*28f6c2f2SEnji Cooper const Matcher<SampleOptional<int>> m1 = Optional(1);
707*28f6c2f2SEnji Cooper const Matcher<SampleOptional<int>> m2 = Optional(Eq(2));
708*28f6c2f2SEnji Cooper const Matcher<SampleOptional<int>> m3 = Optional(Lt(3));
709*28f6c2f2SEnji Cooper SampleOptional<int> opt(1);
710*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(opt));
711*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(opt));
712*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(opt));
713*28f6c2f2SEnji Cooper }
714*28f6c2f2SEnji Cooper
TEST(OptionalTest,DoesNotMatchNullopt)715*28f6c2f2SEnji Cooper TEST(OptionalTest, DoesNotMatchNullopt) {
716*28f6c2f2SEnji Cooper const Matcher<SampleOptional<int>> m = Optional(1);
717*28f6c2f2SEnji Cooper SampleOptional<int> empty;
718*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(empty));
719*28f6c2f2SEnji Cooper }
720*28f6c2f2SEnji Cooper
TEST(OptionalTest,WorksWithMoveOnly)721*28f6c2f2SEnji Cooper TEST(OptionalTest, WorksWithMoveOnly) {
722*28f6c2f2SEnji Cooper Matcher<SampleOptional<std::unique_ptr<int>>> m = Optional(Eq(nullptr));
723*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(SampleOptional<std::unique_ptr<int>>(nullptr)));
724*28f6c2f2SEnji Cooper }
725*28f6c2f2SEnji Cooper
726*28f6c2f2SEnji Cooper class SampleVariantIntString {
727*28f6c2f2SEnji Cooper public:
SampleVariantIntString(int i)728*28f6c2f2SEnji Cooper SampleVariantIntString(int i) : i_(i), has_int_(true) {}
SampleVariantIntString(const std::string & s)729*28f6c2f2SEnji Cooper SampleVariantIntString(const std::string& s) : s_(s), has_int_(false) {}
730*28f6c2f2SEnji Cooper
731*28f6c2f2SEnji Cooper template <typename T>
holds_alternative(const SampleVariantIntString & value)732*28f6c2f2SEnji Cooper friend bool holds_alternative(const SampleVariantIntString& value) {
733*28f6c2f2SEnji Cooper return value.has_int_ == std::is_same<T, int>::value;
734*28f6c2f2SEnji Cooper }
735*28f6c2f2SEnji Cooper
736*28f6c2f2SEnji Cooper template <typename T>
get(const SampleVariantIntString & value)737*28f6c2f2SEnji Cooper friend const T& get(const SampleVariantIntString& value) {
738*28f6c2f2SEnji Cooper return value.get_impl(static_cast<T*>(nullptr));
739*28f6c2f2SEnji Cooper }
740*28f6c2f2SEnji Cooper
741*28f6c2f2SEnji Cooper private:
get_impl(int *) const742*28f6c2f2SEnji Cooper const int& get_impl(int*) const { return i_; }
get_impl(std::string *) const743*28f6c2f2SEnji Cooper const std::string& get_impl(std::string*) const { return s_; }
744*28f6c2f2SEnji Cooper
745*28f6c2f2SEnji Cooper int i_;
746*28f6c2f2SEnji Cooper std::string s_;
747*28f6c2f2SEnji Cooper bool has_int_;
748*28f6c2f2SEnji Cooper };
749*28f6c2f2SEnji Cooper
TEST(VariantTest,DescribesSelf)750*28f6c2f2SEnji Cooper TEST(VariantTest, DescribesSelf) {
751*28f6c2f2SEnji Cooper const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
752*28f6c2f2SEnji Cooper EXPECT_THAT(Describe(m), ContainsRegex("is a variant<> with value of type "
753*28f6c2f2SEnji Cooper "'.*' and the value is equal to 1"));
754*28f6c2f2SEnji Cooper }
755*28f6c2f2SEnji Cooper
TEST(VariantTest,ExplainsSelf)756*28f6c2f2SEnji Cooper TEST(VariantTest, ExplainsSelf) {
757*28f6c2f2SEnji Cooper const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
758*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(m, SampleVariantIntString(1)),
759*28f6c2f2SEnji Cooper ContainsRegex("whose value 1"));
760*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(m, SampleVariantIntString("A")),
761*28f6c2f2SEnji Cooper HasSubstr("whose value is not of type '"));
762*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(m, SampleVariantIntString(2)),
763*28f6c2f2SEnji Cooper "whose value 2 doesn't match");
764*28f6c2f2SEnji Cooper }
765*28f6c2f2SEnji Cooper
TEST(VariantTest,FullMatch)766*28f6c2f2SEnji Cooper TEST(VariantTest, FullMatch) {
767*28f6c2f2SEnji Cooper Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
768*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(SampleVariantIntString(1)));
769*28f6c2f2SEnji Cooper
770*28f6c2f2SEnji Cooper m = VariantWith<std::string>(Eq("1"));
771*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(SampleVariantIntString("1")));
772*28f6c2f2SEnji Cooper }
773*28f6c2f2SEnji Cooper
TEST(VariantTest,TypeDoesNotMatch)774*28f6c2f2SEnji Cooper TEST(VariantTest, TypeDoesNotMatch) {
775*28f6c2f2SEnji Cooper Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
776*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(SampleVariantIntString("1")));
777*28f6c2f2SEnji Cooper
778*28f6c2f2SEnji Cooper m = VariantWith<std::string>(Eq("1"));
779*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(SampleVariantIntString(1)));
780*28f6c2f2SEnji Cooper }
781*28f6c2f2SEnji Cooper
TEST(VariantTest,InnerDoesNotMatch)782*28f6c2f2SEnji Cooper TEST(VariantTest, InnerDoesNotMatch) {
783*28f6c2f2SEnji Cooper Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
784*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(SampleVariantIntString(2)));
785*28f6c2f2SEnji Cooper
786*28f6c2f2SEnji Cooper m = VariantWith<std::string>(Eq("1"));
787*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(SampleVariantIntString("2")));
788*28f6c2f2SEnji Cooper }
789*28f6c2f2SEnji Cooper
790*28f6c2f2SEnji Cooper class SampleAnyType {
791*28f6c2f2SEnji Cooper public:
SampleAnyType(int i)792*28f6c2f2SEnji Cooper explicit SampleAnyType(int i) : index_(0), i_(i) {}
SampleAnyType(const std::string & s)793*28f6c2f2SEnji Cooper explicit SampleAnyType(const std::string& s) : index_(1), s_(s) {}
794*28f6c2f2SEnji Cooper
795*28f6c2f2SEnji Cooper template <typename T>
any_cast(const SampleAnyType * any)796*28f6c2f2SEnji Cooper friend const T* any_cast(const SampleAnyType* any) {
797*28f6c2f2SEnji Cooper return any->get_impl(static_cast<T*>(nullptr));
798*28f6c2f2SEnji Cooper }
799*28f6c2f2SEnji Cooper
800*28f6c2f2SEnji Cooper private:
801*28f6c2f2SEnji Cooper int index_;
802*28f6c2f2SEnji Cooper int i_;
803*28f6c2f2SEnji Cooper std::string s_;
804*28f6c2f2SEnji Cooper
get_impl(int *) const805*28f6c2f2SEnji Cooper const int* get_impl(int*) const { return index_ == 0 ? &i_ : nullptr; }
get_impl(std::string *) const806*28f6c2f2SEnji Cooper const std::string* get_impl(std::string*) const {
807*28f6c2f2SEnji Cooper return index_ == 1 ? &s_ : nullptr;
808*28f6c2f2SEnji Cooper }
809*28f6c2f2SEnji Cooper };
810*28f6c2f2SEnji Cooper
TEST(AnyWithTest,FullMatch)811*28f6c2f2SEnji Cooper TEST(AnyWithTest, FullMatch) {
812*28f6c2f2SEnji Cooper Matcher<SampleAnyType> m = AnyWith<int>(Eq(1));
813*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(SampleAnyType(1)));
814*28f6c2f2SEnji Cooper }
815*28f6c2f2SEnji Cooper
TEST(AnyWithTest,TestBadCastType)816*28f6c2f2SEnji Cooper TEST(AnyWithTest, TestBadCastType) {
817*28f6c2f2SEnji Cooper Matcher<SampleAnyType> m = AnyWith<std::string>(Eq("fail"));
818*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(SampleAnyType(1)));
819*28f6c2f2SEnji Cooper }
820*28f6c2f2SEnji Cooper
TEST(AnyWithTest,TestUseInContainers)821*28f6c2f2SEnji Cooper TEST(AnyWithTest, TestUseInContainers) {
822*28f6c2f2SEnji Cooper std::vector<SampleAnyType> a;
823*28f6c2f2SEnji Cooper a.emplace_back(1);
824*28f6c2f2SEnji Cooper a.emplace_back(2);
825*28f6c2f2SEnji Cooper a.emplace_back(3);
826*28f6c2f2SEnji Cooper EXPECT_THAT(
827*28f6c2f2SEnji Cooper a, ElementsAreArray({AnyWith<int>(1), AnyWith<int>(2), AnyWith<int>(3)}));
828*28f6c2f2SEnji Cooper
829*28f6c2f2SEnji Cooper std::vector<SampleAnyType> b;
830*28f6c2f2SEnji Cooper b.emplace_back("hello");
831*28f6c2f2SEnji Cooper b.emplace_back("merhaba");
832*28f6c2f2SEnji Cooper b.emplace_back("salut");
833*28f6c2f2SEnji Cooper EXPECT_THAT(b, ElementsAreArray({AnyWith<std::string>("hello"),
834*28f6c2f2SEnji Cooper AnyWith<std::string>("merhaba"),
835*28f6c2f2SEnji Cooper AnyWith<std::string>("salut")}));
836*28f6c2f2SEnji Cooper }
TEST(AnyWithTest,TestCompare)837*28f6c2f2SEnji Cooper TEST(AnyWithTest, TestCompare) {
838*28f6c2f2SEnji Cooper EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0)));
839*28f6c2f2SEnji Cooper }
840*28f6c2f2SEnji Cooper
TEST(AnyWithTest,DescribesSelf)841*28f6c2f2SEnji Cooper TEST(AnyWithTest, DescribesSelf) {
842*28f6c2f2SEnji Cooper const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
843*28f6c2f2SEnji Cooper EXPECT_THAT(Describe(m), ContainsRegex("is an 'any' type with value of type "
844*28f6c2f2SEnji Cooper "'.*' and the value is equal to 1"));
845*28f6c2f2SEnji Cooper }
846*28f6c2f2SEnji Cooper
TEST(AnyWithTest,ExplainsSelf)847*28f6c2f2SEnji Cooper TEST(AnyWithTest, ExplainsSelf) {
848*28f6c2f2SEnji Cooper const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
849*28f6c2f2SEnji Cooper
850*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(m, SampleAnyType(1)), ContainsRegex("whose value 1"));
851*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(m, SampleAnyType("A")),
852*28f6c2f2SEnji Cooper HasSubstr("whose value is not of type '"));
853*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(m, SampleAnyType(2)), "whose value 2 doesn't match");
854*28f6c2f2SEnji Cooper }
855*28f6c2f2SEnji Cooper
856*28f6c2f2SEnji Cooper // Tests Args<k0, ..., kn>(m).
857*28f6c2f2SEnji Cooper
TEST(ArgsTest,AcceptsZeroTemplateArg)858*28f6c2f2SEnji Cooper TEST(ArgsTest, AcceptsZeroTemplateArg) {
859*28f6c2f2SEnji Cooper const std::tuple<int, bool> t(5, true);
860*28f6c2f2SEnji Cooper EXPECT_THAT(t, Args<>(Eq(std::tuple<>())));
861*28f6c2f2SEnji Cooper EXPECT_THAT(t, Not(Args<>(Ne(std::tuple<>()))));
862*28f6c2f2SEnji Cooper }
863*28f6c2f2SEnji Cooper
TEST(ArgsTest,AcceptsOneTemplateArg)864*28f6c2f2SEnji Cooper TEST(ArgsTest, AcceptsOneTemplateArg) {
865*28f6c2f2SEnji Cooper const std::tuple<int, bool> t(5, true);
866*28f6c2f2SEnji Cooper EXPECT_THAT(t, Args<0>(Eq(std::make_tuple(5))));
867*28f6c2f2SEnji Cooper EXPECT_THAT(t, Args<1>(Eq(std::make_tuple(true))));
868*28f6c2f2SEnji Cooper EXPECT_THAT(t, Not(Args<1>(Eq(std::make_tuple(false)))));
869*28f6c2f2SEnji Cooper }
870*28f6c2f2SEnji Cooper
TEST(ArgsTest,AcceptsTwoTemplateArgs)871*28f6c2f2SEnji Cooper TEST(ArgsTest, AcceptsTwoTemplateArgs) {
872*28f6c2f2SEnji Cooper const std::tuple<short, int, long> t(short{4}, 5, 6L); // NOLINT
873*28f6c2f2SEnji Cooper
874*28f6c2f2SEnji Cooper EXPECT_THAT(t, (Args<0, 1>(Lt())));
875*28f6c2f2SEnji Cooper EXPECT_THAT(t, (Args<1, 2>(Lt())));
876*28f6c2f2SEnji Cooper EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
877*28f6c2f2SEnji Cooper }
878*28f6c2f2SEnji Cooper
TEST(ArgsTest,AcceptsRepeatedTemplateArgs)879*28f6c2f2SEnji Cooper TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
880*28f6c2f2SEnji Cooper const std::tuple<short, int, long> t(short{4}, 5, 6L); // NOLINT
881*28f6c2f2SEnji Cooper EXPECT_THAT(t, (Args<0, 0>(Eq())));
882*28f6c2f2SEnji Cooper EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
883*28f6c2f2SEnji Cooper }
884*28f6c2f2SEnji Cooper
TEST(ArgsTest,AcceptsDecreasingTemplateArgs)885*28f6c2f2SEnji Cooper TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
886*28f6c2f2SEnji Cooper const std::tuple<short, int, long> t(short{4}, 5, 6L); // NOLINT
887*28f6c2f2SEnji Cooper EXPECT_THAT(t, (Args<2, 0>(Gt())));
888*28f6c2f2SEnji Cooper EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
889*28f6c2f2SEnji Cooper }
890*28f6c2f2SEnji Cooper
891*28f6c2f2SEnji Cooper MATCHER(SumIsZero, "") {
892*28f6c2f2SEnji Cooper return std::get<0>(arg) + std::get<1>(arg) + std::get<2>(arg) == 0;
893*28f6c2f2SEnji Cooper }
894*28f6c2f2SEnji Cooper
TEST(ArgsTest,AcceptsMoreTemplateArgsThanArityOfOriginalTuple)895*28f6c2f2SEnji Cooper TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
896*28f6c2f2SEnji Cooper EXPECT_THAT(std::make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
897*28f6c2f2SEnji Cooper EXPECT_THAT(std::make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
898*28f6c2f2SEnji Cooper }
899*28f6c2f2SEnji Cooper
TEST(ArgsTest,CanBeNested)900*28f6c2f2SEnji Cooper TEST(ArgsTest, CanBeNested) {
901*28f6c2f2SEnji Cooper const std::tuple<short, int, long, int> t(short{4}, 5, 6L, 6); // NOLINT
902*28f6c2f2SEnji Cooper EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
903*28f6c2f2SEnji Cooper EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
904*28f6c2f2SEnji Cooper }
905*28f6c2f2SEnji Cooper
TEST(ArgsTest,CanMatchTupleByValue)906*28f6c2f2SEnji Cooper TEST(ArgsTest, CanMatchTupleByValue) {
907*28f6c2f2SEnji Cooper typedef std::tuple<char, int, int> Tuple3;
908*28f6c2f2SEnji Cooper const Matcher<Tuple3> m = Args<1, 2>(Lt());
909*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
910*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
911*28f6c2f2SEnji Cooper }
912*28f6c2f2SEnji Cooper
TEST(ArgsTest,CanMatchTupleByReference)913*28f6c2f2SEnji Cooper TEST(ArgsTest, CanMatchTupleByReference) {
914*28f6c2f2SEnji Cooper typedef std::tuple<char, char, int> Tuple3;
915*28f6c2f2SEnji Cooper const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
916*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
917*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
918*28f6c2f2SEnji Cooper }
919*28f6c2f2SEnji Cooper
920*28f6c2f2SEnji Cooper // Validates that arg is printed as str.
921*28f6c2f2SEnji Cooper MATCHER_P(PrintsAs, str, "") { return testing::PrintToString(arg) == str; }
922*28f6c2f2SEnji Cooper
TEST(ArgsTest,AcceptsTenTemplateArgs)923*28f6c2f2SEnji Cooper TEST(ArgsTest, AcceptsTenTemplateArgs) {
924*28f6c2f2SEnji Cooper EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
925*28f6c2f2SEnji Cooper (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
926*28f6c2f2SEnji Cooper PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
927*28f6c2f2SEnji Cooper EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
928*28f6c2f2SEnji Cooper Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
929*28f6c2f2SEnji Cooper PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
930*28f6c2f2SEnji Cooper }
931*28f6c2f2SEnji Cooper
TEST(ArgsTest,DescirbesSelfCorrectly)932*28f6c2f2SEnji Cooper TEST(ArgsTest, DescirbesSelfCorrectly) {
933*28f6c2f2SEnji Cooper const Matcher<std::tuple<int, bool, char>> m = Args<2, 0>(Lt());
934*28f6c2f2SEnji Cooper EXPECT_EQ(
935*28f6c2f2SEnji Cooper "are a tuple whose fields (#2, #0) are a pair where "
936*28f6c2f2SEnji Cooper "the first < the second",
937*28f6c2f2SEnji Cooper Describe(m));
938*28f6c2f2SEnji Cooper }
939*28f6c2f2SEnji Cooper
TEST(ArgsTest,DescirbesNestedArgsCorrectly)940*28f6c2f2SEnji Cooper TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
941*28f6c2f2SEnji Cooper const Matcher<const std::tuple<int, bool, char, int>&> m =
942*28f6c2f2SEnji Cooper Args<0, 2, 3>(Args<2, 0>(Lt()));
943*28f6c2f2SEnji Cooper EXPECT_EQ(
944*28f6c2f2SEnji Cooper "are a tuple whose fields (#0, #2, #3) are a tuple "
945*28f6c2f2SEnji Cooper "whose fields (#2, #0) are a pair where the first < the second",
946*28f6c2f2SEnji Cooper Describe(m));
947*28f6c2f2SEnji Cooper }
948*28f6c2f2SEnji Cooper
TEST(ArgsTest,DescribesNegationCorrectly)949*28f6c2f2SEnji Cooper TEST(ArgsTest, DescribesNegationCorrectly) {
950*28f6c2f2SEnji Cooper const Matcher<std::tuple<int, char>> m = Args<1, 0>(Gt());
951*28f6c2f2SEnji Cooper EXPECT_EQ(
952*28f6c2f2SEnji Cooper "are a tuple whose fields (#1, #0) aren't a pair "
953*28f6c2f2SEnji Cooper "where the first > the second",
954*28f6c2f2SEnji Cooper DescribeNegation(m));
955*28f6c2f2SEnji Cooper }
956*28f6c2f2SEnji Cooper
TEST(ArgsTest,ExplainsMatchResultWithoutInnerExplanation)957*28f6c2f2SEnji Cooper TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
958*28f6c2f2SEnji Cooper const Matcher<std::tuple<bool, int, int>> m = Args<1, 2>(Eq());
959*28f6c2f2SEnji Cooper EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
960*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(false, 42, 42)));
961*28f6c2f2SEnji Cooper EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
962*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(false, 42, 43)));
963*28f6c2f2SEnji Cooper }
964*28f6c2f2SEnji Cooper
965*28f6c2f2SEnji Cooper // For testing Args<>'s explanation.
966*28f6c2f2SEnji Cooper class LessThanMatcher : public MatcherInterface<std::tuple<char, int>> {
967*28f6c2f2SEnji Cooper public:
DescribeTo(::std::ostream *) const968*28f6c2f2SEnji Cooper void DescribeTo(::std::ostream* /*os*/) const override {}
969*28f6c2f2SEnji Cooper
MatchAndExplain(std::tuple<char,int> value,MatchResultListener * listener) const970*28f6c2f2SEnji Cooper bool MatchAndExplain(std::tuple<char, int> value,
971*28f6c2f2SEnji Cooper MatchResultListener* listener) const override {
972*28f6c2f2SEnji Cooper const int diff = std::get<0>(value) - std::get<1>(value);
973*28f6c2f2SEnji Cooper if (diff > 0) {
974*28f6c2f2SEnji Cooper *listener << "where the first value is " << diff
975*28f6c2f2SEnji Cooper << " more than the second";
976*28f6c2f2SEnji Cooper }
977*28f6c2f2SEnji Cooper return diff < 0;
978*28f6c2f2SEnji Cooper }
979*28f6c2f2SEnji Cooper };
980*28f6c2f2SEnji Cooper
LessThan()981*28f6c2f2SEnji Cooper Matcher<std::tuple<char, int>> LessThan() {
982*28f6c2f2SEnji Cooper return MakeMatcher(new LessThanMatcher);
983*28f6c2f2SEnji Cooper }
984*28f6c2f2SEnji Cooper
TEST(ArgsTest,ExplainsMatchResultWithInnerExplanation)985*28f6c2f2SEnji Cooper TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
986*28f6c2f2SEnji Cooper const Matcher<std::tuple<char, int, int>> m = Args<0, 2>(LessThan());
987*28f6c2f2SEnji Cooper EXPECT_EQ(
988*28f6c2f2SEnji Cooper "whose fields (#0, #2) are ('a' (97, 0x61), 42), "
989*28f6c2f2SEnji Cooper "where the first value is 55 more than the second",
990*28f6c2f2SEnji Cooper Explain(m, std::make_tuple('a', 42, 42)));
991*28f6c2f2SEnji Cooper EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
992*28f6c2f2SEnji Cooper Explain(m, std::make_tuple('\0', 42, 43)));
993*28f6c2f2SEnji Cooper }
994*28f6c2f2SEnji Cooper
995*28f6c2f2SEnji Cooper // Tests for the MATCHER*() macro family.
996*28f6c2f2SEnji Cooper
997*28f6c2f2SEnji Cooper // Tests that a simple MATCHER() definition works.
998*28f6c2f2SEnji Cooper
999*28f6c2f2SEnji Cooper MATCHER(IsEven, "") { return (arg % 2) == 0; }
1000*28f6c2f2SEnji Cooper
TEST(MatcherMacroTest,Works)1001*28f6c2f2SEnji Cooper TEST(MatcherMacroTest, Works) {
1002*28f6c2f2SEnji Cooper const Matcher<int> m = IsEven();
1003*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(6));
1004*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(7));
1005*28f6c2f2SEnji Cooper
1006*28f6c2f2SEnji Cooper EXPECT_EQ("is even", Describe(m));
1007*28f6c2f2SEnji Cooper EXPECT_EQ("not (is even)", DescribeNegation(m));
1008*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 6));
1009*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 7));
1010*28f6c2f2SEnji Cooper }
1011*28f6c2f2SEnji Cooper
1012*28f6c2f2SEnji Cooper // This also tests that the description string can reference 'negation'.
1013*28f6c2f2SEnji Cooper MATCHER(IsEven2, negation ? "is odd" : "is even") {
1014*28f6c2f2SEnji Cooper if ((arg % 2) == 0) {
1015*28f6c2f2SEnji Cooper // Verifies that we can stream to result_listener, a listener
1016*28f6c2f2SEnji Cooper // supplied by the MATCHER macro implicitly.
1017*28f6c2f2SEnji Cooper *result_listener << "OK";
1018*28f6c2f2SEnji Cooper return true;
1019*28f6c2f2SEnji Cooper } else {
1020*28f6c2f2SEnji Cooper *result_listener << "% 2 == " << (arg % 2);
1021*28f6c2f2SEnji Cooper return false;
1022*28f6c2f2SEnji Cooper }
1023*28f6c2f2SEnji Cooper }
1024*28f6c2f2SEnji Cooper
1025*28f6c2f2SEnji Cooper // This also tests that the description string can reference matcher
1026*28f6c2f2SEnji Cooper // parameters.
1027*28f6c2f2SEnji Cooper MATCHER_P2(EqSumOf, x, y,
1028*28f6c2f2SEnji Cooper std::string(negation ? "doesn't equal" : "equals") + " the sum of " +
1029*28f6c2f2SEnji Cooper PrintToString(x) + " and " + PrintToString(y)) {
1030*28f6c2f2SEnji Cooper if (arg == (x + y)) {
1031*28f6c2f2SEnji Cooper *result_listener << "OK";
1032*28f6c2f2SEnji Cooper return true;
1033*28f6c2f2SEnji Cooper } else {
1034*28f6c2f2SEnji Cooper // Verifies that we can stream to the underlying stream of
1035*28f6c2f2SEnji Cooper // result_listener.
1036*28f6c2f2SEnji Cooper if (result_listener->stream() != nullptr) {
1037*28f6c2f2SEnji Cooper *result_listener->stream() << "diff == " << (x + y - arg);
1038*28f6c2f2SEnji Cooper }
1039*28f6c2f2SEnji Cooper return false;
1040*28f6c2f2SEnji Cooper }
1041*28f6c2f2SEnji Cooper }
1042*28f6c2f2SEnji Cooper
1043*28f6c2f2SEnji Cooper // Tests that the matcher description can reference 'negation' and the
1044*28f6c2f2SEnji Cooper // matcher parameters.
TEST(MatcherMacroTest,DescriptionCanReferenceNegationAndParameters)1045*28f6c2f2SEnji Cooper TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
1046*28f6c2f2SEnji Cooper const Matcher<int> m1 = IsEven2();
1047*28f6c2f2SEnji Cooper EXPECT_EQ("is even", Describe(m1));
1048*28f6c2f2SEnji Cooper EXPECT_EQ("is odd", DescribeNegation(m1));
1049*28f6c2f2SEnji Cooper
1050*28f6c2f2SEnji Cooper const Matcher<int> m2 = EqSumOf(5, 9);
1051*28f6c2f2SEnji Cooper EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
1052*28f6c2f2SEnji Cooper EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
1053*28f6c2f2SEnji Cooper }
1054*28f6c2f2SEnji Cooper
1055*28f6c2f2SEnji Cooper // Tests explaining match result in a MATCHER* macro.
TEST(MatcherMacroTest,CanExplainMatchResult)1056*28f6c2f2SEnji Cooper TEST(MatcherMacroTest, CanExplainMatchResult) {
1057*28f6c2f2SEnji Cooper const Matcher<int> m1 = IsEven2();
1058*28f6c2f2SEnji Cooper EXPECT_EQ("OK", Explain(m1, 4));
1059*28f6c2f2SEnji Cooper EXPECT_EQ("% 2 == 1", Explain(m1, 5));
1060*28f6c2f2SEnji Cooper
1061*28f6c2f2SEnji Cooper const Matcher<int> m2 = EqSumOf(1, 2);
1062*28f6c2f2SEnji Cooper EXPECT_EQ("OK", Explain(m2, 3));
1063*28f6c2f2SEnji Cooper EXPECT_EQ("diff == -1", Explain(m2, 4));
1064*28f6c2f2SEnji Cooper }
1065*28f6c2f2SEnji Cooper
1066*28f6c2f2SEnji Cooper // Tests that the body of MATCHER() can reference the type of the
1067*28f6c2f2SEnji Cooper // value being matched.
1068*28f6c2f2SEnji Cooper
1069*28f6c2f2SEnji Cooper MATCHER(IsEmptyString, "") {
1070*28f6c2f2SEnji Cooper StaticAssertTypeEq<::std::string, arg_type>();
1071*28f6c2f2SEnji Cooper return arg.empty();
1072*28f6c2f2SEnji Cooper }
1073*28f6c2f2SEnji Cooper
1074*28f6c2f2SEnji Cooper MATCHER(IsEmptyStringByRef, "") {
1075*28f6c2f2SEnji Cooper StaticAssertTypeEq<const ::std::string&, arg_type>();
1076*28f6c2f2SEnji Cooper return arg.empty();
1077*28f6c2f2SEnji Cooper }
1078*28f6c2f2SEnji Cooper
TEST(MatcherMacroTest,CanReferenceArgType)1079*28f6c2f2SEnji Cooper TEST(MatcherMacroTest, CanReferenceArgType) {
1080*28f6c2f2SEnji Cooper const Matcher<::std::string> m1 = IsEmptyString();
1081*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(""));
1082*28f6c2f2SEnji Cooper
1083*28f6c2f2SEnji Cooper const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
1084*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(""));
1085*28f6c2f2SEnji Cooper }
1086*28f6c2f2SEnji Cooper
1087*28f6c2f2SEnji Cooper // Tests that MATCHER() can be used in a namespace.
1088*28f6c2f2SEnji Cooper
1089*28f6c2f2SEnji Cooper namespace matcher_test {
1090*28f6c2f2SEnji Cooper MATCHER(IsOdd, "") { return (arg % 2) != 0; }
1091*28f6c2f2SEnji Cooper } // namespace matcher_test
1092*28f6c2f2SEnji Cooper
TEST(MatcherMacroTest,WorksInNamespace)1093*28f6c2f2SEnji Cooper TEST(MatcherMacroTest, WorksInNamespace) {
1094*28f6c2f2SEnji Cooper Matcher<int> m = matcher_test::IsOdd();
1095*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(4));
1096*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(5));
1097*28f6c2f2SEnji Cooper }
1098*28f6c2f2SEnji Cooper
1099*28f6c2f2SEnji Cooper // Tests that Value() can be used to compose matchers.
1100*28f6c2f2SEnji Cooper MATCHER(IsPositiveOdd, "") {
1101*28f6c2f2SEnji Cooper return Value(arg, matcher_test::IsOdd()) && arg > 0;
1102*28f6c2f2SEnji Cooper }
1103*28f6c2f2SEnji Cooper
TEST(MatcherMacroTest,CanBeComposedUsingValue)1104*28f6c2f2SEnji Cooper TEST(MatcherMacroTest, CanBeComposedUsingValue) {
1105*28f6c2f2SEnji Cooper EXPECT_THAT(3, IsPositiveOdd());
1106*28f6c2f2SEnji Cooper EXPECT_THAT(4, Not(IsPositiveOdd()));
1107*28f6c2f2SEnji Cooper EXPECT_THAT(-1, Not(IsPositiveOdd()));
1108*28f6c2f2SEnji Cooper }
1109*28f6c2f2SEnji Cooper
1110*28f6c2f2SEnji Cooper // Tests that a simple MATCHER_P() definition works.
1111*28f6c2f2SEnji Cooper
1112*28f6c2f2SEnji Cooper MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
1113*28f6c2f2SEnji Cooper
TEST(MatcherPMacroTest,Works)1114*28f6c2f2SEnji Cooper TEST(MatcherPMacroTest, Works) {
1115*28f6c2f2SEnji Cooper const Matcher<int> m = IsGreaterThan32And(5);
1116*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(36));
1117*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(5));
1118*28f6c2f2SEnji Cooper
1119*28f6c2f2SEnji Cooper EXPECT_EQ("is greater than 32 and (n: 5)", Describe(m));
1120*28f6c2f2SEnji Cooper EXPECT_EQ("not (is greater than 32 and (n: 5))", DescribeNegation(m));
1121*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 36));
1122*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 5));
1123*28f6c2f2SEnji Cooper }
1124*28f6c2f2SEnji Cooper
1125*28f6c2f2SEnji Cooper // Tests that the description is calculated correctly from the matcher name.
1126*28f6c2f2SEnji Cooper MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
1127*28f6c2f2SEnji Cooper
TEST(MatcherPMacroTest,GeneratesCorrectDescription)1128*28f6c2f2SEnji Cooper TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
1129*28f6c2f2SEnji Cooper const Matcher<int> m = _is_Greater_Than32and_(5);
1130*28f6c2f2SEnji Cooper
1131*28f6c2f2SEnji Cooper EXPECT_EQ("is greater than 32 and (n: 5)", Describe(m));
1132*28f6c2f2SEnji Cooper EXPECT_EQ("not (is greater than 32 and (n: 5))", DescribeNegation(m));
1133*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 36));
1134*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 5));
1135*28f6c2f2SEnji Cooper }
1136*28f6c2f2SEnji Cooper
1137*28f6c2f2SEnji Cooper // Tests that a MATCHER_P matcher can be explicitly instantiated with
1138*28f6c2f2SEnji Cooper // a reference parameter type.
1139*28f6c2f2SEnji Cooper
1140*28f6c2f2SEnji Cooper class UncopyableFoo {
1141*28f6c2f2SEnji Cooper public:
UncopyableFoo(char value)1142*28f6c2f2SEnji Cooper explicit UncopyableFoo(char value) : value_(value) { (void)value_; }
1143*28f6c2f2SEnji Cooper
1144*28f6c2f2SEnji Cooper UncopyableFoo(const UncopyableFoo&) = delete;
1145*28f6c2f2SEnji Cooper void operator=(const UncopyableFoo&) = delete;
1146*28f6c2f2SEnji Cooper
1147*28f6c2f2SEnji Cooper private:
1148*28f6c2f2SEnji Cooper char value_;
1149*28f6c2f2SEnji Cooper };
1150*28f6c2f2SEnji Cooper
1151*28f6c2f2SEnji Cooper MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
1152*28f6c2f2SEnji Cooper
TEST(MatcherPMacroTest,WorksWhenExplicitlyInstantiatedWithReference)1153*28f6c2f2SEnji Cooper TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
1154*28f6c2f2SEnji Cooper UncopyableFoo foo1('1'), foo2('2');
1155*28f6c2f2SEnji Cooper const Matcher<const UncopyableFoo&> m =
1156*28f6c2f2SEnji Cooper ReferencesUncopyable<const UncopyableFoo&>(foo1);
1157*28f6c2f2SEnji Cooper
1158*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(foo1));
1159*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(foo2));
1160*28f6c2f2SEnji Cooper
1161*28f6c2f2SEnji Cooper // We don't want the address of the parameter printed, as most
1162*28f6c2f2SEnji Cooper // likely it will just annoy the user. If the address is
1163*28f6c2f2SEnji Cooper // interesting, the user should consider passing the parameter by
1164*28f6c2f2SEnji Cooper // pointer instead.
1165*28f6c2f2SEnji Cooper EXPECT_EQ("references uncopyable (variable: 1-byte object <31>)",
1166*28f6c2f2SEnji Cooper Describe(m));
1167*28f6c2f2SEnji Cooper }
1168*28f6c2f2SEnji Cooper
1169*28f6c2f2SEnji Cooper // Tests that the body of MATCHER_Pn() can reference the parameter
1170*28f6c2f2SEnji Cooper // types.
1171*28f6c2f2SEnji Cooper
1172*28f6c2f2SEnji Cooper MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
1173*28f6c2f2SEnji Cooper StaticAssertTypeEq<int, foo_type>();
1174*28f6c2f2SEnji Cooper StaticAssertTypeEq<long, bar_type>(); // NOLINT
1175*28f6c2f2SEnji Cooper StaticAssertTypeEq<char, baz_type>();
1176*28f6c2f2SEnji Cooper return arg == 0;
1177*28f6c2f2SEnji Cooper }
1178*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,CanReferenceParamTypes)1179*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
1180*28f6c2f2SEnji Cooper EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
1181*28f6c2f2SEnji Cooper }
1182*28f6c2f2SEnji Cooper
1183*28f6c2f2SEnji Cooper // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
1184*28f6c2f2SEnji Cooper // reference parameter types.
1185*28f6c2f2SEnji Cooper
1186*28f6c2f2SEnji Cooper MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
1187*28f6c2f2SEnji Cooper return &arg == &variable1 || &arg == &variable2;
1188*28f6c2f2SEnji Cooper }
1189*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,WorksWhenExplicitlyInstantiatedWithReferences)1190*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
1191*28f6c2f2SEnji Cooper UncopyableFoo foo1('1'), foo2('2'), foo3('3');
1192*28f6c2f2SEnji Cooper const Matcher<const UncopyableFoo&> const_m =
1193*28f6c2f2SEnji Cooper ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
1194*28f6c2f2SEnji Cooper
1195*28f6c2f2SEnji Cooper EXPECT_TRUE(const_m.Matches(foo1));
1196*28f6c2f2SEnji Cooper EXPECT_TRUE(const_m.Matches(foo2));
1197*28f6c2f2SEnji Cooper EXPECT_FALSE(const_m.Matches(foo3));
1198*28f6c2f2SEnji Cooper
1199*28f6c2f2SEnji Cooper const Matcher<UncopyableFoo&> m =
1200*28f6c2f2SEnji Cooper ReferencesAnyOf<UncopyableFoo&, UncopyableFoo&>(foo1, foo2);
1201*28f6c2f2SEnji Cooper
1202*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(foo1));
1203*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(foo2));
1204*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(foo3));
1205*28f6c2f2SEnji Cooper }
1206*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences)1207*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest,
1208*28f6c2f2SEnji Cooper GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
1209*28f6c2f2SEnji Cooper UncopyableFoo foo1('1'), foo2('2');
1210*28f6c2f2SEnji Cooper const Matcher<const UncopyableFoo&> m =
1211*28f6c2f2SEnji Cooper ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
1212*28f6c2f2SEnji Cooper
1213*28f6c2f2SEnji Cooper // We don't want the addresses of the parameters printed, as most
1214*28f6c2f2SEnji Cooper // likely they will just annoy the user. If the addresses are
1215*28f6c2f2SEnji Cooper // interesting, the user should consider passing the parameters by
1216*28f6c2f2SEnji Cooper // pointers instead.
1217*28f6c2f2SEnji Cooper EXPECT_EQ(
1218*28f6c2f2SEnji Cooper "references any of (variable1: 1-byte object <31>, variable2: 1-byte "
1219*28f6c2f2SEnji Cooper "object <32>)",
1220*28f6c2f2SEnji Cooper Describe(m));
1221*28f6c2f2SEnji Cooper }
1222*28f6c2f2SEnji Cooper
1223*28f6c2f2SEnji Cooper // Tests that a simple MATCHER_P2() definition works.
1224*28f6c2f2SEnji Cooper
1225*28f6c2f2SEnji Cooper MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
1226*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,Works)1227*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, Works) {
1228*28f6c2f2SEnji Cooper const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
1229*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(36L));
1230*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(15L));
1231*28f6c2f2SEnji Cooper
1232*28f6c2f2SEnji Cooper EXPECT_EQ("is not in closed range (low: 10, hi: 20)", Describe(m));
1233*28f6c2f2SEnji Cooper EXPECT_EQ("not (is not in closed range (low: 10, hi: 20))",
1234*28f6c2f2SEnji Cooper DescribeNegation(m));
1235*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 36L));
1236*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 15L));
1237*28f6c2f2SEnji Cooper }
1238*28f6c2f2SEnji Cooper
1239*28f6c2f2SEnji Cooper // Tests that MATCHER*() definitions can be overloaded on the number
1240*28f6c2f2SEnji Cooper // of parameters; also tests MATCHER_Pn() where n >= 3.
1241*28f6c2f2SEnji Cooper
1242*28f6c2f2SEnji Cooper MATCHER(EqualsSumOf, "") { return arg == 0; }
1243*28f6c2f2SEnji Cooper MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
1244*28f6c2f2SEnji Cooper MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
1245*28f6c2f2SEnji Cooper MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
1246*28f6c2f2SEnji Cooper MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
1247*28f6c2f2SEnji Cooper MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
1248*28f6c2f2SEnji Cooper MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
1249*28f6c2f2SEnji Cooper return arg == a + b + c + d + e + f;
1250*28f6c2f2SEnji Cooper }
1251*28f6c2f2SEnji Cooper MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
1252*28f6c2f2SEnji Cooper return arg == a + b + c + d + e + f + g;
1253*28f6c2f2SEnji Cooper }
1254*28f6c2f2SEnji Cooper MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
1255*28f6c2f2SEnji Cooper return arg == a + b + c + d + e + f + g + h;
1256*28f6c2f2SEnji Cooper }
1257*28f6c2f2SEnji Cooper MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
1258*28f6c2f2SEnji Cooper return arg == a + b + c + d + e + f + g + h + i;
1259*28f6c2f2SEnji Cooper }
1260*28f6c2f2SEnji Cooper MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
1261*28f6c2f2SEnji Cooper return arg == a + b + c + d + e + f + g + h + i + j;
1262*28f6c2f2SEnji Cooper }
1263*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,CanBeOverloadedOnNumberOfParameters)1264*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
1265*28f6c2f2SEnji Cooper EXPECT_THAT(0, EqualsSumOf());
1266*28f6c2f2SEnji Cooper EXPECT_THAT(1, EqualsSumOf(1));
1267*28f6c2f2SEnji Cooper EXPECT_THAT(12, EqualsSumOf(10, 2));
1268*28f6c2f2SEnji Cooper EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
1269*28f6c2f2SEnji Cooper EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
1270*28f6c2f2SEnji Cooper EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
1271*28f6c2f2SEnji Cooper EXPECT_THAT("abcdef",
1272*28f6c2f2SEnji Cooper EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
1273*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefg",
1274*28f6c2f2SEnji Cooper EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
1275*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefgh", EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e",
1276*28f6c2f2SEnji Cooper 'f', 'g', "h"));
1277*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefghi", EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e",
1278*28f6c2f2SEnji Cooper 'f', 'g', "h", 'i'));
1279*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefghij",
1280*28f6c2f2SEnji Cooper EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', "h",
1281*28f6c2f2SEnji Cooper 'i', ::std::string("j")));
1282*28f6c2f2SEnji Cooper
1283*28f6c2f2SEnji Cooper EXPECT_THAT(1, Not(EqualsSumOf()));
1284*28f6c2f2SEnji Cooper EXPECT_THAT(-1, Not(EqualsSumOf(1)));
1285*28f6c2f2SEnji Cooper EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
1286*28f6c2f2SEnji Cooper EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
1287*28f6c2f2SEnji Cooper EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
1288*28f6c2f2SEnji Cooper EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
1289*28f6c2f2SEnji Cooper EXPECT_THAT("abcdef ",
1290*28f6c2f2SEnji Cooper Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
1291*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefg ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d",
1292*28f6c2f2SEnji Cooper "e", 'f', 'g')));
1293*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefgh ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d",
1294*28f6c2f2SEnji Cooper "e", 'f', 'g', "h")));
1295*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefghi ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d",
1296*28f6c2f2SEnji Cooper "e", 'f', 'g', "h", 'i')));
1297*28f6c2f2SEnji Cooper EXPECT_THAT("abcdefghij ",
1298*28f6c2f2SEnji Cooper Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1299*28f6c2f2SEnji Cooper "h", 'i', ::std::string("j"))));
1300*28f6c2f2SEnji Cooper }
1301*28f6c2f2SEnji Cooper
1302*28f6c2f2SEnji Cooper // Tests that a MATCHER_Pn() definition can be instantiated with any
1303*28f6c2f2SEnji Cooper // compatible parameter types.
TEST(MatcherPnMacroTest,WorksForDifferentParameterTypes)1304*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
1305*28f6c2f2SEnji Cooper EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
1306*28f6c2f2SEnji Cooper EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
1307*28f6c2f2SEnji Cooper
1308*28f6c2f2SEnji Cooper EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
1309*28f6c2f2SEnji Cooper EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
1310*28f6c2f2SEnji Cooper }
1311*28f6c2f2SEnji Cooper
1312*28f6c2f2SEnji Cooper // Tests that the matcher body can promote the parameter types.
1313*28f6c2f2SEnji Cooper
1314*28f6c2f2SEnji Cooper MATCHER_P2(EqConcat, prefix, suffix, "") {
1315*28f6c2f2SEnji Cooper // The following lines promote the two parameters to desired types.
1316*28f6c2f2SEnji Cooper std::string prefix_str(prefix);
1317*28f6c2f2SEnji Cooper char suffix_char = static_cast<char>(suffix);
1318*28f6c2f2SEnji Cooper return arg == prefix_str + suffix_char;
1319*28f6c2f2SEnji Cooper }
1320*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,SimpleTypePromotion)1321*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, SimpleTypePromotion) {
1322*28f6c2f2SEnji Cooper Matcher<std::string> no_promo = EqConcat(std::string("foo"), 't');
1323*28f6c2f2SEnji Cooper Matcher<const std::string&> promo = EqConcat("foo", static_cast<int>('t'));
1324*28f6c2f2SEnji Cooper EXPECT_FALSE(no_promo.Matches("fool"));
1325*28f6c2f2SEnji Cooper EXPECT_FALSE(promo.Matches("fool"));
1326*28f6c2f2SEnji Cooper EXPECT_TRUE(no_promo.Matches("foot"));
1327*28f6c2f2SEnji Cooper EXPECT_TRUE(promo.Matches("foot"));
1328*28f6c2f2SEnji Cooper }
1329*28f6c2f2SEnji Cooper
1330*28f6c2f2SEnji Cooper // Verifies the type of a MATCHER*.
1331*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,TypesAreCorrect)1332*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, TypesAreCorrect) {
1333*28f6c2f2SEnji Cooper // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1334*28f6c2f2SEnji Cooper EqualsSumOfMatcher a0 = EqualsSumOf();
1335*28f6c2f2SEnji Cooper
1336*28f6c2f2SEnji Cooper // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1337*28f6c2f2SEnji Cooper EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1338*28f6c2f2SEnji Cooper
1339*28f6c2f2SEnji Cooper // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1340*28f6c2f2SEnji Cooper // variable, and so on.
1341*28f6c2f2SEnji Cooper EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
1342*28f6c2f2SEnji Cooper EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
1343*28f6c2f2SEnji Cooper EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
1344*28f6c2f2SEnji Cooper EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
1345*28f6c2f2SEnji Cooper EqualsSumOf(1, 2, 3, 4, '5');
1346*28f6c2f2SEnji Cooper EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
1347*28f6c2f2SEnji Cooper EqualsSumOf(1, 2, 3, 4, 5, '6');
1348*28f6c2f2SEnji Cooper EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
1349*28f6c2f2SEnji Cooper EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
1350*28f6c2f2SEnji Cooper EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
1351*28f6c2f2SEnji Cooper EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
1352*28f6c2f2SEnji Cooper EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
1353*28f6c2f2SEnji Cooper EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1354*28f6c2f2SEnji Cooper EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1355*28f6c2f2SEnji Cooper EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1356*28f6c2f2SEnji Cooper
1357*28f6c2f2SEnji Cooper // Avoid "unused variable" warnings.
1358*28f6c2f2SEnji Cooper (void)a0;
1359*28f6c2f2SEnji Cooper (void)a1;
1360*28f6c2f2SEnji Cooper (void)a2;
1361*28f6c2f2SEnji Cooper (void)a3;
1362*28f6c2f2SEnji Cooper (void)a4;
1363*28f6c2f2SEnji Cooper (void)a5;
1364*28f6c2f2SEnji Cooper (void)a6;
1365*28f6c2f2SEnji Cooper (void)a7;
1366*28f6c2f2SEnji Cooper (void)a8;
1367*28f6c2f2SEnji Cooper (void)a9;
1368*28f6c2f2SEnji Cooper (void)a10;
1369*28f6c2f2SEnji Cooper }
1370*28f6c2f2SEnji Cooper
1371*28f6c2f2SEnji Cooper // Tests that matcher-typed parameters can be used in Value() inside a
1372*28f6c2f2SEnji Cooper // MATCHER_Pn definition.
1373*28f6c2f2SEnji Cooper
1374*28f6c2f2SEnji Cooper // Succeeds if arg matches exactly 2 of the 3 matchers.
1375*28f6c2f2SEnji Cooper MATCHER_P3(TwoOf, m1, m2, m3, "") {
1376*28f6c2f2SEnji Cooper const int count = static_cast<int>(Value(arg, m1)) +
1377*28f6c2f2SEnji Cooper static_cast<int>(Value(arg, m2)) +
1378*28f6c2f2SEnji Cooper static_cast<int>(Value(arg, m3));
1379*28f6c2f2SEnji Cooper return count == 2;
1380*28f6c2f2SEnji Cooper }
1381*28f6c2f2SEnji Cooper
TEST(MatcherPnMacroTest,CanUseMatcherTypedParameterInValue)1382*28f6c2f2SEnji Cooper TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1383*28f6c2f2SEnji Cooper EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1384*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1385*28f6c2f2SEnji Cooper }
1386*28f6c2f2SEnji Cooper
1387*28f6c2f2SEnji Cooper // Tests Contains().Times().
1388*28f6c2f2SEnji Cooper
1389*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(ContainsTimes);
1390*28f6c2f2SEnji Cooper
TEST(ContainsTimes,ListMatchesWhenElementQuantityMatches)1391*28f6c2f2SEnji Cooper TEST(ContainsTimes, ListMatchesWhenElementQuantityMatches) {
1392*28f6c2f2SEnji Cooper list<int> some_list;
1393*28f6c2f2SEnji Cooper some_list.push_back(3);
1394*28f6c2f2SEnji Cooper some_list.push_back(1);
1395*28f6c2f2SEnji Cooper some_list.push_back(2);
1396*28f6c2f2SEnji Cooper some_list.push_back(3);
1397*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(3).Times(2));
1398*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(2).Times(1));
1399*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(Ge(2)).Times(3));
1400*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(Ge(2)).Times(Gt(2)));
1401*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(4).Times(0));
1402*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(_).Times(4));
1403*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Not(Contains(5).Times(1)));
1404*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(5).Times(_)); // Times(_) always matches
1405*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Not(Contains(3).Times(1)));
1406*28f6c2f2SEnji Cooper EXPECT_THAT(some_list, Contains(3).Times(Not(1)));
1407*28f6c2f2SEnji Cooper EXPECT_THAT(list<int>{}, Not(Contains(_)));
1408*28f6c2f2SEnji Cooper }
1409*28f6c2f2SEnji Cooper
TEST_P(ContainsTimesP,ExplainsMatchResultCorrectly)1410*28f6c2f2SEnji Cooper TEST_P(ContainsTimesP, ExplainsMatchResultCorrectly) {
1411*28f6c2f2SEnji Cooper const int a[2] = {1, 2};
1412*28f6c2f2SEnji Cooper Matcher<const int(&)[2]> m = Contains(2).Times(3);
1413*28f6c2f2SEnji Cooper EXPECT_EQ(
1414*28f6c2f2SEnji Cooper "whose element #1 matches but whose match quantity of 1 does not match",
1415*28f6c2f2SEnji Cooper Explain(m, a));
1416*28f6c2f2SEnji Cooper
1417*28f6c2f2SEnji Cooper m = Contains(3).Times(0);
1418*28f6c2f2SEnji Cooper EXPECT_EQ("has no element that matches and whose match quantity of 0 matches",
1419*28f6c2f2SEnji Cooper Explain(m, a));
1420*28f6c2f2SEnji Cooper
1421*28f6c2f2SEnji Cooper m = Contains(3).Times(4);
1422*28f6c2f2SEnji Cooper EXPECT_EQ(
1423*28f6c2f2SEnji Cooper "has no element that matches and whose match quantity of 0 does not "
1424*28f6c2f2SEnji Cooper "match",
1425*28f6c2f2SEnji Cooper Explain(m, a));
1426*28f6c2f2SEnji Cooper
1427*28f6c2f2SEnji Cooper m = Contains(2).Times(4);
1428*28f6c2f2SEnji Cooper EXPECT_EQ(
1429*28f6c2f2SEnji Cooper "whose element #1 matches but whose match quantity of 1 does not "
1430*28f6c2f2SEnji Cooper "match",
1431*28f6c2f2SEnji Cooper Explain(m, a));
1432*28f6c2f2SEnji Cooper
1433*28f6c2f2SEnji Cooper m = Contains(GreaterThan(0)).Times(2);
1434*28f6c2f2SEnji Cooper EXPECT_EQ("whose elements (0, 1) match and whose match quantity of 2 matches",
1435*28f6c2f2SEnji Cooper Explain(m, a));
1436*28f6c2f2SEnji Cooper
1437*28f6c2f2SEnji Cooper m = Contains(GreaterThan(10)).Times(Gt(1));
1438*28f6c2f2SEnji Cooper EXPECT_EQ(
1439*28f6c2f2SEnji Cooper "has no element that matches and whose match quantity of 0 does not "
1440*28f6c2f2SEnji Cooper "match",
1441*28f6c2f2SEnji Cooper Explain(m, a));
1442*28f6c2f2SEnji Cooper
1443*28f6c2f2SEnji Cooper m = Contains(GreaterThan(0)).Times(GreaterThan<size_t>(5));
1444*28f6c2f2SEnji Cooper EXPECT_EQ(
1445*28f6c2f2SEnji Cooper "whose elements (0, 1) match but whose match quantity of 2 does not "
1446*28f6c2f2SEnji Cooper "match, which is 3 less than 5",
1447*28f6c2f2SEnji Cooper Explain(m, a));
1448*28f6c2f2SEnji Cooper }
1449*28f6c2f2SEnji Cooper
TEST(ContainsTimes,DescribesItselfCorrectly)1450*28f6c2f2SEnji Cooper TEST(ContainsTimes, DescribesItselfCorrectly) {
1451*28f6c2f2SEnji Cooper Matcher<vector<int>> m = Contains(1).Times(2);
1452*28f6c2f2SEnji Cooper EXPECT_EQ("quantity of elements that match is equal to 1 is equal to 2",
1453*28f6c2f2SEnji Cooper Describe(m));
1454*28f6c2f2SEnji Cooper
1455*28f6c2f2SEnji Cooper Matcher<vector<int>> m2 = Not(m);
1456*28f6c2f2SEnji Cooper EXPECT_EQ("quantity of elements that match is equal to 1 isn't equal to 2",
1457*28f6c2f2SEnji Cooper Describe(m2));
1458*28f6c2f2SEnji Cooper }
1459*28f6c2f2SEnji Cooper
1460*28f6c2f2SEnji Cooper // Tests AllOfArray()
1461*28f6c2f2SEnji Cooper
TEST(AllOfArrayTest,BasicForms)1462*28f6c2f2SEnji Cooper TEST(AllOfArrayTest, BasicForms) {
1463*28f6c2f2SEnji Cooper // Iterator
1464*28f6c2f2SEnji Cooper std::vector<int> v0{};
1465*28f6c2f2SEnji Cooper std::vector<int> v1{1};
1466*28f6c2f2SEnji Cooper std::vector<int> v2{2, 3};
1467*28f6c2f2SEnji Cooper std::vector<int> v3{4, 4, 4};
1468*28f6c2f2SEnji Cooper EXPECT_THAT(0, AllOfArray(v0.begin(), v0.end()));
1469*28f6c2f2SEnji Cooper EXPECT_THAT(1, AllOfArray(v1.begin(), v1.end()));
1470*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end())));
1471*28f6c2f2SEnji Cooper EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end())));
1472*28f6c2f2SEnji Cooper EXPECT_THAT(4, AllOfArray(v3.begin(), v3.end()));
1473*28f6c2f2SEnji Cooper // Pointer + size
1474*28f6c2f2SEnji Cooper int ar[6] = {1, 2, 3, 4, 4, 4};
1475*28f6c2f2SEnji Cooper EXPECT_THAT(0, AllOfArray(ar, 0));
1476*28f6c2f2SEnji Cooper EXPECT_THAT(1, AllOfArray(ar, 1));
1477*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AllOfArray(ar, 1)));
1478*28f6c2f2SEnji Cooper EXPECT_THAT(3, Not(AllOfArray(ar + 1, 3)));
1479*28f6c2f2SEnji Cooper EXPECT_THAT(4, AllOfArray(ar + 3, 3));
1480*28f6c2f2SEnji Cooper // Array
1481*28f6c2f2SEnji Cooper // int ar0[0]; Not usable
1482*28f6c2f2SEnji Cooper int ar1[1] = {1};
1483*28f6c2f2SEnji Cooper int ar2[2] = {2, 3};
1484*28f6c2f2SEnji Cooper int ar3[3] = {4, 4, 4};
1485*28f6c2f2SEnji Cooper // EXPECT_THAT(0, Not(AllOfArray(ar0))); // Cannot work
1486*28f6c2f2SEnji Cooper EXPECT_THAT(1, AllOfArray(ar1));
1487*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AllOfArray(ar1)));
1488*28f6c2f2SEnji Cooper EXPECT_THAT(3, Not(AllOfArray(ar2)));
1489*28f6c2f2SEnji Cooper EXPECT_THAT(4, AllOfArray(ar3));
1490*28f6c2f2SEnji Cooper // Container
1491*28f6c2f2SEnji Cooper EXPECT_THAT(0, AllOfArray(v0));
1492*28f6c2f2SEnji Cooper EXPECT_THAT(1, AllOfArray(v1));
1493*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AllOfArray(v1)));
1494*28f6c2f2SEnji Cooper EXPECT_THAT(3, Not(AllOfArray(v2)));
1495*28f6c2f2SEnji Cooper EXPECT_THAT(4, AllOfArray(v3));
1496*28f6c2f2SEnji Cooper // Initializer
1497*28f6c2f2SEnji Cooper EXPECT_THAT(0, AllOfArray<int>({})); // Requires template arg.
1498*28f6c2f2SEnji Cooper EXPECT_THAT(1, AllOfArray({1}));
1499*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AllOfArray({1})));
1500*28f6c2f2SEnji Cooper EXPECT_THAT(3, Not(AllOfArray({2, 3})));
1501*28f6c2f2SEnji Cooper EXPECT_THAT(4, AllOfArray({4, 4, 4}));
1502*28f6c2f2SEnji Cooper }
1503*28f6c2f2SEnji Cooper
TEST(AllOfArrayTest,Matchers)1504*28f6c2f2SEnji Cooper TEST(AllOfArrayTest, Matchers) {
1505*28f6c2f2SEnji Cooper // vector
1506*28f6c2f2SEnji Cooper std::vector<Matcher<int>> matchers{Ge(1), Lt(2)};
1507*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(AllOfArray(matchers)));
1508*28f6c2f2SEnji Cooper EXPECT_THAT(1, AllOfArray(matchers));
1509*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AllOfArray(matchers)));
1510*28f6c2f2SEnji Cooper // initializer_list
1511*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(AllOfArray({Ge(0), Ge(1)})));
1512*28f6c2f2SEnji Cooper EXPECT_THAT(1, AllOfArray({Ge(0), Ge(1)}));
1513*28f6c2f2SEnji Cooper }
1514*28f6c2f2SEnji Cooper
1515*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfArrayTest);
1516*28f6c2f2SEnji Cooper
TEST(AnyOfArrayTest,BasicForms)1517*28f6c2f2SEnji Cooper TEST(AnyOfArrayTest, BasicForms) {
1518*28f6c2f2SEnji Cooper // Iterator
1519*28f6c2f2SEnji Cooper std::vector<int> v0{};
1520*28f6c2f2SEnji Cooper std::vector<int> v1{1};
1521*28f6c2f2SEnji Cooper std::vector<int> v2{2, 3};
1522*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end())));
1523*28f6c2f2SEnji Cooper EXPECT_THAT(1, AnyOfArray(v1.begin(), v1.end()));
1524*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end())));
1525*28f6c2f2SEnji Cooper EXPECT_THAT(3, AnyOfArray(v2.begin(), v2.end()));
1526*28f6c2f2SEnji Cooper EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end())));
1527*28f6c2f2SEnji Cooper // Pointer + size
1528*28f6c2f2SEnji Cooper int ar[3] = {1, 2, 3};
1529*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(AnyOfArray(ar, 0)));
1530*28f6c2f2SEnji Cooper EXPECT_THAT(1, AnyOfArray(ar, 1));
1531*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AnyOfArray(ar, 1)));
1532*28f6c2f2SEnji Cooper EXPECT_THAT(3, AnyOfArray(ar + 1, 2));
1533*28f6c2f2SEnji Cooper EXPECT_THAT(4, Not(AnyOfArray(ar + 1, 2)));
1534*28f6c2f2SEnji Cooper // Array
1535*28f6c2f2SEnji Cooper // int ar0[0]; Not usable
1536*28f6c2f2SEnji Cooper int ar1[1] = {1};
1537*28f6c2f2SEnji Cooper int ar2[2] = {2, 3};
1538*28f6c2f2SEnji Cooper // EXPECT_THAT(0, Not(AnyOfArray(ar0))); // Cannot work
1539*28f6c2f2SEnji Cooper EXPECT_THAT(1, AnyOfArray(ar1));
1540*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AnyOfArray(ar1)));
1541*28f6c2f2SEnji Cooper EXPECT_THAT(3, AnyOfArray(ar2));
1542*28f6c2f2SEnji Cooper EXPECT_THAT(4, Not(AnyOfArray(ar2)));
1543*28f6c2f2SEnji Cooper // Container
1544*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(AnyOfArray(v0)));
1545*28f6c2f2SEnji Cooper EXPECT_THAT(1, AnyOfArray(v1));
1546*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AnyOfArray(v1)));
1547*28f6c2f2SEnji Cooper EXPECT_THAT(3, AnyOfArray(v2));
1548*28f6c2f2SEnji Cooper EXPECT_THAT(4, Not(AnyOfArray(v2)));
1549*28f6c2f2SEnji Cooper // Initializer
1550*28f6c2f2SEnji Cooper EXPECT_THAT(0, Not(AnyOfArray<int>({}))); // Requires template arg.
1551*28f6c2f2SEnji Cooper EXPECT_THAT(1, AnyOfArray({1}));
1552*28f6c2f2SEnji Cooper EXPECT_THAT(2, Not(AnyOfArray({1})));
1553*28f6c2f2SEnji Cooper EXPECT_THAT(3, AnyOfArray({2, 3}));
1554*28f6c2f2SEnji Cooper EXPECT_THAT(4, Not(AnyOfArray({2, 3})));
1555*28f6c2f2SEnji Cooper }
1556*28f6c2f2SEnji Cooper
TEST(AnyOfArrayTest,Matchers)1557*28f6c2f2SEnji Cooper TEST(AnyOfArrayTest, Matchers) {
1558*28f6c2f2SEnji Cooper // We negate test AllOfArrayTest.Matchers.
1559*28f6c2f2SEnji Cooper // vector
1560*28f6c2f2SEnji Cooper std::vector<Matcher<int>> matchers{Lt(1), Ge(2)};
1561*28f6c2f2SEnji Cooper EXPECT_THAT(0, AnyOfArray(matchers));
1562*28f6c2f2SEnji Cooper EXPECT_THAT(1, Not(AnyOfArray(matchers)));
1563*28f6c2f2SEnji Cooper EXPECT_THAT(2, AnyOfArray(matchers));
1564*28f6c2f2SEnji Cooper // initializer_list
1565*28f6c2f2SEnji Cooper EXPECT_THAT(0, AnyOfArray({Lt(0), Lt(1)}));
1566*28f6c2f2SEnji Cooper EXPECT_THAT(1, Not(AllOfArray({Lt(0), Lt(1)})));
1567*28f6c2f2SEnji Cooper }
1568*28f6c2f2SEnji Cooper
TEST_P(AnyOfArrayTestP,ExplainsMatchResultCorrectly)1569*28f6c2f2SEnji Cooper TEST_P(AnyOfArrayTestP, ExplainsMatchResultCorrectly) {
1570*28f6c2f2SEnji Cooper // AnyOfArray and AllOfArray use the same underlying template-template,
1571*28f6c2f2SEnji Cooper // thus it is sufficient to test one here.
1572*28f6c2f2SEnji Cooper const std::vector<int> v0{};
1573*28f6c2f2SEnji Cooper const std::vector<int> v1{1};
1574*28f6c2f2SEnji Cooper const std::vector<int> v2{2, 3};
1575*28f6c2f2SEnji Cooper const Matcher<int> m0 = AnyOfArray(v0);
1576*28f6c2f2SEnji Cooper const Matcher<int> m1 = AnyOfArray(v1);
1577*28f6c2f2SEnji Cooper const Matcher<int> m2 = AnyOfArray(v2);
1578*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m0, 0));
1579*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m1, 1));
1580*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m1, 2));
1581*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m2, 3));
1582*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m2, 4));
1583*28f6c2f2SEnji Cooper EXPECT_EQ("()", Describe(m0));
1584*28f6c2f2SEnji Cooper EXPECT_EQ("(is equal to 1)", Describe(m1));
1585*28f6c2f2SEnji Cooper EXPECT_EQ("(is equal to 2) or (is equal to 3)", Describe(m2));
1586*28f6c2f2SEnji Cooper EXPECT_EQ("()", DescribeNegation(m0));
1587*28f6c2f2SEnji Cooper EXPECT_EQ("(isn't equal to 1)", DescribeNegation(m1));
1588*28f6c2f2SEnji Cooper EXPECT_EQ("(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2));
1589*28f6c2f2SEnji Cooper // Explain with matchers
1590*28f6c2f2SEnji Cooper const Matcher<int> g1 = AnyOfArray({GreaterThan(1)});
1591*28f6c2f2SEnji Cooper const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)});
1592*28f6c2f2SEnji Cooper // Explains the first positive match and all prior negative matches...
1593*28f6c2f2SEnji Cooper EXPECT_EQ("which is 1 less than 1", Explain(g1, 0));
1594*28f6c2f2SEnji Cooper EXPECT_EQ("which is the same as 1", Explain(g1, 1));
1595*28f6c2f2SEnji Cooper EXPECT_EQ("which is 1 more than 1", Explain(g1, 2));
1596*28f6c2f2SEnji Cooper EXPECT_EQ("which is 1 less than 1, and which is 2 less than 2",
1597*28f6c2f2SEnji Cooper Explain(g2, 0));
1598*28f6c2f2SEnji Cooper EXPECT_EQ("which is the same as 1, and which is 1 less than 2",
1599*28f6c2f2SEnji Cooper Explain(g2, 1));
1600*28f6c2f2SEnji Cooper EXPECT_EQ("which is 1 more than 1", // Only the first
1601*28f6c2f2SEnji Cooper Explain(g2, 2));
1602*28f6c2f2SEnji Cooper }
1603*28f6c2f2SEnji Cooper
1604*28f6c2f2SEnji Cooper MATCHER(IsNotNull, "") { return arg != nullptr; }
1605*28f6c2f2SEnji Cooper
1606*28f6c2f2SEnji Cooper // Verifies that a matcher defined using MATCHER() can work on
1607*28f6c2f2SEnji Cooper // move-only types.
TEST(MatcherMacroTest,WorksOnMoveOnlyType)1608*28f6c2f2SEnji Cooper TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
1609*28f6c2f2SEnji Cooper std::unique_ptr<int> p(new int(3));
1610*28f6c2f2SEnji Cooper EXPECT_THAT(p, IsNotNull());
1611*28f6c2f2SEnji Cooper EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
1612*28f6c2f2SEnji Cooper }
1613*28f6c2f2SEnji Cooper
1614*28f6c2f2SEnji Cooper MATCHER_P(UniquePointee, pointee, "") { return *arg == pointee; }
1615*28f6c2f2SEnji Cooper
1616*28f6c2f2SEnji Cooper // Verifies that a matcher defined using MATCHER_P*() can work on
1617*28f6c2f2SEnji Cooper // move-only types.
TEST(MatcherPMacroTest,WorksOnMoveOnlyType)1618*28f6c2f2SEnji Cooper TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
1619*28f6c2f2SEnji Cooper std::unique_ptr<int> p(new int(3));
1620*28f6c2f2SEnji Cooper EXPECT_THAT(p, UniquePointee(3));
1621*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(UniquePointee(2)));
1622*28f6c2f2SEnji Cooper }
1623*28f6c2f2SEnji Cooper
1624*28f6c2f2SEnji Cooper MATCHER(EnsureNoUnusedButMarkedUnusedWarning, "") { return (arg % 2) == 0; }
1625*28f6c2f2SEnji Cooper
TEST(MockMethodMockFunctionTest,EnsureNoUnusedButMarkedUnusedWarning)1626*28f6c2f2SEnji Cooper TEST(MockMethodMockFunctionTest, EnsureNoUnusedButMarkedUnusedWarning) {
1627*28f6c2f2SEnji Cooper #ifdef __clang__
1628*28f6c2f2SEnji Cooper #pragma clang diagnostic push
1629*28f6c2f2SEnji Cooper #pragma clang diagnostic error "-Wused-but-marked-unused"
1630*28f6c2f2SEnji Cooper #endif
1631*28f6c2f2SEnji Cooper // https://github.com/google/googletest/issues/4055
1632*28f6c2f2SEnji Cooper EXPECT_THAT(0, EnsureNoUnusedButMarkedUnusedWarning());
1633*28f6c2f2SEnji Cooper #ifdef __clang__
1634*28f6c2f2SEnji Cooper #pragma clang diagnostic pop
1635*28f6c2f2SEnji Cooper #endif
1636*28f6c2f2SEnji Cooper }
1637*28f6c2f2SEnji Cooper
1638*28f6c2f2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
1639*28f6c2f2SEnji Cooper
1640*28f6c2f2SEnji Cooper // std::function<void()> is used below for compatibility with older copies of
1641*28f6c2f2SEnji Cooper // GCC. Normally, a raw lambda is all that is needed.
1642*28f6c2f2SEnji Cooper
1643*28f6c2f2SEnji Cooper // Test that examples from documentation compile
TEST(ThrowsTest,Examples)1644*28f6c2f2SEnji Cooper TEST(ThrowsTest, Examples) {
1645*28f6c2f2SEnji Cooper EXPECT_THAT(
1646*28f6c2f2SEnji Cooper std::function<void()>([]() { throw std::runtime_error("message"); }),
1647*28f6c2f2SEnji Cooper Throws<std::runtime_error>());
1648*28f6c2f2SEnji Cooper
1649*28f6c2f2SEnji Cooper EXPECT_THAT(
1650*28f6c2f2SEnji Cooper std::function<void()>([]() { throw std::runtime_error("message"); }),
1651*28f6c2f2SEnji Cooper ThrowsMessage<std::runtime_error>(HasSubstr("message")));
1652*28f6c2f2SEnji Cooper }
1653*28f6c2f2SEnji Cooper
TEST(ThrowsTest,PrintsExceptionWhat)1654*28f6c2f2SEnji Cooper TEST(ThrowsTest, PrintsExceptionWhat) {
1655*28f6c2f2SEnji Cooper EXPECT_THAT(
1656*28f6c2f2SEnji Cooper std::function<void()>([]() { throw std::runtime_error("ABC123XYZ"); }),
1657*28f6c2f2SEnji Cooper ThrowsMessage<std::runtime_error>(HasSubstr("ABC123XYZ")));
1658*28f6c2f2SEnji Cooper }
1659*28f6c2f2SEnji Cooper
TEST(ThrowsTest,DoesNotGenerateDuplicateCatchClauseWarning)1660*28f6c2f2SEnji Cooper TEST(ThrowsTest, DoesNotGenerateDuplicateCatchClauseWarning) {
1661*28f6c2f2SEnji Cooper EXPECT_THAT(std::function<void()>([]() { throw std::exception(); }),
1662*28f6c2f2SEnji Cooper Throws<std::exception>());
1663*28f6c2f2SEnji Cooper }
1664*28f6c2f2SEnji Cooper
TEST(ThrowsTest,CallableExecutedExactlyOnce)1665*28f6c2f2SEnji Cooper TEST(ThrowsTest, CallableExecutedExactlyOnce) {
1666*28f6c2f2SEnji Cooper size_t a = 0;
1667*28f6c2f2SEnji Cooper
1668*28f6c2f2SEnji Cooper EXPECT_THAT(std::function<void()>([&a]() {
1669*28f6c2f2SEnji Cooper a++;
1670*28f6c2f2SEnji Cooper throw 10;
1671*28f6c2f2SEnji Cooper }),
1672*28f6c2f2SEnji Cooper Throws<int>());
1673*28f6c2f2SEnji Cooper EXPECT_EQ(a, 1u);
1674*28f6c2f2SEnji Cooper
1675*28f6c2f2SEnji Cooper EXPECT_THAT(std::function<void()>([&a]() {
1676*28f6c2f2SEnji Cooper a++;
1677*28f6c2f2SEnji Cooper throw std::runtime_error("message");
1678*28f6c2f2SEnji Cooper }),
1679*28f6c2f2SEnji Cooper Throws<std::runtime_error>());
1680*28f6c2f2SEnji Cooper EXPECT_EQ(a, 2u);
1681*28f6c2f2SEnji Cooper
1682*28f6c2f2SEnji Cooper EXPECT_THAT(std::function<void()>([&a]() {
1683*28f6c2f2SEnji Cooper a++;
1684*28f6c2f2SEnji Cooper throw std::runtime_error("message");
1685*28f6c2f2SEnji Cooper }),
1686*28f6c2f2SEnji Cooper ThrowsMessage<std::runtime_error>(HasSubstr("message")));
1687*28f6c2f2SEnji Cooper EXPECT_EQ(a, 3u);
1688*28f6c2f2SEnji Cooper
1689*28f6c2f2SEnji Cooper EXPECT_THAT(std::function<void()>([&a]() {
1690*28f6c2f2SEnji Cooper a++;
1691*28f6c2f2SEnji Cooper throw std::runtime_error("message");
1692*28f6c2f2SEnji Cooper }),
1693*28f6c2f2SEnji Cooper Throws<std::runtime_error>(
1694*28f6c2f2SEnji Cooper Property(&std::runtime_error::what, HasSubstr("message"))));
1695*28f6c2f2SEnji Cooper EXPECT_EQ(a, 4u);
1696*28f6c2f2SEnji Cooper }
1697*28f6c2f2SEnji Cooper
TEST(ThrowsTest,Describe)1698*28f6c2f2SEnji Cooper TEST(ThrowsTest, Describe) {
1699*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
1700*28f6c2f2SEnji Cooper std::stringstream ss;
1701*28f6c2f2SEnji Cooper matcher.DescribeTo(&ss);
1702*28f6c2f2SEnji Cooper auto explanation = ss.str();
1703*28f6c2f2SEnji Cooper EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
1704*28f6c2f2SEnji Cooper }
1705*28f6c2f2SEnji Cooper
TEST(ThrowsTest,Success)1706*28f6c2f2SEnji Cooper TEST(ThrowsTest, Success) {
1707*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
1708*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1709*28f6c2f2SEnji Cooper EXPECT_TRUE(matcher.MatchAndExplain(
1710*28f6c2f2SEnji Cooper []() { throw std::runtime_error("error message"); }, &listener));
1711*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
1712*28f6c2f2SEnji Cooper }
1713*28f6c2f2SEnji Cooper
TEST(ThrowsTest,FailWrongType)1714*28f6c2f2SEnji Cooper TEST(ThrowsTest, FailWrongType) {
1715*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
1716*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1717*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.MatchAndExplain(
1718*28f6c2f2SEnji Cooper []() { throw std::logic_error("error message"); }, &listener));
1719*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
1720*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
1721*28f6c2f2SEnji Cooper }
1722*28f6c2f2SEnji Cooper
TEST(ThrowsTest,FailWrongTypeNonStd)1723*28f6c2f2SEnji Cooper TEST(ThrowsTest, FailWrongTypeNonStd) {
1724*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
1725*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1726*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
1727*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(),
1728*28f6c2f2SEnji Cooper HasSubstr("throws an exception of an unknown type"));
1729*28f6c2f2SEnji Cooper }
1730*28f6c2f2SEnji Cooper
TEST(ThrowsTest,FailNoThrow)1731*28f6c2f2SEnji Cooper TEST(ThrowsTest, FailNoThrow) {
1732*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
1733*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1734*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.MatchAndExplain([]() { (void)0; }, &listener));
1735*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception"));
1736*28f6c2f2SEnji Cooper }
1737*28f6c2f2SEnji Cooper
1738*28f6c2f2SEnji Cooper class ThrowsPredicateTest
1739*28f6c2f2SEnji Cooper : public TestWithParam<Matcher<std::function<void()>>> {};
1740*28f6c2f2SEnji Cooper
TEST_P(ThrowsPredicateTest,Describe)1741*28f6c2f2SEnji Cooper TEST_P(ThrowsPredicateTest, Describe) {
1742*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = GetParam();
1743*28f6c2f2SEnji Cooper std::stringstream ss;
1744*28f6c2f2SEnji Cooper matcher.DescribeTo(&ss);
1745*28f6c2f2SEnji Cooper auto explanation = ss.str();
1746*28f6c2f2SEnji Cooper EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
1747*28f6c2f2SEnji Cooper EXPECT_THAT(explanation, HasSubstr("error message"));
1748*28f6c2f2SEnji Cooper }
1749*28f6c2f2SEnji Cooper
TEST_P(ThrowsPredicateTest,Success)1750*28f6c2f2SEnji Cooper TEST_P(ThrowsPredicateTest, Success) {
1751*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = GetParam();
1752*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1753*28f6c2f2SEnji Cooper EXPECT_TRUE(matcher.MatchAndExplain(
1754*28f6c2f2SEnji Cooper []() { throw std::runtime_error("error message"); }, &listener));
1755*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
1756*28f6c2f2SEnji Cooper }
1757*28f6c2f2SEnji Cooper
TEST_P(ThrowsPredicateTest,FailWrongType)1758*28f6c2f2SEnji Cooper TEST_P(ThrowsPredicateTest, FailWrongType) {
1759*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = GetParam();
1760*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1761*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.MatchAndExplain(
1762*28f6c2f2SEnji Cooper []() { throw std::logic_error("error message"); }, &listener));
1763*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
1764*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
1765*28f6c2f2SEnji Cooper }
1766*28f6c2f2SEnji Cooper
TEST_P(ThrowsPredicateTest,FailWrongTypeNonStd)1767*28f6c2f2SEnji Cooper TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) {
1768*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = GetParam();
1769*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1770*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
1771*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(),
1772*28f6c2f2SEnji Cooper HasSubstr("throws an exception of an unknown type"));
1773*28f6c2f2SEnji Cooper }
1774*28f6c2f2SEnji Cooper
TEST_P(ThrowsPredicateTest,FailNoThrow)1775*28f6c2f2SEnji Cooper TEST_P(ThrowsPredicateTest, FailNoThrow) {
1776*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = GetParam();
1777*28f6c2f2SEnji Cooper StringMatchResultListener listener;
1778*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.MatchAndExplain([]() {}, &listener));
1779*28f6c2f2SEnji Cooper EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception"));
1780*28f6c2f2SEnji Cooper }
1781*28f6c2f2SEnji Cooper
1782*28f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(
1783*28f6c2f2SEnji Cooper AllMessagePredicates, ThrowsPredicateTest,
1784*28f6c2f2SEnji Cooper Values(Matcher<std::function<void()>>(
1785*28f6c2f2SEnji Cooper ThrowsMessage<std::runtime_error>(HasSubstr("error message")))));
1786*28f6c2f2SEnji Cooper
1787*28f6c2f2SEnji Cooper // Tests that Throws<E1>(Matcher<E2>{}) compiles even when E2 != const E1&.
TEST(ThrowsPredicateCompilesTest,ExceptionMatcherAcceptsBroadType)1788*28f6c2f2SEnji Cooper TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) {
1789*28f6c2f2SEnji Cooper {
1790*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher =
1791*28f6c2f2SEnji Cooper ThrowsMessage<std::runtime_error>(HasSubstr("error message"));
1792*28f6c2f2SEnji Cooper EXPECT_TRUE(
1793*28f6c2f2SEnji Cooper matcher.Matches([]() { throw std::runtime_error("error message"); }));
1794*28f6c2f2SEnji Cooper EXPECT_FALSE(
1795*28f6c2f2SEnji Cooper matcher.Matches([]() { throw std::runtime_error("wrong message"); }));
1796*28f6c2f2SEnji Cooper }
1797*28f6c2f2SEnji Cooper
1798*28f6c2f2SEnji Cooper {
1799*28f6c2f2SEnji Cooper Matcher<uint64_t> inner = Eq(10);
1800*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher = Throws<uint32_t>(inner);
1801*28f6c2f2SEnji Cooper EXPECT_TRUE(matcher.Matches([]() { throw (uint32_t)10; }));
1802*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.Matches([]() { throw (uint32_t)11; }));
1803*28f6c2f2SEnji Cooper }
1804*28f6c2f2SEnji Cooper }
1805*28f6c2f2SEnji Cooper
1806*28f6c2f2SEnji Cooper // Tests that ThrowsMessage("message") is equivalent
1807*28f6c2f2SEnji Cooper // to ThrowsMessage(Eq<std::string>("message")).
TEST(ThrowsPredicateCompilesTest,MessageMatcherAcceptsNonMatcher)1808*28f6c2f2SEnji Cooper TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) {
1809*28f6c2f2SEnji Cooper Matcher<std::function<void()>> matcher =
1810*28f6c2f2SEnji Cooper ThrowsMessage<std::runtime_error>("error message");
1811*28f6c2f2SEnji Cooper EXPECT_TRUE(
1812*28f6c2f2SEnji Cooper matcher.Matches([]() { throw std::runtime_error("error message"); }));
1813*28f6c2f2SEnji Cooper EXPECT_FALSE(matcher.Matches(
1814*28f6c2f2SEnji Cooper []() { throw std::runtime_error("wrong error message"); }));
1815*28f6c2f2SEnji Cooper }
1816*28f6c2f2SEnji Cooper
1817*28f6c2f2SEnji Cooper #endif // GTEST_HAS_EXCEPTIONS
1818*28f6c2f2SEnji Cooper
1819*28f6c2f2SEnji Cooper } // namespace
1820*28f6c2f2SEnji Cooper } // namespace gmock_matchers_test
1821*28f6c2f2SEnji Cooper } // namespace testing
1822*28f6c2f2SEnji Cooper
1823*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100
1824