Add a [prints] matcher to the matcher library.

R=rnystrom@google.com

Review URL: https://codereview.chromium.org//766653005

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/matcher@42270 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4689c31..4e847d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.11.3
+
+* Add a `prints` matcher that matches output a callback emits via `print`.
+
 ## 0.11.2
 
 * Add an `isNotEmpty` matcher.
diff --git a/lib/matcher.dart b/lib/matcher.dart
index 6fd18cc..c7790c9 100644
--- a/lib/matcher.dart
+++ b/lib/matcher.dart
@@ -15,6 +15,7 @@
 export 'src/map_matchers.dart';
 export 'src/numeric_matchers.dart';
 export 'src/operator_matchers.dart';
+export 'src/prints_matcher.dart';
 export 'src/string_matchers.dart';
 export 'src/throws_matcher.dart';
 export 'src/throws_matchers.dart';
diff --git a/lib/src/prints_matcher.dart b/lib/src/prints_matcher.dart
new file mode 100644
index 0000000..8faa895
--- /dev/null
+++ b/lib/src/prints_matcher.dart
@@ -0,0 +1,72 @@
+// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library matcher.prints_matcher;
+
+import 'dart:async';
+
+import 'description.dart';
+import 'expect.dart';
+import 'interfaces.dart';
+import 'future_matchers.dart';
+import 'util.dart';
+
+/// Matches a [Function] that prints text that matches [matcher].
+///
+/// [matcher] may be a String or a [Matcher].
+///
+/// If the function this runs against returns a [Future], all text printed by
+/// the function (using [Zone] scoping) until that Future completes is matched.
+///
+/// This only tracks text printed using the [print] function.
+Matcher prints(matcher) => new _Prints(wrapMatcher(matcher));
+
+class _Prints extends Matcher {
+  final Matcher _matcher;
+
+  _Prints(this._matcher);
+
+  bool matches(item, Map matchState) {
+    if (item is! Function) return false;
+
+    var buffer = new StringBuffer();
+    var result = runZoned(item, zoneSpecification:
+        new ZoneSpecification(print: (_, __, ____, line) {
+      buffer.writeln(line);
+    }));
+
+    if (result is! Future) {
+      var actual = buffer.toString();
+      matchState['prints.actual'] = actual;
+      return _matcher.matches(actual, matchState);
+    }
+
+    return completes.matches(result.then(wrapAsync((_) {
+      expect(buffer.toString(), _matcher);
+    }, 'prints')), matchState);
+  }
+
+  Description describe(Description description) =>
+      description.add('prints ').addDescriptionOf(_matcher);
+
+  Description describeMismatch(item, Description description, Map matchState,
+      bool verbose) {
+    var actual = matchState.remove('prints.actual');
+    if (actual == null) return description;
+    if (actual.isEmpty) return description.add("printed nothing.");
+
+    description.add('printed ').addDescriptionOf(actual);
+
+    // Create a new description for the matcher because at least
+    // [_StringEqualsMatcher] replaces the previous contents of the description.
+    var innerMismatch = _matcher.describeMismatch(
+        actual, new StringDescription(), matchState, verbose).toString();
+
+    if (innerMismatch.isNotEmpty) {
+      description.add('\n   Which: ').add(innerMismatch.toString());
+    }
+
+    return description;
+  }
+}
diff --git a/pubspec.yaml b/pubspec.yaml
index 230ddc3..1951a5b 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: matcher
-version: 0.11.2
+version: 0.11.3
 author: Dart Team <misc@dartlang.org>
 description: Support for specifying test expectations
 homepage: https://pub.dartlang.org/packages/matcher
diff --git a/test/prints_matcher_test.dart b/test/prints_matcher_test.dart
new file mode 100644
index 0000000..183d4ef
--- /dev/null
+++ b/test/prints_matcher_test.dart
@@ -0,0 +1,104 @@
+// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library matcher.prints_matchers_test;
+
+import 'dart:async';
+
+import 'package:matcher/matcher.dart';
+import 'package:unittest/unittest.dart';
+
+import 'test_utils.dart';
+
+void main() {
+  initUtils();
+
+  group('synchronous', () {
+    test("passes with an expected print", () {
+      shouldPass(() => print("Hello, world!"), prints("Hello, world!\n"));
+    });
+
+    test("combines multiple prints", () {
+      shouldPass(() {
+        print("Hello");
+        print("World!");
+      }, prints("Hello\nWorld!\n"));
+    });
+
+    test("works with a Matcher", () {
+      shouldPass(() => print("Hello, world!"), prints(contains("Hello")));
+    });
+
+    test("describes a failure nicely", () {
+      shouldFail(() => print("Hello, world!"), prints("Goodbye, world!\n"),
+          "Expected: prints 'Goodbye, world!\\n' ''"
+          "  Actual: <Closure: () => dynamic> "
+          "   Which: printed 'Hello, world!\\n' ''"
+          "   Which: is different. "
+          "Expected: Goodbye, w ... "
+          "  Actual: Hello, wor ... "
+          "          ^ Differ at offset 0");
+    });
+
+    test("describes a failure with a non-descriptive Matcher nicely", () {
+      shouldFail(() => print("Hello, world!"), prints(contains("Goodbye")),
+          "Expected: prints contains 'Goodbye'"
+          "  Actual: <Closure: () => dynamic> "
+          "   Which: printed 'Hello, world!\\n' ''");
+    });
+
+    test("describes a failure with no text nicely", () {
+      shouldFail(() {}, prints(contains("Goodbye")),
+          "Expected: prints contains 'Goodbye'"
+          "  Actual: <Closure: () => dynamic> "
+          "   Which: printed nothing.");
+    });
+  });
+
+  group('asynchronous', () {
+    test("passes with an expected print", () {
+      shouldPass(() => new Future(() => print("Hello, world!")),
+          prints("Hello, world!\n"));
+    });
+
+    test("combines multiple prints", () {
+      shouldPass(() => new Future(() {
+        print("Hello");
+        print("World!");
+      }), prints("Hello\nWorld!\n"));
+    });
+
+    test("works with a Matcher", () {
+      shouldPass(() => new Future(() => print("Hello, world!")),
+          prints(contains("Hello")));
+    });
+
+    test("describes a failure nicely", () {
+      shouldFail(() => new Future(() => print("Hello, world!")),
+          prints("Goodbye, world!\n"),
+          "Expected: 'Goodbye, world!\\n' ''"
+          "  Actual: 'Hello, world!\\n' ''"
+          "   Which: is different. "
+          "Expected: Goodbye, w ... "
+          "  Actual: Hello, wor ... "
+          "          ^ Differ at offset 0",
+          isAsync: true);
+    });
+
+    test("describes a failure with a non-descriptive Matcher nicely", () {
+      shouldFail(() => new Future(() => print("Hello, world!")),
+          prints(contains("Goodbye")),
+          "Expected: contains 'Goodbye'"
+          "  Actual: 'Hello, world!\\n' ''",
+          isAsync: true);
+    });
+
+    test("describes a failure with no text nicely", () {
+      shouldFail(() => new Future.value(), prints(contains("Goodbye")),
+          "Expected: contains 'Goodbye'"
+          "  Actual: ''",
+          isAsync: true);
+    });
+  });
+}