Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: lib/src/prints_matcher.dart

Issue 869053002: unittest: refactor ahead of matcher changes (Closed) Base URL: https://github.com/dart-lang/unittest.git@master
Patch Set: nits Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/future_matchers.dart ('k') | lib/src/simple_configuration.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library matcher.prints_matcher;
6
7 import 'dart:async';
8
9 import 'package:matcher/matcher.dart' hide completes, wrapAsync, expect;
10
11 import '../unittest.dart';
12
13 /// Matches a [Function] that prints text that matches [matcher].
14 ///
15 /// [matcher] may be a String or a [Matcher].
16 ///
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.
19 ///
20 /// This only tracks text printed using the [print] function.
21 Matcher prints(matcher) => new _Prints(wrapMatcher(matcher));
22
23 class _Prints extends Matcher {
24 final Matcher _matcher;
25
26 _Prints(this._matcher);
27
28 bool matches(item, Map matchState) {
29 if (item is! Function) return false;
30
31 var buffer = new StringBuffer();
32 var result = runZoned(item,
33 zoneSpecification: new ZoneSpecification(print: (_, __, ____, line) {
34 buffer.writeln(line);
35 }));
36
37 if (result is! Future) {
38 var actual = buffer.toString();
39 matchState['prints.actual'] = actual;
40 return _matcher.matches(actual, matchState);
41 }
42
43 return completes.matches(result.then(wrapAsync((_) {
44 expect(buffer.toString(), _matcher);
45 }, 'prints')), matchState);
46 }
47
48 Description describe(Description description) =>
49 description.add('prints ').addDescriptionOf(_matcher);
50
51 Description describeMismatch(
52 item, Description description, Map matchState, bool verbose) {
53 var actual = matchState.remove('prints.actual');
54 if (actual == null) return description;
55 if (actual.isEmpty) return description.add("printed nothing.");
56
57 description.add('printed ').addDescriptionOf(actual);
58
59 // Create a new description for the matcher because at least
60 // [_StringEqualsMatcher] replaces the previous contents of the description.
61 var innerMismatch = _matcher
62 .describeMismatch(actual, new StringDescription(), matchState, verbose)
63 .toString();
64
65 if (innerMismatch.isNotEmpty) {
66 description.add('\n Which: ').add(innerMismatch.toString());
67 }
68
69 return description;
70 }
71 }
OLDNEW
« no previous file with comments | « lib/src/future_matchers.dart ('k') | lib/src/simple_configuration.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698