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 'type_matcher.dart' ;
7
8
import 'util.dart' ;
@@ -70,15 +71,17 @@ const Matcher isNaN = const _IsNaN();
70
71
/// A matcher that matches any non-NaN value.
71
72
const Matcher isNotNaN = const _IsNotNaN ();
72
73
73
- class _IsNaN extends Matcher {
74
+ class _IsNaN extends FeatureMatcher < num > {
74
75
const _IsNaN ();
75
- bool matches (item, Map matchState) => double .nan.compareTo (item) == 0 ;
76
+ bool typedMatches (num item, Map matchState) =>
77
+ double .nan.compareTo (item) == 0 ;
76
78
Description describe (Description description) => description.add ('NaN' );
77
79
}
78
80
79
- class _IsNotNaN extends Matcher {
81
+ class _IsNotNaN extends FeatureMatcher < num > {
80
82
const _IsNotNaN ();
81
- bool matches (item, Map matchState) => double .nan.compareTo (item) != 0 ;
83
+ bool typedMatches (num item, Map matchState) =>
84
+ double .nan.compareTo (item) != 0 ;
82
85
Description describe (Description description) => description.add ('not NaN' );
83
86
}
84
87
@@ -128,10 +131,10 @@ class isInstanceOf<T> extends TypeMatcher<T> {
128
131
/// a wrapper will have to be created.
129
132
const Matcher returnsNormally = const _ReturnsNormally ();
130
133
131
- class _ReturnsNormally extends Matcher {
134
+ class _ReturnsNormally extends FeatureMatcher < Function > {
132
135
const _ReturnsNormally ();
133
136
134
- bool matches ( f, Map matchState) {
137
+ bool typedMatches ( Function f, Map matchState) {
135
138
try {
136
139
f ();
137
140
return true ;
@@ -144,8 +147,8 @@ class _ReturnsNormally extends Matcher {
144
147
Description describe (Description description) =>
145
148
description.add ("return normally" );
146
149
147
- Description describeMismatch (
148
- item, Description mismatchDescription, Map matchState, bool verbose) {
150
+ Description describeTypedMismatch ( Function item,
151
+ Description mismatchDescription, Map matchState, bool verbose) {
149
152
mismatchDescription.add ('threw ' ).addDescriptionOf (matchState['exception' ]);
150
153
if (verbose) {
151
154
mismatchDescription.add (' at ' ).add (matchState['stack' ].toString ());
@@ -216,11 +219,12 @@ class _Contains extends Matcher {
216
219
const _Contains (this ._expected);
217
220
218
221
bool matches (item, Map matchState) {
222
+ var expected = _expected;
219
223
if (item is String ) {
220
- return item.contains ((_expected as Pattern ) );
224
+ return expected is Pattern && item.contains (expected );
221
225
} else if (item is Iterable ) {
222
- if (_expected is Matcher ) {
223
- return item.any ((e) => (_expected as Matcher ) .matches (e, matchState));
226
+ if (expected is Matcher ) {
227
+ return item.any ((e) => expected .matches (e, matchState));
224
228
} else {
225
229
return item.contains (_expected);
226
230
}
@@ -246,27 +250,29 @@ class _Contains extends Matcher {
246
250
247
251
/// Returns a matcher that matches if the match argument is in
248
252
/// the expected value. This is the converse of [contains] .
249
- Matcher isIn (expected) => new _In (expected);
253
+ Matcher isIn (expected) {
254
+ if (expected is Iterable ) {
255
+ return new _In (expected, expected.contains);
256
+ } else if (expected is String ) {
257
+ return new _In <Pattern >(expected, expected.contains);
258
+ } else if (expected is Map ) {
259
+ return new _In (expected, expected.containsKey);
260
+ }
250
261
251
- class _In extends Matcher {
252
- final Object _expected;
262
+ throw new ArgumentError .value (
263
+ expected, 'expected' , 'Only Iterable, Map, and String are supported.' );
264
+ }
253
265
254
- const _In (this ._expected);
266
+ class _In <T > extends FeatureMatcher <T > {
267
+ final Object _source;
268
+ final bool Function (T ) _containsFunction;
255
269
256
- bool matches (item, Map matchState) {
257
- var expected = _expected;
258
- if (expected is String ) {
259
- return expected.contains (item as Pattern );
260
- } else if (expected is Iterable ) {
261
- return expected.contains (item);
262
- } else if (expected is Map ) {
263
- return expected.containsKey (item);
264
- }
265
- return false ;
266
- }
270
+ const _In (this ._source, this ._containsFunction);
271
+
272
+ bool typedMatches (T item, Map matchState) => _containsFunction (item);
267
273
268
274
Description describe (Description description) =>
269
- description.add ('is in ' ).addDescriptionOf (_expected );
275
+ description.add ('is in ' ).addDescriptionOf (_source );
270
276
}
271
277
272
278
/// Returns a matcher that uses an arbitrary function that returns
@@ -281,13 +287,13 @@ Matcher predicate<T>(bool f(T value),
281
287
282
288
typedef bool _PredicateFunction <T >(T value);
283
289
284
- class _Predicate <T > extends Matcher {
290
+ class _Predicate <T > extends FeatureMatcher < T > {
285
291
final _PredicateFunction <T > _matcher;
286
292
final String _description;
287
293
288
294
_Predicate (this ._matcher, this ._description);
289
295
290
- bool matches ( item, Map matchState) => _matcher (item as T );
296
+ bool typedMatches ( T item, Map matchState) => _matcher (item);
291
297
292
298
Description describe (Description description) =>
293
299
description.add (_description);
0 commit comments