OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
kevmoo
2015/02/19 01:54:09
unused import
nweiz
2015/02/19 02:10:18
Done.
| |
6 import 'dart:io'; | |
7 | |
8 import 'package:path/path.dart' as p; | |
9 import 'package:unittest/src/exit_codes.dart' as exit_codes; | |
10 import 'package:unittest/unittest.dart'; | |
11 | |
12 import 'io.dart'; | |
13 | |
14 String _sandbox; | |
15 | |
16 String _success = """ | |
kevmoo
2015/02/19 01:54:09
const
nweiz
2015/02/19 02:10:18
That won't do anything. There's only one instance
kevmoo
2015/02/19 02:15:44
It's not about perf gains. It's about giving folks
nweiz
2015/02/19 02:18:50
Done.
| |
17 import 'dart:async'; | |
18 | |
19 void main() { | |
20 var declarer = Zone.current[#unittest.declarer]; | |
21 declarer.test("success", () {}); | |
22 } | |
23 """; | |
24 | |
25 String _failure = """ | |
kevmoo
2015/02/19 01:54:09
const
nweiz
2015/02/19 02:10:18
ditto
kevmoo
2015/02/19 02:15:44
ditto
| |
26 import 'dart:async'; | |
27 | |
28 import 'package:unittest/unittest.dart'; | |
29 | |
30 void main() { | |
31 var declarer = Zone.current[#unittest.declarer]; | |
32 declarer.test("failure", () => throw new TestFailure("oh no")); | |
33 } | |
34 """; | |
35 | |
36 void main() { | |
37 setUp(() { | |
38 _sandbox = Directory.systemTemp.createTempSync('unittest_').path; | |
39 }); | |
40 | |
41 tearDown(() { | |
42 new Directory(_sandbox).deleteSync(recursive: true); | |
43 }); | |
44 | |
45 test("prints help information", () { | |
46 var result = _runUnittest(["--help"]); | |
47 expect(result.stdout, equals(""" | |
48 Runs tests in this package. | |
49 | |
50 Usage: pub run unittest:unittest [files or directories...] | |
51 | |
52 -h, --help Shows this usage information. | |
53 """)); | |
54 expect(result.exitCode, equals(exit_codes.success)); | |
55 }); | |
56 | |
57 group("fails gracefully if", () { | |
58 test("an invalid option is passed", () { | |
59 var result = _runUnittest(["--asdf"]); | |
60 expect(result.stderr, equals(""" | |
61 Could not find an option named "asdf". | |
62 | |
63 Usage: pub run unittest:unittest [files or directories...] | |
64 | |
65 -h, --help Shows this usage information. | |
66 """)); | |
67 expect(result.exitCode, equals(exit_codes.usage)); | |
68 }); | |
69 | |
70 test("a non-existent file is passed", () { | |
71 var result = _runUnittest(["file"]); | |
72 expect(result.stderr, equals('Failed to load "file": Does not exist.\n')); | |
73 expect(result.exitCode, equals(exit_codes.data)); | |
74 }); | |
75 | |
76 test("the default directory doesn't exist", () { | |
77 var result = _runUnittest([]); | |
78 expect(result.stderr, equals( | |
79 'Failed to load "test": No test files were passed and the default ' | |
80 'directory doesn\'t exist.\n')); | |
81 expect(result.exitCode, equals(exit_codes.data)); | |
82 }); | |
83 | |
84 test("a test file fails to load", () { | |
85 var testPath = p.join(_sandbox, "test.dart"); | |
86 new File(testPath).writeAsStringSync("invalid Dart file"); | |
87 var result = _runUnittest(["test.dart"]); | |
88 | |
89 expect(result.stderr, equals( | |
90 'Failed to load "${p.relative(testPath, from: _sandbox)}":\n' | |
91 "line 1 pos 1: unexpected token 'invalid'\n" | |
92 "invalid Dart file\n" | |
93 "^\n")); | |
94 expect(result.exitCode, equals(exit_codes.data)); | |
95 }); | |
96 | |
97 test("a test file throws", () { | |
98 var testPath = p.join(_sandbox, "test.dart"); | |
99 new File(testPath).writeAsStringSync("void main() => throw 'oh no';"); | |
100 | |
101 var result = _runUnittest(["test.dart"]); | |
102 expect(result.stderr, startsWith( | |
103 'Failed to load "${p.relative(testPath, from: _sandbox)}": oh no\n')); | |
104 expect(result.exitCode, equals(exit_codes.data)); | |
105 }); | |
106 | |
107 test("a test file doesn't have a main defined", () { | |
108 var testPath = p.join(_sandbox, "test.dart"); | |
109 new File(testPath).writeAsStringSync("void foo() {}"); | |
110 | |
111 var result = _runUnittest(["test.dart"]); | |
112 expect(result.stderr, startsWith( | |
113 'Failed to load "${p.relative(testPath, from: _sandbox)}": No ' | |
114 'top-level main() function defined.\n')); | |
115 expect(result.exitCode, equals(exit_codes.data)); | |
116 }); | |
117 | |
118 test("a test file has a non-function main", () { | |
119 var testPath = p.join(_sandbox, "test.dart"); | |
120 new File(testPath).writeAsStringSync("int main;"); | |
121 | |
122 var result = _runUnittest(["test.dart"]); | |
123 expect(result.stderr, startsWith( | |
124 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' | |
125 'main getter is not a function.\n')); | |
126 expect(result.exitCode, equals(exit_codes.data)); | |
127 }); | |
128 | |
129 test("a test file has a main with arguments", () { | |
130 var testPath = p.join(_sandbox, "test.dart"); | |
131 new File(testPath).writeAsStringSync("void main(arg) {}"); | |
132 | |
133 var result = _runUnittest(["test.dart"]); | |
134 expect(result.stderr, startsWith( | |
135 'Failed to load "${p.relative(testPath, from: _sandbox)}": Top-level ' | |
136 'main() function takes arguments.\n')); | |
137 expect(result.exitCode, equals(exit_codes.data)); | |
138 }); | |
139 | |
140 // TODO(nweiz): test what happens when a test file is unreadable once issue | |
141 // 15078 is fixed. | |
142 }); | |
143 | |
144 group("runs successful tests", () { | |
145 test("defined in a single file", () { | |
146 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_success); | |
147 var result = _runUnittest(["test.dart"]); | |
148 expect(result.exitCode, equals(0)); | |
149 }); | |
150 | |
151 test("defined in a directory", () { | |
152 for (var i = 0; i < 3; i++) { | |
153 new File(p.join(_sandbox, "${i}_test.dart")) | |
154 .writeAsStringSync(_success); | |
155 } | |
156 | |
157 var result = _runUnittest(["."]); | |
158 expect(result.exitCode, equals(0)); | |
159 }); | |
160 | |
161 test("defaulting to the test directory", () { | |
162 new Directory(p.join(_sandbox, "test")).createSync(); | |
163 for (var i = 0; i < 3; i++) { | |
164 new File(p.join(_sandbox, "test", "${i}_test.dart")) | |
165 .writeAsStringSync(_success); | |
166 } | |
167 | |
168 var result = _runUnittest([]); | |
169 expect(result.exitCode, equals(0)); | |
170 }); | |
171 }); | |
172 | |
173 group("runs failing tests", () { | |
174 test("defined in a single file", () { | |
175 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(_failure); | |
176 var result = _runUnittest(["test.dart"]); | |
177 expect(result.exitCode, equals(1)); | |
178 }); | |
179 | |
180 test("defined in a directory", () { | |
181 for (var i = 0; i < 3; i++) { | |
182 new File(p.join(_sandbox, "${i}_test.dart")) | |
183 .writeAsStringSync(_failure); | |
184 } | |
185 | |
186 var result = _runUnittest(["."]); | |
187 expect(result.exitCode, equals(1)); | |
188 }); | |
189 | |
190 test("defaulting to the test directory", () { | |
191 new Directory(p.join(_sandbox, "test")).createSync(); | |
192 for (var i = 0; i < 3; i++) { | |
193 new File(p.join(_sandbox, "test", "${i}_test.dart")) | |
194 .writeAsStringSync(_failure); | |
195 } | |
196 | |
197 var result = _runUnittest([]); | |
198 expect(result.exitCode, equals(1)); | |
199 }); | |
200 }); | |
201 } | |
202 | |
203 ProcessResult _runUnittest(List<String> args) => | |
204 runUnittest(args, workingDirectory: _sandbox); | |
OLD | NEW |