OLD | NEW |
1 dart_library.library('unittest', null, /* Imports */[ | 1 dart_library.library('unittest', null, /* Imports */[ |
2 "dart_runtime/dart", | 2 "dart_runtime/dart", |
| 3 'matcher/matcher', |
3 'dom/dom', | 4 'dom/dom', |
4 'dart/core', | 5 'dart/core', |
5 'dart/async' | 6 'dart/async', |
| 7 'matcher/src/interfaces', |
| 8 'matcher/src/util', |
| 9 'matcher/src/description' |
6 ], /* Lazy imports */[ | 10 ], /* Lazy imports */[ |
7 ], function(exports, dart, dom, core, async) { | 11 ], function(exports, dart, matcher, dom, core, async, interfaces, util, descript
ion$) { |
8 'use strict'; | 12 'use strict'; |
9 let dartx = dart.dartx; | 13 let dartx = dart.dartx; |
| 14 dart.export(exports, matcher); |
10 function group(name, body) { | 15 function group(name, body) { |
11 return dart.dsend(dom.window, 'suite', name, body); | 16 return dart.dsend(dom.window, 'suite', name, body); |
12 } | 17 } |
13 dart.fn(group, dart.void, [core.String, dart.functionType(dart.void, [])]); | 18 dart.fn(group, dart.void, [core.String, dart.functionType(dart.void, [])]); |
14 function test(name, body, opts) { | 19 function test(name, body, opts) { |
15 let skip = opts && 'skip' in opts ? opts.skip : null; | 20 let skip = opts && 'skip' in opts ? opts.skip : null; |
16 if (skip != null) { | 21 if (skip != null) { |
17 core.print(`SKIP ${name}: ${skip}`); | 22 core.print(`SKIP ${name}: ${skip}`); |
18 return; | 23 return; |
19 } | 24 } |
20 dart.dsend(dom.window, 'test', name, dart.fn(done => { | 25 dart.dsend(dom.window, 'test', name, dart.fn(done => { |
21 function _finishTest(f) { | 26 function _finishTest(f) { |
22 if (dart.is(f, async.Future)) { | 27 if (dart.is(f, async.Future)) { |
23 dart.dsend(f, 'then', _finishTest); | 28 dart.dsend(f, 'then', _finishTest); |
24 } else { | 29 } else { |
25 dart.dcall(done); | 30 dart.dcall(done); |
26 } | 31 } |
27 } | 32 } |
28 dart.fn(_finishTest); | 33 dart.fn(_finishTest); |
29 _finishTest(body()); | 34 _finishTest(body()); |
30 })); | 35 })); |
31 } | 36 } |
32 dart.fn(test, dart.void, [core.String, dart.functionType(dart.dynamic, [])], {
skip: core.String}); | 37 dart.fn(test, dart.void, [core.String, dart.functionType(dart.dynamic, [])], {
skip: core.String}); |
33 function expect(actual, matcher) { | 38 class TestFailure extends core.Object { |
34 if (!dart.is(matcher, Matcher)) | 39 TestFailure(message) { |
35 matcher = equals(matcher); | 40 this.message = message; |
36 if (!dart.notNull(dart.as(dart.dcall(matcher, actual), core.bool))) { | 41 } |
37 dart.throw(`Expect failed to match ${actual} with ${matcher}`); | 42 toString() { |
| 43 return this.message; |
38 } | 44 } |
39 } | 45 } |
40 dart.fn(expect, dart.void, [core.Object, dart.dynamic]); | 46 dart.setSignature(TestFailure, { |
| 47 constructors: () => ({TestFailure: [TestFailure, [core.String]]}) |
| 48 }); |
| 49 let ErrorFormatter = dart.typedef('ErrorFormatter', () => dart.functionType(co
re.String, [dart.dynamic, interfaces.Matcher, core.String, core.Map, core.bool])
); |
| 50 function expect(actual, matcher, opts) { |
| 51 let reason = opts && 'reason' in opts ? opts.reason : null; |
| 52 let verbose = opts && 'verbose' in opts ? opts.verbose : false; |
| 53 let formatter = opts && 'formatter' in opts ? opts.formatter : null; |
| 54 matcher = util.wrapMatcher(matcher); |
| 55 let matchState = dart.map(); |
| 56 try { |
| 57 if (dart.notNull(dart.as(dart.dsend(matcher, 'matches', actual, matchState
), core.bool))) |
| 58 return; |
| 59 } catch (e) { |
| 60 let trace = dart.stackTrace(e); |
| 61 if (reason == null) { |
| 62 reason = `${typeof e == 'string' ? e : dart.toString(e)} at ${trace}`; |
| 63 } |
| 64 } |
| 65 |
| 66 if (formatter == null) |
| 67 formatter = _defaultFailFormatter; |
| 68 fail(dart.dcall(formatter, actual, matcher, reason, matchState, verbose)); |
| 69 } |
| 70 dart.fn(expect, dart.void, [dart.dynamic, dart.dynamic], {reason: core.String,
verbose: core.bool, formatter: ErrorFormatter}); |
41 function fail(message) { | 71 function fail(message) { |
42 dart.throw('TestFailure: ' + dart.notNull(message)); | 72 return dart.throw(new TestFailure(message)); |
43 } | 73 } |
44 dart.fn(fail, dart.void, [core.String]); | 74 dart.fn(fail, dart.void, [core.String]); |
45 function equals(expected) { | 75 function _defaultFailFormatter(actual, matcher, reason, matchState, verbose) { |
46 return dart.fn(actual => { | 76 let description = new description$.StringDescription(); |
47 if (dart.is(expected, core.List) && dart.is(actual, core.List)) { | 77 description.add('Expected: ').addDescriptionOf(matcher).add('\n'); |
48 let len = expected[dartx.length]; | 78 description.add(' Actual: ').addDescriptionOf(actual).add('\n'); |
49 if (!dart.equals(len, dart.dload(actual, 'length'))) | 79 let mismatchDescription = new description$.StringDescription(); |
50 return false; | 80 matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); |
51 for (let i = 0; dart.notNull(i) < dart.notNull(len); i = dart.notNull(i)
+ 1) { | 81 if (dart.notNull(mismatchDescription.length) > 0) { |
52 if (!dart.notNull(dart.as(dart.dcall(equals(expected[dartx.get](i)), d
art.dindex(actual, i)), core.bool))) | 82 description.add(` Which: ${mismatchDescription}\n`); |
53 return false; | 83 } |
54 } | 84 if (reason != null) |
55 return true; | 85 description.add(reason).add('\n'); |
56 } else { | 86 return dart.toString(description); |
57 return dart.equals(expected, actual); | |
58 } | |
59 }); | |
60 } | 87 } |
61 dart.fn(equals, () => dart.definiteFunctionType(Matcher, [core.Object])); | 88 dart.fn(_defaultFailFormatter, core.String, [dart.dynamic, interfaces.Matcher,
core.String, core.Map, core.bool]); |
62 function same(expected) { | |
63 return dart.fn(actual => core.identical(expected, actual), core.bool, [dart.
dynamic]); | |
64 } | |
65 dart.fn(same, () => dart.definiteFunctionType(Matcher, [core.Object])); | |
66 function isNot(matcher) { | |
67 if (!dart.is(matcher, Matcher)) | |
68 matcher = equals(matcher); | |
69 return dart.fn(actual => !dart.notNull(dart.as(dart.dcall(matcher, actual),
core.bool)), core.bool, [dart.dynamic]); | |
70 } | |
71 dart.fn(isNot, () => dart.definiteFunctionType(Matcher, [dart.dynamic])); | |
72 function isTrue(actual) { | |
73 return dart.equals(actual, true); | |
74 } | |
75 dart.fn(isTrue, core.bool, [dart.dynamic]); | |
76 function isNull(actual) { | |
77 return actual == null; | |
78 } | |
79 dart.fn(isNull, core.bool, [dart.dynamic]); | |
80 dart.defineLazyProperties(exports, { | |
81 get isNotNull() { | |
82 return isNot(isNull); | |
83 } | |
84 }); | |
85 function isRangeError(actual) { | |
86 return dart.is(actual, core.RangeError); | |
87 } | |
88 dart.fn(isRangeError, core.bool, [dart.dynamic]); | |
89 function isNoSuchMethodError(actual) { | |
90 return dart.is(actual, core.NoSuchMethodError); | |
91 } | |
92 dart.fn(isNoSuchMethodError, core.bool, [dart.dynamic]); | |
93 function lessThan(expected) { | |
94 return dart.fn(actual => dart.dsend(actual, '<', expected)); | |
95 } | |
96 dart.fn(lessThan, () => dart.definiteFunctionType(Matcher, [dart.dynamic])); | |
97 function greaterThan(expected) { | |
98 return dart.fn(actual => dart.dsend(actual, '>', expected)); | |
99 } | |
100 dart.fn(greaterThan, () => dart.definiteFunctionType(Matcher, [dart.dynamic]))
; | |
101 function throwsA(matcher) { | |
102 if (!dart.is(matcher, Matcher)) | |
103 matcher = equals(matcher); | |
104 return dart.fn(actual => { | |
105 try { | |
106 dart.dcall(actual); | |
107 return false; | |
108 } catch (e) { | |
109 return dart.dcall(matcher, e); | |
110 } | |
111 | |
112 }); | |
113 } | |
114 dart.fn(throwsA, () => dart.definiteFunctionType(Matcher, [dart.dynamic])); | |
115 dart.defineLazyProperties(exports, { | |
116 get throws() { | |
117 return throwsA(dart.fn(a => true, core.bool, [dart.dynamic])); | |
118 } | |
119 }); | |
120 let Matcher = dart.typedef('Matcher', () => dart.functionType(dart.dynamic, [d
art.dynamic])); | |
121 // Exports: | 89 // Exports: |
122 exports.group = group; | 90 exports.group = group; |
123 exports.test = test; | 91 exports.test = test; |
| 92 exports.TestFailure = TestFailure; |
| 93 exports.ErrorFormatter = ErrorFormatter; |
124 exports.expect = expect; | 94 exports.expect = expect; |
125 exports.fail = fail; | 95 exports.fail = fail; |
126 exports.equals = equals; | |
127 exports.same = same; | |
128 exports.isNot = isNot; | |
129 exports.isTrue = isTrue; | |
130 exports.isNull = isNull; | |
131 exports.isRangeError = isRangeError; | |
132 exports.isNoSuchMethodError = isNoSuchMethodError; | |
133 exports.lessThan = lessThan; | |
134 exports.greaterThan = greaterThan; | |
135 exports.throwsA = throwsA; | |
136 exports.Matcher = Matcher; | |
137 }); | 96 }); |
OLD | NEW |