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 'dart:async'; |
8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
9 | 9 |
10 class TestConfiguration extends SimpleConfiguration { | 10 class TestConfiguration extends SimpleConfiguration { |
(...skipping 15 matching lines...) Expand all Loading... |
26 .then(_completer.complete) | 26 .then(_completer.complete) |
27 .catchError(_completer.completeError); | 27 .catchError(_completer.completeError); |
28 } | 28 } |
29 | 29 |
30 bool checkIfTestRan(String testName) { | 30 bool checkIfTestRan(String testName) { |
31 return _results.any((test) => test.description == testName); | 31 return _results.any((test) => test.description == testName); |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 Future runUnittests(Function callback) { | 35 Future runUnittests(Function callback) { |
36 var config = new TestConfiguration(); | 36 TestConfiguration config = unittestConfiguration = new TestConfiguration(); |
37 unittestConfiguration = config; | |
38 callback(); | 37 callback(); |
39 | 38 |
40 return config.done; | 39 return config.done; |
41 } | 40 } |
42 | 41 |
43 void runTests() { | 42 void runTests() { |
44 test('test', () => expect(2 + 3, equals(5))); | 43 test('test', () => expect(2 + 3, equals(5))); |
45 } | 44 } |
46 | 45 |
47 void runTests1() { | 46 void runTests1() { |
48 test('test1', () => expect(4 + 3, equals(7))); | 47 test('test1', () => expect(4 + 3, equals(7))); |
49 } | 48 } |
50 | 49 |
51 // Test that we can run two different sets of tests in the same run using the | 50 // Test that we can run two different sets of tests in the same run using the |
52 // withTestEnvironment method. | 51 // withTestEnvironment method. |
53 void main() { | 52 void main() { |
54 // First check that we cannot call runUnittests twice in a row without it | 53 // Check that setting config a second time does not change the configuration |
55 // throwing a StateError due to the unittestConfiguration being set globally | 54 runUnittests(runTests); |
56 // in the first call. | 55 var config = unittestConfiguration; |
57 try { | 56 runUnittests(runTests1); |
58 runUnittests(runTests); | 57 expect(config, unittestConfiguration); |
59 runUnittests(runTests1); | |
60 throw 'Expected this to be unreachable since 2nd run above should throw'; | |
61 } on StateError catch (error) { | |
62 // expected, silently ignore. | |
63 } | |
64 | 58 |
65 // Test that we can run both when encapsulating in their own private test | 59 // Test that we can run both when encapsulating in their own private test |
66 // environment. Also test that the tests actually running are the ones | 60 // environment. Also test that the tests actually running are the ones |
67 // scheduled in the runTests/runTests1 methods. | 61 // scheduled in the runTests/runTests1 methods. |
68 withTestEnvironment(() { | 62 withTestEnvironment(() { |
69 runUnittests(runTests).whenComplete(() { | 63 runUnittests(runTests).whenComplete(() { |
70 TestConfiguration config = unittestConfiguration; | 64 TestConfiguration config = unittestConfiguration; |
71 expect(config.checkIfTestRan('test'), true); | 65 expect(config.checkIfTestRan('test'), true); |
72 expect(config.checkIfTestRan('test1'), false); | 66 expect(config.checkIfTestRan('test1'), false); |
73 }); | 67 }); |
74 }); | 68 }); |
75 withTestEnvironment(() { | 69 withTestEnvironment(() { |
76 runUnittests(runTests1).whenComplete(() { | 70 runUnittests(runTests1).whenComplete(() { |
77 TestConfiguration config = unittestConfiguration; | 71 TestConfiguration config = unittestConfiguration; |
78 expect(config.checkIfTestRan('test'), false); | 72 expect(config.checkIfTestRan('test'), false); |
79 expect(config.checkIfTestRan('test1'), true); | 73 expect(config.checkIfTestRan('test1'), true); |
80 }); | 74 }); |
81 }); | 75 }); |
82 | 76 |
83 // Third test that we can run with two nested test environments. | 77 // Third test that we can run with two nested test environments. |
84 withTestEnvironment(() { | 78 withTestEnvironment(() { |
85 runUnittests(runTests); | 79 runUnittests(runTests); |
86 withTestEnvironment(() { | 80 withTestEnvironment(() { |
87 runUnittests(runTests1); | 81 runUnittests(runTests1); |
88 }); | 82 }); |
89 }); | 83 }); |
90 } | 84 } |
OLD | NEW |