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