diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/gmock-matchers_test.cc | 144 | ||||
-rw-r--r-- | test/gmock-port_test.cc | 59 |
2 files changed, 116 insertions, 87 deletions
diff --git a/test/gmock-matchers_test.cc b/test/gmock-matchers_test.cc index 8926e944..ef878898 100644 --- a/test/gmock-matchers_test.cc +++ b/test/gmock-matchers_test.cc @@ -42,6 +42,7 @@ #include <set> #include <sstream> #include <string> +#include <utility> #include <vector> #include <gmock/gmock.h> #include <gtest/gtest.h> @@ -91,6 +92,7 @@ using testing::NanSensitiveFloatEq; using testing::Ne; using testing::Not; using testing::NotNull; +using testing::Pair; using testing::Pointee; using testing::PolymorphicMatcher; using testing::Property; @@ -126,6 +128,35 @@ using testing::MatchesRegex; using testing::internal::RE; #endif // GMOCK_HAS_REGEX +// For testing ExplainMatchResultTo(). +class GreaterThanMatcher : public MatcherInterface<int> { + public: + explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} + + virtual bool Matches(int lhs) const { return lhs > rhs_; } + + virtual void DescribeTo(::std::ostream* os) const { + *os << "is greater than " << rhs_; + } + + virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { + const int diff = lhs - rhs_; + if (diff > 0) { + *os << "is " << diff << " more than " << rhs_; + } else if (diff == 0) { + *os << "is the same as " << rhs_; + } else { + *os << "is " << -diff << " less than " << rhs_; + } + } + private: + const int rhs_; +}; + +Matcher<int> GreaterThan(int n) { + return MakeMatcher(new GreaterThanMatcher(n)); +} + // Returns the description of the given matcher. template <typename T> string Describe(const Matcher<T>& m) { @@ -899,6 +930,90 @@ TEST(KeyTest, InsideContainsUsingMultimap) { EXPECT_THAT(container, Not(Contains(Key(3)))); } +TEST(PairTest, Typing) { + // Test verifies the following type conversions can be compiled. + Matcher<const std::pair<const char*, int>&> m1 = Pair("foo", 42); + Matcher<const std::pair<const char*, int> > m2 = Pair("foo", 42); + Matcher<std::pair<const char*, int> > m3 = Pair("foo", 42); + + Matcher<std::pair<int, const std::string> > m4 = Pair(25, "42"); + Matcher<std::pair<const std::string, int> > m5 = Pair("25", 42); +} + +TEST(PairTest, CanDescribeSelf) { + Matcher<const std::pair<std::string, int>&> m1 = Pair("foo", 42); + EXPECT_EQ("has a first field that is equal to \"foo\"" + ", and has a second field that is equal to 42", + Describe(m1)); + EXPECT_EQ("has a first field that is not equal to \"foo\"" + ", or has a second field that is not equal to 42", + DescribeNegation(m1)); + // Double and triple negation (1 or 2 times not and description of negation). + Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42)); + EXPECT_EQ("has a first field that is not equal to 13" + ", and has a second field that is equal to 42", + DescribeNegation(m2)); +} + +TEST(PairTest, CanExplainMatchResultTo) { + const Matcher<std::pair<int, int> > m0 = Pair(0, 0); + EXPECT_EQ("", Explain(m0, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m1 = Pair(GreaterThan(0), 0); + EXPECT_EQ("the first field is 25 more than 0", + Explain(m1, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m2 = Pair(0, GreaterThan(0)); + EXPECT_EQ("the second field is 42 more than 0", + Explain(m2, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m3 = Pair(GreaterThan(0), GreaterThan(0)); + EXPECT_EQ("the first field is 25 more than 0" + ", and the second field is 42 more than 0", + Explain(m3, std::make_pair(25, 42))); +} + +TEST(PairTest, MatchesCorrectly) { + std::pair<int, std::string> p(25, "foo"); + + // Both fields match. + EXPECT_THAT(p, Pair(25, "foo")); + EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); + + // 'first' doesnt' match, but 'second' matches. + EXPECT_THAT(p, Not(Pair(42, "foo"))); + EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); + + // 'first' matches, but 'second' doesn't match. + EXPECT_THAT(p, Not(Pair(25, "bar"))); + EXPECT_THAT(p, Not(Pair(25, Not("foo")))); + + // Neither field matches. + EXPECT_THAT(p, Not(Pair(13, "bar"))); + EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); +} + +TEST(PairTest, SafelyCastsInnerMatchers) { + Matcher<int> is_positive = Gt(0); + Matcher<int> is_negative = Lt(0); + std::pair<char, bool> p('a', true); + EXPECT_THAT(p, Pair(is_positive, _)); + EXPECT_THAT(p, Not(Pair(is_negative, _))); + EXPECT_THAT(p, Pair(_, is_positive)); + EXPECT_THAT(p, Not(Pair(_, is_negative))); +} + +TEST(PairTest, InsideContainsUsingMap) { + std::map<int, std::string> container; + container.insert(std::make_pair(1, "foo")); + container.insert(std::make_pair(2, "bar")); + container.insert(std::make_pair(4, "baz")); + EXPECT_THAT(container, Contains(Pair(1, "foo"))); + EXPECT_THAT(container, Contains(Pair(1, _))); + EXPECT_THAT(container, Contains(Pair(_, "foo"))); + EXPECT_THAT(container, Not(Contains(Pair(3, _)))); +} + // Tests StartsWith(s). TEST(StartsWithTest, MatchesStringWithGivenPrefix) { @@ -2150,35 +2265,6 @@ TEST(PointeeTest, CanDescribeSelf) { DescribeNegation(m)); } -// For testing ExplainMatchResultTo(). -class GreaterThanMatcher : public MatcherInterface<int> { - public: - explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} - - virtual bool Matches(int lhs) const { return lhs > rhs_; } - - virtual void DescribeTo(::std::ostream* os) const { - *os << "is greater than " << rhs_; - } - - virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { - const int diff = lhs - rhs_; - if (diff > 0) { - *os << "is " << diff << " more than " << rhs_; - } else if (diff == 0) { - *os << "is the same as " << rhs_; - } else { - *os << "is " << -diff << " less than " << rhs_; - } - } - private: - const int rhs_; -}; - -Matcher<int> GreaterThan(int n) { - return MakeMatcher(new GreaterThanMatcher(n)); -} - TEST(PointeeTest, CanExplainMatchResult) { const Matcher<const string*> m = Pointee(StartsWith("Hi")); diff --git a/test/gmock-port_test.cc b/test/gmock-port_test.cc index 7335405c..9a64ec33 100644 --- a/test/gmock-port_test.cc +++ b/test/gmock-port_test.cc @@ -36,61 +36,4 @@ #include <gmock/internal/gmock-port.h> #include <gtest/gtest.h> -TEST(GmockCheckSyntaxTest, BehavesLikeASingleStatement) { - if (false) - GMOCK_CHECK_(false) << "This should never be executed; " - "It's a compilation test only."; - - if (true) - GMOCK_CHECK_(true); - else - ; - - if (false) - ; - else - GMOCK_CHECK_(true) << ""; -} - -TEST(GmockCheckSyntaxTest, WorksWithSwitch) { - switch (0) { - case 1: - break; - default: - GMOCK_CHECK_(true); - } - - switch(0) - case 0: - GMOCK_CHECK_(true) << "Check failed in switch case"; -} - -TEST(GmockCheckDeathTest, DiesWithCorrectOutputOnFailure) { - const bool a_false_condition = false; - // MSVC and gcc use different formats to print source file locations. - // Google Mock's failure messages use the same format as used by the - // compiler, in order for the IDE to recognize them. Therefore we look - // for different patterns here depending on the compiler. - const char regex[] = -#ifdef _MSC_VER - "gmock-port_test\\.cc\\(\\d+\\):" -#else - "gmock-port_test\\.cc:[0-9]+" -#endif // _MSC_VER - ".*a_false_condition.*Extra info"; - - EXPECT_DEATH_IF_SUPPORTED(GMOCK_CHECK_(a_false_condition) << "Extra info", - regex); -} - -#if GTEST_HAS_DEATH_TEST - -TEST(GmockCheckDeathTest, LivesSilentlyOnSuccess) { - EXPECT_EXIT({ - GMOCK_CHECK_(true) << "Extra info"; - ::std::cerr << "Success\n"; - exit(0); }, - ::testing::ExitedWithCode(0), "Success"); -} - -#endif // GTEST_HAS_DEATH_TEST +// This file intentionally contains no test at this moment. |