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 /// A test library for testing test libraries? We must go deeper. | 5 /// A test library for testing test libraries? We must go deeper. |
6 /// | 6 /// |
7 /// Since unit testing code tends to use a lot of global state, it can be tough | 7 /// Since unit testing code tends to use a lot of global state, it can be tough |
8 /// to test. This library manages it by running each test case in a child | 8 /// to test. This library manages it by running each test case in a child |
9 /// isolate, then reporting the results back to the parent isolate. | 9 /// isolate, then reporting the results back to the parent isolate. |
10 library metatest; | 10 library metatest; |
11 | 11 |
12 import 'dart:async'; | 12 import 'dart:async'; |
13 | 13 |
14 // TODO(nweiz): Stop importing from src when dart-lang/test#48 is fixed. | 14 // TODO(nweiz): Stop importing from src when dart-lang/test#48 is fixed. |
15 import 'package:test/src/backend/declarer.dart'; | 15 import 'package:test/src/backend/declarer.dart'; |
16 import 'package:test/src/backend/live_test.dart'; | 16 import 'package:test/src/backend/live_test.dart'; |
17 import 'package:test/src/backend/state.dart'; | 17 import 'package:test/src/backend/state.dart'; |
18 import 'package:test/src/backend/suite.dart'; | |
19 import 'package:test/src/runner/engine.dart'; | 18 import 'package:test/src/runner/engine.dart'; |
| 19 import 'package:test/src/runner/runner_suite.dart'; |
| 20 import 'package:test/src/runner/vm/environment.dart'; |
20 import 'package:test/test.dart'; | 21 import 'package:test/test.dart'; |
21 | 22 |
22 /// Declares a test with the given [description] and [body]. | 23 /// Declares a test with the given [description] and [body]. |
23 /// | 24 /// |
24 /// [body] corresponds to the `main` method of a test file. By default, this | 25 /// [body] corresponds to the `main` method of a test file. By default, this |
25 /// expects that all tests defined in [body] pass, but if [passing] is passed, | 26 /// expects that all tests defined in [body] pass, but if [passing] is passed, |
26 /// only tests listed there are expected to pass. | 27 /// only tests listed there are expected to pass. |
27 void expectTestsPass(String description, void body(), {List<String> passing, | 28 void expectTestsPass(String description, void body(), {List<String> passing, |
28 String testOn, Timeout timeout, skip, Map<String, dynamic> onPlatform}) { | 29 String testOn, Timeout timeout, skip, Map<String, dynamic> onPlatform}) { |
29 _setUpTest(description, body, (liveTests) { | 30 _setUpTest(description, body, (liveTests) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 64 |
64 /// Sets up a test with the given [description] and [body]. After the test runs, | 65 /// Sets up a test with the given [description] and [body]. After the test runs, |
65 /// calls [validate] with the result map. | 66 /// calls [validate] with the result map. |
66 void _setUpTest(String description, void body(), | 67 void _setUpTest(String description, void body(), |
67 void validate(List<LiveTest> liveTests), {String testOn, Timeout timeout, | 68 void validate(List<LiveTest> liveTests), {String testOn, Timeout timeout, |
68 skip, Map<String, dynamic> onPlatform}) { | 69 skip, Map<String, dynamic> onPlatform}) { |
69 test(description, () async { | 70 test(description, () async { |
70 var declarer = new Declarer(); | 71 var declarer = new Declarer(); |
71 runZoned(body, zoneValues: {#test.declarer: declarer}); | 72 runZoned(body, zoneValues: {#test.declarer: declarer}); |
72 | 73 |
73 var engine = new Engine.withSuites([new Suite(declarer.tests)]); | 74 var engine = new Engine.withSuites([ |
| 75 new RunnerSuite(new VMEnvironment(), declarer.tests) |
| 76 ]); |
74 for (var test in engine.liveTests) { | 77 for (var test in engine.liveTests) { |
75 test.onPrint.listen(print); | 78 test.onPrint.listen(print); |
76 } | 79 } |
77 await engine.run(); | 80 await engine.run(); |
78 | 81 |
79 validate(engine.liveTests); | 82 validate(engine.liveTests); |
80 }, testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform); | 83 }, testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform); |
81 } | 84 } |
82 | 85 |
83 /// Returns a string description of the test run descibed by [liveTests]. | 86 /// Returns a string description of the test run descibed by [liveTests]. |
84 String _summarizeTests(List<LiveTest> liveTests) { | 87 String _summarizeTests(List<LiveTest> liveTests) { |
85 var buffer = new StringBuffer(); | 88 var buffer = new StringBuffer(); |
86 for (var liveTest in liveTests) { | 89 for (var liveTest in liveTests) { |
87 buffer.writeln("${liveTest.state.result}: ${liveTest.test.name}"); | 90 buffer.writeln("${liveTest.state.result}: ${liveTest.test.name}"); |
88 for (var error in liveTest.errors) { | 91 for (var error in liveTest.errors) { |
89 buffer.writeln(error.error); | 92 buffer.writeln(error.error); |
90 if (error.stackTrace != null) buffer.writeln(error.stackTrace); | 93 if (error.stackTrace != null) buffer.writeln(error.stackTrace); |
91 } | 94 } |
92 } | 95 } |
93 return buffer.toString(); | 96 return buffer.toString(); |
94 } | 97 } |
OLD | NEW |