Index: test/codegen/unittest.dart |
diff --git a/test/codegen/unittest.dart b/test/codegen/unittest.dart |
index e964b80a38a393def9675266352dc6735c360e19..18d0c41def6d125e6252a7b83954e8d1d7f4aa5d 100644 |
--- a/test/codegen/unittest.dart |
+++ b/test/codegen/unittest.dart |
@@ -8,6 +8,8 @@ library minitest; |
import 'dart:async'; |
import 'package:dom/dom.dart'; |
+import 'package:matcher/matcher.dart'; |
+export 'package:matcher/matcher.dart'; |
void group(String name, void body()) => (window as dynamic).suite(name, body); |
@@ -28,58 +30,71 @@ void test(String name, body(), {String skip}) { |
}); |
} |
-void expect(Object actual, matcher) { |
- if (matcher is! Matcher) matcher = equals(matcher); |
- if (!matcher(actual)) { |
- throw 'Expect failed to match $actual with $matcher'; |
- } |
-} |
-void fail(String message) { |
- throw 'TestFailure: ' + message; |
-} |
+// TODO(jmesserly): everything below this was stolen from |
+// package:test/src/frontend/expect.dart |
-Matcher equals(Object expected) { |
- return (actual) { |
- if (expected is List && actual is List) { |
- int len = expected.length; |
- if (len != actual.length) return false; |
- for (int i = 0; i < len; i++) { |
- if (!equals(expected[i])(actual[i])) return false; |
- } |
- return true; |
- } else { |
- return expected == actual; |
- } |
- }; |
-} |
+/// An exception thrown when a test assertion fails. |
+class TestFailure { |
+ final String message; |
+ |
+ TestFailure(this.message); |
-Matcher same(Object expected) => (actual) => identical(expected, actual); |
-Matcher isNot(matcher) { |
- if (matcher is! Matcher) matcher = equals(matcher); |
- return (actual) => !matcher(actual); |
+ String toString() => message; |
} |
-bool isTrue(actual) => actual == true; |
-bool isNull(actual) => actual == null; |
-final Matcher isNotNull = isNot(isNull); |
-bool isRangeError(actual) => actual is RangeError; |
-bool isNoSuchMethodError(actual) => actual is NoSuchMethodError; |
-Matcher lessThan(expected) => (actual) => actual < expected; |
-Matcher greaterThan(expected) => (actual) => actual > expected; |
- |
-Matcher throwsA(matcher) { |
- if (matcher is! Matcher) matcher = equals(matcher); |
- return (actual) { |
- try { |
- actual(); |
- return false; |
- } catch(e) { |
- return matcher(e); |
+/// The type used for functions that can be used to build up error reports |
+/// upon failures in [expect]. |
+typedef String ErrorFormatter( |
+ actual, Matcher matcher, String reason, Map matchState, bool verbose); |
+ |
+/// Assert that [actual] matches [matcher]. |
+/// |
+/// This is the main assertion function. [reason] is optional and is typically |
+/// not supplied, as a reason is generated from [matcher]; if [reason] |
+/// is included it is appended to the reason generated by the matcher. |
+/// |
+/// [matcher] can be a value in which case it will be wrapped in an |
+/// [equals] matcher. |
+/// |
+/// If the assertion fails a [TestFailure] is thrown. |
+/// |
+/// In some cases extra diagnostic info can be produced on failure (for |
+/// example, stack traces on mismatched exceptions). To enable these, |
+/// [verbose] should be specified as `true`. |
+void expect(actual, matcher, |
+ {String reason, bool verbose: false, ErrorFormatter formatter}) { |
+ |
+ matcher = wrapMatcher(matcher); |
+ var matchState = {}; |
+ try { |
+ if (matcher.matches(actual, matchState)) return; |
+ } catch (e, trace) { |
+ if (reason == null) { |
+ reason = '${(e is String) ? e : e.toString()} at $trace'; |
} |
- }; |
+ } |
+ if (formatter == null) formatter = _defaultFailFormatter; |
+ fail(formatter(actual, matcher, reason, matchState, verbose)); |
} |
-final Matcher throws = throwsA((a) => true); |
+/// Convenience method for throwing a new [TestFailure] with the provided |
+/// [message]. |
+void fail(String message) => throw new TestFailure(message); |
+ |
+// The default error formatter. |
+String _defaultFailFormatter( |
+ actual, Matcher matcher, String reason, Map matchState, bool verbose) { |
+ var description = new StringDescription(); |
+ description.add('Expected: ').addDescriptionOf(matcher).add('\n'); |
+ description.add(' Actual: ').addDescriptionOf(actual).add('\n'); |
-typedef Matcher(actual); |
+ var mismatchDescription = new StringDescription(); |
+ matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); |
+ |
+ if (mismatchDescription.length > 0) { |
+ description.add(' Which: ${mismatchDescription}\n'); |
+ } |
+ if (reason != null) description.add(reason).add('\n'); |
+ return description.toString(); |
+} |