OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library unittest.with_test_environment_test; | 5 library unittest.with_test_environment_test; |
6 | 6 |
7 import 'dart:async'; | |
7 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
8 | 9 |
9 void runUnittests(Function callback) { | 10 class TestConfiguration extends SimpleConfiguration { |
10 unittestConfiguration = new SimpleConfiguration(); | 11 final Completer _completer = new Completer(); |
12 List<TestCase> _results; | |
13 | |
14 TestConfiguration(); | |
15 | |
16 void onSummary(int passed, int failed, int errors, List<TestCase> results, | |
17 String uncaughtError) { | |
18 super.onSummary(passed, failed, errors, results, uncaughtError); | |
19 _results = results; | |
20 } | |
21 | |
22 Future get done => _completer.future; | |
23 | |
24 onDone(success) { | |
25 new Future.sync(() { | |
26 super.onDone(success); | |
27 }).then((_) => _completer.complete(_)) | |
28 .catchError((error, stack) => _completer.completeError(error, stack)); | |
29 } | |
30 | |
31 bool checkIfTestRan(String testName) { | |
32 for (final t in _results) { | |
33 if (t.description == testName) { | |
34 return true; | |
35 } | |
36 } | |
37 return false; | |
38 } | |
39 } | |
40 | |
41 Future runUnittests(Function callback) { | |
42 var config = new TestConfiguration(); | |
43 unittestConfiguration = config; | |
11 callback(); | 44 callback(); |
45 | |
46 return config.done; | |
12 } | 47 } |
13 | 48 |
14 void runTests() { | 49 void runTests() { |
15 test('test', () => expect(2 + 3, equals(5))); | 50 test('test', () => expect(2 + 3, equals(5))); |
16 } | 51 } |
17 | 52 |
18 void runTests1() { | 53 void runTests1() { |
19 test('test1', () => expect(4 + 3, equals(7))); | 54 test('test1', () => expect(4 + 3, equals(7))); |
kustermann
2014/11/18 10:35:37
Maybe make the second test fail and check that it
| |
20 } | 55 } |
21 | 56 |
22 // Test that we can run two different sets of tests in the same run using the | 57 // Test that we can run two different sets of tests in the same run using the |
23 // withTestEnvironment method. | 58 // withTestEnvironment method. |
24 void main() { | 59 void main() { |
25 // First check that we cannot call runUnittests twice in a row without it | 60 // First check that we cannot call runUnittests twice in a row without it |
26 // throwing a StateError due to the unittestConfiguration being set globally | 61 // throwing a StateError due to the unittestConfiguration being set globally |
27 // in the first call. | 62 // in the first call. |
28 try { | 63 try { |
29 runUnittests(runTests); | 64 runUnittests(runTests); |
30 runUnittests(runTests1); | 65 runUnittests(runTests1); |
31 throw 'Expected this to be unreachable since 2nd run above should throw'; | 66 throw 'Expected this to be unreachable since 2nd run above should throw'; |
32 } on StateError catch (error) { | 67 } on StateError catch (error) { |
33 // expected, silently ignore. | 68 // expected, silently ignore. |
34 } | 69 } |
35 | 70 |
36 // Second test that we can run both when encapsulating in their own private | 71 // Test that we can run both when encapsulating in their own private test |
37 // test environment. | 72 // environment. Also test that the tests actually running are the ones |
38 withTestEnvironment(() => runUnittests(runTests)); | 73 // scheduled in the runTests/runTests1 methods. |
39 withTestEnvironment(() => runUnittests(runTests1)); | 74 withTestEnvironment(() { |
75 runUnittests(runTests) | |
76 .whenComplete(() { | |
kustermann
2014/11/18 10:35:37
I'd move the .whenComplete() to the previous line:
| |
77 TestConfiguration config = unittestConfiguration; | |
78 expect(config.checkIfTestRan('test'), true); | |
79 expect(config.checkIfTestRan('test1'), false); | |
80 }); | |
81 }); | |
82 withTestEnvironment(() { | |
83 runUnittests(runTests1) | |
84 .whenComplete(() { | |
kustermann
2014/11/18 10:35:37
ditto.
| |
85 TestConfiguration config = unittestConfiguration; | |
86 expect(config.checkIfTestRan('test'), false); | |
87 expect(config.checkIfTestRan('test1'), true); | |
88 }); | |
89 }); | |
40 | 90 |
41 // Third test that we can run with two nested test environments. | 91 // Third test that we can run with two nested test environments. |
42 withTestEnvironment(() { | 92 withTestEnvironment(() { |
43 runUnittests(runTests); | 93 runUnittests(runTests); |
44 withTestEnvironment(() { | 94 withTestEnvironment(() { |
45 runUnittests(runTests1); | 95 runUnittests(runTests1); |
46 }); | 96 }); |
47 }); | 97 }); |
48 } | 98 } |
OLD | NEW |