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 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 return PolymorphicMatcher<Impl>(impl); | 313 return PolymorphicMatcher<Impl>(impl); |
314 } | 314 } |
315 | 315 |
316 // In order to be safe and clear, casting between different matcher | 316 // In order to be safe and clear, casting between different matcher |
317 // types is done explicitly via MatcherCast<T>(m), which takes a | 317 // types is done explicitly via MatcherCast<T>(m), which takes a |
318 // matcher m and returns a Matcher<T>. It compiles only when T can be | 318 // matcher m and returns a Matcher<T>. It compiles only when T can be |
319 // statically converted to the argument type of m. | 319 // statically converted to the argument type of m. |
320 template <typename T, typename M> | 320 template <typename T, typename M> |
321 Matcher<T> MatcherCast(M m); | 321 Matcher<T> MatcherCast(M m); |
322 | 322 |
| 323 // Implements SafeMatcherCast(). |
| 324 // |
| 325 // TODO(vladl@google.com): Modify the implementation to reject casting |
| 326 // Matcher<int> to Matcher<double>. |
| 327 // |
323 // We use an intermediate class to do the actual safe casting as Nokia's | 328 // We use an intermediate class to do the actual safe casting as Nokia's |
324 // Symbian compiler cannot decide between | 329 // Symbian compiler cannot decide between |
325 // template <T, M> ... (M) and | 330 // template <T, M> ... (M) and |
326 // template <T, U> ... (const Matcher<U>&) | 331 // template <T, U> ... (const Matcher<U>&) |
327 // for function templates but can for member function templates. | 332 // for function templates but can for member function templates. |
328 template <typename T> | 333 template <typename T> |
329 class SafeMatcherCastImpl { | 334 class SafeMatcherCastImpl { |
330 public: | 335 public: |
| 336 // This overload handles polymorphic matchers only since monomorphic |
| 337 // matchers are handled by the next one. |
331 template <typename M> | 338 template <typename M> |
332 static inline Matcher<T> Cast(M polymorphic_matcher) { | 339 static inline Matcher<T> Cast(M polymorphic_matcher) { |
333 return Matcher<T>(polymorphic_matcher); | 340 return Matcher<T>(polymorphic_matcher); |
334 } | 341 } |
335 | 342 |
| 343 // This overload handles monomorphic matchers. |
| 344 // |
| 345 // In general, if type T can be implicitly converted to type U, we can |
| 346 // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is |
| 347 // contravariant): just keep a copy of the original Matcher<U>, convert the |
| 348 // argument from type T to U, and then pass it to the underlying Matcher<U>. |
| 349 // The only exception is when U is a reference and T is not, as the |
| 350 // underlying Matcher<U> may be interested in the argument's address, which |
| 351 // is not preserved in the conversion from T to U. |
336 template <typename U> | 352 template <typename U> |
337 static inline Matcher<T> Cast(const Matcher<U>& matcher) { | 353 static inline Matcher<T> Cast(const Matcher<U>& matcher) { |
338 // Enforce that T can be implicitly converted to U. | 354 // Enforce that T can be implicitly converted to U. |
339 GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), | 355 GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), |
340 T_must_be_implicitly_convertible_to_U); | 356 T_must_be_implicitly_convertible_to_U); |
341 // Enforce that we are not converting a non-reference type T to a reference | 357 // Enforce that we are not converting a non-reference type T to a reference |
342 // type U. | 358 // type U. |
343 GMOCK_COMPILE_ASSERT_( | 359 GMOCK_COMPILE_ASSERT_( |
344 internal::is_reference<T>::value || !internal::is_reference<U>::value, | 360 internal::is_reference<T>::value || !internal::is_reference<U>::value, |
345 cannot_convert_non_referentce_arg_to_reference); | 361 cannot_convert_non_referentce_arg_to_reference); |
346 // In case both T and U are arithmetic types, enforce that the | 362 // In case both T and U are arithmetic types, enforce that the |
347 // conversion is not lossy. | 363 // conversion is not lossy. |
348 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(T)) RawT; | 364 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(T)) RawT; |
349 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(U)) RawU; | 365 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(U)) RawU; |
350 const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; | 366 const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; |
351 const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; | 367 const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; |
352 GMOCK_COMPILE_ASSERT_( | 368 GMOCK_COMPILE_ASSERT_( |
353 kTIsOther || kUIsOther || | 369 kTIsOther || kUIsOther || |
354 (internal::LosslessArithmeticConvertible<RawT, RawU>::value), | 370 (internal::LosslessArithmeticConvertible<RawT, RawU>::value), |
355 conversion_of_arithmetic_types_must_be_lossless); | 371 conversion_of_arithmetic_types_must_be_lossless); |
356 return MatcherCast<T>(matcher); | 372 return MatcherCast<T>(matcher); |
357 } | 373 } |
358 }; | 374 }; |
359 | 375 |
360 // TODO(vladl@google.com): Modify the implementation to reject casting | |
361 // Matcher<int> to Matcher<double>. | |
362 // Implements SafeMatcherCast(). | |
363 // | |
364 // This overload handles polymorphic matchers only since monomorphic | |
365 // matchers are handled by the next one. | |
366 template <typename T, typename M> | 376 template <typename T, typename M> |
367 inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) { | 377 inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) { |
368 return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher); | 378 return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher); |
369 } | 379 } |
370 | |
371 // This overload handles monomorphic matchers. | |
372 // | |
373 // In general, if type T can be implicitly converted to type U, we can | |
374 // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is | |
375 // contravariant): just keep a copy of the original Matcher<U>, convert the | |
376 // argument from type T to U, and then pass it to the underlying Matcher<U>. | |
377 // The only exception is when U is a reference and T is not, as the | |
378 // underlying Matcher<U> may be interested in the argument's address, which | |
379 // is not preserved in the conversion from T to U. | |
380 | 380 |
381 // A<T>() returns a matcher that matches any value of type T. | 381 // A<T>() returns a matcher that matches any value of type T. |
382 template <typename T> | 382 template <typename T> |
383 Matcher<T> A(); | 383 Matcher<T> A(); |
384 | 384 |
385 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION | 385 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION |
386 // and MUST NOT BE USED IN USER CODE!!! | 386 // and MUST NOT BE USED IN USER CODE!!! |
387 namespace internal { | 387 namespace internal { |
388 | 388 |
389 // Appends the explanation on the result of matcher.Matches(value) to | 389 // Appends the explanation on the result of matcher.Matches(value) to |
(...skipping 1241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1631 static ResultType Invoke(Functor f, T arg) { return f(arg); } | 1631 static ResultType Invoke(Functor f, T arg) { return f(arg); } |
1632 }; | 1632 }; |
1633 | 1633 |
1634 // Specialization for function pointers. | 1634 // Specialization for function pointers. |
1635 template <typename ArgType, typename ResType> | 1635 template <typename ArgType, typename ResType> |
1636 struct CallableTraits<ResType(*)(ArgType)> { | 1636 struct CallableTraits<ResType(*)(ArgType)> { |
1637 typedef ResType ResultType; | 1637 typedef ResType ResultType; |
1638 typedef ResType(*StorageType)(ArgType); | 1638 typedef ResType(*StorageType)(ArgType); |
1639 | 1639 |
1640 static void CheckIsValid(ResType(*f)(ArgType)) { | 1640 static void CheckIsValid(ResType(*f)(ArgType)) { |
1641 GMOCK_CHECK_(f != NULL) | 1641 GTEST_CHECK_(f != NULL) |
1642 << "NULL function pointer is passed into ResultOf()."; | 1642 << "NULL function pointer is passed into ResultOf()."; |
1643 } | 1643 } |
1644 template <typename T> | 1644 template <typename T> |
1645 static ResType Invoke(ResType(*f)(ArgType), T arg) { | 1645 static ResType Invoke(ResType(*f)(ArgType), T arg) { |
1646 return (*f)(arg); | 1646 return (*f)(arg); |
1647 } | 1647 } |
1648 }; | 1648 }; |
1649 | 1649 |
1650 // Implements the ResultOf() matcher for matching a return value of a | 1650 // Implements the ResultOf() matcher for matching a return value of a |
1651 // unary function of an object. | 1651 // unary function of an object. |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1940 public: | 1940 public: |
1941 explicit KeyMatcher(M m) : matcher_for_key_(m) {} | 1941 explicit KeyMatcher(M m) : matcher_for_key_(m) {} |
1942 | 1942 |
1943 template <typename PairType> | 1943 template <typename PairType> |
1944 operator Matcher<PairType>() const { | 1944 operator Matcher<PairType>() const { |
1945 return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_)); | 1945 return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_)); |
1946 } | 1946 } |
1947 | 1947 |
1948 private: | 1948 private: |
1949 const M matcher_for_key_; | 1949 const M matcher_for_key_; |
| 1950 }; |
| 1951 |
| 1952 // Implements Pair(first_matcher, second_matcher) for the given argument pair |
| 1953 // type with its two matchers. See Pair() function below. |
| 1954 template <typename PairType> |
| 1955 class PairMatcherImpl : public MatcherInterface<PairType> { |
| 1956 public: |
| 1957 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; |
| 1958 typedef typename RawPairType::first_type FirstType; |
| 1959 typedef typename RawPairType::second_type SecondType; |
| 1960 |
| 1961 template <typename FirstMatcher, typename SecondMatcher> |
| 1962 PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher) |
| 1963 : first_matcher_( |
| 1964 testing::SafeMatcherCast<const FirstType&>(first_matcher)), |
| 1965 second_matcher_( |
| 1966 testing::SafeMatcherCast<const SecondType&>(second_matcher)) { |
| 1967 } |
| 1968 |
| 1969 // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second' |
| 1970 // matches second_matcher. |
| 1971 virtual bool Matches(PairType a_pair) const { |
| 1972 return first_matcher_.Matches(a_pair.first) && |
| 1973 second_matcher_.Matches(a_pair.second); |
| 1974 } |
| 1975 |
| 1976 // Describes what this matcher does. |
| 1977 virtual void DescribeTo(::std::ostream* os) const { |
| 1978 *os << "has a first field that "; |
| 1979 first_matcher_.DescribeTo(os); |
| 1980 *os << ", and has a second field that "; |
| 1981 second_matcher_.DescribeTo(os); |
| 1982 } |
| 1983 |
| 1984 // Describes what the negation of this matcher does. |
| 1985 virtual void DescribeNegationTo(::std::ostream* os) const { |
| 1986 *os << "has a first field that "; |
| 1987 first_matcher_.DescribeNegationTo(os); |
| 1988 *os << ", or has a second field that "; |
| 1989 second_matcher_.DescribeNegationTo(os); |
| 1990 } |
| 1991 |
| 1992 // Explains why 'a_pair' matches, or doesn't match, this matcher. |
| 1993 virtual void ExplainMatchResultTo(PairType a_pair, |
| 1994 ::std::ostream* os) const { |
| 1995 ::std::stringstream ss1; |
| 1996 first_matcher_.ExplainMatchResultTo(a_pair.first, &ss1); |
| 1997 internal::string s1 = ss1.str(); |
| 1998 if (s1 != "") { |
| 1999 s1 = "the first field " + s1; |
| 2000 } |
| 2001 |
| 2002 ::std::stringstream ss2; |
| 2003 second_matcher_.ExplainMatchResultTo(a_pair.second, &ss2); |
| 2004 internal::string s2 = ss2.str(); |
| 2005 if (s2 != "") { |
| 2006 s2 = "the second field " + s2; |
| 2007 } |
| 2008 |
| 2009 *os << s1; |
| 2010 if (s1 != "" && s2 != "") { |
| 2011 *os << ", and "; |
| 2012 } |
| 2013 *os << s2; |
| 2014 } |
| 2015 |
| 2016 private: |
| 2017 const Matcher<const FirstType&> first_matcher_; |
| 2018 const Matcher<const SecondType&> second_matcher_; |
| 2019 }; |
| 2020 |
| 2021 // Implements polymorphic Pair(first_matcher, second_matcher). |
| 2022 template <typename FirstMatcher, typename SecondMatcher> |
| 2023 class PairMatcher { |
| 2024 public: |
| 2025 PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher) |
| 2026 : first_matcher_(first_matcher), second_matcher_(second_matcher) {} |
| 2027 |
| 2028 template <typename PairType> |
| 2029 operator Matcher<PairType> () const { |
| 2030 return MakeMatcher( |
| 2031 new PairMatcherImpl<PairType>( |
| 2032 first_matcher_, second_matcher_)); |
| 2033 } |
| 2034 |
| 2035 private: |
| 2036 const FirstMatcher first_matcher_; |
| 2037 const SecondMatcher second_matcher_; |
1950 }; | 2038 }; |
1951 | 2039 |
1952 // Implements ElementsAre() and ElementsAreArray(). | 2040 // Implements ElementsAre() and ElementsAreArray(). |
1953 template <typename Container> | 2041 template <typename Container> |
1954 class ElementsAreMatcherImpl : public MatcherInterface<Container> { | 2042 class ElementsAreMatcherImpl : public MatcherInterface<Container> { |
1955 public: | 2043 public: |
1956 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; | 2044 typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; |
1957 typedef internal::StlContainerView<RawContainer> View; | 2045 typedef internal::StlContainerView<RawContainer> View; |
1958 typedef typename View::type StlContainer; | 2046 typedef typename View::type StlContainer; |
1959 typedef typename View::const_reference StlContainerReference; | 2047 typedef typename View::const_reference StlContainerReference; |
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2640 } | 2728 } |
2641 | 2729 |
2642 // Key(inner_matcher) matches an std::pair whose 'first' field matches | 2730 // Key(inner_matcher) matches an std::pair whose 'first' field matches |
2643 // inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an | 2731 // inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an |
2644 // std::map that contains at least one element whose key is >= 5. | 2732 // std::map that contains at least one element whose key is >= 5. |
2645 template <typename M> | 2733 template <typename M> |
2646 inline internal::KeyMatcher<M> Key(M inner_matcher) { | 2734 inline internal::KeyMatcher<M> Key(M inner_matcher) { |
2647 return internal::KeyMatcher<M>(inner_matcher); | 2735 return internal::KeyMatcher<M>(inner_matcher); |
2648 } | 2736 } |
2649 | 2737 |
| 2738 // Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field |
| 2739 // matches first_matcher and whose 'second' field matches second_matcher. For |
| 2740 // example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used |
| 2741 // to match a std::map<int, string> that contains exactly one element whose key |
| 2742 // is >= 5 and whose value equals "foo". |
| 2743 template <typename FirstMatcher, typename SecondMatcher> |
| 2744 inline internal::PairMatcher<FirstMatcher, SecondMatcher> |
| 2745 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) { |
| 2746 return internal::PairMatcher<FirstMatcher, SecondMatcher>( |
| 2747 first_matcher, second_matcher); |
| 2748 } |
| 2749 |
2650 // Returns a predicate that is satisfied by anything that matches the | 2750 // Returns a predicate that is satisfied by anything that matches the |
2651 // given matcher. | 2751 // given matcher. |
2652 template <typename M> | 2752 template <typename M> |
2653 inline internal::MatcherAsPredicate<M> Matches(M matcher) { | 2753 inline internal::MatcherAsPredicate<M> Matches(M matcher) { |
2654 return internal::MatcherAsPredicate<M>(matcher); | 2754 return internal::MatcherAsPredicate<M>(matcher); |
2655 } | 2755 } |
2656 | 2756 |
2657 // Returns true iff the value matches the matcher. | 2757 // Returns true iff the value matches the matcher. |
2658 template <typename T, typename M> | 2758 template <typename T, typename M> |
2659 inline bool Value(const T& value, M matcher) { | 2759 inline bool Value(const T& value, M matcher) { |
(...skipping 15 matching lines...) Expand all Loading... |
2675 // succeed iff the value matches the matcher. If the assertion fails, | 2775 // succeed iff the value matches the matcher. If the assertion fails, |
2676 // the value and the description of the matcher will be printed. | 2776 // the value and the description of the matcher will be printed. |
2677 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ | 2777 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ |
2678 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) | 2778 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) |
2679 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ | 2779 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ |
2680 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) | 2780 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) |
2681 | 2781 |
2682 } // namespace testing | 2782 } // namespace testing |
2683 | 2783 |
2684 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ | 2784 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ |
LEFT | RIGHT |