Index: test/codegen/expect/unittest.js |
diff --git a/test/codegen/expect/unittest.js b/test/codegen/expect/unittest.js |
index e6b818c85e45ed285b3940614ad1a35155a6f7e8..abc813b08aac12cb97b51ea14435d16111152fd7 100644 |
--- a/test/codegen/expect/unittest.js |
+++ b/test/codegen/expect/unittest.js |
@@ -1,12 +1,17 @@ |
dart_library.library('unittest', null, /* Imports */[ |
"dart_runtime/dart", |
+ 'matcher/matcher', |
'dom/dom', |
'dart/core', |
- 'dart/async' |
+ 'dart/async', |
+ 'matcher/src/interfaces', |
+ 'matcher/src/util', |
+ 'matcher/src/description' |
], /* Lazy imports */[ |
-], function(exports, dart, dom, core, async) { |
+], function(exports, dart, matcher, dom, core, async, interfaces, util, description$) { |
'use strict'; |
let dartx = dart.dartx; |
+ dart.export(exports, matcher); |
function group(name, body) { |
return dart.dsend(dom.window, 'suite', name, body); |
} |
@@ -30,108 +35,62 @@ dart_library.library('unittest', null, /* Imports */[ |
})); |
} |
dart.fn(test, dart.void, [core.String, dart.functionType(dart.dynamic, [])], {skip: core.String}); |
- function expect(actual, matcher) { |
- if (!dart.is(matcher, Matcher)) |
- matcher = equals(matcher); |
- if (!dart.notNull(dart.as(dart.dcall(matcher, actual), core.bool))) { |
- dart.throw(`Expect failed to match ${actual} with ${matcher}`); |
+ class TestFailure extends core.Object { |
+ TestFailure(message) { |
+ this.message = message; |
} |
- } |
- dart.fn(expect, dart.void, [core.Object, dart.dynamic]); |
- function fail(message) { |
- dart.throw('TestFailure: ' + dart.notNull(message)); |
- } |
- dart.fn(fail, dart.void, [core.String]); |
- function equals(expected) { |
- return dart.fn(actual => { |
- if (dart.is(expected, core.List) && dart.is(actual, core.List)) { |
- let len = expected[dartx.length]; |
- if (!dart.equals(len, dart.dload(actual, 'length'))) |
- return false; |
- for (let i = 0; dart.notNull(i) < dart.notNull(len); i = dart.notNull(i) + 1) { |
- if (!dart.notNull(dart.as(dart.dcall(equals(expected[dartx.get](i)), dart.dindex(actual, i)), core.bool))) |
- return false; |
- } |
- return true; |
- } else { |
- return dart.equals(expected, actual); |
- } |
- }); |
- } |
- dart.fn(equals, () => dart.definiteFunctionType(Matcher, [core.Object])); |
- function same(expected) { |
- return dart.fn(actual => core.identical(expected, actual), core.bool, [dart.dynamic]); |
- } |
- dart.fn(same, () => dart.definiteFunctionType(Matcher, [core.Object])); |
- function isNot(matcher) { |
- if (!dart.is(matcher, Matcher)) |
- matcher = equals(matcher); |
- return dart.fn(actual => !dart.notNull(dart.as(dart.dcall(matcher, actual), core.bool)), core.bool, [dart.dynamic]); |
- } |
- dart.fn(isNot, () => dart.definiteFunctionType(Matcher, [dart.dynamic])); |
- function isTrue(actual) { |
- return dart.equals(actual, true); |
- } |
- dart.fn(isTrue, core.bool, [dart.dynamic]); |
- function isNull(actual) { |
- return actual == null; |
- } |
- dart.fn(isNull, core.bool, [dart.dynamic]); |
- dart.defineLazyProperties(exports, { |
- get isNotNull() { |
- return isNot(isNull); |
+ toString() { |
+ return this.message; |
} |
- }); |
- function isRangeError(actual) { |
- return dart.is(actual, core.RangeError); |
- } |
- dart.fn(isRangeError, core.bool, [dart.dynamic]); |
- function isNoSuchMethodError(actual) { |
- return dart.is(actual, core.NoSuchMethodError); |
- } |
- dart.fn(isNoSuchMethodError, core.bool, [dart.dynamic]); |
- function lessThan(expected) { |
- return dart.fn(actual => dart.dsend(actual, '<', expected)); |
} |
- dart.fn(lessThan, () => dart.definiteFunctionType(Matcher, [dart.dynamic])); |
- function greaterThan(expected) { |
- return dart.fn(actual => dart.dsend(actual, '>', expected)); |
- } |
- dart.fn(greaterThan, () => dart.definiteFunctionType(Matcher, [dart.dynamic])); |
- function throwsA(matcher) { |
- if (!dart.is(matcher, Matcher)) |
- matcher = equals(matcher); |
- return dart.fn(actual => { |
- try { |
- dart.dcall(actual); |
- return false; |
- } catch (e) { |
- return dart.dcall(matcher, e); |
+ dart.setSignature(TestFailure, { |
+ constructors: () => ({TestFailure: [TestFailure, [core.String]]}) |
+ }); |
+ let ErrorFormatter = dart.typedef('ErrorFormatter', () => dart.functionType(core.String, [dart.dynamic, interfaces.Matcher, core.String, core.Map, core.bool])); |
+ function expect(actual, matcher, opts) { |
+ let reason = opts && 'reason' in opts ? opts.reason : null; |
+ let verbose = opts && 'verbose' in opts ? opts.verbose : false; |
+ let formatter = opts && 'formatter' in opts ? opts.formatter : null; |
+ matcher = util.wrapMatcher(matcher); |
+ let matchState = dart.map(); |
+ try { |
+ if (dart.notNull(dart.as(dart.dsend(matcher, 'matches', actual, matchState), core.bool))) |
+ return; |
+ } catch (e) { |
+ let trace = dart.stackTrace(e); |
+ if (reason == null) { |
+ reason = `${typeof e == 'string' ? e : dart.toString(e)} at ${trace}`; |
} |
+ } |
- }); |
+ if (formatter == null) |
+ formatter = _defaultFailFormatter; |
+ fail(dart.dcall(formatter, actual, matcher, reason, matchState, verbose)); |
} |
- dart.fn(throwsA, () => dart.definiteFunctionType(Matcher, [dart.dynamic])); |
- dart.defineLazyProperties(exports, { |
- get throws() { |
- return throwsA(dart.fn(a => true, core.bool, [dart.dynamic])); |
+ dart.fn(expect, dart.void, [dart.dynamic, dart.dynamic], {reason: core.String, verbose: core.bool, formatter: ErrorFormatter}); |
+ function fail(message) { |
+ return dart.throw(new TestFailure(message)); |
+ } |
+ dart.fn(fail, dart.void, [core.String]); |
+ function _defaultFailFormatter(actual, matcher, reason, matchState, verbose) { |
+ let description = new description$.StringDescription(); |
+ description.add('Expected: ').addDescriptionOf(matcher).add('\n'); |
+ description.add(' Actual: ').addDescriptionOf(actual).add('\n'); |
+ let mismatchDescription = new description$.StringDescription(); |
+ matcher.describeMismatch(actual, mismatchDescription, matchState, verbose); |
+ if (dart.notNull(mismatchDescription.length) > 0) { |
+ description.add(` Which: ${mismatchDescription}\n`); |
} |
- }); |
- let Matcher = dart.typedef('Matcher', () => dart.functionType(dart.dynamic, [dart.dynamic])); |
+ if (reason != null) |
+ description.add(reason).add('\n'); |
+ return dart.toString(description); |
+ } |
+ dart.fn(_defaultFailFormatter, core.String, [dart.dynamic, interfaces.Matcher, core.String, core.Map, core.bool]); |
// Exports: |
exports.group = group; |
exports.test = test; |
+ exports.TestFailure = TestFailure; |
+ exports.ErrorFormatter = ErrorFormatter; |
exports.expect = expect; |
exports.fail = fail; |
- exports.equals = equals; |
- exports.same = same; |
- exports.isNot = isNot; |
- exports.isTrue = isTrue; |
- exports.isNull = isNull; |
- exports.isRangeError = isRangeError; |
- exports.isNoSuchMethodError = isNoSuchMethodError; |
- exports.lessThan = lessThan; |
- exports.greaterThan = greaterThan; |
- exports.throwsA = throwsA; |
- exports.Matcher = Matcher; |
}); |