Left: | ||
Right: |
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 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
869 TEST(KeyTest, SafelyCastsInnerMatcher) { | 900 TEST(KeyTest, SafelyCastsInnerMatcher) { |
870 Matcher<int> is_positive = Gt(0); | 901 Matcher<int> is_positive = Gt(0); |
871 Matcher<int> is_negative = Lt(0); | 902 Matcher<int> is_negative = Lt(0); |
872 std::pair<char, bool> p('a', true); | 903 std::pair<char, bool> p('a', true); |
873 EXPECT_THAT(p, Key(is_positive)); | 904 EXPECT_THAT(p, Key(is_positive)); |
874 EXPECT_THAT(p, Not(Key(is_negative))); | 905 EXPECT_THAT(p, Not(Key(is_negative))); |
875 } | 906 } |
876 | 907 |
877 TEST(KeyTest, InsideContainsUsingMap) { | 908 TEST(KeyTest, InsideContainsUsingMap) { |
878 std::map<int, std::string> container; | 909 std::map<int, std::string> container; |
879 // std::make_pair(1, "foo") gives | 910 // std::make_pair(1, "foo") gives 'illegal ctor initializer' |
Zhanyong Wan
2009/09/15 21:32:50
Can this comment fit on 2 lines?
mika.raento
2009/09/16 14:21:29
On 2009/09/15 21:32:50, Zhanyong Wan wrote:
Done.
| |
880 // illegal ctor initializer | 911 // on Nokia's Symbian compiler. |
881 // on Nokia's Symbian compiler | 912 container.insert(std::make_pair(1, std::string("foo"))); |
Zhanyong Wan
2009/09/15 21:32:50
End this with a period.
mika.raento
2009/09/16 14:21:29
On 2009/09/15 21:32:50, Zhanyong Wan wrote:
Done.
| |
882 container.insert(std::make_pair(1, string("foo"))); | 913 container.insert(std::make_pair(2, std::string("bar"))); |
Zhanyong Wan
2009/09/15 21:32:50
Use std::string instead of string.
mika.raento
2009/09/16 14:21:29
On 2009/09/15 21:32:50, Zhanyong Wan wrote:
Done.
| |
883 container.insert(std::make_pair(2, string("bar"))); | 914 container.insert(std::make_pair(4, std::string("baz"))); |
884 container.insert(std::make_pair(4, string("baz"))); | |
885 EXPECT_THAT(container, Contains(Key(1))); | 915 EXPECT_THAT(container, Contains(Key(1))); |
886 EXPECT_THAT(container, Not(Contains(Key(3)))); | 916 EXPECT_THAT(container, Not(Contains(Key(3)))); |
887 } | 917 } |
888 | 918 |
889 TEST(KeyTest, InsideContainsUsingMultimap) { | 919 TEST(KeyTest, InsideContainsUsingMultimap) { |
890 std::multimap<int, std::string> container; | 920 std::multimap<int, std::string> container; |
891 container.insert(std::make_pair(1, string("foo"))); | 921 container.insert(std::make_pair(1, std::string("foo"))); |
892 container.insert(std::make_pair(2, string("bar"))); | 922 container.insert(std::make_pair(2, std::string("bar"))); |
893 container.insert(std::make_pair(4, string("baz"))); | 923 container.insert(std::make_pair(4, std::string("baz"))); |
894 | 924 |
895 EXPECT_THAT(container, Not(Contains(Key(25)))); | 925 EXPECT_THAT(container, Not(Contains(Key(25)))); |
896 container.insert(std::make_pair(25, string("more foo"))); | 926 container.insert(std::make_pair(25, std::string("more foo"))); |
897 EXPECT_THAT(container, Contains(Key(25))); | 927 EXPECT_THAT(container, Contains(Key(25))); |
898 container.insert(std::make_pair(25, string("more bar"))); | 928 container.insert(std::make_pair(25, std::string("more bar"))); |
899 EXPECT_THAT(container, Contains(Key(25))); | 929 EXPECT_THAT(container, Contains(Key(25))); |
900 | 930 |
901 EXPECT_THAT(container, Contains(Key(1))); | 931 EXPECT_THAT(container, Contains(Key(1))); |
902 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, _)))); | |
903 } | 1017 } |
904 | 1018 |
905 // Tests StartsWith(s). | 1019 // Tests StartsWith(s). |
906 | 1020 |
907 TEST(StartsWithTest, MatchesStringWithGivenPrefix) { | 1021 TEST(StartsWithTest, MatchesStringWithGivenPrefix) { |
908 const Matcher<const char*> m1 = StartsWith(string("")); | 1022 const Matcher<const char*> m1 = StartsWith(string("")); |
909 EXPECT_TRUE(m1.Matches("Hi")); | 1023 EXPECT_TRUE(m1.Matches("Hi")); |
910 EXPECT_TRUE(m1.Matches("")); | 1024 EXPECT_TRUE(m1.Matches("")); |
911 EXPECT_FALSE(m1.Matches(NULL)); | 1025 EXPECT_FALSE(m1.Matches(NULL)); |
912 | 1026 |
(...skipping 906 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1819 "Expected: does not reference the variable @"); | 1933 "Expected: does not reference the variable @"); |
1820 // Tests the "Actual" part. | 1934 // Tests the "Actual" part. |
1821 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), | 1935 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), |
1822 "Actual: 0 (is located @"); | 1936 "Actual: 0 (is located @"); |
1823 } | 1937 } |
1824 | 1938 |
1825 #if !GTEST_OS_SYMBIAN | 1939 #if !GTEST_OS_SYMBIAN |
1826 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is | 1940 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is |
1827 // monomorphic. | 1941 // monomorphic. |
1828 | 1942 |
1829 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's | 1943 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's |
Zhanyong Wan
2009/09/15 21:32:50
Does it still fail with your change to SafeMatcher
mika.raento
2009/09/16 14:21:29
On 2009/09/15 21:32:50, Zhanyong Wan wrote:
Yeah.
| |
1830 // Symbian compiler: it tries to compile | 1944 // Symbian compiler: it tries to compile |
1831 // template<T, U> class MatcherCastImpl { ... | 1945 // template<T, U> class MatcherCastImpl { ... |
1832 // virtual bool Matches(T x) const { | 1946 // virtual bool Matches(T x) const { |
1833 // return source_matcher_.Matches(static_cast<U>(x)); | 1947 // return source_matcher_.Matches(static_cast<U>(x)); |
1834 // with U == string and T == const char* | 1948 // with U == string and T == const char* |
1835 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) | 1949 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) |
1836 // the compiler silently crashes with no output. | 1950 // the compiler silently crashes with no output. |
1837 // If MatcherCastImpl is changed to use U(x) in stead of static_cast<U>(x) | 1951 // If MatcherCastImpl is changed to use U(x) in stead of static_cast<U>(x) |
1838 // the code compiles but the converted string is bogus. | 1952 // the code compiles but the converted string is bogus. |
1839 | 1953 |
1840 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { | 1954 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { |
1841 Matcher<const char*> starts_with_he = StartsWith("he"); | 1955 Matcher<const char*> starts_with_he = StartsWith("he"); |
1842 ASSERT_THAT("hello", starts_with_he); | 1956 ASSERT_THAT("hello", starts_with_he); |
Zhanyong Wan
2009/09/15 21:32:50
Does this work on Symbian?
const char* s = "hel
mika.raento
2009/09/16 14:21:29
On 2009/09/15 21:32:50, Zhanyong Wan wrote:
No.
| |
1843 | 1957 |
1844 Matcher<const string&> ends_with_ok = EndsWith("ok"); | 1958 Matcher<const string&> ends_with_ok = EndsWith("ok"); |
1845 ASSERT_THAT("book", ends_with_ok); | 1959 ASSERT_THAT("book", ends_with_ok); |
1846 | 1960 |
1847 Matcher<int> is_greater_than_5 = Gt(5); | 1961 Matcher<int> is_greater_than_5 = Gt(5); |
1848 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), | 1962 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), |
1849 "Value of: 5\n" | 1963 "Value of: 5\n" |
1850 "Expected: is greater than 5\n" | 1964 "Expected: is greater than 5\n" |
1851 " Actual: 5"); | 1965 " Actual: 5"); |
1852 } | 1966 } |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2160 EXPECT_FALSE(m.Matches(NULL)); | 2274 EXPECT_FALSE(m.Matches(NULL)); |
2161 } | 2275 } |
2162 | 2276 |
2163 TEST(PointeeTest, CanDescribeSelf) { | 2277 TEST(PointeeTest, CanDescribeSelf) { |
2164 const Matcher<int*> m = Pointee(Gt(3)); | 2278 const Matcher<int*> m = Pointee(Gt(3)); |
2165 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)); |
2166 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", |
2167 DescribeNegation(m)); | 2281 DescribeNegation(m)); |
2168 } | 2282 } |
2169 | 2283 |
2170 // For testing ExplainMatchResultTo(). | |
2171 class GreaterThanMatcher : public MatcherInterface<int> { | |
2172 public: | |
2173 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | |
2174 | |
2175 virtual bool Matches(int lhs) const { return lhs > rhs_; } | |
2176 | |
2177 virtual void DescribeTo(::std::ostream* os) const { | |
2178 *os << "is greater than " << rhs_; | |
2179 } | |
2180 | |
2181 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { | |
2182 const int diff = lhs - rhs_; | |
2183 if (diff > 0) { | |
2184 *os << "is " << diff << " more than " << rhs_; | |
2185 } else if (diff == 0) { | |
2186 *os << "is the same as " << rhs_; | |
2187 } else { | |
2188 *os << "is " << -diff << " less than " << rhs_; | |
2189 } | |
2190 } | |
2191 private: | |
2192 const int rhs_; | |
2193 }; | |
2194 | |
2195 Matcher<int> GreaterThan(int n) { | |
2196 return MakeMatcher(new GreaterThanMatcher(n)); | |
2197 } | |
2198 | |
2199 TEST(PointeeTest, CanExplainMatchResult) { | 2284 TEST(PointeeTest, CanExplainMatchResult) { |
2200 const Matcher<const string*> m = Pointee(StartsWith("Hi")); | 2285 const Matcher<const string*> m = Pointee(StartsWith("Hi")); |
2201 | 2286 |
2202 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); | 2287 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); |
2203 | 2288 |
2204 const Matcher<int*> m2 = Pointee(GreaterThan(1)); | 2289 const Matcher<int*> m2 = Pointee(GreaterThan(1)); |
2205 int n = 3; | 2290 int n = 3; |
2206 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)); |
2207 } | 2292 } |
2208 | 2293 |
(...skipping 1172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3381 | 3466 |
3382 // Tests PolymorphicMatcher::impl(). | 3467 // Tests PolymorphicMatcher::impl(). |
3383 TEST(PolymorphicMatcherTest, CanAccessImpl) { | 3468 TEST(PolymorphicMatcherTest, CanAccessImpl) { |
3384 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); | 3469 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
3385 const DivisibleByImpl& impl = m.impl(); | 3470 const DivisibleByImpl& impl = m.impl(); |
3386 EXPECT_EQ(42, impl.divider()); | 3471 EXPECT_EQ(42, impl.divider()); |
3387 } | 3472 } |
3388 | 3473 |
3389 } // namespace gmock_matchers_test | 3474 } // namespace gmock_matchers_test |
3390 } // namespace testing | 3475 } // namespace testing |
LEFT | RIGHT |