diff --git a/lib/src/spread_args_helper.dart b/lib/src/spread_args_helper.dart index 5d555722ce28555a710ebf8ff67c9b45a72586f9..41411b93780f4f2d68f4f6941dad6186b668f4f4 100644 --- a/lib/src/spread_args_helper.dart +++ b/lib/src/spread_args_helper.dart @@ -1,15 +1,9 @@ part of unittest; -const _PLACE_HOLDER = const _ArgPlaceHolder(); - -class _ArgPlaceHolder { - const _ArgPlaceHolder(); -} - /** Simulates spread arguments using named arguments. */ // TODO(sigmund): remove this class and simply use a closure with named // arguments (if still applicable). -class _SpreadArgsHelper implements Function { +class _SpreadArgsHelper { final Function callback; final int minExpectedCalls; final int maxExpectedCalls; @@ -20,7 +14,7 @@ class _SpreadArgsHelper implements Function { bool complete; _SpreadArgsHelper(Function callback, int minExpected, int maxExpected, - String id, {bool isDone()}) + Function isDone, String id) : this.callback = callback, minExpectedCalls = minExpected, maxExpectedCalls = (maxExpected == 0 && minExpected > 0) @@ -96,16 +90,31 @@ class _SpreadArgsHelper implements Function { } } - call([a0 = _PLACE_HOLDER, a1 = _PLACE_HOLDER, a2 = _PLACE_HOLDER, - a3 = _PLACE_HOLDER, a4 = _PLACE_HOLDER, a5 = _PLACE_HOLDER]) { + invoke0() { + return _guardAsync( + () { + if (shouldCallBack()) { + return callback(); + } + }, + after, testCase); + } - var args = [a0, a1, a2, a3, a4, a5]; - args.removeWhere((a) => a == _PLACE_HOLDER); + invoke1(arg1) { + return _guardAsync( + () { + if (shouldCallBack()) { + return callback(arg1); + } + }, + after, testCase); + } + invoke2(arg1, arg2) { return _guardAsync( () { if (shouldCallBack()) { - return Function.apply(callback, args); + return callback(arg1, arg2); } }, after, testCase); diff --git a/lib/unittest.dart b/lib/unittest.dart index 39263470bbab6bcbe67ea342e3266cd09c7e7197..da95faecc9f03ca1ef539d431f88a8372c0f7b85 100644 --- a/lib/unittest.dart +++ b/lib/unittest.dart @@ -325,8 +325,22 @@ void solo_test(String spec, TestFunction body) { * bound. */ Function expectAsync(Function callback, - {int count: 1, int max: 0, String id}) => - new _SpreadArgsHelper(callback, count, max, id); + {int count: 1, int max: 0, String id}) { + var minArgs = _minArgs(callback); + + switch(minArgs) { + case 0: + return new _SpreadArgsHelper(callback, count, max, null, id).invoke0; + case 1: + return new _SpreadArgsHelper(callback, count, max, null, id).invoke1; + case 2: + return new _SpreadArgsHelper(callback, count, max, null, id).invoke2; + default: + // _minArgs throws an argument exception if the arg count is > 2. + // this is just for paranoia + throw new StateError('Should never get here'); + } +} /** * *Deprecated* @@ -368,8 +382,22 @@ Function expectAsync2(Function callback, * identify the callback in error messages (for example if it is called * after the test case is complete). */ -Function expectAsyncUntil(Function callback, bool isDone(), {String id}) => - new _SpreadArgsHelper(callback, 0, -1, id, isDone: isDone); +Function expectAsyncUntil(Function callback, Function isDone, {String id}) { + var minArgs = _minArgs(callback); + + switch(minArgs) { + case 0: + return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke0; + case 1: + return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke1; + case 2: + return new _SpreadArgsHelper(callback, 0, -1, isDone, id).invoke2; + default: + // _minArgs throws an argument exception if the arg count is > 2. + // this is just for paranoia + throw new StateError('Should never get here'); + } +} /** * *Deprecated* @@ -742,3 +770,18 @@ Trace _getTrace(stack) { return frame.package != 'unittest' || frame.member != 'TestCase._runTest'; })).terse.foldFrames((frame) => frame.package == 'unittest' || frame.isCore); } + +typedef _Func0(); +typedef _Func1(a); +typedef _Func2(a, b); + +/** + * Throws an [ArgumentError] if the callback has more than 2 required arguments. + */ +int _minArgs(Function callback) { + if (callback is _Func0) return 0; + if (callback is _Func1) return 1; + if (callback is _Func2) return 2; + throw new ArgumentError( + 'The callback argument has more than 2 required arguments.'); +} diff --git a/test/missing_tick_test.dart b/test/missing_tick_test.dart index 19c5b8946ebf00754d92cbce4a43e308cadb1f14..92ab8914704d7eac0c2491d2fbf3d1c307990fdb 100644 --- a/test/missing_tick_test.dart +++ b/test/missing_tick_test.dart @@ -10,7 +10,7 @@ main() { config.timeout = const Duration(seconds: 2); group('Broken', () { test('test that should time out', () { - expectAsync(() {}); + expectAsync0(() {}); }); }); } diff --git a/test/test_utils.dart b/test/test_utils.dart index 783f71ba91839e7310c9a119de5fb3e36d5906c2..de783bedcf09abf5b13449ad96ef732a592f9b73 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -36,7 +36,7 @@ void shouldFail(value, Matcher matcher, expected, {bool isAsync: false}) { if (expected is String) { expect(errorString, equalsIgnoringWhitespace(expected)); } else { - expect(errorString.replaceAll('\n', ''), expected); + expect(errorString.replaceAll('\n',''), expected); } } diff --git a/test/unittest_async_exception_test.dart b/test/unittest_async_exception_test.dart index cc351169debb8aee01d16c735e75e39e843541ba..34298b7d275502227fa80a1d3a0ce6c024e465d3 100644 --- a/test/unittest_async_exception_test.dart +++ b/test/unittest_async_exception_test.dart @@ -13,8 +13,8 @@ var testName = 'async exception test'; var testFunction = (_) { test(testName, () { - expectAsync(() {}); - _defer(() { throw "error!"; }); + expectAsync0(() {}); + _defer(() => guardAsync(() { throw "error!"; })); }); }; diff --git a/test/unittest_async_setup_teardown_test.dart b/test/unittest_async_setup_teardown_test.dart index 067dbf951208a6e0bef895f968dbf3bda08224ef..a73efc8e4b97c7174918f6b722aebf7ff4c7234d 100644 --- a/test/unittest_async_setup_teardown_test.dart +++ b/test/unittest_async_setup_teardown_test.dart @@ -19,7 +19,7 @@ var testFunction = (_) { tearDown(() { return new Future.value(0); }); - test('foo1', () {}); + test('foo1', (){}); }); group('good setup/bad teardown', () { setUp(() { @@ -28,7 +28,7 @@ var testFunction = (_) { tearDown(() { return new Future.error("Failed to complete tearDown"); }); - test('foo2', () {}); + test('foo2', (){}); }); group('bad setup/good teardown', () { setUp(() { @@ -37,7 +37,7 @@ var testFunction = (_) { tearDown(() { return new Future.value(0); }); - test('foo3', () {}); + test('foo3', (){}); }); group('bad setup/bad teardown', () { setUp(() { @@ -46,7 +46,7 @@ var testFunction = (_) { tearDown(() { return new Future.error("Failed to complete tearDown"); }); - test('foo4', () {}); + test('foo4', (){}); }); // The next test is just to make sure we make steady progress // through the tests. diff --git a/test/unittest_completion_test.dart b/test/unittest_completion_test.dart index 037dee1e2a2f53821387d416d57cf5085cd4061f..843f9d06b2155db2b314659d40725339ad2c8362 100644 --- a/test/unittest_completion_test.dart +++ b/test/unittest_completion_test.dart @@ -14,7 +14,7 @@ var testName = 'completion test'; var testFunction = (TestConfiguration testConfig) { test(testName, () { var _callback; - _callback = expectAsyncUntil(() { + _callback = expectAsyncUntil0(() { if (++testConfig.count < 10) { _defer(_callback); } diff --git a/test/unittest_correct_callback_test.dart b/test/unittest_correct_callback_test.dart index 43b40eab2672f7fea8fc9a5dd61acdba1da52481..cd52ba060a13f5f8563b4f7edffcd1a52c6ef491 100644 --- a/test/unittest_correct_callback_test.dart +++ b/test/unittest_correct_callback_test.dart @@ -13,7 +13,7 @@ var testName = 'correct callback test'; var testFunction = (TestConfiguration testConfig) { test(testName, - () =>_defer(expectAsync((){ ++testConfig.count;}))); + () =>_defer(expectAsync0((){ ++testConfig.count;}))); }; var expected = buildStatusString(1, 0, 0, testName, count: 1); diff --git a/test/unittest_excess_callback_test.dart b/test/unittest_excess_callback_test.dart index 76a3a9ee0949f0ae409c7ced0b4584fdd24f8d2f..f3876b518f69c487a76cb65fbd3f1609110dcd0f 100644 --- a/test/unittest_excess_callback_test.dart +++ b/test/unittest_excess_callback_test.dart @@ -13,9 +13,9 @@ var testName = 'excess callback test'; var testFunction = (TestConfiguration testConfig) { test(testName, () { - var _callback0 = expectAsync(() => ++testConfig.count); - var _callback1 = expectAsync(() => ++testConfig.count); - var _callback2 = expectAsync(() { + var _callback0 = expectAsync0(() => ++testConfig.count); + var _callback1 = expectAsync0(() => ++testConfig.count); + var _callback2 = expectAsync0(() { _callback1(); _callback1(); _callback0(); diff --git a/test/unittest_expect_async_args_test.dart b/test/unittest_expect_async_args_test.dart deleted file mode 100644 index 3c36ebc4fa361a730b44229102c7440f894cdf2c..0000000000000000000000000000000000000000 --- a/test/unittest_expect_async_args_test.dart +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2013, 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 unittestTest; - -import 'dart:isolate'; -import 'dart:async'; -import 'package:unittest/unittest.dart'; - -part 'unittest_test_utils.dart'; - -var testName = 'expect async args'; - -var testFunction = (TestConfiguration testConfig) { - List<int> _getArgs([a = 0, b = 0, c = 0, d = 0, e = 0, f = 0]) { - testConfig.count++; - return [a, b, c, d, e, f]; - } - - test(testName, () { - expect(expectAsync(_getArgs)(), [0, 0, 0, 0, 0, 0]); - expect(expectAsync(_getArgs)(5), [5, 0, 0, 0, 0, 0]); - expect(expectAsync(_getArgs)(1, 2, 3, 4, 5, 6), [1, 2, 3, 4, 5, 6]); - }); - - test('invoked with too many args', () { - expectAsync(_getArgs)(1, 2, 3, 4, 5, 6, 7); - }); - - test('created with too many args', () { - expectAsync((a1, a2, a3, a4, a5, a6, a7) { - testConfig.count++; - })(); - }); -}; - -final expected = startsWith('1:1:1:3:3:::null:expect async args::' - 'invoked with too many args:Test failed:'); diff --git a/test/unittest_late_exception_test.dart b/test/unittest_late_exception_test.dart index c80de1263329dab9e81ad440aceee9b8036db5b5..7a5c3e61cbdb86ddfad33eb31649c6ff00c1a6f2 100644 --- a/test/unittest_late_exception_test.dart +++ b/test/unittest_late_exception_test.dart @@ -14,11 +14,11 @@ var testName = 'late exception test'; var testFunction = (_) { var f; test('testOne', () { - f = expectAsync(() {}); + f = expectAsync0(() {}); _defer(f); }); test('testTwo', () { - _defer(expectAsync(() { f(); })); + _defer(expectAsync0(() { f(); })); }); }; diff --git a/test/unittest_middle_exception_test.dart b/test/unittest_middle_exception_test.dart index ba7751470b30046bbe22f6dd27f360ce13fb44e3..d405500c57d05476bbd634172ccb99aab6034427 100644 --- a/test/unittest_middle_exception_test.dart +++ b/test/unittest_middle_exception_test.dart @@ -15,7 +15,7 @@ var testFunction = (_) { test('testOne', () { expect(true, isTrue); }); test('testTwo', () { expect(true, isFalse); }); test('testThree', () { - var done = expectAsync(() {}); + var done = expectAsync0((){}); _defer(() { expect(true, isTrue); done(); diff --git a/test/unittest_protect_async_test.dart b/test/unittest_protect_async_test.dart index 15d339484d8f3cd63499a0fae0a02a90f8ab2a2e..12ded3cecd0b03be67db1b7e7ac18ba92969f018 100644 --- a/test/unittest_protect_async_test.dart +++ b/test/unittest_protect_async_test.dart @@ -11,23 +11,23 @@ part 'unittest_test_utils.dart'; var testFunction = (_) { test('protectAsync0', () { - var protected = () { + var protected = protectAsync0(() { throw new StateError('error during protectAsync0'); - }; + }); new Future(protected); }); test('protectAsync1', () { - var protected = (arg) { + var protected = protectAsync1((arg) { throw new StateError('error during protectAsync1: $arg'); - }; + }); new Future(() => protected('one arg')); }); test('protectAsync2', () { - var protected = (arg1, arg2) { + var protected = protectAsync2((arg1, arg2) { throw new StateError('error during protectAsync2: $arg1, $arg2'); - }; + }); new Future(() => protected('arg1', 'arg2')); }); diff --git a/test/unittest_test_returning_future_test.dart b/test/unittest_test_returning_future_test.dart index 83c4a5d3673d734333a3bb71b7a7c16f1f4b11ed..a74a8db55479d6522cbfc9feceb136aeea82fc4a 100644 --- a/test/unittest_test_returning_future_test.dart +++ b/test/unittest_test_returning_future_test.dart @@ -21,8 +21,8 @@ var testFunction = (_) { // I had a situation where either worked fine on their own, and // error/fail worked, but fail/error would time out. test("error1", () { - var callback = expectAsync(() {}); - var excesscallback = expectAsync(() {}); + var callback = expectAsync0((){}); + var excesscallback = expectAsync0((){}); return _defer(() { excesscallback(); excesscallback(); @@ -36,8 +36,8 @@ var testFunction = (_) { }); }); test("error2", () { - var callback = expectAsync(() {}); - var excesscallback = expectAsync(() {}); + var callback = expectAsync0((){}); + var excesscallback = expectAsync0((){}); return _defer(() { excesscallback(); excesscallback(); diff --git a/test/unittest_test_returning_future_using_runasync_test.dart b/test/unittest_test_returning_future_using_runasync_test.dart index e5fc304d7168b80a52c06d0f9512f35a1f5d14f2..98f276b825f04a9d3b8d856f1266f0ea612247b4 100644 --- a/test/unittest_test_returning_future_using_runasync_test.dart +++ b/test/unittest_test_returning_future_using_runasync_test.dart @@ -15,48 +15,58 @@ var testFunction = (_) { test("successful", () { return _defer(() { scheduleMicrotask(() { - expect(true, true); + guardAsync(() { + expect(true, true); + }); }); }); }); test("fail1", () { - var callback = expectAsync(() {}); + var callback = expectAsync0((){}); return _defer(() { scheduleMicrotask(() { - expect(true, false); - callback(); + guardAsync(() { + expect(true, false); + callback(); + }); }); }); }); test('error1', () { - var callback = expectAsync(() {}); - var excesscallback = expectAsync(() {}); + var callback = expectAsync0((){}); + var excesscallback = expectAsync0((){}); return _defer(() { scheduleMicrotask(() { - excesscallback(); - excesscallback(); - callback(); + guardAsync(() { + excesscallback(); + excesscallback(); + callback(); + }); }); }); }); test("fail2", () { - var callback = expectAsync(() {}); + var callback = expectAsync0((){}); return _defer(() { scheduleMicrotask(() { - fail('failure'); - callback(); + guardAsync(() { + fail('failure'); + callback(); + }); }); }); }); test('error2', () { - var callback = expectAsync(() {}); - var excesscallback = expectAsync(() {}); + var callback = expectAsync0((){}); + var excesscallback = expectAsync0((){}); return _defer(() { scheduleMicrotask(() { - excesscallback(); - excesscallback(); - excesscallback(); - callback(); + guardAsync(() { + excesscallback(); + excesscallback(); + excesscallback(); + callback(); + }); }); }); }); diff --git a/test/unittest_test_utils.dart b/test/unittest_test_utils.dart index 8385a1b7b767eea7d4a658943fead7473dc7f382..adb9823f366111179ed0b6bc437ebc8ba7b23de9 100644 --- a/test/unittest_test_utils.dart +++ b/test/unittest_test_utils.dart @@ -16,7 +16,7 @@ String buildStatusString(int passed, int failed, int errors, String message: ''}) { var totalTests = 0; var testDetails = new StringBuffer(); - if (results == null) { + if(results == null) { // no op assert(message == ''); } else if (results is String) { @@ -44,7 +44,7 @@ class TestConfiguration extends Configuration { final SendPort _port; String _result; - TestConfiguration(this._port): super.blank(); + TestConfiguration(this._port) : super.blank(); void onSummary(int passed, int failed, int errors, List<TestCase> results, String uncaughtError) { @@ -88,6 +88,6 @@ main() { var replyPort = new ReceivePort(); Isolate.spawn(runTestInIsolate, replyPort.sendPort); replyPort.first.then((String msg) { - expect(msg.trim(), expected); + expect(msg.trim(), equals(expected)); }); }