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 test_utils; | 5 library matcher.test_utils; |
6 | |
7 import 'package:matcher/matcher.dart'; | |
8 import 'package:unittest/unittest.dart' as ut; | |
9 | 6 |
10 import 'dart:async'; | 7 import 'dart:async'; |
11 | 8 |
12 int errorCount; | 9 import 'package:matcher/matcher.dart'; |
13 String errorString; | 10 import 'package:unittest/unittest.dart' show test, expectAsync; |
14 var _testHandler = null; | 11 |
| 12 int _errorCount; |
| 13 String _errorString; |
| 14 FailureHandler _testHandler = null; |
15 | 15 |
16 class MyFailureHandler extends DefaultFailureHandler { | 16 class MyFailureHandler extends DefaultFailureHandler { |
17 void fail(String reason) { | 17 void fail(String reason) { |
18 ++errorCount; | 18 ++_errorCount; |
19 errorString = reason; | 19 _errorString = reason; |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 void initUtils() { | 23 void initUtils() { |
24 if (_testHandler == null) { | 24 if (_testHandler == null) { |
25 _testHandler = new MyFailureHandler(); | 25 _testHandler = new MyFailureHandler(); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 void shouldFail(value, Matcher matcher, expected, {bool isAsync: false}) { | 29 void shouldFail(value, Matcher matcher, expected, {bool isAsync: false}) { |
30 configureExpectFailureHandler(_testHandler); | 30 configureExpectFailureHandler(_testHandler); |
31 errorCount = 0; | 31 _errorCount = 0; |
32 errorString = ''; | 32 _errorString = ''; |
33 expect(value, matcher); | 33 expect(value, matcher); |
34 afterTest() { | 34 afterTest() { |
35 configureExpectFailureHandler(null); | 35 configureExpectFailureHandler(null); |
36 expect(errorCount, equals(1)); | 36 expect(_errorCount, equals(1)); |
37 if (expected is String) { | 37 if (expected is String) { |
38 expect(errorString, equalsIgnoringWhitespace(expected)); | 38 expect(_errorString, equalsIgnoringWhitespace(expected)); |
39 } else { | 39 } else { |
40 expect(errorString.replaceAll('\n', ''), expected); | 40 expect(_errorString.replaceAll('\n', ''), expected); |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
44 if (isAsync) { | 44 if (isAsync) { |
45 Timer.run(ut.expectAsync(afterTest)); | 45 Timer.run(expectAsync(afterTest)); |
46 } else { | 46 } else { |
47 afterTest(); | 47 afterTest(); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 void shouldPass(value, Matcher matcher, {bool isAsync: false}) { | 51 void shouldPass(value, Matcher matcher, {bool isAsync: false}) { |
52 configureExpectFailureHandler(_testHandler); | 52 configureExpectFailureHandler(_testHandler); |
53 errorCount = 0; | 53 _errorCount = 0; |
54 errorString = ''; | 54 _errorString = ''; |
55 expect(value, matcher); | 55 expect(value, matcher); |
56 afterTest() { | 56 afterTest() { |
57 configureExpectFailureHandler(null); | 57 configureExpectFailureHandler(null); |
58 expect(errorCount, equals(0)); | 58 expect(_errorCount, equals(0)); |
59 } | 59 } |
60 if (isAsync) { | 60 if (isAsync) { |
61 Timer.run(ut.expectAsync(afterTest)); | 61 Timer.run(expectAsync(afterTest)); |
62 } else { | 62 } else { |
63 afterTest(); | 63 afterTest(); |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 doesNotThrow() {} | 67 doesNotThrow() {} |
68 doesThrow() { throw 'X'; } | 68 doesThrow() { throw 'X'; } |
69 | 69 |
70 class PrefixMatcher extends Matcher { | 70 class PrefixMatcher extends Matcher { |
71 final String _prefix; | 71 final String _prefix; |
72 const PrefixMatcher(this._prefix); | 72 const PrefixMatcher(this._prefix); |
73 bool matches(item, Map matchState) { | 73 bool matches(item, Map matchState) { |
74 return item is String && | 74 return item is String && |
75 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | 75 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); |
76 } | 76 } |
77 | 77 |
78 Description describe(Description description) => | 78 Description describe(Description description) => |
79 description.add('a string starting with '). | 79 description.add('a string starting with '). |
80 addDescriptionOf(collapseWhitespace(_prefix)). | 80 addDescriptionOf(collapseWhitespace(_prefix)). |
81 add(' ignoring whitespace'); | 81 add(' ignoring whitespace'); |
82 } | 82 } |
OLD | NEW |