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

Side by Side Diff: test/codegen/unittest.dart

Issue 1321683004: use the real matchers for running language tests (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: add TODO Created 5 years, 3 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
« karma.conf.js ('K') | « test/codegen/expect/unittest/unittest.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 // TODO(jmesserly): replace this with the real package:test. 5 // TODO(jmesserly): replace this with the real package:test.
6 // Not possible yet due to various bugs we still have. 6 // Not possible yet due to various bugs we still have.
7 library minitest; 7 library minitest;
8 8
9 import 'dart:async'; 9 import 'dart:async';
10 import 'package:dom/dom.dart'; 10 import 'package:dom/dom.dart';
11 import 'package:matcher/matcher.dart';
12 export 'package:matcher/matcher.dart';
11 13
12 void group(String name, void body()) => (window as dynamic).suite(name, body); 14 void group(String name, void body()) => (window as dynamic).suite(name, body);
13 15
14 void test(String name, body(), {String skip}) { 16 void test(String name, body(), {String skip}) {
15 if (skip != null) { 17 if (skip != null) {
16 print('SKIP $name: $skip'); 18 print('SKIP $name: $skip');
17 return; 19 return;
18 } 20 }
19 (window as dynamic).test(name, (done) { 21 (window as dynamic).test(name, (done) {
20 _finishTest(f) { 22 _finishTest(f) {
21 if (f is Future) { 23 if (f is Future) {
22 f.then(_finishTest); 24 f.then(_finishTest);
23 } else { 25 } else {
24 done(); 26 done();
25 } 27 }
26 } 28 }
27 _finishTest(body()); 29 _finishTest(body());
28 }); 30 });
29 } 31 }
30 32
31 void expect(Object actual, matcher) { 33
32 if (matcher is! Matcher) matcher = equals(matcher); 34 // TODO(jmesserly): everything below this was stolen from
33 if (!matcher(actual)) { 35 // package:test/src/frontend/expect.dart
34 throw 'Expect failed to match $actual with $matcher'; 36
35 } 37 /// An exception thrown when a test assertion fails.
38 class TestFailure {
39 final String message;
40
41 TestFailure(this.message);
42
43 String toString() => message;
36 } 44 }
37 45
38 void fail(String message) { 46 /// The type used for functions that can be used to build up error reports
39 throw 'TestFailure: ' + message; 47 /// upon failures in [expect].
48 typedef String ErrorFormatter(
49 actual, Matcher matcher, String reason, Map matchState, bool verbose);
50
51 /// Assert that [actual] matches [matcher].
52 ///
53 /// This is the main assertion function. [reason] is optional and is typically
54 /// not supplied, as a reason is generated from [matcher]; if [reason]
55 /// is included it is appended to the reason generated by the matcher.
56 ///
57 /// [matcher] can be a value in which case it will be wrapped in an
58 /// [equals] matcher.
59 ///
60 /// If the assertion fails a [TestFailure] is thrown.
61 ///
62 /// In some cases extra diagnostic info can be produced on failure (for
63 /// example, stack traces on mismatched exceptions). To enable these,
64 /// [verbose] should be specified as `true`.
65 void expect(actual, matcher,
66 {String reason, bool verbose: false, ErrorFormatter formatter}) {
67
68 matcher = wrapMatcher(matcher);
69 var matchState = {};
70 try {
71 if (matcher.matches(actual, matchState)) return;
72 } catch (e, trace) {
73 if (reason == null) {
74 reason = '${(e is String) ? e : e.toString()} at $trace';
75 }
76 }
77 if (formatter == null) formatter = _defaultFailFormatter;
78 fail(formatter(actual, matcher, reason, matchState, verbose));
40 } 79 }
41 80
42 Matcher equals(Object expected) { 81 /// Convenience method for throwing a new [TestFailure] with the provided
43 return (actual) { 82 /// [message].
44 if (expected is List && actual is List) { 83 void fail(String message) => throw new TestFailure(message);
45 int len = expected.length; 84
46 if (len != actual.length) return false; 85 // The default error formatter.
47 for (int i = 0; i < len; i++) { 86 String _defaultFailFormatter(
48 if (!equals(expected[i])(actual[i])) return false; 87 actual, Matcher matcher, String reason, Map matchState, bool verbose) {
49 } 88 var description = new StringDescription();
50 return true; 89 description.add('Expected: ').addDescriptionOf(matcher).add('\n');
51 } else { 90 description.add(' Actual: ').addDescriptionOf(actual).add('\n');
52 return expected == actual; 91
53 } 92 var mismatchDescription = new StringDescription();
54 }; 93 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose);
94
95 if (mismatchDescription.length > 0) {
96 description.add(' Which: ${mismatchDescription}\n');
97 }
98 if (reason != null) description.add(reason).add('\n');
99 return description.toString();
55 } 100 }
56
57 Matcher same(Object expected) => (actual) => identical(expected, actual);
58 Matcher isNot(matcher) {
59 if (matcher is! Matcher) matcher = equals(matcher);
60 return (actual) => !matcher(actual);
61 }
62
63 bool isTrue(actual) => actual == true;
64 bool isNull(actual) => actual == null;
65 final Matcher isNotNull = isNot(isNull);
66 bool isRangeError(actual) => actual is RangeError;
67 bool isNoSuchMethodError(actual) => actual is NoSuchMethodError;
68 Matcher lessThan(expected) => (actual) => actual < expected;
69 Matcher greaterThan(expected) => (actual) => actual > expected;
70
71 Matcher throwsA(matcher) {
72 if (matcher is! Matcher) matcher = equals(matcher);
73 return (actual) {
74 try {
75 actual();
76 return false;
77 } catch(e) {
78 return matcher(e);
79 }
80 };
81 }
82
83 final Matcher throws = throwsA((a) => true);
84
85 typedef Matcher(actual);
OLDNEW
« karma.conf.js ('K') | « test/codegen/expect/unittest/unittest.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698