OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 unittest.prints_matcher; | 5 library unittest.prints_matcher; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:matcher/matcher.dart' hide completes, expect; | 9 import 'package:matcher/matcher.dart' hide completes, expect; |
10 | 10 |
11 import '../unittest.dart'; | 11 import 'future_matchers.dart'; |
| 12 import 'expect.dart'; |
12 | 13 |
13 /// Matches a [Function] that prints text that matches [matcher]. | 14 /// Matches a [Function] that prints text that matches [matcher]. |
14 /// | 15 /// |
15 /// [matcher] may be a String or a [Matcher]. | 16 /// [matcher] may be a String or a [Matcher]. |
16 /// | 17 /// |
17 /// If the function this runs against returns a [Future], all text printed by | 18 /// If the function this runs against returns a [Future], all text printed by |
18 /// the function (using [Zone] scoping) until that Future completes is matched. | 19 /// the function (using [Zone] scoping) until that Future completes is matched. |
19 /// | 20 /// |
20 /// This only tracks text printed using the [print] function. | 21 /// This only tracks text printed using the [print] function. |
21 Matcher prints(matcher) => new _Prints(wrapMatcher(matcher)); | 22 Matcher prints(matcher) => new _Prints(wrapMatcher(matcher)); |
(...skipping 12 matching lines...) Expand all Loading... |
34 buffer.writeln(line); | 35 buffer.writeln(line); |
35 })); | 36 })); |
36 | 37 |
37 if (result is! Future) { | 38 if (result is! Future) { |
38 var actual = buffer.toString(); | 39 var actual = buffer.toString(); |
39 matchState['prints.actual'] = actual; | 40 matchState['prints.actual'] = actual; |
40 return _matcher.matches(actual, matchState); | 41 return _matcher.matches(actual, matchState); |
41 } | 42 } |
42 | 43 |
43 return completes.matches(result.then((_) { | 44 return completes.matches(result.then((_) { |
44 expect(buffer.toString(), _matcher); | 45 // Re-run expect() so we get the same formatting as we would without being |
| 46 // asynchronous. |
| 47 expect(() { |
| 48 var actual = buffer.toString(); |
| 49 if (actual.isEmpty) return; |
| 50 |
| 51 // Strip off the final newline because [print] will re-add it. |
| 52 actual = actual.substring(0, actual.length - 1); |
| 53 print(actual); |
| 54 }, this); |
45 }), matchState); | 55 }), matchState); |
46 } | 56 } |
47 | 57 |
48 Description describe(Description description) => | 58 Description describe(Description description) => |
49 description.add('prints ').addDescriptionOf(_matcher); | 59 description.add('prints ').addDescriptionOf(_matcher); |
50 | 60 |
51 Description describeMismatch( | 61 Description describeMismatch( |
52 item, Description description, Map matchState, bool verbose) { | 62 item, Description description, Map matchState, bool verbose) { |
53 var actual = matchState.remove('prints.actual'); | 63 var actual = matchState.remove('prints.actual'); |
54 if (actual == null) return description; | 64 if (actual == null) return description; |
55 if (actual.isEmpty) return description.add("printed nothing."); | 65 if (actual.isEmpty) return description.add("printed nothing."); |
56 | 66 |
57 description.add('printed ').addDescriptionOf(actual); | 67 description.add('printed ').addDescriptionOf(actual); |
58 | 68 |
59 // Create a new description for the matcher because at least | 69 // Create a new description for the matcher because at least |
60 // [_StringEqualsMatcher] replaces the previous contents of the description. | 70 // [_StringEqualsMatcher] replaces the previous contents of the description. |
61 var innerMismatch = _matcher | 71 var innerMismatch = _matcher |
62 .describeMismatch(actual, new StringDescription(), matchState, verbose) | 72 .describeMismatch(actual, new StringDescription(), matchState, verbose) |
63 .toString(); | 73 .toString(); |
64 | 74 |
65 if (innerMismatch.isNotEmpty) { | 75 if (innerMismatch.isNotEmpty) { |
66 description.add('\n Which: ').add(innerMismatch.toString()); | 76 description.add('\n Which: ').add(innerMismatch.toString()); |
67 } | 77 } |
68 | 78 |
69 return description; | 79 return description; |
70 } | 80 } |
71 } | 81 } |
OLD | NEW |