OLD | NEW |
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 namespace internal { | 54 namespace internal { |
55 string FormatMatcherDescriptionSyntaxError(const char* description, | 55 string FormatMatcherDescriptionSyntaxError(const char* description, |
56 const char* error_pos); | 56 const char* error_pos); |
57 int GetParamIndex(const char* param_names[], const string& param_name); | 57 int GetParamIndex(const char* param_names[], const string& param_name); |
58 string JoinAsTuple(const Strings& fields); | 58 string JoinAsTuple(const Strings& fields); |
59 bool SkipPrefix(const char* prefix, const char** pstr); | 59 bool SkipPrefix(const char* prefix, const char** pstr); |
60 } // namespace internal | 60 } // namespace internal |
61 | 61 |
62 namespace gmock_matchers_test { | 62 namespace gmock_matchers_test { |
63 | 63 |
| 64 using std::make_pair; |
64 using std::map; | 65 using std::map; |
65 using std::multimap; | 66 using std::multimap; |
| 67 using std::pair; |
66 using std::stringstream; | 68 using std::stringstream; |
67 using std::tr1::make_tuple; | 69 using std::tr1::make_tuple; |
68 using testing::A; | 70 using testing::A; |
69 using testing::AllArgs; | 71 using testing::AllArgs; |
70 using testing::AllOf; | 72 using testing::AllOf; |
71 using testing::An; | 73 using testing::An; |
72 using testing::AnyOf; | 74 using testing::AnyOf; |
73 using testing::ByRef; | 75 using testing::ByRef; |
| 76 using testing::ContainsRegex; |
74 using testing::DoubleEq; | 77 using testing::DoubleEq; |
75 using testing::EndsWith; | 78 using testing::EndsWith; |
76 using testing::Eq; | 79 using testing::Eq; |
| 80 using testing::ExplainMatchResult; |
77 using testing::Field; | 81 using testing::Field; |
78 using testing::FloatEq; | 82 using testing::FloatEq; |
79 using testing::Ge; | 83 using testing::Ge; |
80 using testing::Gt; | 84 using testing::Gt; |
81 using testing::HasSubstr; | 85 using testing::HasSubstr; |
82 using testing::IsNull; | 86 using testing::IsNull; |
83 using testing::Key; | 87 using testing::Key; |
84 using testing::Le; | 88 using testing::Le; |
85 using testing::Lt; | 89 using testing::Lt; |
86 using testing::MakeMatcher; | 90 using testing::MakeMatcher; |
87 using testing::MakePolymorphicMatcher; | 91 using testing::MakePolymorphicMatcher; |
| 92 using testing::MatchResultListener; |
88 using testing::Matcher; | 93 using testing::Matcher; |
89 using testing::MatcherCast; | 94 using testing::MatcherCast; |
90 using testing::MatcherInterface; | 95 using testing::MatcherInterface; |
91 using testing::Matches; | 96 using testing::Matches; |
92 using testing::ExplainMatchResult; | 97 using testing::MatchesRegex; |
93 using testing::MatchResultListener; | |
94 using testing::NanSensitiveDoubleEq; | 98 using testing::NanSensitiveDoubleEq; |
95 using testing::NanSensitiveFloatEq; | 99 using testing::NanSensitiveFloatEq; |
96 using testing::Ne; | 100 using testing::Ne; |
97 using testing::Not; | 101 using testing::Not; |
98 using testing::NotNull; | 102 using testing::NotNull; |
99 using testing::Pair; | 103 using testing::Pair; |
100 using testing::Pointee; | 104 using testing::Pointee; |
101 using testing::PolymorphicMatcher; | 105 using testing::PolymorphicMatcher; |
102 using testing::Property; | 106 using testing::Property; |
103 using testing::Ref; | 107 using testing::Ref; |
104 using testing::ResultOf; | 108 using testing::ResultOf; |
105 using testing::StartsWith; | 109 using testing::StartsWith; |
106 using testing::StrCaseEq; | 110 using testing::StrCaseEq; |
107 using testing::StrCaseNe; | 111 using testing::StrCaseNe; |
108 using testing::StrEq; | 112 using testing::StrEq; |
109 using testing::StrNe; | 113 using testing::StrNe; |
110 using testing::Truly; | 114 using testing::Truly; |
111 using testing::TypedEq; | 115 using testing::TypedEq; |
112 using testing::Value; | 116 using testing::Value; |
113 using testing::_; | 117 using testing::_; |
114 using testing::internal::DummyMatchResultListener; | 118 using testing::internal::DummyMatchResultListener; |
| 119 using testing::internal::ExplainMatchFailureTupleTo; |
115 using testing::internal::FloatingEqMatcher; | 120 using testing::internal::FloatingEqMatcher; |
116 using testing::internal::FormatMatcherDescriptionSyntaxError; | 121 using testing::internal::FormatMatcherDescriptionSyntaxError; |
117 using testing::internal::GetParamIndex; | 122 using testing::internal::GetParamIndex; |
118 using testing::internal::Interpolation; | 123 using testing::internal::Interpolation; |
119 using testing::internal::Interpolations; | 124 using testing::internal::Interpolations; |
120 using testing::internal::JoinAsTuple; | 125 using testing::internal::JoinAsTuple; |
| 126 using testing::internal::RE; |
121 using testing::internal::SkipPrefix; | 127 using testing::internal::SkipPrefix; |
122 using testing::internal::StreamMatchResultListener; | 128 using testing::internal::StreamMatchResultListener; |
123 using testing::internal::String; | 129 using testing::internal::String; |
| 130 using testing::internal::StringMatchResultListener; |
124 using testing::internal::Strings; | 131 using testing::internal::Strings; |
125 using testing::internal::StringMatchResultListener; | |
126 using testing::internal::ValidateMatcherDescription; | 132 using testing::internal::ValidateMatcherDescription; |
127 using testing::internal::kInvalidInterpolation; | 133 using testing::internal::kInvalidInterpolation; |
128 using testing::internal::kPercentInterpolation; | 134 using testing::internal::kPercentInterpolation; |
129 using testing::internal::kTupleInterpolation; | 135 using testing::internal::kTupleInterpolation; |
130 using testing::internal::linked_ptr; | 136 using testing::internal::linked_ptr; |
131 using testing::internal::scoped_ptr; | 137 using testing::internal::scoped_ptr; |
132 using testing::internal::string; | 138 using testing::internal::string; |
133 | 139 |
134 using testing::ContainsRegex; | |
135 using testing::MatchesRegex; | |
136 using testing::internal::RE; | |
137 | |
138 // For testing ExplainMatchResultTo(). | 140 // For testing ExplainMatchResultTo(). |
139 class GreaterThanMatcher : public MatcherInterface<int> { | 141 class GreaterThanMatcher : public MatcherInterface<int> { |
140 public: | 142 public: |
141 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} | 143 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
142 | 144 |
143 virtual void DescribeTo(::std::ostream* os) const { | 145 virtual void DescribeTo(::std::ostream* os) const { |
144 *os << "is greater than " << rhs_; | 146 *os << "is > " << rhs_; |
145 } | 147 } |
146 | 148 |
147 virtual bool MatchAndExplain(int lhs, | 149 virtual bool MatchAndExplain(int lhs, |
148 MatchResultListener* listener) const { | 150 MatchResultListener* listener) const { |
149 const int diff = lhs - rhs_; | 151 const int diff = lhs - rhs_; |
150 if (diff > 0) { | 152 if (diff > 0) { |
151 *listener << "which is " << diff << " more than " << rhs_; | 153 *listener << "which is " << diff << " more than " << rhs_; |
152 } else if (diff == 0) { | 154 } else if (diff == 0) { |
153 *listener << "which is the same as " << rhs_; | 155 *listener << "which is the same as " << rhs_; |
154 } else { | 156 } else { |
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 TEST(GeTest, ImplementsGreaterThanOrEqual) { | 752 TEST(GeTest, ImplementsGreaterThanOrEqual) { |
751 Matcher<int> m1 = Ge(0); | 753 Matcher<int> m1 = Ge(0); |
752 EXPECT_TRUE(m1.Matches(1)); | 754 EXPECT_TRUE(m1.Matches(1)); |
753 EXPECT_TRUE(m1.Matches(0)); | 755 EXPECT_TRUE(m1.Matches(0)); |
754 EXPECT_FALSE(m1.Matches(-1)); | 756 EXPECT_FALSE(m1.Matches(-1)); |
755 } | 757 } |
756 | 758 |
757 // Tests that Ge(v) describes itself properly. | 759 // Tests that Ge(v) describes itself properly. |
758 TEST(GeTest, CanDescribeSelf) { | 760 TEST(GeTest, CanDescribeSelf) { |
759 Matcher<int> m = Ge(5); | 761 Matcher<int> m = Ge(5); |
760 EXPECT_EQ("is greater than or equal to 5", Describe(m)); | 762 EXPECT_EQ("is >= 5", Describe(m)); |
761 } | 763 } |
762 | 764 |
763 // Tests that Gt(v) matches anything > v. | 765 // Tests that Gt(v) matches anything > v. |
764 TEST(GtTest, ImplementsGreaterThan) { | 766 TEST(GtTest, ImplementsGreaterThan) { |
765 Matcher<double> m1 = Gt(0); | 767 Matcher<double> m1 = Gt(0); |
766 EXPECT_TRUE(m1.Matches(1.0)); | 768 EXPECT_TRUE(m1.Matches(1.0)); |
767 EXPECT_FALSE(m1.Matches(0.0)); | 769 EXPECT_FALSE(m1.Matches(0.0)); |
768 EXPECT_FALSE(m1.Matches(-1.0)); | 770 EXPECT_FALSE(m1.Matches(-1.0)); |
769 } | 771 } |
770 | 772 |
771 // Tests that Gt(v) describes itself properly. | 773 // Tests that Gt(v) describes itself properly. |
772 TEST(GtTest, CanDescribeSelf) { | 774 TEST(GtTest, CanDescribeSelf) { |
773 Matcher<int> m = Gt(5); | 775 Matcher<int> m = Gt(5); |
774 EXPECT_EQ("is greater than 5", Describe(m)); | 776 EXPECT_EQ("is > 5", Describe(m)); |
775 } | 777 } |
776 | 778 |
777 // Tests that Le(v) matches anything <= v. | 779 // Tests that Le(v) matches anything <= v. |
778 TEST(LeTest, ImplementsLessThanOrEqual) { | 780 TEST(LeTest, ImplementsLessThanOrEqual) { |
779 Matcher<char> m1 = Le('b'); | 781 Matcher<char> m1 = Le('b'); |
780 EXPECT_TRUE(m1.Matches('a')); | 782 EXPECT_TRUE(m1.Matches('a')); |
781 EXPECT_TRUE(m1.Matches('b')); | 783 EXPECT_TRUE(m1.Matches('b')); |
782 EXPECT_FALSE(m1.Matches('c')); | 784 EXPECT_FALSE(m1.Matches('c')); |
783 } | 785 } |
784 | 786 |
785 // Tests that Le(v) describes itself properly. | 787 // Tests that Le(v) describes itself properly. |
786 TEST(LeTest, CanDescribeSelf) { | 788 TEST(LeTest, CanDescribeSelf) { |
787 Matcher<int> m = Le(5); | 789 Matcher<int> m = Le(5); |
788 EXPECT_EQ("is less than or equal to 5", Describe(m)); | 790 EXPECT_EQ("is <= 5", Describe(m)); |
789 } | 791 } |
790 | 792 |
791 // Tests that Lt(v) matches anything < v. | 793 // Tests that Lt(v) matches anything < v. |
792 TEST(LtTest, ImplementsLessThan) { | 794 TEST(LtTest, ImplementsLessThan) { |
793 Matcher<const string&> m1 = Lt("Hello"); | 795 Matcher<const string&> m1 = Lt("Hello"); |
794 EXPECT_TRUE(m1.Matches("Abc")); | 796 EXPECT_TRUE(m1.Matches("Abc")); |
795 EXPECT_FALSE(m1.Matches("Hello")); | 797 EXPECT_FALSE(m1.Matches("Hello")); |
796 EXPECT_FALSE(m1.Matches("Hello, world!")); | 798 EXPECT_FALSE(m1.Matches("Hello, world!")); |
797 } | 799 } |
798 | 800 |
799 // Tests that Lt(v) describes itself properly. | 801 // Tests that Lt(v) describes itself properly. |
800 TEST(LtTest, CanDescribeSelf) { | 802 TEST(LtTest, CanDescribeSelf) { |
801 Matcher<int> m = Lt(5); | 803 Matcher<int> m = Lt(5); |
802 EXPECT_EQ("is less than 5", Describe(m)); | 804 EXPECT_EQ("is < 5", Describe(m)); |
803 } | 805 } |
804 | 806 |
805 // Tests that Ne(v) matches anything != v. | 807 // Tests that Ne(v) matches anything != v. |
806 TEST(NeTest, ImplementsNotEqual) { | 808 TEST(NeTest, ImplementsNotEqual) { |
807 Matcher<int> m1 = Ne(0); | 809 Matcher<int> m1 = Ne(0); |
808 EXPECT_TRUE(m1.Matches(1)); | 810 EXPECT_TRUE(m1.Matches(1)); |
809 EXPECT_TRUE(m1.Matches(-1)); | 811 EXPECT_TRUE(m1.Matches(-1)); |
810 EXPECT_FALSE(m1.Matches(0)); | 812 EXPECT_FALSE(m1.Matches(0)); |
811 } | 813 } |
812 | 814 |
813 // Tests that Ne(v) describes itself properly. | 815 // Tests that Ne(v) describes itself properly. |
814 TEST(NeTest, CanDescribeSelf) { | 816 TEST(NeTest, CanDescribeSelf) { |
815 Matcher<int> m = Ne(5); | 817 Matcher<int> m = Ne(5); |
816 EXPECT_EQ("is not equal to 5", Describe(m)); | 818 EXPECT_EQ("isn't equal to 5", Describe(m)); |
817 } | 819 } |
818 | 820 |
819 // Tests that IsNull() matches any NULL pointer of any type. | 821 // Tests that IsNull() matches any NULL pointer of any type. |
820 TEST(IsNullTest, MatchesNullPointer) { | 822 TEST(IsNullTest, MatchesNullPointer) { |
821 Matcher<int*> m1 = IsNull(); | 823 Matcher<int*> m1 = IsNull(); |
822 int* p1 = NULL; | 824 int* p1 = NULL; |
823 int n = 0; | 825 int n = 0; |
824 EXPECT_TRUE(m1.Matches(p1)); | 826 EXPECT_TRUE(m1.Matches(p1)); |
825 EXPECT_FALSE(m1.Matches(&n)); | 827 EXPECT_FALSE(m1.Matches(&n)); |
826 | 828 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 const scoped_ptr<double> non_null_p(new double); | 871 const scoped_ptr<double> non_null_p(new double); |
870 | 872 |
871 EXPECT_TRUE(m.Matches(null_p)); | 873 EXPECT_TRUE(m.Matches(null_p)); |
872 EXPECT_FALSE(m.Matches(non_null_p)); | 874 EXPECT_FALSE(m.Matches(non_null_p)); |
873 } | 875 } |
874 | 876 |
875 // Tests that IsNull() describes itself properly. | 877 // Tests that IsNull() describes itself properly. |
876 TEST(IsNullTest, CanDescribeSelf) { | 878 TEST(IsNullTest, CanDescribeSelf) { |
877 Matcher<int*> m = IsNull(); | 879 Matcher<int*> m = IsNull(); |
878 EXPECT_EQ("is NULL", Describe(m)); | 880 EXPECT_EQ("is NULL", Describe(m)); |
879 EXPECT_EQ("is not NULL", DescribeNegation(m)); | 881 EXPECT_EQ("isn't NULL", DescribeNegation(m)); |
880 } | 882 } |
881 | 883 |
882 // Tests that NotNull() matches any non-NULL pointer of any type. | 884 // Tests that NotNull() matches any non-NULL pointer of any type. |
883 TEST(NotNullTest, MatchesNonNullPointer) { | 885 TEST(NotNullTest, MatchesNonNullPointer) { |
884 Matcher<int*> m1 = NotNull(); | 886 Matcher<int*> m1 = NotNull(); |
885 int* p1 = NULL; | 887 int* p1 = NULL; |
886 int n = 0; | 888 int n = 0; |
887 EXPECT_FALSE(m1.Matches(p1)); | 889 EXPECT_FALSE(m1.Matches(p1)); |
888 EXPECT_TRUE(m1.Matches(&n)); | 890 EXPECT_TRUE(m1.Matches(&n)); |
889 | 891 |
(...skipping 26 matching lines...) Expand all Loading... |
916 const scoped_ptr<double> null_p; | 918 const scoped_ptr<double> null_p; |
917 const scoped_ptr<double> non_null_p(new double); | 919 const scoped_ptr<double> non_null_p(new double); |
918 | 920 |
919 EXPECT_FALSE(m.Matches(null_p)); | 921 EXPECT_FALSE(m.Matches(null_p)); |
920 EXPECT_TRUE(m.Matches(non_null_p)); | 922 EXPECT_TRUE(m.Matches(non_null_p)); |
921 } | 923 } |
922 | 924 |
923 // Tests that NotNull() describes itself properly. | 925 // Tests that NotNull() describes itself properly. |
924 TEST(NotNullTest, CanDescribeSelf) { | 926 TEST(NotNullTest, CanDescribeSelf) { |
925 Matcher<int*> m = NotNull(); | 927 Matcher<int*> m = NotNull(); |
926 EXPECT_EQ("is not NULL", Describe(m)); | 928 EXPECT_EQ("isn't NULL", Describe(m)); |
927 } | 929 } |
928 | 930 |
929 // Tests that Ref(variable) matches an argument that references | 931 // Tests that Ref(variable) matches an argument that references |
930 // 'variable'. | 932 // 'variable'. |
931 TEST(RefTest, MatchesSameVariable) { | 933 TEST(RefTest, MatchesSameVariable) { |
932 int a = 0; | 934 int a = 0; |
933 int b = 0; | 935 int b = 0; |
934 Matcher<int&> m = Ref(a); | 936 Matcher<int&> m = Ref(a); |
935 EXPECT_TRUE(m.Matches(a)); | 937 EXPECT_TRUE(m.Matches(a)); |
936 EXPECT_FALSE(m.Matches(b)); | 938 EXPECT_FALSE(m.Matches(b)); |
(...skipping 29 matching lines...) Expand all Loading... |
966 EXPECT_TRUE(m1.Matches(base)); | 968 EXPECT_TRUE(m1.Matches(base)); |
967 EXPECT_FALSE(m1.Matches(base2)); | 969 EXPECT_FALSE(m1.Matches(base2)); |
968 EXPECT_FALSE(m1.Matches(derived)); | 970 EXPECT_FALSE(m1.Matches(derived)); |
969 | 971 |
970 m1 = Ref(derived); | 972 m1 = Ref(derived); |
971 EXPECT_TRUE(m1.Matches(derived)); | 973 EXPECT_TRUE(m1.Matches(derived)); |
972 EXPECT_FALSE(m1.Matches(base)); | 974 EXPECT_FALSE(m1.Matches(base)); |
973 EXPECT_FALSE(m1.Matches(base2)); | 975 EXPECT_FALSE(m1.Matches(base2)); |
974 } | 976 } |
975 | 977 |
| 978 TEST(RefTest, ExplainsResult) { |
| 979 int n = 0; |
| 980 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n), |
| 981 StartsWith("which is located @")); |
| 982 |
| 983 int m = 0; |
| 984 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m), |
| 985 StartsWith("which is located @")); |
| 986 } |
| 987 |
976 // Tests string comparison matchers. | 988 // Tests string comparison matchers. |
977 | 989 |
978 TEST(StrEqTest, MatchesEqualString) { | 990 TEST(StrEqTest, MatchesEqualString) { |
979 Matcher<const char*> m = StrEq(string("Hello")); | 991 Matcher<const char*> m = StrEq(string("Hello")); |
980 EXPECT_TRUE(m.Matches("Hello")); | 992 EXPECT_TRUE(m.Matches("Hello")); |
981 EXPECT_FALSE(m.Matches("hello")); | 993 EXPECT_FALSE(m.Matches("hello")); |
982 EXPECT_FALSE(m.Matches(NULL)); | 994 EXPECT_FALSE(m.Matches(NULL)); |
983 | 995 |
984 Matcher<const string&> m2 = StrEq("Hello"); | 996 Matcher<const string&> m2 = StrEq("Hello"); |
985 EXPECT_TRUE(m2.Matches("Hello")); | 997 EXPECT_TRUE(m2.Matches("Hello")); |
(...skipping 20 matching lines...) Expand all Loading... |
1006 EXPECT_TRUE(m.Matches(NULL)); | 1018 EXPECT_TRUE(m.Matches(NULL)); |
1007 EXPECT_FALSE(m.Matches("Hello")); | 1019 EXPECT_FALSE(m.Matches("Hello")); |
1008 | 1020 |
1009 Matcher<string> m2 = StrNe(string("Hello")); | 1021 Matcher<string> m2 = StrNe(string("Hello")); |
1010 EXPECT_TRUE(m2.Matches("hello")); | 1022 EXPECT_TRUE(m2.Matches("hello")); |
1011 EXPECT_FALSE(m2.Matches("Hello")); | 1023 EXPECT_FALSE(m2.Matches("Hello")); |
1012 } | 1024 } |
1013 | 1025 |
1014 TEST(StrNeTest, CanDescribeSelf) { | 1026 TEST(StrNeTest, CanDescribeSelf) { |
1015 Matcher<const char*> m = StrNe("Hi"); | 1027 Matcher<const char*> m = StrNe("Hi"); |
1016 EXPECT_EQ("is not equal to \"Hi\"", Describe(m)); | 1028 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m)); |
1017 } | 1029 } |
1018 | 1030 |
1019 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { | 1031 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { |
1020 Matcher<const char*> m = StrCaseEq(string("Hello")); | 1032 Matcher<const char*> m = StrCaseEq(string("Hello")); |
1021 EXPECT_TRUE(m.Matches("Hello")); | 1033 EXPECT_TRUE(m.Matches("Hello")); |
1022 EXPECT_TRUE(m.Matches("hello")); | 1034 EXPECT_TRUE(m.Matches("hello")); |
1023 EXPECT_FALSE(m.Matches("Hi")); | 1035 EXPECT_FALSE(m.Matches("Hi")); |
1024 EXPECT_FALSE(m.Matches(NULL)); | 1036 EXPECT_FALSE(m.Matches(NULL)); |
1025 | 1037 |
1026 Matcher<const string&> m2 = StrCaseEq("Hello"); | 1038 Matcher<const string&> m2 = StrCaseEq("Hello"); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1065 EXPECT_FALSE(m.Matches("Hello")); | 1077 EXPECT_FALSE(m.Matches("Hello")); |
1066 EXPECT_FALSE(m.Matches("hello")); | 1078 EXPECT_FALSE(m.Matches("hello")); |
1067 | 1079 |
1068 Matcher<string> m2 = StrCaseNe(string("Hello")); | 1080 Matcher<string> m2 = StrCaseNe(string("Hello")); |
1069 EXPECT_TRUE(m2.Matches("")); | 1081 EXPECT_TRUE(m2.Matches("")); |
1070 EXPECT_FALSE(m2.Matches("Hello")); | 1082 EXPECT_FALSE(m2.Matches("Hello")); |
1071 } | 1083 } |
1072 | 1084 |
1073 TEST(StrCaseNeTest, CanDescribeSelf) { | 1085 TEST(StrCaseNeTest, CanDescribeSelf) { |
1074 Matcher<const char*> m = StrCaseNe("Hi"); | 1086 Matcher<const char*> m = StrCaseNe("Hi"); |
1075 EXPECT_EQ("is not equal to (ignoring case) \"Hi\"", Describe(m)); | 1087 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m)); |
1076 } | 1088 } |
1077 | 1089 |
1078 // Tests that HasSubstr() works for matching string-typed values. | 1090 // Tests that HasSubstr() works for matching string-typed values. |
1079 TEST(HasSubstrTest, WorksForStringClasses) { | 1091 TEST(HasSubstrTest, WorksForStringClasses) { |
1080 const Matcher<string> m1 = HasSubstr("foo"); | 1092 const Matcher<string> m1 = HasSubstr("foo"); |
1081 EXPECT_TRUE(m1.Matches(string("I love food."))); | 1093 EXPECT_TRUE(m1.Matches(string("I love food."))); |
1082 EXPECT_FALSE(m1.Matches(string("tofo"))); | 1094 EXPECT_FALSE(m1.Matches(string("tofo"))); |
1083 | 1095 |
1084 const Matcher<const std::string&> m2 = HasSubstr("foo"); | 1096 const Matcher<const std::string&> m2 = HasSubstr("foo"); |
1085 EXPECT_TRUE(m2.Matches(std::string("I love food."))); | 1097 EXPECT_TRUE(m2.Matches(std::string("I love food."))); |
(...skipping 13 matching lines...) Expand all Loading... |
1099 EXPECT_FALSE(m2.Matches(NULL)); | 1111 EXPECT_FALSE(m2.Matches(NULL)); |
1100 } | 1112 } |
1101 | 1113 |
1102 // Tests that HasSubstr(s) describes itself properly. | 1114 // Tests that HasSubstr(s) describes itself properly. |
1103 TEST(HasSubstrTest, CanDescribeSelf) { | 1115 TEST(HasSubstrTest, CanDescribeSelf) { |
1104 Matcher<string> m = HasSubstr("foo\n\""); | 1116 Matcher<string> m = HasSubstr("foo\n\""); |
1105 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); | 1117 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); |
1106 } | 1118 } |
1107 | 1119 |
1108 TEST(KeyTest, CanDescribeSelf) { | 1120 TEST(KeyTest, CanDescribeSelf) { |
1109 Matcher<const std::pair<std::string, int>&> m = Key("foo"); | 1121 Matcher<const pair<std::string, int>&> m = Key("foo"); |
1110 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); | 1122 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); |
| 1123 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m)); |
| 1124 } |
| 1125 |
| 1126 TEST(KeyTest, ExplainsResult) { |
| 1127 Matcher<pair<int, bool> > m = Key(GreaterThan(10)); |
| 1128 EXPECT_EQ("whose first field is a value which is 5 less than 10", |
| 1129 Explain(m, make_pair(5, true))); |
| 1130 EXPECT_EQ("whose first field is a value which is 5 more than 10", |
| 1131 Explain(m, make_pair(15, true))); |
1111 } | 1132 } |
1112 | 1133 |
1113 TEST(KeyTest, MatchesCorrectly) { | 1134 TEST(KeyTest, MatchesCorrectly) { |
1114 std::pair<int, std::string> p(25, "foo"); | 1135 pair<int, std::string> p(25, "foo"); |
1115 EXPECT_THAT(p, Key(25)); | 1136 EXPECT_THAT(p, Key(25)); |
1116 EXPECT_THAT(p, Not(Key(42))); | 1137 EXPECT_THAT(p, Not(Key(42))); |
1117 EXPECT_THAT(p, Key(Ge(20))); | 1138 EXPECT_THAT(p, Key(Ge(20))); |
1118 EXPECT_THAT(p, Not(Key(Lt(25)))); | 1139 EXPECT_THAT(p, Not(Key(Lt(25)))); |
1119 } | 1140 } |
1120 | 1141 |
1121 TEST(KeyTest, SafelyCastsInnerMatcher) { | 1142 TEST(KeyTest, SafelyCastsInnerMatcher) { |
1122 Matcher<int> is_positive = Gt(0); | 1143 Matcher<int> is_positive = Gt(0); |
1123 Matcher<int> is_negative = Lt(0); | 1144 Matcher<int> is_negative = Lt(0); |
1124 std::pair<char, bool> p('a', true); | 1145 pair<char, bool> p('a', true); |
1125 EXPECT_THAT(p, Key(is_positive)); | 1146 EXPECT_THAT(p, Key(is_positive)); |
1126 EXPECT_THAT(p, Not(Key(is_negative))); | 1147 EXPECT_THAT(p, Not(Key(is_negative))); |
1127 } | 1148 } |
1128 | 1149 |
1129 TEST(KeyTest, InsideContainsUsingMap) { | 1150 TEST(KeyTest, InsideContainsUsingMap) { |
1130 std::map<int, char> container; | 1151 std::map<int, char> container; |
1131 container.insert(std::make_pair(1, 'a')); | 1152 container.insert(make_pair(1, 'a')); |
1132 container.insert(std::make_pair(2, 'b')); | 1153 container.insert(make_pair(2, 'b')); |
1133 container.insert(std::make_pair(4, 'c')); | 1154 container.insert(make_pair(4, 'c')); |
1134 EXPECT_THAT(container, Contains(Key(1))); | 1155 EXPECT_THAT(container, Contains(Key(1))); |
1135 EXPECT_THAT(container, Not(Contains(Key(3)))); | 1156 EXPECT_THAT(container, Not(Contains(Key(3)))); |
1136 } | 1157 } |
1137 | 1158 |
1138 TEST(KeyTest, InsideContainsUsingMultimap) { | 1159 TEST(KeyTest, InsideContainsUsingMultimap) { |
1139 std::multimap<int, char> container; | 1160 std::multimap<int, char> container; |
1140 container.insert(std::make_pair(1, 'a')); | 1161 container.insert(make_pair(1, 'a')); |
1141 container.insert(std::make_pair(2, 'b')); | 1162 container.insert(make_pair(2, 'b')); |
1142 container.insert(std::make_pair(4, 'c')); | 1163 container.insert(make_pair(4, 'c')); |
1143 | 1164 |
1144 EXPECT_THAT(container, Not(Contains(Key(25)))); | 1165 EXPECT_THAT(container, Not(Contains(Key(25)))); |
1145 container.insert(std::make_pair(25, 'd')); | 1166 container.insert(make_pair(25, 'd')); |
1146 EXPECT_THAT(container, Contains(Key(25))); | 1167 EXPECT_THAT(container, Contains(Key(25))); |
1147 container.insert(std::make_pair(25, 'e')); | 1168 container.insert(make_pair(25, 'e')); |
1148 EXPECT_THAT(container, Contains(Key(25))); | 1169 EXPECT_THAT(container, Contains(Key(25))); |
1149 | 1170 |
1150 EXPECT_THAT(container, Contains(Key(1))); | 1171 EXPECT_THAT(container, Contains(Key(1))); |
1151 EXPECT_THAT(container, Not(Contains(Key(3)))); | 1172 EXPECT_THAT(container, Not(Contains(Key(3)))); |
1152 } | 1173 } |
1153 | 1174 |
1154 TEST(PairTest, Typing) { | 1175 TEST(PairTest, Typing) { |
1155 // Test verifies the following type conversions can be compiled. | 1176 // Test verifies the following type conversions can be compiled. |
1156 Matcher<const std::pair<const char*, int>&> m1 = Pair("foo", 42); | 1177 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42); |
1157 Matcher<const std::pair<const char*, int> > m2 = Pair("foo", 42); | 1178 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42); |
1158 Matcher<std::pair<const char*, int> > m3 = Pair("foo", 42); | 1179 Matcher<pair<const char*, int> > m3 = Pair("foo", 42); |
1159 | 1180 |
1160 Matcher<std::pair<int, const std::string> > m4 = Pair(25, "42"); | 1181 Matcher<pair<int, const std::string> > m4 = Pair(25, "42"); |
1161 Matcher<std::pair<const std::string, int> > m5 = Pair("25", 42); | 1182 Matcher<pair<const std::string, int> > m5 = Pair("25", 42); |
1162 } | 1183 } |
1163 | 1184 |
1164 TEST(PairTest, CanDescribeSelf) { | 1185 TEST(PairTest, CanDescribeSelf) { |
1165 Matcher<const std::pair<std::string, int>&> m1 = Pair("foo", 42); | 1186 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42); |
1166 EXPECT_EQ("has a first field that is equal to \"foo\"" | 1187 EXPECT_EQ("has a first field that is equal to \"foo\"" |
1167 ", and has a second field that is equal to 42", | 1188 ", and has a second field that is equal to 42", |
1168 Describe(m1)); | 1189 Describe(m1)); |
1169 EXPECT_EQ("has a first field that is not equal to \"foo\"" | 1190 EXPECT_EQ("has a first field that isn't equal to \"foo\"" |
1170 ", or has a second field that is not equal to 42", | 1191 ", or has a second field that isn't equal to 42", |
1171 DescribeNegation(m1)); | 1192 DescribeNegation(m1)); |
1172 // Double and triple negation (1 or 2 times not and description of negation). | 1193 // Double and triple negation (1 or 2 times not and description of negation). |
1173 Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42)); | 1194 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42)); |
1174 EXPECT_EQ("has a first field that is not equal to 13" | 1195 EXPECT_EQ("has a first field that isn't equal to 13" |
1175 ", and has a second field that is equal to 42", | 1196 ", and has a second field that is equal to 42", |
1176 DescribeNegation(m2)); | 1197 DescribeNegation(m2)); |
1177 } | 1198 } |
1178 | 1199 |
1179 TEST(PairTest, CanExplainMatchResultTo) { | 1200 TEST(PairTest, CanExplainMatchResultTo) { |
1180 // If neither field matches, Pair() should explain about the first | 1201 // If neither field matches, Pair() should explain about the first |
1181 // field. | 1202 // field. |
1182 const Matcher<std::pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0)); | 1203 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0)); |
1183 EXPECT_EQ("whose first field does not match, which is 1 less than 0", | 1204 EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
1184 Explain(m, std::make_pair(-1, -2))); | 1205 Explain(m, make_pair(-1, -2))); |
1185 | 1206 |
1186 // If the first field matches but the second doesn't, Pair() should | 1207 // If the first field matches but the second doesn't, Pair() should |
1187 // explain about the second field. | 1208 // explain about the second field. |
1188 EXPECT_EQ("whose second field does not match, which is 2 less than 0", | 1209 EXPECT_EQ("whose second field does not match, which is 2 less than 0", |
1189 Explain(m, std::make_pair(1, -2))); | 1210 Explain(m, make_pair(1, -2))); |
1190 | 1211 |
1191 // If the first field doesn't match but the second does, Pair() | 1212 // If the first field doesn't match but the second does, Pair() |
1192 // should explain about the first field. | 1213 // should explain about the first field. |
1193 EXPECT_EQ("whose first field does not match, which is 1 less than 0", | 1214 EXPECT_EQ("whose first field does not match, which is 1 less than 0", |
1194 Explain(m, std::make_pair(-1, 2))); | 1215 Explain(m, make_pair(-1, 2))); |
1195 | 1216 |
1196 // If both fields match, Pair() should explain about them both. | 1217 // If both fields match, Pair() should explain about them both. |
1197 EXPECT_EQ("whose both fields match, where the first field is a value " | 1218 EXPECT_EQ("whose both fields match, where the first field is a value " |
1198 "which is 1 more than 0, and the second field is a value " | 1219 "which is 1 more than 0, and the second field is a value " |
1199 "which is 2 more than 0", | 1220 "which is 2 more than 0", |
1200 Explain(m, std::make_pair(1, 2))); | 1221 Explain(m, make_pair(1, 2))); |
1201 | 1222 |
1202 // If only the first match has an explanation, only this explanation should | 1223 // If only the first match has an explanation, only this explanation should |
1203 // be printed. | 1224 // be printed. |
1204 const Matcher<std::pair<int, int> > explain_first = Pair(GreaterThan(0), 0); | 1225 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0); |
1205 EXPECT_EQ("whose both fields match, where the first field is a value " | 1226 EXPECT_EQ("whose both fields match, where the first field is a value " |
1206 "which is 1 more than 0", | 1227 "which is 1 more than 0", |
1207 Explain(explain_first, std::make_pair(1, 0))); | 1228 Explain(explain_first, make_pair(1, 0))); |
1208 | 1229 |
1209 // If only the second match has an explanation, only this explanation should | 1230 // If only the second match has an explanation, only this explanation should |
1210 // be printed. | 1231 // be printed. |
1211 const Matcher<std::pair<int, int> > explain_second = Pair(0, GreaterThan(0)); | 1232 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0)); |
1212 EXPECT_EQ("whose both fields match, where the second field is a value " | 1233 EXPECT_EQ("whose both fields match, where the second field is a value " |
1213 "which is 1 more than 0", | 1234 "which is 1 more than 0", |
1214 Explain(explain_second, std::make_pair(0, 1))); | 1235 Explain(explain_second, make_pair(0, 1))); |
1215 } | 1236 } |
1216 | 1237 |
1217 TEST(PairTest, MatchesCorrectly) { | 1238 TEST(PairTest, MatchesCorrectly) { |
1218 std::pair<int, std::string> p(25, "foo"); | 1239 pair<int, std::string> p(25, "foo"); |
1219 | 1240 |
1220 // Both fields match. | 1241 // Both fields match. |
1221 EXPECT_THAT(p, Pair(25, "foo")); | 1242 EXPECT_THAT(p, Pair(25, "foo")); |
1222 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); | 1243 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); |
1223 | 1244 |
1224 // 'first' doesnt' match, but 'second' matches. | 1245 // 'first' doesnt' match, but 'second' matches. |
1225 EXPECT_THAT(p, Not(Pair(42, "foo"))); | 1246 EXPECT_THAT(p, Not(Pair(42, "foo"))); |
1226 EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); | 1247 EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); |
1227 | 1248 |
1228 // 'first' matches, but 'second' doesn't match. | 1249 // 'first' matches, but 'second' doesn't match. |
1229 EXPECT_THAT(p, Not(Pair(25, "bar"))); | 1250 EXPECT_THAT(p, Not(Pair(25, "bar"))); |
1230 EXPECT_THAT(p, Not(Pair(25, Not("foo")))); | 1251 EXPECT_THAT(p, Not(Pair(25, Not("foo")))); |
1231 | 1252 |
1232 // Neither field matches. | 1253 // Neither field matches. |
1233 EXPECT_THAT(p, Not(Pair(13, "bar"))); | 1254 EXPECT_THAT(p, Not(Pair(13, "bar"))); |
1234 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); | 1255 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); |
1235 } | 1256 } |
1236 | 1257 |
1237 TEST(PairTest, SafelyCastsInnerMatchers) { | 1258 TEST(PairTest, SafelyCastsInnerMatchers) { |
1238 Matcher<int> is_positive = Gt(0); | 1259 Matcher<int> is_positive = Gt(0); |
1239 Matcher<int> is_negative = Lt(0); | 1260 Matcher<int> is_negative = Lt(0); |
1240 std::pair<char, bool> p('a', true); | 1261 pair<char, bool> p('a', true); |
1241 EXPECT_THAT(p, Pair(is_positive, _)); | 1262 EXPECT_THAT(p, Pair(is_positive, _)); |
1242 EXPECT_THAT(p, Not(Pair(is_negative, _))); | 1263 EXPECT_THAT(p, Not(Pair(is_negative, _))); |
1243 EXPECT_THAT(p, Pair(_, is_positive)); | 1264 EXPECT_THAT(p, Pair(_, is_positive)); |
1244 EXPECT_THAT(p, Not(Pair(_, is_negative))); | 1265 EXPECT_THAT(p, Not(Pair(_, is_negative))); |
1245 } | 1266 } |
1246 | 1267 |
1247 TEST(PairTest, InsideContainsUsingMap) { | 1268 TEST(PairTest, InsideContainsUsingMap) { |
1248 std::map<int, char> container; | 1269 std::map<int, char> container; |
1249 container.insert(std::make_pair(1, 'a')); | 1270 container.insert(make_pair(1, 'a')); |
1250 container.insert(std::make_pair(2, 'b')); | 1271 container.insert(make_pair(2, 'b')); |
1251 container.insert(std::make_pair(4, 'c')); | 1272 container.insert(make_pair(4, 'c')); |
1252 EXPECT_THAT(container, Contains(Pair(1, 'a'))); | 1273 EXPECT_THAT(container, Contains(Pair(1, 'a'))); |
1253 EXPECT_THAT(container, Contains(Pair(1, _))); | 1274 EXPECT_THAT(container, Contains(Pair(1, _))); |
1254 EXPECT_THAT(container, Contains(Pair(_, 'a'))); | 1275 EXPECT_THAT(container, Contains(Pair(_, 'a'))); |
1255 EXPECT_THAT(container, Not(Contains(Pair(3, _)))); | 1276 EXPECT_THAT(container, Not(Contains(Pair(3, _)))); |
1256 } | 1277 } |
1257 | 1278 |
1258 // Tests StartsWith(s). | 1279 // Tests StartsWith(s). |
1259 | 1280 |
1260 TEST(StartsWithTest, MatchesStringWithGivenPrefix) { | 1281 TEST(StartsWithTest, MatchesStringWithGivenPrefix) { |
1261 const Matcher<const char*> m1 = StartsWith(string("")); | 1282 const Matcher<const char*> m1 = StartsWith(string("")); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1390 EXPECT_TRUE(m.Matches(NULL)); | 1411 EXPECT_TRUE(m.Matches(NULL)); |
1391 EXPECT_FALSE(m.Matches(L"Hello")); | 1412 EXPECT_FALSE(m.Matches(L"Hello")); |
1392 | 1413 |
1393 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); | 1414 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); |
1394 EXPECT_TRUE(m2.Matches(L"hello")); | 1415 EXPECT_TRUE(m2.Matches(L"hello")); |
1395 EXPECT_FALSE(m2.Matches(L"Hello")); | 1416 EXPECT_FALSE(m2.Matches(L"Hello")); |
1396 } | 1417 } |
1397 | 1418 |
1398 TEST(StdWideStrNeTest, CanDescribeSelf) { | 1419 TEST(StdWideStrNeTest, CanDescribeSelf) { |
1399 Matcher<const wchar_t*> m = StrNe(L"Hi"); | 1420 Matcher<const wchar_t*> m = StrNe(L"Hi"); |
1400 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m)); | 1421 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); |
1401 } | 1422 } |
1402 | 1423 |
1403 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { | 1424 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
1404 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); | 1425 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); |
1405 EXPECT_TRUE(m.Matches(L"Hello")); | 1426 EXPECT_TRUE(m.Matches(L"Hello")); |
1406 EXPECT_TRUE(m.Matches(L"hello")); | 1427 EXPECT_TRUE(m.Matches(L"hello")); |
1407 EXPECT_FALSE(m.Matches(L"Hi")); | 1428 EXPECT_FALSE(m.Matches(L"Hi")); |
1408 EXPECT_FALSE(m.Matches(NULL)); | 1429 EXPECT_FALSE(m.Matches(NULL)); |
1409 | 1430 |
1410 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); | 1431 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1449 EXPECT_FALSE(m.Matches(L"Hello")); | 1470 EXPECT_FALSE(m.Matches(L"Hello")); |
1450 EXPECT_FALSE(m.Matches(L"hello")); | 1471 EXPECT_FALSE(m.Matches(L"hello")); |
1451 | 1472 |
1452 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); | 1473 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); |
1453 EXPECT_TRUE(m2.Matches(L"")); | 1474 EXPECT_TRUE(m2.Matches(L"")); |
1454 EXPECT_FALSE(m2.Matches(L"Hello")); | 1475 EXPECT_FALSE(m2.Matches(L"Hello")); |
1455 } | 1476 } |
1456 | 1477 |
1457 TEST(StdWideStrCaseNeTest, CanDescribeSelf) { | 1478 TEST(StdWideStrCaseNeTest, CanDescribeSelf) { |
1458 Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); | 1479 Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
1459 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m)); | 1480 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); |
1460 } | 1481 } |
1461 | 1482 |
1462 // Tests that HasSubstr() works for matching wstring-typed values. | 1483 // Tests that HasSubstr() works for matching wstring-typed values. |
1463 TEST(StdWideHasSubstrTest, WorksForStringClasses) { | 1484 TEST(StdWideHasSubstrTest, WorksForStringClasses) { |
1464 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo"); | 1485 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo"); |
1465 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); | 1486 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); |
1466 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); | 1487 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); |
1467 | 1488 |
1468 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); | 1489 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); |
1469 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); | 1490 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1581 EXPECT_TRUE(m.Matches(NULL)); | 1602 EXPECT_TRUE(m.Matches(NULL)); |
1582 EXPECT_FALSE(m.Matches(L"Hello")); | 1603 EXPECT_FALSE(m.Matches(L"Hello")); |
1583 | 1604 |
1584 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello")); | 1605 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello")); |
1585 EXPECT_TRUE(m2.Matches(L"hello")); | 1606 EXPECT_TRUE(m2.Matches(L"hello")); |
1586 EXPECT_FALSE(m2.Matches(L"Hello")); | 1607 EXPECT_FALSE(m2.Matches(L"Hello")); |
1587 } | 1608 } |
1588 | 1609 |
1589 TEST(GlobalWideStrNeTest, CanDescribeSelf) { | 1610 TEST(GlobalWideStrNeTest, CanDescribeSelf) { |
1590 Matcher<const wchar_t*> m = StrNe(L"Hi"); | 1611 Matcher<const wchar_t*> m = StrNe(L"Hi"); |
1591 EXPECT_EQ("is not equal to L\"Hi\"", Describe(m)); | 1612 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); |
1592 } | 1613 } |
1593 | 1614 |
1594 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { | 1615 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { |
1595 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello")); | 1616 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello")); |
1596 EXPECT_TRUE(m.Matches(L"Hello")); | 1617 EXPECT_TRUE(m.Matches(L"Hello")); |
1597 EXPECT_TRUE(m.Matches(L"hello")); | 1618 EXPECT_TRUE(m.Matches(L"hello")); |
1598 EXPECT_FALSE(m.Matches(L"Hi")); | 1619 EXPECT_FALSE(m.Matches(L"Hi")); |
1599 EXPECT_FALSE(m.Matches(NULL)); | 1620 EXPECT_FALSE(m.Matches(NULL)); |
1600 | 1621 |
1601 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello"); | 1622 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello"); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1640 EXPECT_FALSE(m.Matches(L"Hello")); | 1661 EXPECT_FALSE(m.Matches(L"Hello")); |
1641 EXPECT_FALSE(m.Matches(L"hello")); | 1662 EXPECT_FALSE(m.Matches(L"hello")); |
1642 | 1663 |
1643 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello")); | 1664 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello")); |
1644 EXPECT_TRUE(m2.Matches(L"")); | 1665 EXPECT_TRUE(m2.Matches(L"")); |
1645 EXPECT_FALSE(m2.Matches(L"Hello")); | 1666 EXPECT_FALSE(m2.Matches(L"Hello")); |
1646 } | 1667 } |
1647 | 1668 |
1648 TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) { | 1669 TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) { |
1649 Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); | 1670 Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); |
1650 EXPECT_EQ("is not equal to (ignoring case) L\"Hi\"", Describe(m)); | 1671 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); |
1651 } | 1672 } |
1652 | 1673 |
1653 // Tests that HasSubstr() works for matching wstring-typed values. | 1674 // Tests that HasSubstr() works for matching wstring-typed values. |
1654 TEST(GlobalWideHasSubstrTest, WorksForStringClasses) { | 1675 TEST(GlobalWideHasSubstrTest, WorksForStringClasses) { |
1655 const Matcher< ::wstring> m1 = HasSubstr(L"foo"); | 1676 const Matcher< ::wstring> m1 = HasSubstr(L"foo"); |
1656 EXPECT_TRUE(m1.Matches(::wstring(L"I love food."))); | 1677 EXPECT_TRUE(m1.Matches(::wstring(L"I love food."))); |
1657 EXPECT_FALSE(m1.Matches(::wstring(L"tofo"))); | 1678 EXPECT_FALSE(m1.Matches(::wstring(L"tofo"))); |
1658 | 1679 |
1659 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo"); | 1680 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo"); |
1660 EXPECT_TRUE(m2.Matches(::wstring(L"I love food."))); | 1681 EXPECT_TRUE(m2.Matches(::wstring(L"I love food."))); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1820 TEST(NotTest, NegatesMatcher) { | 1841 TEST(NotTest, NegatesMatcher) { |
1821 Matcher<int> m; | 1842 Matcher<int> m; |
1822 m = Not(Eq(2)); | 1843 m = Not(Eq(2)); |
1823 EXPECT_TRUE(m.Matches(3)); | 1844 EXPECT_TRUE(m.Matches(3)); |
1824 EXPECT_FALSE(m.Matches(2)); | 1845 EXPECT_FALSE(m.Matches(2)); |
1825 } | 1846 } |
1826 | 1847 |
1827 // Tests that Not(m) describes itself properly. | 1848 // Tests that Not(m) describes itself properly. |
1828 TEST(NotTest, CanDescribeSelf) { | 1849 TEST(NotTest, CanDescribeSelf) { |
1829 Matcher<int> m = Not(Eq(5)); | 1850 Matcher<int> m = Not(Eq(5)); |
1830 EXPECT_EQ("is not equal to 5", Describe(m)); | 1851 EXPECT_EQ("isn't equal to 5", Describe(m)); |
1831 } | 1852 } |
1832 | 1853 |
1833 // Tests that monomorphic matchers are safely cast by the Not matcher. | 1854 // Tests that monomorphic matchers are safely cast by the Not matcher. |
1834 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { | 1855 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { |
1835 // greater_than_5 is a monomorphic matcher. | 1856 // greater_than_5 is a monomorphic matcher. |
1836 Matcher<int> greater_than_5 = Gt(5); | 1857 Matcher<int> greater_than_5 = Gt(5); |
1837 | 1858 |
1838 Matcher<const int&> m = Not(greater_than_5); | 1859 Matcher<const int&> m = Not(greater_than_5); |
1839 Matcher<int&> m2 = Not(greater_than_5); | 1860 Matcher<int&> m2 = Not(greater_than_5); |
1840 Matcher<int&> m3 = Not(m); | 1861 Matcher<int&> m3 = Not(m); |
(...skipping 25 matching lines...) Expand all Loading... |
1866 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); | 1887 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
1867 EXPECT_TRUE(m.Matches(0)); | 1888 EXPECT_TRUE(m.Matches(0)); |
1868 EXPECT_TRUE(m.Matches(1)); | 1889 EXPECT_TRUE(m.Matches(1)); |
1869 EXPECT_FALSE(m.Matches(3)); | 1890 EXPECT_FALSE(m.Matches(3)); |
1870 } | 1891 } |
1871 | 1892 |
1872 // Tests that AllOf(m1, ..., mn) describes itself properly. | 1893 // Tests that AllOf(m1, ..., mn) describes itself properly. |
1873 TEST(AllOfTest, CanDescribeSelf) { | 1894 TEST(AllOfTest, CanDescribeSelf) { |
1874 Matcher<int> m; | 1895 Matcher<int> m; |
1875 m = AllOf(Le(2), Ge(1)); | 1896 m = AllOf(Le(2), Ge(1)); |
1876 EXPECT_EQ("(is less than or equal to 2) and " | 1897 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m)); |
1877 "(is greater than or equal to 1)", | |
1878 Describe(m)); | |
1879 | 1898 |
1880 m = AllOf(Gt(0), Ne(1), Ne(2)); | 1899 m = AllOf(Gt(0), Ne(1), Ne(2)); |
1881 EXPECT_EQ("(is greater than 0) and " | 1900 EXPECT_EQ("(is > 0) and " |
1882 "((is not equal to 1) and " | 1901 "((isn't equal to 1) and " |
1883 "(is not equal to 2))", | 1902 "(isn't equal to 2))", |
1884 Describe(m)); | 1903 Describe(m)); |
1885 | 1904 |
1886 | 1905 |
1887 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); | 1906 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
1888 EXPECT_EQ("(is greater than 0) and " | 1907 EXPECT_EQ("(is > 0) and " |
1889 "((is not equal to 1) and " | 1908 "((isn't equal to 1) and " |
1890 "((is not equal to 2) and " | 1909 "((isn't equal to 2) and " |
1891 "(is not equal to 3)))", | 1910 "(isn't equal to 3)))", |
1892 Describe(m)); | 1911 Describe(m)); |
1893 | 1912 |
1894 | 1913 |
1895 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); | 1914 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
1896 EXPECT_EQ("(is greater than or equal to 0) and " | 1915 EXPECT_EQ("(is >= 0) and " |
1897 "((is less than 10) and " | 1916 "((is < 10) and " |
1898 "((is not equal to 3) and " | 1917 "((isn't equal to 3) and " |
1899 "((is not equal to 5) and " | 1918 "((isn't equal to 5) and " |
1900 "(is not equal to 7))))", Describe(m)); | 1919 "(isn't equal to 7))))", |
| 1920 Describe(m)); |
| 1921 } |
| 1922 |
| 1923 // Tests that AllOf(m1, ..., mn) describes its negation properly. |
| 1924 TEST(AllOfTest, CanDescribeNegation) { |
| 1925 Matcher<int> m; |
| 1926 m = AllOf(Le(2), Ge(1)); |
| 1927 EXPECT_EQ("(isn't <= 2) or " |
| 1928 "(isn't >= 1)", |
| 1929 DescribeNegation(m)); |
| 1930 |
| 1931 m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 1932 EXPECT_EQ("(isn't > 0) or " |
| 1933 "((is equal to 1) or " |
| 1934 "(is equal to 2))", |
| 1935 DescribeNegation(m)); |
| 1936 |
| 1937 |
| 1938 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 1939 EXPECT_EQ("(isn't > 0) or " |
| 1940 "((is equal to 1) or " |
| 1941 "((is equal to 2) or " |
| 1942 "(is equal to 3)))", |
| 1943 DescribeNegation(m)); |
| 1944 |
| 1945 |
| 1946 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 1947 EXPECT_EQ("(isn't >= 0) or " |
| 1948 "((isn't < 10) or " |
| 1949 "((is equal to 3) or " |
| 1950 "((is equal to 5) or " |
| 1951 "(is equal to 7))))", |
| 1952 DescribeNegation(m)); |
1901 } | 1953 } |
1902 | 1954 |
1903 // Tests that monomorphic matchers are safely cast by the AllOf matcher. | 1955 // Tests that monomorphic matchers are safely cast by the AllOf matcher. |
1904 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { | 1956 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { |
1905 // greater_than_5 and less_than_10 are monomorphic matchers. | 1957 // greater_than_5 and less_than_10 are monomorphic matchers. |
1906 Matcher<int> greater_than_5 = Gt(5); | 1958 Matcher<int> greater_than_5 = Gt(5); |
1907 Matcher<int> less_than_10 = Lt(10); | 1959 Matcher<int> less_than_10 = Lt(10); |
1908 | 1960 |
1909 Matcher<const int&> m = AllOf(greater_than_5, less_than_10); | 1961 Matcher<const int&> m = AllOf(greater_than_5, less_than_10); |
1910 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); | 1962 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); |
1911 Matcher<int&> m3 = AllOf(greater_than_5, m2); | 1963 Matcher<int&> m3 = AllOf(greater_than_5, m2); |
1912 | 1964 |
1913 // Tests that BothOf works when composing itself. | 1965 // Tests that BothOf works when composing itself. |
1914 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); | 1966 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); |
1915 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); | 1967 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); |
1916 } | 1968 } |
1917 | 1969 |
| 1970 TEST(AllOfTest, ExplainsResult) { |
| 1971 Matcher<int> m; |
| 1972 |
| 1973 // Successful match. Both matchers need to explain. The second |
| 1974 // matcher doesn't give an explanation, so only the first matcher's |
| 1975 // explanation is printed. |
| 1976 m = AllOf(GreaterThan(10), Lt(30)); |
| 1977 EXPECT_EQ("which is 15 more than 10", Explain(m, 25)); |
| 1978 |
| 1979 // Successful match. Both matchers need to explain. |
| 1980 m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 1981 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20", |
| 1982 Explain(m, 30)); |
| 1983 |
| 1984 // Successful match. All matchers need to explain. The second |
| 1985 // matcher doesn't given an explanation. |
| 1986 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); |
| 1987 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20", |
| 1988 Explain(m, 25)); |
| 1989 |
| 1990 // Successful match. All matchers need to explain. |
| 1991 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 1992 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, " |
| 1993 "and which is 10 more than 30", |
| 1994 Explain(m, 40)); |
| 1995 |
| 1996 // Failed match. The first matcher, which failed, needs to |
| 1997 // explain. |
| 1998 m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 1999 EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2000 |
| 2001 // Failed match. The second matcher, which failed, needs to |
| 2002 // explain. Since it doesn't given an explanation, nothing is |
| 2003 // printed. |
| 2004 m = AllOf(GreaterThan(10), Lt(30)); |
| 2005 EXPECT_EQ("", Explain(m, 40)); |
| 2006 |
| 2007 // Failed match. The second matcher, which failed, needs to |
| 2008 // explain. |
| 2009 m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 2010 EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); |
| 2011 } |
| 2012 |
1918 // Tests that AnyOf(m1, ..., mn) matches any value that matches at | 2013 // Tests that AnyOf(m1, ..., mn) matches any value that matches at |
1919 // least one of the given matchers. | 2014 // least one of the given matchers. |
1920 TEST(AnyOfTest, MatchesWhenAnyMatches) { | 2015 TEST(AnyOfTest, MatchesWhenAnyMatches) { |
1921 Matcher<int> m; | 2016 Matcher<int> m; |
1922 m = AnyOf(Le(1), Ge(3)); | 2017 m = AnyOf(Le(1), Ge(3)); |
1923 EXPECT_TRUE(m.Matches(1)); | 2018 EXPECT_TRUE(m.Matches(1)); |
1924 EXPECT_TRUE(m.Matches(4)); | 2019 EXPECT_TRUE(m.Matches(4)); |
1925 EXPECT_FALSE(m.Matches(2)); | 2020 EXPECT_FALSE(m.Matches(2)); |
1926 | 2021 |
1927 m = AnyOf(Lt(0), Eq(1), Eq(2)); | 2022 m = AnyOf(Lt(0), Eq(1), Eq(2)); |
(...skipping 13 matching lines...) Expand all Loading... |
1941 EXPECT_TRUE(m.Matches(0)); | 2036 EXPECT_TRUE(m.Matches(0)); |
1942 EXPECT_TRUE(m.Matches(11)); | 2037 EXPECT_TRUE(m.Matches(11)); |
1943 EXPECT_TRUE(m.Matches(3)); | 2038 EXPECT_TRUE(m.Matches(3)); |
1944 EXPECT_FALSE(m.Matches(2)); | 2039 EXPECT_FALSE(m.Matches(2)); |
1945 } | 2040 } |
1946 | 2041 |
1947 // Tests that AnyOf(m1, ..., mn) describes itself properly. | 2042 // Tests that AnyOf(m1, ..., mn) describes itself properly. |
1948 TEST(AnyOfTest, CanDescribeSelf) { | 2043 TEST(AnyOfTest, CanDescribeSelf) { |
1949 Matcher<int> m; | 2044 Matcher<int> m; |
1950 m = AnyOf(Le(1), Ge(3)); | 2045 m = AnyOf(Le(1), Ge(3)); |
1951 EXPECT_EQ("(is less than or equal to 1) or " | 2046 EXPECT_EQ("(is <= 1) or (is >= 3)", |
1952 "(is greater than or equal to 3)", | |
1953 Describe(m)); | 2047 Describe(m)); |
1954 | 2048 |
1955 m = AnyOf(Lt(0), Eq(1), Eq(2)); | 2049 m = AnyOf(Lt(0), Eq(1), Eq(2)); |
1956 EXPECT_EQ("(is less than 0) or " | 2050 EXPECT_EQ("(is < 0) or " |
1957 "((is equal to 1) or (is equal to 2))", | 2051 "((is equal to 1) or (is equal to 2))", |
1958 Describe(m)); | 2052 Describe(m)); |
1959 | 2053 |
1960 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); | 2054 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
1961 EXPECT_EQ("(is less than 0) or " | 2055 EXPECT_EQ("(is < 0) or " |
1962 "((is equal to 1) or " | 2056 "((is equal to 1) or " |
1963 "((is equal to 2) or " | 2057 "((is equal to 2) or " |
1964 "(is equal to 3)))", | 2058 "(is equal to 3)))", |
1965 Describe(m)); | 2059 Describe(m)); |
1966 | 2060 |
1967 m = AnyOf(Le(0), Gt(10), 3, 5, 7); | 2061 m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
1968 EXPECT_EQ("(is less than or equal to 0) or " | 2062 EXPECT_EQ("(is <= 0) or " |
1969 "((is greater than 10) or " | 2063 "((is > 10) or " |
1970 "((is equal to 3) or " | 2064 "((is equal to 3) or " |
1971 "((is equal to 5) or " | 2065 "((is equal to 5) or " |
1972 "(is equal to 7))))", | 2066 "(is equal to 7))))", |
1973 Describe(m)); | 2067 Describe(m)); |
1974 } | 2068 } |
1975 | 2069 |
| 2070 // Tests that AnyOf(m1, ..., mn) describes its negation properly. |
| 2071 TEST(AnyOfTest, CanDescribeNegation) { |
| 2072 Matcher<int> m; |
| 2073 m = AnyOf(Le(1), Ge(3)); |
| 2074 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", |
| 2075 DescribeNegation(m)); |
| 2076 |
| 2077 m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 2078 EXPECT_EQ("(isn't < 0) and " |
| 2079 "((isn't equal to 1) and (isn't equal to 2))", |
| 2080 DescribeNegation(m)); |
| 2081 |
| 2082 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 2083 EXPECT_EQ("(isn't < 0) and " |
| 2084 "((isn't equal to 1) and " |
| 2085 "((isn't equal to 2) and " |
| 2086 "(isn't equal to 3)))", |
| 2087 DescribeNegation(m)); |
| 2088 |
| 2089 m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 2090 EXPECT_EQ("(isn't <= 0) and " |
| 2091 "((isn't > 10) and " |
| 2092 "((isn't equal to 3) and " |
| 2093 "((isn't equal to 5) and " |
| 2094 "(isn't equal to 7))))", |
| 2095 DescribeNegation(m)); |
| 2096 } |
| 2097 |
1976 // Tests that monomorphic matchers are safely cast by the AnyOf matcher. | 2098 // Tests that monomorphic matchers are safely cast by the AnyOf matcher. |
1977 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { | 2099 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { |
1978 // greater_than_5 and less_than_10 are monomorphic matchers. | 2100 // greater_than_5 and less_than_10 are monomorphic matchers. |
1979 Matcher<int> greater_than_5 = Gt(5); | 2101 Matcher<int> greater_than_5 = Gt(5); |
1980 Matcher<int> less_than_10 = Lt(10); | 2102 Matcher<int> less_than_10 = Lt(10); |
1981 | 2103 |
1982 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); | 2104 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); |
1983 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); | 2105 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); |
1984 Matcher<int&> m3 = AnyOf(greater_than_5, m2); | 2106 Matcher<int&> m3 = AnyOf(greater_than_5, m2); |
1985 | 2107 |
1986 // Tests that EitherOf works when composing itself. | 2108 // Tests that EitherOf works when composing itself. |
1987 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); | 2109 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); |
1988 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); | 2110 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); |
1989 } | 2111 } |
1990 | 2112 |
| 2113 TEST(AnyOfTest, ExplainsResult) { |
| 2114 Matcher<int> m; |
| 2115 |
| 2116 // Failed match. Both matchers need to explain. The second |
| 2117 // matcher doesn't give an explanation, so only the first matcher's |
| 2118 // explanation is printed. |
| 2119 m = AnyOf(GreaterThan(10), Lt(0)); |
| 2120 EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 2121 |
| 2122 // Failed match. Both matchers need to explain. |
| 2123 m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2124 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", |
| 2125 Explain(m, 5)); |
| 2126 |
| 2127 // Failed match. All matchers need to explain. The second |
| 2128 // matcher doesn't given an explanation. |
| 2129 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); |
| 2130 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30", |
| 2131 Explain(m, 5)); |
| 2132 |
| 2133 // Failed match. All matchers need to explain. |
| 2134 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 2135 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, " |
| 2136 "and which is 25 less than 30", |
| 2137 Explain(m, 5)); |
| 2138 |
| 2139 // Successful match. The first matcher, which succeeded, needs to |
| 2140 // explain. |
| 2141 m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 2142 EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); |
| 2143 |
| 2144 // Successful match. The second matcher, which succeeded, needs to |
| 2145 // explain. Since it doesn't given an explanation, nothing is |
| 2146 // printed. |
| 2147 m = AnyOf(GreaterThan(10), Lt(30)); |
| 2148 EXPECT_EQ("", Explain(m, 0)); |
| 2149 |
| 2150 // Successful match. The second matcher, which succeeded, needs to |
| 2151 // explain. |
| 2152 m = AnyOf(GreaterThan(30), GreaterThan(20)); |
| 2153 EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); |
| 2154 } |
| 2155 |
1991 // The following predicate function and predicate functor are for | 2156 // The following predicate function and predicate functor are for |
1992 // testing the Truly(predicate) matcher. | 2157 // testing the Truly(predicate) matcher. |
1993 | 2158 |
1994 // Returns non-zero if the input is positive. Note that the return | 2159 // Returns non-zero if the input is positive. Note that the return |
1995 // type of this function is not bool. It's OK as Truly() accepts any | 2160 // type of this function is not bool. It's OK as Truly() accepts any |
1996 // unary function or functor whose return type can be implicitly | 2161 // unary function or functor whose return type can be implicitly |
1997 // converted to bool. | 2162 // converted to bool. |
1998 int IsPositive(double x) { | 2163 int IsPositive(double x) { |
1999 return x > 0 ? 1 : 0; | 2164 return x > 0 ? 1 : 0; |
2000 } | 2165 } |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2174 static int n; | 2339 static int n; |
2175 n = 5; | 2340 n = 5; |
2176 | 2341 |
2177 // VC++ prior to version 8.0 SP1 has a bug where it will not see any | 2342 // VC++ prior to version 8.0 SP1 has a bug where it will not see any |
2178 // functions declared in the namespace scope from within nested classes. | 2343 // functions declared in the namespace scope from within nested classes. |
2179 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all | 2344 // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all |
2180 // namespace-level functions invoked inside them need to be explicitly | 2345 // namespace-level functions invoked inside them need to be explicitly |
2181 // resolved. | 2346 // resolved. |
2182 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)), | 2347 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)), |
2183 "Value of: n\n" | 2348 "Value of: n\n" |
2184 "Expected: is greater than 10\n" | 2349 "Expected: is > 10\n" |
2185 " Actual: 5"); | 2350 " Actual: 5"); |
2186 n = 0; | 2351 n = 0; |
2187 EXPECT_NONFATAL_FAILURE( | 2352 EXPECT_NONFATAL_FAILURE( |
2188 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))), | 2353 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))), |
2189 "Value of: n\n" | 2354 "Value of: n\n" |
2190 "Expected: (is less than or equal to 7) and " | 2355 "Expected: (is <= 7) and (is >= 5)\n" |
2191 "(is greater than or equal to 5)\n" | |
2192 " Actual: 0"); | 2356 " Actual: 0"); |
2193 } | 2357 } |
2194 | 2358 |
2195 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument | 2359 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument |
2196 // has a reference type. | 2360 // has a reference type. |
2197 TEST(MatcherAssertionTest, WorksForByRefArguments) { | 2361 TEST(MatcherAssertionTest, WorksForByRefArguments) { |
2198 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot | 2362 // We use a static variable here as EXPECT_FATAL_FAILURE() cannot |
2199 // reference auto variables. | 2363 // reference auto variables. |
2200 static int n; | 2364 static int n; |
2201 n = 0; | 2365 n = 0; |
2202 EXPECT_THAT(n, AllOf(Le(7), Ref(n))); | 2366 EXPECT_THAT(n, AllOf(Le(7), Ref(n))); |
2203 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), | 2367 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), |
2204 "Value of: n\n" | 2368 "Value of: n\n" |
2205 "Expected: does not reference the variable @"); | 2369 "Expected: does not reference the variable @"); |
2206 // Tests the "Actual" part. | 2370 // Tests the "Actual" part. |
2207 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), | 2371 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), |
2208 "Actual: 0 (is located @"); | 2372 "Actual: 0, which is located @"); |
2209 } | 2373 } |
2210 | 2374 |
2211 #if !GTEST_OS_SYMBIAN | 2375 #if !GTEST_OS_SYMBIAN |
2212 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is | 2376 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is |
2213 // monomorphic. | 2377 // monomorphic. |
2214 | 2378 |
2215 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's | 2379 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's |
2216 // Symbian compiler: it tries to compile | 2380 // Symbian compiler: it tries to compile |
2217 // template<T, U> class MatcherCastImpl { ... | 2381 // template<T, U> class MatcherCastImpl { ... |
2218 // virtual bool MatchAndExplain(T x, ...) const { | 2382 // virtual bool MatchAndExplain(T x, ...) const { |
2219 // return source_matcher_.MatchAndExplain(static_cast<U>(x), ...); | 2383 // return source_matcher_.MatchAndExplain(static_cast<U>(x), ...); |
2220 // with U == string and T == const char* | 2384 // with U == string and T == const char* |
2221 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) | 2385 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) |
2222 // the compiler silently crashes with no output. | 2386 // the compiler silently crashes with no output. |
2223 // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) | 2387 // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) |
2224 // the code compiles but the converted string is bogus. | 2388 // the code compiles but the converted string is bogus. |
2225 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { | 2389 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { |
2226 Matcher<const char*> starts_with_he = StartsWith("he"); | 2390 Matcher<const char*> starts_with_he = StartsWith("he"); |
2227 ASSERT_THAT("hello", starts_with_he); | 2391 ASSERT_THAT("hello", starts_with_he); |
2228 | 2392 |
2229 Matcher<const string&> ends_with_ok = EndsWith("ok"); | 2393 Matcher<const string&> ends_with_ok = EndsWith("ok"); |
2230 ASSERT_THAT("book", ends_with_ok); | 2394 ASSERT_THAT("book", ends_with_ok); |
2231 | 2395 |
2232 Matcher<int> is_greater_than_5 = Gt(5); | 2396 Matcher<int> is_greater_than_5 = Gt(5); |
2233 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), | 2397 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5), |
2234 "Value of: 5\n" | 2398 "Value of: 5\n" |
2235 "Expected: is greater than 5\n" | 2399 "Expected: is > 5\n" |
2236 " Actual: 5"); | 2400 " Actual: 5"); |
2237 } | 2401 } |
2238 #endif // !GTEST_OS_SYMBIAN | 2402 #endif // !GTEST_OS_SYMBIAN |
2239 | 2403 |
2240 // Tests floating-point matchers. | 2404 // Tests floating-point matchers. |
2241 template <typename RawType> | 2405 template <typename RawType> |
2242 class FloatingPointTest : public testing::Test { | 2406 class FloatingPointTest : public testing::Test { |
2243 protected: | 2407 protected: |
2244 typedef typename testing::internal::FloatingPoint<RawType> Floating; | 2408 typedef typename testing::internal::FloatingPoint<RawType> Floating; |
2245 typedef typename Floating::Bits Bits; | 2409 typedef typename Floating::Bits Bits; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2400 // NanSensitiveFloatEq will match NaN. | 2564 // NanSensitiveFloatEq will match NaN. |
2401 Matcher<float> m = NanSensitiveFloatEq(nan1_); | 2565 Matcher<float> m = NanSensitiveFloatEq(nan1_); |
2402 EXPECT_TRUE(m.Matches(nan1_)); | 2566 EXPECT_TRUE(m.Matches(nan1_)); |
2403 EXPECT_TRUE(m.Matches(nan2_)); | 2567 EXPECT_TRUE(m.Matches(nan2_)); |
2404 EXPECT_FALSE(m.Matches(1.0)); | 2568 EXPECT_FALSE(m.Matches(1.0)); |
2405 } | 2569 } |
2406 | 2570 |
2407 TEST_F(FloatTest, FloatEqCanDescribeSelf) { | 2571 TEST_F(FloatTest, FloatEqCanDescribeSelf) { |
2408 Matcher<float> m1 = FloatEq(2.0f); | 2572 Matcher<float> m1 = FloatEq(2.0f); |
2409 EXPECT_EQ("is approximately 2", Describe(m1)); | 2573 EXPECT_EQ("is approximately 2", Describe(m1)); |
2410 EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); | 2574 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
2411 | 2575 |
2412 Matcher<float> m2 = FloatEq(0.5f); | 2576 Matcher<float> m2 = FloatEq(0.5f); |
2413 EXPECT_EQ("is approximately 0.5", Describe(m2)); | 2577 EXPECT_EQ("is approximately 0.5", Describe(m2)); |
2414 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); | 2578 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
2415 | 2579 |
2416 Matcher<float> m3 = FloatEq(nan1_); | 2580 Matcher<float> m3 = FloatEq(nan1_); |
2417 EXPECT_EQ("never matches", Describe(m3)); | 2581 EXPECT_EQ("never matches", Describe(m3)); |
2418 EXPECT_EQ("is anything", DescribeNegation(m3)); | 2582 EXPECT_EQ("is anything", DescribeNegation(m3)); |
2419 } | 2583 } |
2420 | 2584 |
2421 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { | 2585 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { |
2422 Matcher<float> m1 = NanSensitiveFloatEq(2.0f); | 2586 Matcher<float> m1 = NanSensitiveFloatEq(2.0f); |
2423 EXPECT_EQ("is approximately 2", Describe(m1)); | 2587 EXPECT_EQ("is approximately 2", Describe(m1)); |
2424 EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); | 2588 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
2425 | 2589 |
2426 Matcher<float> m2 = NanSensitiveFloatEq(0.5f); | 2590 Matcher<float> m2 = NanSensitiveFloatEq(0.5f); |
2427 EXPECT_EQ("is approximately 0.5", Describe(m2)); | 2591 EXPECT_EQ("is approximately 0.5", Describe(m2)); |
2428 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); | 2592 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
2429 | 2593 |
2430 Matcher<float> m3 = NanSensitiveFloatEq(nan1_); | 2594 Matcher<float> m3 = NanSensitiveFloatEq(nan1_); |
2431 EXPECT_EQ("is NaN", Describe(m3)); | 2595 EXPECT_EQ("is NaN", Describe(m3)); |
2432 EXPECT_EQ("is not NaN", DescribeNegation(m3)); | 2596 EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
2433 } | 2597 } |
2434 | 2598 |
2435 // Instantiate FloatingPointTest for testing doubles. | 2599 // Instantiate FloatingPointTest for testing doubles. |
2436 typedef FloatingPointTest<double> DoubleTest; | 2600 typedef FloatingPointTest<double> DoubleTest; |
2437 | 2601 |
2438 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { | 2602 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { |
2439 TestMatches(&DoubleEq); | 2603 TestMatches(&DoubleEq); |
2440 } | 2604 } |
2441 | 2605 |
2442 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { | 2606 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { |
(...skipping 12 matching lines...) Expand all Loading... |
2455 // NanSensitiveDoubleEq will match NaN. | 2619 // NanSensitiveDoubleEq will match NaN. |
2456 Matcher<double> m = NanSensitiveDoubleEq(nan1_); | 2620 Matcher<double> m = NanSensitiveDoubleEq(nan1_); |
2457 EXPECT_TRUE(m.Matches(nan1_)); | 2621 EXPECT_TRUE(m.Matches(nan1_)); |
2458 EXPECT_TRUE(m.Matches(nan2_)); | 2622 EXPECT_TRUE(m.Matches(nan2_)); |
2459 EXPECT_FALSE(m.Matches(1.0)); | 2623 EXPECT_FALSE(m.Matches(1.0)); |
2460 } | 2624 } |
2461 | 2625 |
2462 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { | 2626 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { |
2463 Matcher<double> m1 = DoubleEq(2.0); | 2627 Matcher<double> m1 = DoubleEq(2.0); |
2464 EXPECT_EQ("is approximately 2", Describe(m1)); | 2628 EXPECT_EQ("is approximately 2", Describe(m1)); |
2465 EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); | 2629 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
2466 | 2630 |
2467 Matcher<double> m2 = DoubleEq(0.5); | 2631 Matcher<double> m2 = DoubleEq(0.5); |
2468 EXPECT_EQ("is approximately 0.5", Describe(m2)); | 2632 EXPECT_EQ("is approximately 0.5", Describe(m2)); |
2469 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); | 2633 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
2470 | 2634 |
2471 Matcher<double> m3 = DoubleEq(nan1_); | 2635 Matcher<double> m3 = DoubleEq(nan1_); |
2472 EXPECT_EQ("never matches", Describe(m3)); | 2636 EXPECT_EQ("never matches", Describe(m3)); |
2473 EXPECT_EQ("is anything", DescribeNegation(m3)); | 2637 EXPECT_EQ("is anything", DescribeNegation(m3)); |
2474 } | 2638 } |
2475 | 2639 |
2476 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { | 2640 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { |
2477 Matcher<double> m1 = NanSensitiveDoubleEq(2.0); | 2641 Matcher<double> m1 = NanSensitiveDoubleEq(2.0); |
2478 EXPECT_EQ("is approximately 2", Describe(m1)); | 2642 EXPECT_EQ("is approximately 2", Describe(m1)); |
2479 EXPECT_EQ("is not approximately 2", DescribeNegation(m1)); | 2643 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
2480 | 2644 |
2481 Matcher<double> m2 = NanSensitiveDoubleEq(0.5); | 2645 Matcher<double> m2 = NanSensitiveDoubleEq(0.5); |
2482 EXPECT_EQ("is approximately 0.5", Describe(m2)); | 2646 EXPECT_EQ("is approximately 0.5", Describe(m2)); |
2483 EXPECT_EQ("is not approximately 0.5", DescribeNegation(m2)); | 2647 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
2484 | 2648 |
2485 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); | 2649 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); |
2486 EXPECT_EQ("is NaN", Describe(m3)); | 2650 EXPECT_EQ("is NaN", Describe(m3)); |
2487 EXPECT_EQ("is not NaN", DescribeNegation(m3)); | 2651 EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
2488 } | 2652 } |
2489 | 2653 |
2490 TEST(PointeeTest, RawPointer) { | 2654 TEST(PointeeTest, RawPointer) { |
2491 const Matcher<int*> m = Pointee(Ge(0)); | 2655 const Matcher<int*> m = Pointee(Ge(0)); |
2492 | 2656 |
2493 int n = 1; | 2657 int n = 1; |
2494 EXPECT_TRUE(m.Matches(&n)); | 2658 EXPECT_TRUE(m.Matches(&n)); |
2495 n = -1; | 2659 n = -1; |
2496 EXPECT_FALSE(m.Matches(&n)); | 2660 EXPECT_FALSE(m.Matches(&n)); |
2497 EXPECT_FALSE(m.Matches(NULL)); | 2661 EXPECT_FALSE(m.Matches(NULL)); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2540 | 2704 |
2541 int n = 5; | 2705 int n = 5; |
2542 EXPECT_TRUE(m.Matches(&n)); | 2706 EXPECT_TRUE(m.Matches(&n)); |
2543 n = -1; | 2707 n = -1; |
2544 EXPECT_FALSE(m.Matches(&n)); | 2708 EXPECT_FALSE(m.Matches(&n)); |
2545 EXPECT_FALSE(m.Matches(NULL)); | 2709 EXPECT_FALSE(m.Matches(NULL)); |
2546 } | 2710 } |
2547 | 2711 |
2548 TEST(PointeeTest, CanDescribeSelf) { | 2712 TEST(PointeeTest, CanDescribeSelf) { |
2549 const Matcher<int*> m = Pointee(Gt(3)); | 2713 const Matcher<int*> m = Pointee(Gt(3)); |
2550 EXPECT_EQ("points to a value that is greater than 3", Describe(m)); | 2714 EXPECT_EQ("points to a value that is > 3", Describe(m)); |
2551 EXPECT_EQ("does not point to a value that is greater than 3", | 2715 EXPECT_EQ("does not point to a value that is > 3", |
2552 DescribeNegation(m)); | 2716 DescribeNegation(m)); |
2553 } | 2717 } |
2554 | 2718 |
2555 TEST(PointeeTest, CanExplainMatchResult) { | 2719 TEST(PointeeTest, CanExplainMatchResult) { |
2556 const Matcher<const string*> m = Pointee(StartsWith("Hi")); | 2720 const Matcher<const string*> m = Pointee(StartsWith("Hi")); |
2557 | 2721 |
2558 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); | 2722 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL))); |
2559 | 2723 |
2560 const Matcher<int*> m2 = Pointee(GreaterThan(1)); | 2724 const Matcher<int*> m2 = Pointee(GreaterThan(1)); |
2561 int n = 3; | 2725 int n = 3; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2686 AStruct a; | 2850 AStruct a; |
2687 EXPECT_TRUE(m.Matches(a)); | 2851 EXPECT_TRUE(m.Matches(a)); |
2688 a.x = -1; | 2852 a.x = -1; |
2689 EXPECT_FALSE(m.Matches(a)); | 2853 EXPECT_FALSE(m.Matches(a)); |
2690 } | 2854 } |
2691 | 2855 |
2692 // Tests that Field() can describe itself. | 2856 // Tests that Field() can describe itself. |
2693 TEST(FieldTest, CanDescribeSelf) { | 2857 TEST(FieldTest, CanDescribeSelf) { |
2694 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); | 2858 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
2695 | 2859 |
2696 EXPECT_EQ("is an object whose given field is greater than or equal to 0", | 2860 EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
2697 Describe(m)); | 2861 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
2698 EXPECT_EQ("is an object whose given field is not greater than or equal to 0", | |
2699 DescribeNegation(m)); | |
2700 } | 2862 } |
2701 | 2863 |
2702 // Tests that Field() can explain the match result. | 2864 // Tests that Field() can explain the match result. |
2703 TEST(FieldTest, CanExplainMatchResult) { | 2865 TEST(FieldTest, CanExplainMatchResult) { |
2704 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); | 2866 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0)); |
2705 | 2867 |
2706 AStruct a; | 2868 AStruct a; |
2707 a.x = 1; | 2869 a.x = 1; |
2708 EXPECT_EQ("whose given field is 1", Explain(m, a)); | 2870 EXPECT_EQ("whose given field is 1", Explain(m, a)); |
2709 | 2871 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2757 DerivedStruct d; | 2919 DerivedStruct d; |
2758 EXPECT_TRUE(m.Matches(&d)); | 2920 EXPECT_TRUE(m.Matches(&d)); |
2759 d.x = -1; | 2921 d.x = -1; |
2760 EXPECT_FALSE(m.Matches(&d)); | 2922 EXPECT_FALSE(m.Matches(&d)); |
2761 } | 2923 } |
2762 | 2924 |
2763 // Tests that Field() can describe itself when used to match a pointer. | 2925 // Tests that Field() can describe itself when used to match a pointer. |
2764 TEST(FieldForPointerTest, CanDescribeSelf) { | 2926 TEST(FieldForPointerTest, CanDescribeSelf) { |
2765 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); | 2927 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
2766 | 2928 |
2767 EXPECT_EQ("is an object whose given field is greater than or equal to 0", | 2929 EXPECT_EQ("is an object whose given field is >= 0", Describe(m)); |
2768 Describe(m)); | 2930 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m)); |
2769 EXPECT_EQ("is an object whose given field is not greater than or equal to 0", | |
2770 DescribeNegation(m)); | |
2771 } | 2931 } |
2772 | 2932 |
2773 // Tests that Field() can explain the result of matching a pointer. | 2933 // Tests that Field() can explain the result of matching a pointer. |
2774 TEST(FieldForPointerTest, CanExplainMatchResult) { | 2934 TEST(FieldForPointerTest, CanExplainMatchResult) { |
2775 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); | 2935 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0)); |
2776 | 2936 |
2777 AStruct a; | 2937 AStruct a; |
2778 a.x = 1; | 2938 a.x = 1; |
2779 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL))); | 2939 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL))); |
2780 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a)); | 2940 EXPECT_EQ("which points to an object whose given field is 1", Explain(m, &a)); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2893 AClass a; | 3053 AClass a; |
2894 EXPECT_TRUE(m.Matches(a)); | 3054 EXPECT_TRUE(m.Matches(a)); |
2895 a.set_n(-1); | 3055 a.set_n(-1); |
2896 EXPECT_FALSE(m.Matches(a)); | 3056 EXPECT_FALSE(m.Matches(a)); |
2897 } | 3057 } |
2898 | 3058 |
2899 // Tests that Property() can describe itself. | 3059 // Tests that Property() can describe itself. |
2900 TEST(PropertyTest, CanDescribeSelf) { | 3060 TEST(PropertyTest, CanDescribeSelf) { |
2901 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); | 3061 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
2902 | 3062 |
2903 EXPECT_EQ("is an object whose given property is greater than or equal to 0", | 3063 EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
2904 Describe(m)); | 3064 EXPECT_EQ("is an object whose given property isn't >= 0", |
2905 EXPECT_EQ("is an object whose given property " | 3065 DescribeNegation(m)); |
2906 "is not greater than or equal to 0", DescribeNegation(m)); | |
2907 } | 3066 } |
2908 | 3067 |
2909 // Tests that Property() can explain the match result. | 3068 // Tests that Property() can explain the match result. |
2910 TEST(PropertyTest, CanExplainMatchResult) { | 3069 TEST(PropertyTest, CanExplainMatchResult) { |
2911 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); | 3070 Matcher<const AClass&> m = Property(&AClass::n, Ge(0)); |
2912 | 3071 |
2913 AClass a; | 3072 AClass a; |
2914 a.set_n(1); | 3073 a.set_n(1); |
2915 EXPECT_EQ("whose given property is 1", Explain(m, a)); | 3074 EXPECT_EQ("whose given property is 1", Explain(m, a)); |
2916 | 3075 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2973 EXPECT_TRUE(m.Matches(&d)); | 3132 EXPECT_TRUE(m.Matches(&d)); |
2974 | 3133 |
2975 d.set_n(-1); | 3134 d.set_n(-1); |
2976 EXPECT_FALSE(m.Matches(&d)); | 3135 EXPECT_FALSE(m.Matches(&d)); |
2977 } | 3136 } |
2978 | 3137 |
2979 // Tests that Property() can describe itself when used to match a pointer. | 3138 // Tests that Property() can describe itself when used to match a pointer. |
2980 TEST(PropertyForPointerTest, CanDescribeSelf) { | 3139 TEST(PropertyForPointerTest, CanDescribeSelf) { |
2981 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); | 3140 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
2982 | 3141 |
2983 EXPECT_EQ("is an object whose given property is greater than or equal to 0", | 3142 EXPECT_EQ("is an object whose given property is >= 0", Describe(m)); |
2984 Describe(m)); | 3143 EXPECT_EQ("is an object whose given property isn't >= 0", |
2985 EXPECT_EQ("is an object whose given property " | 3144 DescribeNegation(m)); |
2986 "is not greater than or equal to 0", DescribeNegation(m)); | |
2987 } | 3145 } |
2988 | 3146 |
2989 // Tests that Property() can explain the result of matching a pointer. | 3147 // Tests that Property() can explain the result of matching a pointer. |
2990 TEST(PropertyForPointerTest, CanExplainMatchResult) { | 3148 TEST(PropertyForPointerTest, CanExplainMatchResult) { |
2991 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); | 3149 Matcher<const AClass*> m = Property(&AClass::n, Ge(0)); |
2992 | 3150 |
2993 AClass a; | 3151 AClass a; |
2994 a.set_n(1); | 3152 a.set_n(1); |
2995 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL))); | 3153 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL))); |
2996 EXPECT_EQ("which points to an object whose given property is 1", | 3154 EXPECT_EQ("which points to an object whose given property is 1", |
(...skipping 17 matching lines...) Expand all Loading... |
3014 EXPECT_FALSE(matcher.Matches(2)); | 3172 EXPECT_FALSE(matcher.Matches(2)); |
3015 } | 3173 } |
3016 | 3174 |
3017 // Tests that ResultOf() can describe itself. | 3175 // Tests that ResultOf() can describe itself. |
3018 TEST(ResultOfTest, CanDescribeItself) { | 3176 TEST(ResultOfTest, CanDescribeItself) { |
3019 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); | 3177 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo")); |
3020 | 3178 |
3021 EXPECT_EQ("is mapped by the given callable to a value that " | 3179 EXPECT_EQ("is mapped by the given callable to a value that " |
3022 "is equal to \"foo\"", Describe(matcher)); | 3180 "is equal to \"foo\"", Describe(matcher)); |
3023 EXPECT_EQ("is mapped by the given callable to a value that " | 3181 EXPECT_EQ("is mapped by the given callable to a value that " |
3024 "is not equal to \"foo\"", DescribeNegation(matcher)); | 3182 "isn't equal to \"foo\"", DescribeNegation(matcher)); |
3025 } | 3183 } |
3026 | 3184 |
3027 // Tests that ResultOf() can explain the match result. | 3185 // Tests that ResultOf() can explain the match result. |
3028 int IntFunction(int input) { return input == 42 ? 80 : 90; } | 3186 int IntFunction(int input) { return input == 42 ? 80 : 90; } |
3029 | 3187 |
3030 TEST(ResultOfTest, CanExplainMatchResult) { | 3188 TEST(ResultOfTest, CanExplainMatchResult) { |
3031 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); | 3189 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85)); |
3032 EXPECT_EQ("which is mapped by the given callable to 90", | 3190 EXPECT_EQ("which is mapped by the given callable to 90", |
3033 Explain(matcher, 36)); | 3191 Explain(matcher, 36)); |
3034 | 3192 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3166 EXPECT_FALSE(matcher3.Matches(n2)); | 3324 EXPECT_FALSE(matcher3.Matches(n2)); |
3167 } | 3325 } |
3168 | 3326 |
3169 class DivisibleByImpl { | 3327 class DivisibleByImpl { |
3170 public: | 3328 public: |
3171 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} | 3329 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} |
3172 | 3330 |
3173 // For testing using ExplainMatchResultTo() with polymorphic matchers. | 3331 // For testing using ExplainMatchResultTo() with polymorphic matchers. |
3174 template <typename T> | 3332 template <typename T> |
3175 bool MatchAndExplain(const T& n, MatchResultListener* listener) const { | 3333 bool MatchAndExplain(const T& n, MatchResultListener* listener) const { |
3176 *listener << "is " << (n % divider_) << " modulo " | 3334 *listener << "which is " << (n % divider_) << " modulo " |
3177 << divider_; | 3335 << divider_; |
3178 return (n % divider_) == 0; | 3336 return (n % divider_) == 0; |
3179 } | 3337 } |
3180 | 3338 |
3181 void DescribeTo(::std::ostream* os) const { | 3339 void DescribeTo(::std::ostream* os) const { |
3182 *os << "is divisible by " << divider_; | 3340 *os << "is divisible by " << divider_; |
3183 } | 3341 } |
3184 | 3342 |
3185 void DescribeNegationTo(::std::ostream* os) const { | 3343 void DescribeNegationTo(::std::ostream* os) const { |
3186 *os << "is not divisible by " << divider_; | 3344 *os << "is not divisible by " << divider_; |
3187 } | 3345 } |
3188 | 3346 |
3189 void set_divider(int a_divider) { divider_ = a_divider; } | 3347 void set_divider(int a_divider) { divider_ = a_divider; } |
3190 int divider() const { return divider_; } | 3348 int divider() const { return divider_; } |
3191 | 3349 |
3192 private: | 3350 private: |
3193 int divider_; | 3351 int divider_; |
3194 }; | 3352 }; |
3195 | 3353 |
3196 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { | 3354 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { |
3197 return MakePolymorphicMatcher(DivisibleByImpl(n)); | 3355 return MakePolymorphicMatcher(DivisibleByImpl(n)); |
3198 } | 3356 } |
3199 | 3357 |
3200 // Tests that when AllOf() fails, only the first failing matcher is | 3358 // Tests that when AllOf() fails, only the first failing matcher is |
3201 // asked to explain why. | 3359 // asked to explain why. |
3202 TEST(ExplainMatchResultTest, AllOf_False_False) { | 3360 TEST(ExplainMatchResultTest, AllOf_False_False) { |
3203 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); | 3361 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
3204 EXPECT_EQ("is 1 modulo 4", Explain(m, 5)); | 3362 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5)); |
3205 } | 3363 } |
3206 | 3364 |
3207 // Tests that when AllOf() fails, only the first failing matcher is | 3365 // Tests that when AllOf() fails, only the first failing matcher is |
3208 // asked to explain why. | 3366 // asked to explain why. |
3209 TEST(ExplainMatchResultTest, AllOf_False_True) { | 3367 TEST(ExplainMatchResultTest, AllOf_False_True) { |
3210 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); | 3368 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); |
3211 EXPECT_EQ("is 2 modulo 4", Explain(m, 6)); | 3369 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6)); |
3212 } | 3370 } |
3213 | 3371 |
3214 // Tests that when AllOf() fails, only the first failing matcher is | 3372 // Tests that when AllOf() fails, only the first failing matcher is |
3215 // asked to explain why. | 3373 // asked to explain why. |
3216 TEST(ExplainMatchResultTest, AllOf_True_False) { | 3374 TEST(ExplainMatchResultTest, AllOf_True_False) { |
3217 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); | 3375 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); |
3218 EXPECT_EQ("is 2 modulo 3", Explain(m, 5)); | 3376 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5)); |
3219 } | 3377 } |
3220 | 3378 |
3221 // Tests that when AllOf() succeeds, all matchers are asked to explain | 3379 // Tests that when AllOf() succeeds, all matchers are asked to explain |
3222 // why. | 3380 // why. |
3223 TEST(ExplainMatchResultTest, AllOf_True_True) { | 3381 TEST(ExplainMatchResultTest, AllOf_True_True) { |
3224 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); | 3382 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); |
3225 EXPECT_EQ("is 0 modulo 2; is 0 modulo 3", Explain(m, 6)); | 3383 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6)); |
3226 } | 3384 } |
3227 | 3385 |
3228 TEST(ExplainMatchResultTest, AllOf_True_True_2) { | 3386 TEST(ExplainMatchResultTest, AllOf_True_True_2) { |
3229 const Matcher<int> m = AllOf(Ge(2), Le(3)); | 3387 const Matcher<int> m = AllOf(Ge(2), Le(3)); |
3230 EXPECT_EQ("", Explain(m, 2)); | 3388 EXPECT_EQ("", Explain(m, 2)); |
3231 } | 3389 } |
3232 | 3390 |
3233 TEST(ExplainmatcherResultTest, MonomorphicMatcher) { | 3391 TEST(ExplainmatcherResultTest, MonomorphicMatcher) { |
3234 const Matcher<int> m = GreaterThan(5); | 3392 const Matcher<int> m = GreaterThan(5); |
3235 EXPECT_EQ("which is 1 more than 5", Explain(m, 6)); | 3393 EXPECT_EQ("which is 1 more than 5", Explain(m, 6)); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3302 } | 3460 } |
3303 | 3461 |
3304 // Tests that missing values are reported. | 3462 // Tests that missing values are reported. |
3305 TYPED_TEST(ContainerEqTest, ValueMissing) { | 3463 TYPED_TEST(ContainerEqTest, ValueMissing) { |
3306 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3464 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3307 static const int test_vals[] = {2, 1, 8, 5}; | 3465 static const int test_vals[] = {2, 1, 8, 5}; |
3308 TypeParam my_set(vals, vals + 6); | 3466 TypeParam my_set(vals, vals + 6); |
3309 TypeParam test_set(test_vals, test_vals + 4); | 3467 TypeParam test_set(test_vals, test_vals + 4); |
3310 const Matcher<TypeParam> m = ContainerEq(my_set); | 3468 const Matcher<TypeParam> m = ContainerEq(my_set); |
3311 EXPECT_FALSE(m.Matches(test_set)); | 3469 EXPECT_FALSE(m.Matches(test_set)); |
3312 EXPECT_EQ("Not in actual: 3", Explain(m, test_set)); | 3470 EXPECT_EQ("which doesn't have these expected elements: 3", |
| 3471 Explain(m, test_set)); |
3313 } | 3472 } |
3314 | 3473 |
3315 // Tests that added values are reported. | 3474 // Tests that added values are reported. |
3316 TYPED_TEST(ContainerEqTest, ValueAdded) { | 3475 TYPED_TEST(ContainerEqTest, ValueAdded) { |
3317 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3476 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3318 static const int test_vals[] = {1, 2, 3, 5, 8, 46}; | 3477 static const int test_vals[] = {1, 2, 3, 5, 8, 46}; |
3319 TypeParam my_set(vals, vals + 6); | 3478 TypeParam my_set(vals, vals + 6); |
3320 TypeParam test_set(test_vals, test_vals + 6); | 3479 TypeParam test_set(test_vals, test_vals + 6); |
3321 const Matcher<const TypeParam&> m = ContainerEq(my_set); | 3480 const Matcher<const TypeParam&> m = ContainerEq(my_set); |
3322 EXPECT_FALSE(m.Matches(test_set)); | 3481 EXPECT_FALSE(m.Matches(test_set)); |
3323 EXPECT_EQ("Only in actual: 46", Explain(m, test_set)); | 3482 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set)); |
3324 } | 3483 } |
3325 | 3484 |
3326 // Tests that added and missing values are reported together. | 3485 // Tests that added and missing values are reported together. |
3327 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) { | 3486 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) { |
3328 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3487 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3329 static const int test_vals[] = {1, 2, 3, 8, 46}; | 3488 static const int test_vals[] = {1, 2, 3, 8, 46}; |
3330 TypeParam my_set(vals, vals + 6); | 3489 TypeParam my_set(vals, vals + 6); |
3331 TypeParam test_set(test_vals, test_vals + 5); | 3490 TypeParam test_set(test_vals, test_vals + 5); |
3332 const Matcher<TypeParam> m = ContainerEq(my_set); | 3491 const Matcher<TypeParam> m = ContainerEq(my_set); |
3333 EXPECT_FALSE(m.Matches(test_set)); | 3492 EXPECT_FALSE(m.Matches(test_set)); |
3334 EXPECT_EQ("Only in actual: 46; not in actual: 5", Explain(m, test_set)); | 3493 EXPECT_EQ("which has these unexpected elements: 46,\n" |
| 3494 "and doesn't have these expected elements: 5", |
| 3495 Explain(m, test_set)); |
3335 } | 3496 } |
3336 | 3497 |
3337 // Tests duplicated value -- expect no explanation. | 3498 // Tests duplicated value -- expect no explanation. |
3338 TYPED_TEST(ContainerEqTest, DuplicateDifference) { | 3499 TYPED_TEST(ContainerEqTest, DuplicateDifference) { |
3339 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3500 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3340 static const int test_vals[] = {1, 2, 3, 5, 8}; | 3501 static const int test_vals[] = {1, 2, 3, 5, 8}; |
3341 TypeParam my_set(vals, vals + 6); | 3502 TypeParam my_set(vals, vals + 6); |
3342 TypeParam test_set(test_vals, test_vals + 5); | 3503 TypeParam test_set(test_vals, test_vals + 5); |
3343 const Matcher<const TypeParam&> m = ContainerEq(my_set); | 3504 const Matcher<const TypeParam&> m = ContainerEq(my_set); |
3344 // Depending on the container, match may be true or false | 3505 // Depending on the container, match may be true or false |
3345 // But in any case there should be no explanation. | 3506 // But in any case there should be no explanation. |
3346 EXPECT_EQ("", Explain(m, test_set)); | 3507 EXPECT_EQ("", Explain(m, test_set)); |
3347 } | 3508 } |
3348 #endif // GTEST_HAS_TYPED_TEST | 3509 #endif // GTEST_HAS_TYPED_TEST |
3349 | 3510 |
3350 // Tests that mutliple missing values are reported. | 3511 // Tests that mutliple missing values are reported. |
3351 // Using just vector here, so order is predicatble. | 3512 // Using just vector here, so order is predicatble. |
3352 TEST(ContainerEqExtraTest, MultipleValuesMissing) { | 3513 TEST(ContainerEqExtraTest, MultipleValuesMissing) { |
3353 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3514 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3354 static const int test_vals[] = {2, 1, 5}; | 3515 static const int test_vals[] = {2, 1, 5}; |
3355 std::vector<int> my_set(vals, vals + 6); | 3516 std::vector<int> my_set(vals, vals + 6); |
3356 std::vector<int> test_set(test_vals, test_vals + 3); | 3517 std::vector<int> test_set(test_vals, test_vals + 3); |
3357 const Matcher<std::vector<int> > m = ContainerEq(my_set); | 3518 const Matcher<std::vector<int> > m = ContainerEq(my_set); |
3358 EXPECT_FALSE(m.Matches(test_set)); | 3519 EXPECT_FALSE(m.Matches(test_set)); |
3359 EXPECT_EQ("Not in actual: 3, 8", Explain(m, test_set)); | 3520 EXPECT_EQ("which doesn't have these expected elements: 3, 8", |
| 3521 Explain(m, test_set)); |
3360 } | 3522 } |
3361 | 3523 |
3362 // Tests that added values are reported. | 3524 // Tests that added values are reported. |
3363 // Using just vector here, so order is predicatble. | 3525 // Using just vector here, so order is predicatble. |
3364 TEST(ContainerEqExtraTest, MultipleValuesAdded) { | 3526 TEST(ContainerEqExtraTest, MultipleValuesAdded) { |
3365 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3527 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3366 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; | 3528 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46}; |
3367 std::list<size_t> my_set(vals, vals + 6); | 3529 std::list<size_t> my_set(vals, vals + 6); |
3368 std::list<size_t> test_set(test_vals, test_vals + 7); | 3530 std::list<size_t> test_set(test_vals, test_vals + 7); |
3369 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set); | 3531 const Matcher<const std::list<size_t>&> m = ContainerEq(my_set); |
3370 EXPECT_FALSE(m.Matches(test_set)); | 3532 EXPECT_FALSE(m.Matches(test_set)); |
3371 EXPECT_EQ("Only in actual: 92, 46", Explain(m, test_set)); | 3533 EXPECT_EQ("which has these unexpected elements: 92, 46", |
| 3534 Explain(m, test_set)); |
3372 } | 3535 } |
3373 | 3536 |
3374 // Tests that added and missing values are reported together. | 3537 // Tests that added and missing values are reported together. |
3375 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { | 3538 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) { |
3376 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3539 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3377 static const int test_vals[] = {1, 2, 3, 92, 46}; | 3540 static const int test_vals[] = {1, 2, 3, 92, 46}; |
3378 std::list<size_t> my_set(vals, vals + 6); | 3541 std::list<size_t> my_set(vals, vals + 6); |
3379 std::list<size_t> test_set(test_vals, test_vals + 5); | 3542 std::list<size_t> test_set(test_vals, test_vals + 5); |
3380 const Matcher<const std::list<size_t> > m = ContainerEq(my_set); | 3543 const Matcher<const std::list<size_t> > m = ContainerEq(my_set); |
3381 EXPECT_FALSE(m.Matches(test_set)); | 3544 EXPECT_FALSE(m.Matches(test_set)); |
3382 EXPECT_EQ("Only in actual: 92, 46; not in actual: 5, 8", | 3545 EXPECT_EQ("which has these unexpected elements: 92, 46,\n" |
| 3546 "and doesn't have these expected elements: 5, 8", |
3383 Explain(m, test_set)); | 3547 Explain(m, test_set)); |
3384 } | 3548 } |
3385 | 3549 |
3386 // Tests to see that duplicate elements are detected, | 3550 // Tests to see that duplicate elements are detected, |
3387 // but (as above) not reported in the explanation. | 3551 // but (as above) not reported in the explanation. |
3388 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { | 3552 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) { |
3389 static const int vals[] = {1, 1, 2, 3, 5, 8}; | 3553 static const int vals[] = {1, 1, 2, 3, 5, 8}; |
3390 static const int test_vals[] = {1, 2, 3, 5, 8}; | 3554 static const int test_vals[] = {1, 2, 3, 5, 8}; |
3391 std::vector<int> my_set(vals, vals + 6); | 3555 std::vector<int> my_set(vals, vals + 6); |
3392 std::vector<int> test_set(test_vals, test_vals + 5); | 3556 std::vector<int> test_set(test_vals, test_vals + 5); |
(...skipping 12 matching lines...) Expand all Loading... |
3405 my_map[1] = "b"; | 3569 my_map[1] = "b"; |
3406 | 3570 |
3407 std::map<int, std::string> test_map; | 3571 std::map<int, std::string> test_map; |
3408 test_map[0] = "aa"; | 3572 test_map[0] = "aa"; |
3409 test_map[1] = "b"; | 3573 test_map[1] = "b"; |
3410 | 3574 |
3411 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map); | 3575 const Matcher<const std::map<int, std::string>&> m = ContainerEq(my_map); |
3412 EXPECT_TRUE(m.Matches(my_map)); | 3576 EXPECT_TRUE(m.Matches(my_map)); |
3413 EXPECT_FALSE(m.Matches(test_map)); | 3577 EXPECT_FALSE(m.Matches(test_map)); |
3414 | 3578 |
3415 EXPECT_EQ("Only in actual: (0, \"aa\"); not in actual: (0, \"a\")", | 3579 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n" |
| 3580 "and doesn't have these expected elements: (0, \"a\")", |
3416 Explain(m, test_map)); | 3581 Explain(m, test_map)); |
3417 } | 3582 } |
3418 | 3583 |
3419 TEST(ContainerEqExtraTest, WorksForNativeArray) { | 3584 TEST(ContainerEqExtraTest, WorksForNativeArray) { |
3420 int a1[] = { 1, 2, 3 }; | 3585 int a1[] = { 1, 2, 3 }; |
3421 int a2[] = { 1, 2, 3 }; | 3586 int a2[] = { 1, 2, 3 }; |
3422 int b[] = { 1, 2, 4 }; | 3587 int b[] = { 1, 2, 4 }; |
3423 | 3588 |
3424 EXPECT_THAT(a1, ContainerEq(a2)); | 3589 EXPECT_THAT(a1, ContainerEq(a2)); |
3425 EXPECT_THAT(a1, Not(ContainerEq(b))); | 3590 EXPECT_THAT(a1, Not(ContainerEq(b))); |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3788 EXPECT_EQ(0, m.mutable_impl().divider()); | 3953 EXPECT_EQ(0, m.mutable_impl().divider()); |
3789 } | 3954 } |
3790 | 3955 |
3791 // Tests PolymorphicMatcher::impl(). | 3956 // Tests PolymorphicMatcher::impl(). |
3792 TEST(PolymorphicMatcherTest, CanAccessImpl) { | 3957 TEST(PolymorphicMatcherTest, CanAccessImpl) { |
3793 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); | 3958 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); |
3794 const DivisibleByImpl& impl = m.impl(); | 3959 const DivisibleByImpl& impl = m.impl(); |
3795 EXPECT_EQ(42, impl.divider()); | 3960 EXPECT_EQ(42, impl.divider()); |
3796 } | 3961 } |
3797 | 3962 |
| 3963 TEST(MatcherTupleTest, ExplainsMatchFailure) { |
| 3964 stringstream ss1; |
| 3965 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)), |
| 3966 make_tuple('a', 10), &ss1); |
| 3967 EXPECT_EQ("", ss1.str()); // Successful match. |
| 3968 |
| 3969 stringstream ss2; |
| 3970 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 3971 make_tuple(2, 'b'), &ss2); |
| 3972 EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 3973 " Actual: 2, which is 3 less than 5\n" |
| 3974 " Expected arg #1: is equal to 'a' (97)\n" |
| 3975 " Actual: 'b' (98)\n", |
| 3976 ss2.str()); // Failed match where both arguments need explanation. |
| 3977 |
| 3978 stringstream ss3; |
| 3979 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))), |
| 3980 make_tuple(2, 'a'), &ss3); |
| 3981 EXPECT_EQ(" Expected arg #0: is > 5\n" |
| 3982 " Actual: 2, which is 3 less than 5\n", |
| 3983 ss3.str()); // Failed match where only one argument needs |
| 3984 // explanation. |
| 3985 } |
| 3986 |
3798 } // namespace gmock_matchers_test | 3987 } // namespace gmock_matchers_test |
3799 } // namespace testing | 3988 } // namespace testing |
OLD | NEW |