LEFT | RIGHT |
1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 #include <gmock/gmock-matchers.h> | 36 #include <gmock/gmock-matchers.h> |
37 | 37 |
38 #include <string.h> | 38 #include <string.h> |
39 #include <functional> | 39 #include <functional> |
40 #include <list> | 40 #include <list> |
41 #include <map> | 41 #include <map> |
42 #include <set> | 42 #include <set> |
43 #include <sstream> | 43 #include <sstream> |
44 #include <string> | 44 #include <string> |
| 45 #include <utility> |
45 #include <vector> | 46 #include <vector> |
46 #include <gmock/gmock.h> | 47 #include <gmock/gmock.h> |
47 #include <gtest/gtest.h> | 48 #include <gtest/gtest.h> |
48 #include <gtest/gtest-spi.h> | 49 #include <gtest/gtest-spi.h> |
49 | 50 |
50 namespace testing { | 51 namespace testing { |
51 | 52 |
52 namespace internal { | 53 namespace internal { |
53 string FormatMatcherDescriptionSyntaxError(const char* description, | 54 string FormatMatcherDescriptionSyntaxError(const char* description, |
54 const char* error_pos); | 55 const char* error_pos); |
(...skipping 29 matching lines...) Expand all Loading... |
84 using testing::MakePolymorphicMatcher; | 85 using testing::MakePolymorphicMatcher; |
85 using testing::Matcher; | 86 using testing::Matcher; |
86 using testing::MatcherCast; | 87 using testing::MatcherCast; |
87 using testing::MatcherInterface; | 88 using testing::MatcherInterface; |
88 using testing::Matches; | 89 using testing::Matches; |
89 using testing::NanSensitiveDoubleEq; | 90 using testing::NanSensitiveDoubleEq; |
90 using testing::NanSensitiveFloatEq; | 91 using testing::NanSensitiveFloatEq; |
91 using testing::Ne; | 92 using testing::Ne; |
92 using testing::Not; | 93 using testing::Not; |
93 using testing::NotNull; | 94 using testing::NotNull; |
| 95 using testing::Pair; |
94 using testing::Pointee; | 96 using testing::Pointee; |
95 using testing::PolymorphicMatcher; | 97 using testing::PolymorphicMatcher; |
96 using testing::Property; | 98 using testing::Property; |
97 using testing::Ref; | 99 using testing::Ref; |
98 using testing::ResultOf; | 100 using testing::ResultOf; |
99 using testing::StartsWith; | 101 using testing::StartsWith; |
100 using testing::StrCaseEq; | 102 using testing::StrCaseEq; |
101 using testing::StrCaseNe; | 103 using testing::StrCaseNe; |
102 using testing::StrEq; | 104 using testing::StrEq; |
103 using testing::StrNe; | 105 using testing::StrNe; |
(...skipping 15 matching lines...) Expand all Loading... |
119 using testing::internal::kPercentInterpolation; | 121 using testing::internal::kPercentInterpolation; |
120 using testing::internal::kTupleInterpolation; | 122 using testing::internal::kTupleInterpolation; |
121 using testing::internal::string; | 123 using testing::internal::string; |
122 | 124 |
123 #ifdef GMOCK_HAS_REGEX | 125 #ifdef GMOCK_HAS_REGEX |
124 using testing::ContainsRegex; | 126 using testing::ContainsRegex; |
125 using testing::MatchesRegex; | 127 using testing::MatchesRegex; |
126 using testing::internal::RE; | 128 using testing::internal::RE; |
127 #endif // GMOCK_HAS_REGEX | 129 #endif // GMOCK_HAS_REGEX |
128 | 130 |
| 131 // For testing ExplainMatchResultTo(). |
| 132 class GreaterThanMatcher : public MatcherInterface<int> { |
| 133 public: |
| 134 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
| 135 |
| 136 virtual bool Matches(int lhs) const { return lhs > rhs_; } |
| 137 |
| 138 virtual void DescribeTo(::std::ostream* os) const { |
| 139 *os << "is greater than " << rhs_; |
| 140 } |
| 141 |
| 142 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { |
| 143 const int diff = lhs - rhs_; |
| 144 if (diff > 0) { |
| 145 *os << "is " << diff << " more than " << rhs_; |
| 146 } else if (diff == 0) { |
| 147 *os << "is the same as " << rhs_; |
| 148 } else { |
| 149 *os << "is " << -diff << " less than " << rhs_; |
| 150 } |
| 151 } |
| 152 private: |
| 153 const int rhs_; |
| 154 }; |
| 155 |
| 156 Matcher<int> GreaterThan(int n) { |
| 157 return MakeMatcher(new GreaterThanMatcher(n)); |
| 158 } |
| 159 |
129 // Returns the description of the given matcher. | 160 // Returns the description of the given matcher. |
130 template <typename T> | 161 template <typename T> |
131 string Describe(const Matcher<T>& m) { | 162 string Describe(const Matcher<T>& m) { |
132 stringstream ss; | 163 stringstream ss; |
133 m.DescribeTo(&ss); | 164 m.DescribeTo(&ss); |
134 return ss.str(); | 165 return ss.str(); |
135 } | 166 } |
136 | 167 |
137 // Returns the description of the negation of the given matcher. | 168 // Returns the description of the negation of the given matcher. |
138 template <typename T> | 169 template <typename T> |
(...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 container.insert(std::make_pair(4, std::string("baz"))); | 923 container.insert(std::make_pair(4, std::string("baz"))); |
893 | 924 |
894 EXPECT_THAT(container, Not(Contains(Key(25)))); | 925 EXPECT_THAT(container, Not(Contains(Key(25)))); |
895 container.insert(std::make_pair(25, std::string("more foo"))); | 926 container.insert(std::make_pair(25, std::string("more foo"))); |
896 EXPECT_THAT(container, Contains(Key(25))); | 927 EXPECT_THAT(container, Contains(Key(25))); |
897 container.insert(std::make_pair(25, std::string("more bar"))); | 928 container.insert(std::make_pair(25, std::string("more bar"))); |
898 EXPECT_THAT(container, Contains(Key(25))); | 929 EXPECT_THAT(container, Contains(Key(25))); |
899 | 930 |
900 EXPECT_THAT(container, Contains(Key(1))); | 931 EXPECT_THAT(container, Contains(Key(1))); |
901 EXPECT_THAT(container, Not(Contains(Key(3)))); | 932 EXPECT_THAT(container, Not(Contains(Key(3)))); |
| 933 } |
| 934 |
| 935 TEST(PairTest, Typing) { |
| 936 // Test verifies the following type conversions can be compiled. |
| 937 Matcher<const std::pair<const char*, int>&> m1 = Pair("foo", 42); |
| 938 Matcher<const std::pair<const char*, int> > m2 = Pair("foo", 42); |
| 939 Matcher<std::pair<const char*, int> > m3 = Pair("foo", 42); |
| 940 |
| 941 Matcher<std::pair<int, const std::string> > m4 = Pair(25, "42"); |
| 942 Matcher<std::pair<const std::string, int> > m5 = Pair("25", 42); |
| 943 } |
| 944 |
| 945 TEST(PairTest, CanDescribeSelf) { |
| 946 Matcher<const std::pair<std::string, int>&> m1 = Pair("foo", 42); |
| 947 EXPECT_EQ("has a first field that is equal to \"foo\"" |
| 948 ", and has a second field that is equal to 42", |
| 949 Describe(m1)); |
| 950 EXPECT_EQ("has a first field that is not equal to \"foo\"" |
| 951 ", or has a second field that is not equal to 42", |
| 952 DescribeNegation(m1)); |
| 953 // Double and triple negation (1 or 2 times not and description of negation). |
| 954 Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42)); |
| 955 EXPECT_EQ("has a first field that is not equal to 13" |
| 956 ", and has a second field that is equal to 42", |
| 957 DescribeNegation(m2)); |
| 958 } |
| 959 |
| 960 TEST(PairTest, CanExplainMatchResultTo) { |
| 961 const Matcher<std::pair<int, int> > m0 = Pair(0, 0); |
| 962 EXPECT_EQ("", Explain(m0, std::make_pair(25, 42))); |
| 963 |
| 964 const Matcher<std::pair<int, int> > m1 = Pair(GreaterThan(0), 0); |
| 965 EXPECT_EQ("the first field is 25 more than 0", |
| 966 Explain(m1, std::make_pair(25, 42))); |
| 967 |
| 968 const Matcher<std::pair<int, int> > m2 = Pair(0, GreaterThan(0)); |
| 969 EXPECT_EQ("the second field is 42 more than 0", |
| 970 Explain(m2, std::make_pair(25, 42))); |
| 971 |
| 972 const Matcher<std::pair<int, int> > m3 = Pair(GreaterThan(0), GreaterThan(0)); |
| 973 EXPECT_EQ("the first field is 25 more than 0" |
| 974 ", and the second field is 42 more than 0", |
| 975 Explain(m3, std::make_pair(25, 42))); |
| 976 } |
| 977 |
| 978 TEST(PairTest, MatchesCorrectly) { |
| 979 std::pair<int, std::string> p(25, "foo"); |
| 980 |
| 981 // Both fields match. |
| 982 EXPECT_THAT(p, Pair(25, "foo")); |
| 983 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); |
| 984 |
| 985 // 'first' doesnt' match, but 'second' matches. |
| 986 EXPECT_THAT(p, Not(Pair(42, "foo"))); |
| 987 EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); |
| 988 |
| 989 // 'first' matches, but 'second' doesn't match. |
| 990 EXPECT_THAT(p, Not(Pair(25, "bar"))); |
| 991 EXPECT_THAT(p, Not(Pair(25, Not("foo")))); |
| 992 |
| 993 // Neither field matches. |
| 994 EXPECT_THAT(p, Not(Pair(13, "bar"))); |
| 995 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); |
| 996 } |
| 997 |
| 998 TEST(PairTest, SafelyCastsInnerMatchers) { |
| 999 Matcher<int> is_positive = Gt(0); |
| 1000 Matcher<int> is_negative = Lt(0); |
| 1001 std::pair<char, bool> p('a', true); |
| 1002 EXPECT_THAT(p, Pair(is_positive, _)); |
| 1003 EXPECT_THAT(p, Not(Pair(is_negative, _))); |
| 1004 EXPECT_THAT(p, Pair(_, is_positive)); |
| 1005 EXPECT_THAT(p, Not(Pair(_, is_negative))); |
| 1006 } |
| 1007 |
| 1008 TEST(PairTest, InsideContainsUsingMap) { |
| 1009 std::map<int, std::string> container; |
| 1010 container.insert(std::make_pair(1, std::string("foo"))); |
| 1011 container.insert(std::make_pair(2, std::string("bar"))); |
| 1012 container.insert(std::make_pair(4, std::string("baz"))); |
| 1013 EXPECT_THAT(container, Contains(Pair(1, "foo"))); |
| 1014 EXPECT_THAT(container, Contains(Pair(1, _))); |
| 1015 EXPECT_THAT(container, Contains(Pair(_, "foo"))); |
| 1016 EXPECT_THAT(container, Not(Contains(Pair(3, _)))); |
902 } | 1017 } |
903 | 1018 |
904 // Tests StartsWith(s). | 1019 // Tests StartsWith(s). |
905 | 1020 |
906 TEST(StartsWithTest, MatchesStringWithGivenPrefix) { | 1021 TEST(StartsWithTest, MatchesStringWithGivenPrefix) { |
907 const Matcher<const char*> m1 = StartsWith(string("")); | 1022 const Matcher<const char*> m1 = StartsWith(string("")); |
908 EXPECT_TRUE(m1.Matches("Hi")); | 1023 EXPECT_TRUE(m1.Matches("Hi")); |
909 EXPECT_TRUE(m1.Matches("")); | 1024 EXPECT_TRUE(m1.Matches("")); |
910 EXPECT_FALSE(m1.Matches(NULL)); | 1025 EXPECT_FALSE(m1.Matches(NULL)); |
911 | 1026 |
(...skipping 1247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2159 EXPECT_FALSE(m.Matches(NULL)); | 2274 EXPECT_FALSE(m.Matches(NULL)); |
2160 } | 2275 } |
2161 | 2276 |
2162 TEST(PointeeTest, CanDescribeSelf) { | 2277 TEST(PointeeTest, CanDescribeSelf) { |
2163 const Matcher<int*> m = Pointee(Gt(3)); | 2278 const Matcher<int*> m = Pointee(Gt(3)); |
2164 EXPECT_EQ("points to a value that is greater than 3", Describe(m)); | 2279 EXPECT_EQ("points to a value that is greater than 3", Describe(m)); |
2165 EXPECT_EQ("does not point to a value that is greater than 3", | 2280 EXPECT_EQ("does not point to a value that is greater than 3", |
2166 DescribeNegation(m)); | 2281 DescribeNegation(m)); |
2167 } | 2282 } |
2168 | 2283 |
2169 // For testing ExplainMatchResultTo(). | |
2170 class GreaterThanMatcher : public MatcherInterface<int> { | |
2171 public: | |
2172 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | |
2173 | |
2174 virtual bool Matches(int lhs) const { return lhs > rhs_; } | |
2175 | |
2176 virtual void DescribeTo(::std::ostream* os) const { | |
2177 *os << "is greater than " << rhs_; | |
2178 } | |
2179 | |
2180 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { | |
2181 const int diff = lhs - rhs_; | |
2182 if (diff > 0) { | |
2183 *os << "is " << diff << " more than " << rhs_; | |
2184 } else if (diff == 0) { | |
2185 *os << "is the same as " << rhs_; | |
2186 } else { | |
2187 *os << "is " << -diff << " less than " << rhs_; | |
2188 } | |
2189 } | |
2190 private: | |
2191 const int rhs_; | |
2192 }; | |
2193 | |
2194 Matcher<int> GreaterThan(int n) { | |
2195 return MakeMatcher(new GreaterThanMatcher(n)); | |
2196 } | |
2197 | |
2198 TEST(PointeeTest, CanExplainMatchResult) { | 2284 TEST(PointeeTest, CanExplainMatchResult) { |
2199 const Matcher<const string*> m = Pointee(StartsWith("Hi")); | 2285 const Matcher<const string*> m = Pointee(StartsWith("Hi")); |
2200 | 2286 |
2201 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); | 2287 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); |
2202 | 2288 |
2203 const Matcher<int*> m2 = Pointee(GreaterThan(1)); | 2289 const Matcher<int*> m2 = Pointee(GreaterThan(1)); |
2204 int n = 3; | 2290 int n = 3; |
2205 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n)); | 2291 EXPECT_EQ("points to a value that is 2 more than 1", Explain(m2, &n)); |
2206 } | 2292 } |
2207 | 2293 |
(...skipping 1172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3380 | 3466 |
3381 // Tests PolymorphicMatcher::impl(). | 3467 // Tests PolymorphicMatcher::impl(). |
3382 TEST(PolymorphicMatcherTest, CanAccessImpl) { | 3468 TEST(PolymorphicMatcherTest, CanAccessImpl) { |
3383 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); | 3469 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
3384 const DivisibleByImpl& impl = m.impl(); | 3470 const DivisibleByImpl& impl = m.impl(); |
3385 EXPECT_EQ(42, impl.divider()); | 3471 EXPECT_EQ(42, impl.divider()); |
3386 } | 3472 } |
3387 | 3473 |
3388 } // namespace gmock_matchers_test | 3474 } // namespace gmock_matchers_test |
3389 } // namespace testing | 3475 } // namespace testing |
LEFT | RIGHT |