@@ -18,7 +18,7 @@ use crate as googletest;
18
18
use crate :: matchers:: count_elements:: count_elements;
19
19
#[ cfg( google3) ]
20
20
use count_elements:: count_elements;
21
- use googletest:: matcher:: { Matcher , MatcherResult } ;
21
+ use googletest:: matcher:: { MatchExplanation , Matcher , MatcherResult } ;
22
22
use std:: fmt:: Debug ;
23
23
24
24
/// Matches a container whose size matches `expected`.
70
70
}
71
71
}
72
72
}
73
+
74
+ fn explain_match ( & self , actual : & T ) -> MatchExplanation {
75
+ let actual_size = count_elements ( actual) ;
76
+ MatchExplanation :: create ( format ! (
77
+ "which has size {}, {}" ,
78
+ actual_size,
79
+ self . expected. explain_match( & actual_size)
80
+ ) )
81
+ }
73
82
}
74
83
75
84
#[ cfg( test) ]
@@ -80,7 +89,8 @@ mod tests {
80
89
#[ cfg( not( google3) ) ]
81
90
use googletest:: matchers;
82
91
use googletest:: { google_test, verify_that, Result } ;
83
- use matchers:: eq;
92
+ use indoc:: indoc;
93
+ use matchers:: { contains_substring, displays_as, eq, err} ;
84
94
use std:: collections:: {
85
95
BTreeMap , BTreeSet , BinaryHeap , HashMap , HashSet , LinkedList , VecDeque ,
86
96
} ;
@@ -157,4 +167,45 @@ mod tests {
157
167
let value = VecDeque :: from ( [ 1 , 2 , 3 ] ) ;
158
168
verify_that ! ( value, size( eq( 3 ) ) )
159
169
}
170
+
171
+ #[ google_test]
172
+ fn size_matcher_explain_match ( ) -> Result < ( ) > {
173
+ struct TestMatcher ;
174
+ impl < T : Debug > Matcher < T > for TestMatcher {
175
+ fn matches ( & self , _: & T ) -> MatcherResult {
176
+ false . into ( )
177
+ }
178
+
179
+ fn describe ( & self , _: MatcherResult ) -> String {
180
+ "called described" . into ( )
181
+ }
182
+
183
+ fn explain_match ( & self , _: & T ) -> MatchExplanation {
184
+ MatchExplanation :: create ( "called explain_match" . into ( ) )
185
+ }
186
+ }
187
+ verify_that ! (
188
+ size( TestMatcher { } ) . explain_match( & [ 1 , 2 , 3 ] ) ,
189
+ displays_as( eq( "which has size 3, called explain_match" ) )
190
+ )
191
+ }
192
+
193
+ #[ google_test]
194
+ fn size_matcher_error_message ( ) -> Result < ( ) > {
195
+ let result = verify_that ! ( vec![ 1 , 2 , 3 , 4 ] , size( eq( 3 ) ) ) ;
196
+ verify_that ! (
197
+ result,
198
+ err( displays_as( contains_substring( indoc!(
199
+ "
200
+ Value of: vec![1, 2, 3, 4]
201
+ Expected: has size, which is equal to 3
202
+ Actual: [
203
+ 1,
204
+ 2,
205
+ 3,
206
+ 4,
207
+ ], which has size 4, which isn't equal to 3"
208
+ ) ) ) )
209
+ )
210
+ }
160
211
}
0 commit comments