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

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

Issue 766653005: Add a [prints] matcher to the matcher library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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 | Annotate | Revision Log
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 'description.dart';
10 import 'expect.dart';
11 import 'interfaces.dart';
12 import 'future_matchers.dart';
13 import 'util.dart';
14
15 /// Matches a [Function] that prints text that matches [matcher].
16 ///
17 /// [matcher] may be a String or a [Matcher].
18 ///
19 /// If the function this runs against returns a [Future], all text printed by
20 /// the function (using [Zone] scoping) until that Future completes will be
Bob Nystrom 2014/12/05 18:14:53 "will be" -> "is".
nweiz 2014/12/09 01:01:01 Done.
21 /// matched.
22 ///
23 /// This only tracks text printed using the [print] function.
Bob Nystrom 2014/12/05 18:14:53 This means going between print(), stdout.write(),
nweiz 2014/12/09 01:01:01 That's already true; anyone could be using a custo
24 Matcher prints(matcher) => new _Prints(wrapMatcher(matcher));
25
26 class _Prints extends Matcher {
27 final Matcher _matcher;
28
29 _Prints(this._matcher);
30
31 bool matches(item, Map matchState) {
32 if (item is! Function) return false;
33
34 var buffer = new StringBuffer();
35 var result = runZoned(item, zoneSpecification:
36 new ZoneSpecification(print: (_, __, ____, line) {
37 buffer.writeln(line);
38 }));
39
40 if (result is! Future) {
41 var actual = buffer.toString();
42 matchState['prints.actual'] = actual;
43 return _matcher.matches(actual, matchState);
44 }
45
46 return completes.matches(result.then(wrapAsync((_) {
47 expect(buffer.toString(), _matcher);
48 }, 'prints')), matchState);
49 }
50
51 Description describe(Description description) =>
52 description.add('prints ').addDescriptionOf(_matcher);
53
54 Description describeMismatch(item, Description description, Map matchState,
55 bool verbose) {
56 var actual = matchState.remove('prints.actual');
57 if (actual == null) return description;
58 if (actual.isEmpty) return description.add("printed nothing.");
59
60 description.add('printed ').addDescriptionOf(actual);
61
62 // Create a new description for the matcher because at least
63 // [_StringEqualsMatcher] replaces the previous contents of the description.
64 var innerMismatch = _matcher.describeMismatch(
65 actual, new StringDescription(), matchState, verbose).toString();
66
67 if (innerMismatch.isNotEmpty) {
68 description.add('\n Which: ').add(innerMismatch.toString());
69 }
70
71 return description;
72 }
73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698