2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import 'feature_matcher.dart' ;
5
6
import 'interfaces.dart' ;
6
7
import 'util.dart' ;
7
8
@@ -69,15 +70,17 @@ const Matcher isNaN = const _IsNaN();
69
70
/// A matcher that matches any non-NaN value.
70
71
const Matcher isNotNaN = const _IsNotNaN ();
71
72
72
- class _IsNaN extends Matcher {
73
+ class _IsNaN extends FeatureMatcher < num > {
73
74
const _IsNaN ();
74
- bool matches (item, Map matchState) => double .nan.compareTo (item) == 0 ;
75
+ bool typedMatches (num item, Map matchState) =>
76
+ double .nan.compareTo (item) == 0 ;
75
77
Description describe (Description description) => description.add ('NaN' );
76
78
}
77
79
78
- class _IsNotNaN extends Matcher {
80
+ class _IsNotNaN extends FeatureMatcher < num > {
79
81
const _IsNotNaN ();
80
- bool matches (item, Map matchState) => double .nan.compareTo (item) != 0 ;
82
+ bool typedMatches (num item, Map matchState) =>
83
+ double .nan.compareTo (item) != 0 ;
81
84
Description describe (Description description) => description.add ('not NaN' );
82
85
}
83
86
@@ -127,10 +130,10 @@ class isInstanceOf<T> extends TypeMatcher<T> {
127
130
/// a wrapper will have to be created.
128
131
const Matcher returnsNormally = const _ReturnsNormally ();
129
132
130
- class _ReturnsNormally extends Matcher {
133
+ class _ReturnsNormally extends FeatureMatcher < Function > {
131
134
const _ReturnsNormally ();
132
135
133
- bool matches ( f, Map matchState) {
136
+ bool typedMatches ( Function f, Map matchState) {
134
137
try {
135
138
f ();
136
139
return true ;
@@ -143,8 +146,8 @@ class _ReturnsNormally extends Matcher {
143
146
Description describe (Description description) =>
144
147
description.add ("return normally" );
145
148
146
- Description describeMismatch (
147
- item, Description mismatchDescription, Map matchState, bool verbose) {
149
+ Description describeTypedMismatch ( Function item,
150
+ Description mismatchDescription, Map matchState, bool verbose) {
148
151
mismatchDescription.add ('threw ' ).addDescriptionOf (matchState['exception' ]);
149
152
if (verbose) {
150
153
mismatchDescription.add (' at ' ).add (matchState['stack' ].toString ());
@@ -257,11 +260,12 @@ class _Contains extends Matcher {
257
260
const _Contains (this ._expected);
258
261
259
262
bool matches (item, Map matchState) {
263
+ var expected = _expected;
260
264
if (item is String ) {
261
- return item.contains ((_expected as Pattern ) );
265
+ return expected is Pattern && item.contains (expected );
262
266
} else if (item is Iterable ) {
263
- if (_expected is Matcher ) {
264
- return item.any ((e) => (_expected as Matcher ) .matches (e, matchState));
267
+ if (expected is Matcher ) {
268
+ return item.any ((e) => expected .matches (e, matchState));
265
269
} else {
266
270
return item.contains (_expected);
267
271
}
@@ -287,27 +291,29 @@ class _Contains extends Matcher {
287
291
288
292
/// Returns a matcher that matches if the match argument is in
289
293
/// the expected value. This is the converse of [contains] .
290
- Matcher isIn (expected) => new _In (expected);
294
+ Matcher isIn (expected) {
295
+ if (expected is Iterable ) {
296
+ return new _In (expected, expected.contains);
297
+ } else if (expected is String ) {
298
+ return new _In <Pattern >(expected, expected.contains);
299
+ } else if (expected is Map ) {
300
+ return new _In (expected, expected.containsKey);
301
+ }
291
302
292
- class _In extends Matcher {
293
- final Object _expected;
303
+ throw new ArgumentError .value (
304
+ expected, 'expected' , 'Only Iterable, Map, and String are supported.' );
305
+ }
294
306
295
- const _In (this ._expected);
307
+ class _In <T > extends FeatureMatcher <T > {
308
+ final Object _source;
309
+ final bool Function (T ) _containsFunction;
296
310
297
- bool matches (item, Map matchState) {
298
- var expected = _expected;
299
- if (expected is String ) {
300
- return expected.contains (item as Pattern );
301
- } else if (expected is Iterable ) {
302
- return expected.contains (item);
303
- } else if (expected is Map ) {
304
- return expected.containsKey (item);
305
- }
306
- return false ;
307
- }
311
+ const _In (this ._source, this ._containsFunction);
312
+
313
+ bool typedMatches (T item, Map matchState) => _containsFunction (item);
308
314
309
315
Description describe (Description description) =>
310
- description.add ('is in ' ).addDescriptionOf (_expected );
316
+ description.add ('is in ' ).addDescriptionOf (_source );
311
317
}
312
318
313
319
/// Returns a matcher that uses an arbitrary function that returns
@@ -322,13 +328,13 @@ Matcher predicate<T>(bool f(T value),
322
328
323
329
typedef bool _PredicateFunction <T >(T value);
324
330
325
- class _Predicate <T > extends Matcher {
331
+ class _Predicate <T > extends FeatureMatcher < T > {
326
332
final _PredicateFunction <T > _matcher;
327
333
final String _description;
328
334
329
335
_Predicate (this ._matcher, this ._description);
330
336
331
- bool matches ( item, Map matchState) => _matcher (item as T );
337
+ bool typedMatches ( T item, Map matchState) => _matcher (item);
332
338
333
339
Description describe (Description description) =>
334
340
description.add (_description);
0 commit comments