OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library matcher.test_utils; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:matcher/matcher.dart'; | |
10 import 'package:unittest/unittest.dart' show test, expectAsync; | |
11 | |
12 int _errorCount; | |
13 String _errorString; | |
14 FailureHandler _testHandler = null; | |
15 | |
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) { | |
38 expect(_errorString, equalsIgnoringWhitespace(expected)); | |
39 } else { | |
40 expect(_errorString.replaceAll('\n', ''), expected); | |
41 } | |
42 } | |
43 | |
44 if (isAsync) { | |
45 Timer.run(expectAsync(afterTest)); | |
46 } else { | |
47 afterTest(); | |
48 } | |
49 } | |
50 | |
51 void shouldPass(value, Matcher matcher, {bool isAsync: false}) { | |
52 configureExpectFailureHandler(_testHandler); | |
53 _errorCount = 0; | |
54 _errorString = ''; | |
55 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 } | |
66 | |
67 doesNotThrow() {} | |
68 doesThrow() { throw 'X'; } | |
69 | |
70 class PrefixMatcher extends Matcher { | |
71 final String _prefix; | |
72 const PrefixMatcher(this._prefix); | |
73 bool matches(item, Map matchState) { | |
74 return item is String && | |
75 (collapseWhitespace(item)).startsWith(collapseWhitespace(_prefix)); | |
76 } | |
77 | |
78 Description describe(Description description) => | |
79 description.add('a string starting with '). | |
80 addDescriptionOf(collapseWhitespace(_prefix)). | |
81 add(' ignoring whitespace'); | |
82 } | |
OLD | NEW |