OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import 'package:path/path.dart' as p; | 7 import 'package:path/path.dart' as p; |
8 import 'package:unittest/src/io.dart'; | 8 import 'package:unittest/src/io.dart'; |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 | 10 |
11 import 'io.dart'; | 11 import 'io.dart'; |
12 | 12 |
13 String _sandbox; | 13 String _sandbox; |
14 | 14 |
15 void main() { | 15 void main() { |
16 test("reports when no tests are run", () { | 16 test("reports when no tests are run", () { |
17 return withTempDir((path) { | 17 return withTempDir((path) { |
18 new File(p.join(path, "test.dart")).writeAsStringSync("void main() {}"); | 18 new File(p.join(path, "test.dart")).writeAsStringSync("void main() {}"); |
19 var result = runUnittest(["test.dart"], workingDirectory: path); | 19 var result = runUnittest(["test.dart"], workingDirectory: path); |
20 expect(result.stdout, equals("No tests ran.\n")); | 20 expect(result.stdout, equals("No tests ran.\n")); |
21 }); | 21 }); |
22 }); | 22 }); |
23 | 23 |
24 test("runs several successful tests and reports when each completes", () { | 24 test("runs several successful tests and reports when each completes", () { |
25 _expectReport(""" | 25 _expectReport(""" |
26 declarer.test('success 1', () {}); | 26 test('success 1', () {}); |
27 declarer.test('success 2', () {}); | 27 test('success 2', () {}); |
28 declarer.test('success 3', () {});""", | 28 test('success 3', () {});""", |
29 """ | 29 """ |
30 +0: success 1 | 30 +0: success 1 |
31 +1: success 1 | 31 +1: success 1 |
32 +1: success 2 | 32 +1: success 2 |
33 +2: success 2 | 33 +2: success 2 |
34 +2: success 3 | 34 +2: success 3 |
35 +3: success 3 | 35 +3: success 3 |
36 +3: All tests passed!"""); | 36 +3: All tests passed!"""); |
37 }); | 37 }); |
38 | 38 |
39 test("runs several failing tests and reports when each fails", () { | 39 test("runs several failing tests and reports when each fails", () { |
40 _expectReport(""" | 40 _expectReport(""" |
41 declarer.test('failure 1', () => throw new TestFailure('oh no')); | 41 test('failure 1', () => throw new TestFailure('oh no')); |
42 declarer.test('failure 2', () => throw new TestFailure('oh no')); | 42 test('failure 2', () => throw new TestFailure('oh no')); |
43 declarer.test('failure 3', () => throw new TestFailure('oh no'));""", | 43 test('failure 3', () => throw new TestFailure('oh no'));""", |
44 """ | 44 """ |
45 +0: failure 1 | 45 +0: failure 1 |
46 +0 -1: failure 1 | 46 +0 -1: failure 1 |
47 oh no | 47 oh no |
48 test.dart 7:42 main.<fn> | 48 test.dart 6:33 main.<fn> |
49 dart:isolate _RawReceivePortImpl._handleMessage | 49 dart:isolate _RawReceivePortImpl._handleMessage |
50 | 50 |
51 | 51 |
52 +0 -1: failure 2 | 52 +0 -1: failure 2 |
53 +0 -2: failure 2 | 53 +0 -2: failure 2 |
54 oh no | 54 oh no |
55 test.dart 8:42 main.<fn> | 55 test.dart 7:33 main.<fn> |
56 dart:isolate _RawReceivePortImpl._handleMessage | 56 dart:isolate _RawReceivePortImpl._handleMessage |
57 | 57 |
58 | 58 |
59 +0 -2: failure 3 | 59 +0 -2: failure 3 |
60 +0 -3: failure 3 | 60 +0 -3: failure 3 |
61 oh no | 61 oh no |
62 test.dart 9:42 main.<fn> | 62 test.dart 8:33 main.<fn> |
63 dart:isolate _RawReceivePortImpl._handleMessage | 63 dart:isolate _RawReceivePortImpl._handleMessage |
64 | 64 |
65 | 65 |
66 +0 -3: Some tests failed."""); | 66 +0 -3: Some tests failed."""); |
67 }); | 67 }); |
68 | 68 |
69 test("runs failing tests along with successful tests", () { | 69 test("runs failing tests along with successful tests", () { |
70 _expectReport(""" | 70 _expectReport(""" |
71 declarer.test('failure 1', () => throw new TestFailure('oh no')); | 71 test('failure 1', () => throw new TestFailure('oh no')); |
72 declarer.test('success 1', () {}); | 72 test('success 1', () {}); |
73 declarer.test('failure 2', () => throw new TestFailure('oh no')); | 73 test('failure 2', () => throw new TestFailure('oh no')); |
74 declarer.test('success 2', () {});""", | 74 test('success 2', () {});""", |
75 """ | 75 """ |
76 +0: failure 1 | 76 +0: failure 1 |
77 +0 -1: failure 1 | 77 +0 -1: failure 1 |
78 oh no | 78 oh no |
79 test.dart 7:42 main.<fn> | 79 test.dart 6:33 main.<fn> |
80 dart:isolate _RawReceivePortImpl._handleMessage | 80 dart:isolate _RawReceivePortImpl._handleMessage |
81 | 81 |
82 | 82 |
83 +0 -1: success 1 | 83 +0 -1: success 1 |
84 +1 -1: success 1 | 84 +1 -1: success 1 |
85 +1 -1: failure 2 | 85 +1 -1: failure 2 |
86 +1 -2: failure 2 | 86 +1 -2: failure 2 |
87 oh no | 87 oh no |
88 test.dart 9:42 main.<fn> | 88 test.dart 8:33 main.<fn> |
89 dart:isolate _RawReceivePortImpl._handleMessage | 89 dart:isolate _RawReceivePortImpl._handleMessage |
90 | 90 |
91 | 91 |
92 +1 -2: success 2 | 92 +1 -2: success 2 |
93 +2 -2: success 2 | 93 +2 -2: success 2 |
94 +2 -2: Some tests failed."""); | 94 +2 -2: Some tests failed."""); |
95 }); | 95 }); |
96 | 96 |
97 test("gracefully handles multiple test failures in a row", () { | 97 test("gracefully handles multiple test failures in a row", () { |
98 _expectReport(""" | 98 _expectReport(""" |
99 // This completer ensures that the test isolate isn't killed until all | 99 // This completer ensures that the test isolate isn't killed until all |
100 // errors have been thrown. | 100 // errors have been thrown. |
101 var completer = new Completer(); | 101 var completer = new Completer(); |
102 declarer.test('failures', () { | 102 test('failures', () { |
103 new Future.microtask(() => throw 'first error'); | 103 new Future.microtask(() => throw 'first error'); |
104 new Future.microtask(() => throw 'second error'); | 104 new Future.microtask(() => throw 'second error'); |
105 new Future.microtask(() => throw 'third error'); | 105 new Future.microtask(() => throw 'third error'); |
106 new Future.microtask(completer.complete); | 106 new Future.microtask(completer.complete); |
107 }); | 107 }); |
108 declarer.test('wait', () => completer.future);""", | 108 test('wait', () => completer.future);""", |
109 """ | 109 """ |
110 +0: failures | 110 +0: failures |
111 +0 -1: failures | 111 +0 -1: failures |
112 first error | 112 first error |
| 113 test.dart 10:38 main.<fn>.<fn> |
| 114 dart:isolate _RawReceivePortImpl._handleMessage |
| 115 ===== asynchronous gap =========================== |
| 116 dart:async Future.Future.microtask |
| 117 test.dart 10:15 main.<fn> |
| 118 dart:isolate _RawReceivePortImpl._handleMessage |
| 119 |
| 120 |
| 121 second error |
113 test.dart 11:38 main.<fn>.<fn> | 122 test.dart 11:38 main.<fn>.<fn> |
114 dart:isolate _RawReceivePortImpl._handleMessage | 123 dart:isolate _RawReceivePortImpl._handleMessage |
115 ===== asynchronous gap =========================== | 124 ===== asynchronous gap =========================== |
116 dart:async Future.Future.microtask | 125 dart:async Future.Future.microtask |
117 test.dart 11:15 main.<fn> | 126 test.dart 11:15 main.<fn> |
118 dart:isolate _RawReceivePortImpl._handleMessage | 127 dart:isolate _RawReceivePortImpl._handleMessage |
119 | 128 |
120 | 129 |
121 second error | 130 third error |
122 test.dart 12:38 main.<fn>.<fn> | 131 test.dart 12:38 main.<fn>.<fn> |
123 dart:isolate _RawReceivePortImpl._handleMessage | 132 dart:isolate _RawReceivePortImpl._handleMessage |
124 ===== asynchronous gap =========================== | 133 ===== asynchronous gap =========================== |
125 dart:async Future.Future.microtask | 134 dart:async Future.Future.microtask |
126 test.dart 12:15 main.<fn> | 135 test.dart 12:15 main.<fn> |
127 dart:isolate _RawReceivePortImpl._handleMessage | 136 dart:isolate _RawReceivePortImpl._handleMessage |
128 | 137 |
129 | 138 |
130 third error | |
131 test.dart 13:38 main.<fn>.<fn> | |
132 dart:isolate _RawReceivePortImpl._handleMessage | |
133 ===== asynchronous gap =========================== | |
134 dart:async Future.Future.microtask | |
135 test.dart 13:15 main.<fn> | |
136 dart:isolate _RawReceivePortImpl._handleMessage | |
137 | |
138 | |
139 +0 -1: wait | 139 +0 -1: wait |
140 +1 -1: wait | 140 +1 -1: wait |
141 +1 -1: Some tests failed."""); | 141 +1 -1: Some tests failed."""); |
142 }); | 142 }); |
143 } | 143 } |
144 | 144 |
145 final _prefixLength = "XX:XX ".length; | 145 final _prefixLength = "XX:XX ".length; |
146 | 146 |
147 void _expectReport(String tests, String expected) { | 147 void _expectReport(String tests, String expected) { |
148 var dart = """ | 148 var dart = """ |
149 import 'dart:async'; | 149 import 'dart:async'; |
150 | 150 |
151 import 'package:unittest/unittest.dart'; | 151 import 'package:unittest/unittest.dart'; |
152 | 152 |
153 void main() { | 153 void main() { |
154 var declarer = Zone.current[#unittest.declarer]; | |
155 $tests | 154 $tests |
156 } | 155 } |
157 """; | 156 """; |
158 | 157 |
159 expect(withTempDir((path) { | 158 expect(withTempDir((path) { |
160 new File(p.join(path, "test.dart")).writeAsStringSync(dart); | 159 new File(p.join(path, "test.dart")).writeAsStringSync(dart); |
161 var result = runUnittest(["test.dart"], workingDirectory: path); | 160 var result = runUnittest(["test.dart"], workingDirectory: path); |
162 | 161 |
163 // Convert CRs into newlines, remove excess trailing whitespace, and trim | 162 // Convert CRs into newlines, remove excess trailing whitespace, and trim |
164 // off timestamps. | 163 // off timestamps. |
165 var actual = result.stdout.trim().split(new RegExp(r"[\r\n]")).map((line) { | 164 var actual = result.stdout.trim().split(new RegExp(r"[\r\n]")).map((line) { |
166 if (line.startsWith(" ") || line.isEmpty) return line.trimRight(); | 165 if (line.startsWith(" ") || line.isEmpty) return line.trimRight(); |
167 return line.trim().substring(_prefixLength); | 166 return line.trim().substring(_prefixLength); |
168 }).join("\n"); | 167 }).join("\n"); |
169 | 168 |
170 // Un-indent the expected string. | 169 // Un-indent the expected string. |
171 var indentation = expected.indexOf(new RegExp("[^ ]")); | 170 var indentation = expected.indexOf(new RegExp("[^ ]")); |
172 expected = expected.split("\n").map((line) { | 171 expected = expected.split("\n").map((line) { |
173 if (line.isEmpty) return line; | 172 if (line.isEmpty) return line; |
174 return line.substring(indentation); | 173 return line.substring(indentation); |
175 }).join("\n"); | 174 }).join("\n"); |
176 | 175 |
177 expect(actual, equals(expected)); | 176 expect(actual, equals(expected)); |
178 }), completes); | 177 }), completes); |
179 } | 178 } |
OLD | NEW |