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:io'; | |
6 | |
7 import 'package:path/path.dart' as p; | |
8 import 'package:unittest/src/backend/state.dart'; | |
9 import 'package:unittest/src/runner/loader.dart'; | |
10 import 'package:unittest/unittest.dart'; | |
11 | |
12 import 'io.dart'; | |
13 import 'utils.dart'; | |
14 | |
15 Loader _loader; | |
16 String _sandbox; | |
17 | |
18 final _tests = """ | |
19 import 'dart:async'; | |
20 | |
21 import 'package:unittest/unittest.dart'; | |
22 | |
23 void main() { | |
24 test("success", () {}); | |
25 test("failure", () => throw new TestFailure('oh no')); | |
26 test("error", () => throw 'oh no'); | |
27 } | |
28 """; | |
29 | |
30 void main() { | |
31 setUp(() { | |
32 _loader = new Loader(packageRoot: p.join(packageDir, 'packages')); | |
33 _sandbox = Directory.systemTemp.createTempSync('unittest_').path; | |
34 }); | |
35 | |
36 tearDown(() { | |
37 new Directory(_sandbox).deleteSync(recursive: true); | |
38 return _loader.close(); | |
39 }); | |
40 | |
41 group(".loadFile()", () { | |
42 var suite; | |
43 setUp(() { | |
44 /// TODO(nweiz): Use scheduled_test for this once it's compatible with | |
45 /// this version of unittest. | |
46 new File(p.join(_sandbox, 'a_test.dart')).writeAsStringSync(_tests); | |
47 return _loader.loadFile(p.join(_sandbox, 'a_test.dart')) | |
48 .then((suite_) => suite = suite_); | |
49 }); | |
50 | |
51 test("returns a suite with a name matching the file path", () { | |
52 expect(suite.name, equals(p.join(_sandbox, 'a_test.dart'))); | |
53 }); | |
54 | |
55 test("returns tests with the correct names", () { | |
56 expect(suite.tests, hasLength(3)); | |
57 expect(suite.tests[0].name, equals("success")); | |
58 expect(suite.tests[1].name, equals("failure")); | |
59 expect(suite.tests[2].name, equals("error")); | |
60 }); | |
61 | |
62 test("can load and run a successful test", () { | |
63 var liveTest = suite.tests[0].load(suite); | |
64 | |
65 expectStates(liveTest, [ | |
66 const State(Status.running, Result.success), | |
67 const State(Status.complete, Result.success) | |
68 ]); | |
69 expectErrors(liveTest, []); | |
70 | |
71 return liveTest.run().whenComplete(() => liveTest.close()); | |
72 }); | |
73 | |
74 test("can load and run a failing test", () { | |
75 var liveTest = suite.tests[1].load(suite); | |
76 expectSingleFailure(liveTest); | |
77 return liveTest.run().whenComplete(() => liveTest.close()); | |
78 }); | |
79 | |
80 test("throws a nice error if the package root doesn't exist", () { | |
81 var loader = new Loader(); | |
82 expect(() => loader.loadFile(p.join(_sandbox, 'a_test.dart')), | |
83 throwsA(isLoadException( | |
84 "Directory ${p.join(_sandbox, 'packages')} does not exist."))); | |
85 }); | |
86 }); | |
87 | |
88 group(".loadDir()", () { | |
89 test("ignores non-Dart files", () { | |
90 new File(p.join(_sandbox, 'a_test.txt')).writeAsStringSync(_tests); | |
91 expect(_loader.loadDir(_sandbox), completion(isEmpty)); | |
92 }); | |
93 | |
94 test("ignores files in packages/ directories", () { | |
95 var dir = p.join(_sandbox, 'packages'); | |
96 new Directory(dir).createSync(); | |
97 new File(p.join(dir, 'a_test.dart')).writeAsStringSync(_tests); | |
98 expect(_loader.loadDir(_sandbox), completion(isEmpty)); | |
99 }); | |
100 | |
101 test("ignores files that don't end in _test.dart", () { | |
102 new File(p.join(_sandbox, 'test.dart')).writeAsStringSync(_tests); | |
103 expect(_loader.loadDir(_sandbox), completion(isEmpty)); | |
104 }); | |
105 | |
106 group("with suites loaded from a directory", () { | |
107 var suites; | |
108 setUp(() { | |
109 /// TODO(nweiz): Use scheduled_test for this once it's compatible with | |
110 /// this version of unittest. | |
111 new File(p.join(_sandbox, 'a_test.dart')).writeAsStringSync(_tests); | |
112 new File(p.join(_sandbox, 'another_test.dart')) | |
113 .writeAsStringSync(_tests); | |
114 new Directory(p.join(_sandbox, 'dir')).createSync(); | |
115 new File(p.join(_sandbox, 'dir/sub_test.dart')) | |
116 .writeAsStringSync(_tests); | |
117 | |
118 return _loader.loadDir(_sandbox).then((suites_) => suites = suites_); | |
119 }); | |
120 | |
121 test("names those suites after their files", () { | |
122 expect(suites.map((suite) => suite.name), unorderedEquals([ | |
123 p.join(_sandbox, 'a_test.dart'), | |
124 p.join(_sandbox, 'another_test.dart'), | |
125 p.join(_sandbox, 'dir/sub_test.dart') | |
126 ])); | |
127 }); | |
128 | |
129 test("can run tests in those suites", () { | |
130 var suite = suites.firstWhere((suite) => suite.name.contains("a_test")); | |
131 var liveTest = suite.tests[1].load(suite); | |
132 expectSingleFailure(liveTest); | |
133 return liveTest.run().whenComplete(() => liveTest.close()); | |
134 }); | |
135 }); | |
136 }); | |
137 } | |
OLD | NEW |