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, wrapAsync, expect; | 9 import 'package:matcher/matcher.dart' hide completes, expect; |
10 | 10 |
11 import '../unittest.dart'; | 11 import '../unittest.dart'; |
12 | 12 |
13 /// Matches a [Function] that prints text that matches [matcher]. | 13 /// Matches a [Function] that prints text that matches [matcher]. |
14 /// | 14 /// |
15 /// [matcher] may be a String or a [Matcher]. | 15 /// [matcher] may be a String or a [Matcher]. |
16 /// | 16 /// |
17 /// If the function this runs against returns a [Future], all text printed by | 17 /// 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. | 18 /// the function (using [Zone] scoping) until that Future completes is matched. |
19 /// | 19 /// |
(...skipping 13 matching lines...) Loading... |
33 zoneSpecification: new ZoneSpecification(print: (_, __, ____, line) { | 33 zoneSpecification: new ZoneSpecification(print: (_, __, ____, line) { |
34 buffer.writeln(line); | 34 buffer.writeln(line); |
35 })); | 35 })); |
36 | 36 |
37 if (result is! Future) { | 37 if (result is! Future) { |
38 var actual = buffer.toString(); | 38 var actual = buffer.toString(); |
39 matchState['prints.actual'] = actual; | 39 matchState['prints.actual'] = actual; |
40 return _matcher.matches(actual, matchState); | 40 return _matcher.matches(actual, matchState); |
41 } | 41 } |
42 | 42 |
43 return completes.matches(result.then(wrapAsync((_) { | 43 return completes.matches(result.then((_) { |
44 expect(buffer.toString(), _matcher); | 44 expect(buffer.toString(), _matcher); |
45 }, 'prints')), matchState); | 45 }), matchState); |
46 } | 46 } |
47 | 47 |
48 Description describe(Description description) => | 48 Description describe(Description description) => |
49 description.add('prints ').addDescriptionOf(_matcher); | 49 description.add('prints ').addDescriptionOf(_matcher); |
50 | 50 |
51 Description describeMismatch( | 51 Description describeMismatch( |
52 item, Description description, Map matchState, bool verbose) { | 52 item, Description description, Map matchState, bool verbose) { |
53 var actual = matchState.remove('prints.actual'); | 53 var actual = matchState.remove('prints.actual'); |
54 if (actual == null) return description; | 54 if (actual == null) return description; |
55 if (actual.isEmpty) return description.add("printed nothing."); | 55 if (actual.isEmpty) return description.add("printed nothing."); |
56 | 56 |
57 description.add('printed ').addDescriptionOf(actual); | 57 description.add('printed ').addDescriptionOf(actual); |
58 | 58 |
59 // Create a new description for the matcher because at least | 59 // Create a new description for the matcher because at least |
60 // [_StringEqualsMatcher] replaces the previous contents of the description. | 60 // [_StringEqualsMatcher] replaces the previous contents of the description. |
61 var innerMismatch = _matcher | 61 var innerMismatch = _matcher |
62 .describeMismatch(actual, new StringDescription(), matchState, verbose) | 62 .describeMismatch(actual, new StringDescription(), matchState, verbose) |
63 .toString(); | 63 .toString(); |
64 | 64 |
65 if (innerMismatch.isNotEmpty) { | 65 if (innerMismatch.isNotEmpty) { |
66 description.add('\n Which: ').add(innerMismatch.toString()); | 66 description.add('\n Which: ').add(innerMismatch.toString()); |
67 } | 67 } |
68 | 68 |
69 return description; | 69 return description; |
70 } | 70 } |
71 } | 71 } |
OLD | NEW |