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