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 'dart:collection'; |
| 8 |
7 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
8 | 10 |
9 void shouldFail(value, Matcher matcher, expected) { | 11 void shouldFail(value, Matcher matcher, expected) { |
10 var failed = false; | 12 var failed = false; |
11 try { | 13 try { |
12 expect(value, matcher); | 14 expect(value, matcher); |
13 } on TestFailure catch (err) { | 15 } on TestFailure catch (err) { |
14 failed = true; | 16 failed = true; |
15 | 17 |
16 var _errorString = err.message; | 18 var _errorString = err.message; |
17 | 19 |
18 if (expected is String) { | 20 if (expected is String) { |
19 expect(_errorString, equalsIgnoringWhitespace(expected)); | 21 expect(_errorString, equalsIgnoringWhitespace(expected)); |
20 } else { | 22 } else { |
21 expect(_errorString.replaceAll('\n', ''), expected); | 23 expect(_errorString.replaceAll('\n', ''), expected); |
22 } | 24 } |
23 } | 25 } |
24 | 26 |
25 expect(failed, isTrue, reason: 'Expected to fail.'); | 27 expect(failed, isTrue, reason: 'Expected to fail.'); |
26 } | 28 } |
27 | 29 |
28 void shouldPass(value, Matcher matcher) { | 30 void shouldPass(value, Matcher matcher) { |
29 expect(value, matcher); | 31 expect(value, matcher); |
30 } | 32 } |
31 | 33 |
32 doesNotThrow() {} | 34 doesNotThrow() {} |
33 doesThrow() { | 35 doesThrow() { |
34 throw 'X'; | 36 throw 'X'; |
35 } | 37 } |
| 38 |
| 39 class Widget { |
| 40 int price; |
| 41 } |
| 42 |
| 43 class HasPrice extends CustomMatcher { |
| 44 HasPrice(matcher) : super("Widget with a price that is", "price", matcher); |
| 45 featureValueOf(actual) => actual.price; |
| 46 } |
| 47 |
| 48 class SimpleIterable extends IterableBase<int> { |
| 49 final int count; |
| 50 |
| 51 SimpleIterable(this.count); |
| 52 |
| 53 bool contains(int val) => count < val ? false : true; |
| 54 |
| 55 bool any(bool f(element)) { |
| 56 for (var i = 0; i <= count; i++) { |
| 57 if (f(i)) return true; |
| 58 } |
| 59 return false; |
| 60 } |
| 61 |
| 62 String toString() => "<[$count]>"; |
| 63 |
| 64 Iterator get iterator { |
| 65 return new _SimpleIterator(count); |
| 66 } |
| 67 } |
| 68 |
| 69 class _SimpleIterator implements Iterator<int> { |
| 70 int _count; |
| 71 int _current; |
| 72 |
| 73 _SimpleIterator(this._count); |
| 74 |
| 75 bool moveNext() { |
| 76 if (_count > 0) { |
| 77 _current = _count; |
| 78 _count--; |
| 79 return true; |
| 80 } |
| 81 _current = null; |
| 82 return false; |
| 83 } |
| 84 |
| 85 int get current => _current; |
| 86 } |
OLD | NEW |