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