Chromium Code Reviews| 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)); | |
|
nweiz
2014/11/19 21:44:18
Indent this +4 spaces.
You can write .then(_compl
wibling
2014/11/20 14:34:51
Done.
| |
| 29 } | |
| 30 | |
| 31 bool checkIfTestRan(String testName) { | |
| 32 for (final t in _results) { | |
|
nweiz
2014/11/19 21:44:18
We don't use "final" for local variables.
Also, t
wibling
2014/11/20 14:34:51
Done.
| |
| 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))); |
| 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).whenComplete(() { | |
| 76 TestConfiguration config = unittestConfiguration; | |
| 77 expect(config.checkIfTestRan('test'), true); | |
| 78 expect(config.checkIfTestRan('test1'), false); | |
| 79 }); | |
| 80 }); | |
| 81 withTestEnvironment(() { | |
| 82 runUnittests(runTests1).whenComplete(() { | |
| 83 TestConfiguration config = unittestConfiguration; | |
| 84 expect(config.checkIfTestRan('test'), false); | |
| 85 expect(config.checkIfTestRan('test1'), true); | |
| 86 }); | |
| 87 }); | |
| 40 | 88 |
| 41 // Third test that we can run with two nested test environments. | 89 // Third test that we can run with two nested test environments. |
| 42 withTestEnvironment(() { | 90 withTestEnvironment(() { |
| 43 runUnittests(runTests); | 91 runUnittests(runTests); |
| 44 withTestEnvironment(() { | 92 withTestEnvironment(() { |
| 45 runUnittests(runTests1); | 93 runUnittests(runTests1); |
| 46 }); | 94 }); |
| 47 }); | 95 }); |
| 48 } | 96 } |
| OLD | NEW |