| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library unittest.with_test_environment_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'package:unittest/unittest.dart'; | |
| 9 | |
| 10 class TestConfiguration extends 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(() => super.onDone(success)) | |
| 26 .then(_completer.complete) | |
| 27 .catchError(_completer.completeError); | |
| 28 } | |
| 29 | |
| 30 bool checkIfTestRan(String testName) { | |
| 31 return _results.any((test) => test.description == testName); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 Future runUnittests(Function callback) { | |
| 36 TestConfiguration config = unittestConfiguration = new TestConfiguration(); | |
| 37 callback(); | |
| 38 | |
| 39 return config.done; | |
| 40 } | |
| 41 | |
| 42 void runTests() { | |
| 43 test('test', () => expect(2 + 3, equals(5))); | |
| 44 } | |
| 45 | |
| 46 void runTests1() { | |
| 47 test('test1', () => expect(4 + 3, equals(7))); | |
| 48 } | |
| 49 | |
| 50 // Test that we can run two different sets of tests in the same run using the | |
| 51 // withTestEnvironment method. | |
| 52 void main() { | |
| 53 // Check that setting config a second time does not change the configuration | |
| 54 runUnittests(runTests); | |
| 55 var config = unittestConfiguration; | |
| 56 runUnittests(runTests1); | |
| 57 expect(config, unittestConfiguration); | |
| 58 | |
| 59 // Test that we can run both when encapsulating in their own private test | |
| 60 // environment. Also test that the tests actually running are the ones | |
| 61 // scheduled in the runTests/runTests1 methods. | |
| 62 withTestEnvironment(() { | |
| 63 runUnittests(runTests).whenComplete(() { | |
| 64 TestConfiguration config = unittestConfiguration; | |
| 65 expect(config.checkIfTestRan('test'), true); | |
| 66 expect(config.checkIfTestRan('test1'), false); | |
| 67 }); | |
| 68 }); | |
| 69 withTestEnvironment(() { | |
| 70 runUnittests(runTests1).whenComplete(() { | |
| 71 TestConfiguration config = unittestConfiguration; | |
| 72 expect(config.checkIfTestRan('test'), false); | |
| 73 expect(config.checkIfTestRan('test1'), true); | |
| 74 }); | |
| 75 }); | |
| 76 | |
| 77 // Third test that we can run with two nested test environments. | |
| 78 withTestEnvironment(() { | |
| 79 runUnittests(runTests); | |
| 80 withTestEnvironment(() { | |
| 81 runUnittests(runTests1); | |
| 82 }); | |
| 83 }); | |
| 84 } | |
| OLD | NEW |