OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library matcher.test_utils; | 5 library matcher.test_utils; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
kevmoo
2015/02/18 04:11:08
Need to import 'dart:collection'!
nweiz
2015/02/19 00:31:08
Done.
| |
8 | 8 |
9 void shouldFail(value, Matcher matcher, expected) { | 9 void shouldFail(value, Matcher matcher, expected) { |
10 var failed = false; | 10 var failed = false; |
11 try { | 11 try { |
12 expect(value, matcher); | 12 expect(value, matcher); |
13 } on TestFailure catch (err) { | 13 } on TestFailure catch (err) { |
14 failed = true; | 14 failed = true; |
15 | 15 |
16 var _errorString = err.message; | 16 var _errorString = err.message; |
17 | 17 |
18 if (expected is String) { | 18 if (expected is String) { |
19 expect(_errorString, equalsIgnoringWhitespace(expected)); | 19 expect(_errorString, equalsIgnoringWhitespace(expected)); |
20 } else { | 20 } else { |
21 expect(_errorString.replaceAll('\n', ''), expected); | 21 expect(_errorString.replaceAll('\n', ''), expected); |
22 } | 22 } |
23 } | 23 } |
24 | 24 |
25 expect(failed, isTrue, reason: 'Expected to fail.'); | 25 expect(failed, isTrue, reason: 'Expected to fail.'); |
26 } | 26 } |
27 | 27 |
28 void shouldPass(value, Matcher matcher) { | 28 void shouldPass(value, Matcher matcher) { |
29 expect(value, matcher); | 29 expect(value, matcher); |
30 } | 30 } |
31 | 31 |
32 doesNotThrow() {} | 32 doesNotThrow() {} |
33 doesThrow() { | 33 doesThrow() { |
34 throw 'X'; | 34 throw 'X'; |
35 } | 35 } |
36 | |
37 class Widget { | |
38 int price; | |
39 } | |
40 | |
41 class HasPrice extends CustomMatcher { | |
42 HasPrice(matcher) : super("Widget with a price that is", "price", matcher); | |
43 featureValueOf(actual) => actual.price; | |
44 } | |
45 | |
46 class SimpleIterable extends IterableBase<int> { | |
47 final int count; | |
48 | |
49 SimpleIterable(this.count); | |
50 | |
51 bool contains(int val) => count < val ? false : true; | |
52 | |
53 bool any(bool f(element)) { | |
54 for (var i = 0; i <= count; i++) { | |
55 if (f(i)) return true; | |
56 } | |
57 return false; | |
58 } | |
59 | |
60 String toString() => "<[$count]>"; | |
61 | |
62 Iterator get iterator { | |
63 return new _SimpleIterator(count); | |
64 } | |
65 } | |
66 | |
67 class _SimpleIterator implements Iterator<int> { | |
68 int _count; | |
69 int _current; | |
70 | |
71 _SimpleIterator(this._count); | |
72 | |
73 bool moveNext() { | |
74 if (_count > 0) { | |
75 _current = _count; | |
76 _count--; | |
77 return true; | |
78 } | |
79 _current = null; | |
80 return false; | |
81 } | |
82 | |
83 int get current => _current; | |
84 } | |
OLD | NEW |