Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(687)

Side by Side Diff: test/runner/test_on_test.dart

Issue 1036943002: Add a testOn parameter to test() and group(). (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/runner/isolate_listener_test.dart ('k') | test/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 @TestOn("vm") 5 @TestOn("vm")
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 8
9 import 'package:path/path.dart' as p; 9 import 'package:path/path.dart' as p;
10 import 'package:unittest/src/util/io.dart'; 10 import 'package:unittest/src/util/io.dart';
11 import 'package:unittest/unittest.dart'; 11 import 'package:unittest/unittest.dart';
12 12
13 import '../io.dart'; 13 import '../io.dart';
14 14
15 String _sandbox; 15 String _sandbox;
16 16
17 final _vm = """ 17 final _otherOS = Platform.isWindows ? "mac-os" : "windows";
18 @TestOn("vm")
19
20 import 'package:unittest/unittest.dart';
21
22 void main() {
23 test("success", () {});
24 }
25 """;
26
27 final _chrome = """
28 @TestOn("chrome")
29
30 // Make sure that loading this test file on the VM will break.
31 import 'dart:html';
32
33 import 'package:unittest/unittest.dart';
34
35 void main() {
36 test("success", () {});
37 }
38 """;
39
40 final _thisOS = """
41 @TestOn("$currentOS")
42
43 import 'package:unittest/unittest.dart';
44
45 void main() {
46 test("success", () {});
47 }
48 """;
49
50 final _otherOS = """
51 @TestOn("${Platform.isWindows ? "mac-os" : "windows"}")
52
53 // Make sure that loading this test file on the VM will break.
54 import 'dart:html';
55
56 import 'package:unittest/unittest.dart';
57
58 void main() {
59 test("success", () {});
60 }
61 """;
62 18
63 void main() { 19 void main() {
64 setUp(() { 20 setUp(() {
65 _sandbox = Directory.systemTemp.createTempSync('unittest_').path; 21 _sandbox = Directory.systemTemp.createTempSync('unittest_').path;
66 }); 22 });
67 23
68 tearDown(() { 24 tearDown(() {
69 new Directory(_sandbox).deleteSync(recursive: true); 25 new Directory(_sandbox).deleteSync(recursive: true);
70 }); 26 });
71 27
72 test("runs a test suite on a matching platform", () { 28 group("for suite", () {
73 new File(p.join(_sandbox, "vm_test.dart")).writeAsStringSync(_vm); 29 test("runs a test suite on a matching platform", () {
74 30 _writeTestFile("vm_test.dart", suiteTestOn: "vm");
75 var result = _runUnittest(["vm_test.dart"]); 31
76 expect(result.stdout, contains("All tests passed!")); 32 var result = _runUnittest(["vm_test.dart"]);
77 expect(result.exitCode, equals(0)); 33 expect(result.stdout, contains("All tests passed!"));
78 }); 34 expect(result.exitCode, equals(0));
79 35 });
80 test("doesn't run a test suite on a non-matching platform", () { 36
81 new File(p.join(_sandbox, "vm_test.dart")).writeAsStringSync(_vm); 37 test("doesn't run a test suite on a non-matching platform", () {
82 38 _writeTestFile("vm_test.dart", suiteTestOn: "vm");
83 var result = _runUnittest(["--platform", "chrome", "vm_test.dart"]); 39
84 expect(result.stdout, contains("No tests ran.")); 40 var result = _runUnittest(["--platform", "chrome", "vm_test.dart"]);
85 expect(result.exitCode, equals(0)); 41 expect(result.stdout, contains("No tests ran."));
86 }); 42 expect(result.exitCode, equals(0));
87 43 });
88 test("runs a test suite on a matching operating system", () { 44
89 new File(p.join(_sandbox, "os_test.dart")).writeAsStringSync(_thisOS); 45 test("runs a test suite on a matching operating system", () {
90 46 _writeTestFile("os_test.dart", suiteTestOn: currentOS.name);
91 var result = _runUnittest(["os_test.dart"]); 47
92 expect(result.stdout, contains("All tests passed!")); 48 var result = _runUnittest(["os_test.dart"]);
93 expect(result.exitCode, equals(0)); 49 expect(result.stdout, contains("All tests passed!"));
94 }); 50 expect(result.exitCode, equals(0));
95 51 });
96 test("doesn't run a test suite on a non-matching operating system", () { 52
97 new File(p.join(_sandbox, "os_test.dart")).writeAsStringSync(_otherOS); 53 test("doesn't run a test suite on a non-matching operating system", () {
98 54 _writeTestFile("os_test.dart", suiteTestOn: _otherOS,
99 var result = _runUnittest(["os_test.dart"]); 55 loadable: false);
100 expect(result.stdout, contains("No tests ran.")); 56
101 expect(result.exitCode, equals(0)); 57 var result = _runUnittest(["os_test.dart"]);
102 }); 58 expect(result.stdout, contains("No tests ran."));
103 59 expect(result.exitCode, equals(0));
104 test("only loads matching files when loading as a group", () { 60 });
105 new File(p.join(_sandbox, "vm_test.dart")).writeAsStringSync(_vm); 61
106 new File(p.join(_sandbox, "chrome_test.dart")).writeAsStringSync(_chrome); 62 test("only loads matching files when loading as a group", () {
107 new File(p.join(_sandbox, "this_os_test.dart")).writeAsStringSync(_thisOS); 63 _writeTestFile("vm_test.dart", suiteTestOn: "vm");
108 new File(p.join(_sandbox, "other_os_test.dart")) 64 _writeTestFile("chrome_test.dart",
109 .writeAsStringSync(_otherOS); 65 suiteTestOn: "chrome", loadable: false);
110 66 _writeTestFile("this_os_test.dart", suiteTestOn: currentOS.name);
111 var result = _runUnittest(["."]); 67 _writeTestFile("other_os_test.dart",
112 expect(result.stdout, contains("+2: All tests passed!")); 68 suiteTestOn: _otherOS, loadable: false);
113 expect(result.exitCode, equals(0)); 69
114 }); 70 var result = _runUnittest(["."]);
71 expect(result.stdout, contains("+2: All tests passed!"));
72 expect(result.exitCode, equals(0));
73 });
74 });
75
76 group("for group", () {
77 test("runs a VM group on the VM", () {
78 _writeTestFile("vm_test.dart", groupTestOn: "vm");
79
80 var result = _runUnittest(["vm_test.dart"]);
81 expect(result.stdout, contains("All tests passed!"));
82 expect(result.exitCode, equals(0));
83 });
84
85 test("doesn't run a Chrome group on the VM", () {
86 _writeTestFile("chrome_test.dart", groupTestOn: "chrome");
87
88 var result = _runUnittest(["chrome_test.dart"]);
89 expect(result.stdout, contains("No tests ran."));
90 expect(result.exitCode, equals(0));
91 });
92
93 test("runs a Chrome group on Chrome", () {
94 _writeTestFile("chrome_test.dart", groupTestOn: "chrome");
95
96 var result = _runUnittest(["--platform", "chrome", "chrome_test.dart"]);
97 expect(result.stdout, contains("All tests passed!"));
98 expect(result.exitCode, equals(0));
99 });
100
101 test("doesn't run a VM group on Chrome", () {
102 _writeTestFile("vm_test.dart", groupTestOn: "vm");
103
104 var result = _runUnittest(["--platform", "chrome", "vm_test.dart"]);
105 expect(result.stdout, contains("No tests ran."));
106 expect(result.exitCode, equals(0));
107 });
108 });
109
110 group("for test", () {
111 test("runs a VM test on the VM", () {
112 _writeTestFile("vm_test.dart", testTestOn: "vm");
113
114 var result = _runUnittest(["vm_test.dart"]);
115 expect(result.stdout, contains("All tests passed!"));
116 expect(result.exitCode, equals(0));
117 });
118
119 test("doesn't run a Chrome test on the VM", () {
120 _writeTestFile("chrome_test.dart", testTestOn: "chrome");
121
122 var result = _runUnittest(["chrome_test.dart"]);
123 expect(result.stdout, contains("No tests ran."));
124 expect(result.exitCode, equals(0));
125 });
126
127 test("runs a Chrome test on Chrome", () {
128 _writeTestFile("chrome_test.dart", testTestOn: "chrome");
129
130 var result = _runUnittest(["--platform", "chrome", "chrome_test.dart"]);
131 expect(result.stdout, contains("All tests passed!"));
132 expect(result.exitCode, equals(0));
133 });
134
135 test("doesn't run a VM test on Chrome", () {
136 _writeTestFile("vm_test.dart", testTestOn: "vm");
137
138 var result = _runUnittest(["--platform", "chrome", "vm_test.dart"]);
139 expect(result.stdout, contains("No tests ran."));
140 expect(result.exitCode, equals(0));
141 });
142 });
143
144 group("with suite, group, and test selectors", () {
145 test("runs the test if all selectors match", () {
146 _writeTestFile("vm_test.dart", suiteTestOn: "!browser",
147 groupTestOn: "!js", testTestOn: "vm");
148
149 var result = _runUnittest(["vm_test.dart"]);
150 expect(result.stdout, contains("All tests passed!"));
151 expect(result.exitCode, equals(0));
152 });
153
154 test("doesn't runs the test if the suite doesn't match", () {
155 _writeTestFile("vm_test.dart", suiteTestOn: "chrome",
156 groupTestOn: "!js", testTestOn: "vm");
157
158 var result = _runUnittest(["vm_test.dart"]);
159 expect(result.stdout, contains("No tests ran."));
160 expect(result.exitCode, equals(0));
161 });
162
163 test("doesn't runs the test if the group doesn't match", () {
164 _writeTestFile("vm_test.dart", suiteTestOn: "!browser",
165 groupTestOn: "chrome", testTestOn: "vm");
166
167 var result = _runUnittest(["vm_test.dart"]);
168 expect(result.stdout, contains("No tests ran."));
169 expect(result.exitCode, equals(0));
170 });
171
172 test("doesn't runs the test if the test doesn't match", () {
173 _writeTestFile("vm_test.dart", suiteTestOn: "!browser",
174 groupTestOn: "!js", testTestOn: "chrome");
175
176 var result = _runUnittest(["vm_test.dart"]);
177 expect(result.stdout, contains("No tests ran."));
178 expect(result.exitCode, equals(0));
179 });
180 });
181 }
182
183 /// Writes a test file with some platform selectors to [filename].
184 ///
185 /// Each of [suiteTestOn], [groupTestOn], and [testTestOn] is a platform
186 /// selector that's suite-, group-, and test-level respectively. If [loadable]
187 /// is `false`, the test file will be made unloadable on the Dart VM.
188 void _writeTestFile(String filename, {String suiteTestOn, String groupTestOn,
189 String testTestOn, bool loadable: true}) {
190 var buffer = new StringBuffer();
191 if (suiteTestOn != null) buffer.writeln("@TestOn('$suiteTestOn')");
192 if (!loadable) buffer.writeln("import 'dart:html';");
193
194 buffer
195 ..writeln("import 'package:unittest/unittest.dart';")
196 ..writeln("void main() {")
197 ..writeln(" group('group', () {");
198
199 if (testTestOn != null) {
200 buffer.writeln(" test('test', () {}, testOn: '$testTestOn');");
201 } else {
202 buffer.writeln(" test('test', () {});");
203 }
204
205 if (groupTestOn != null) {
206 buffer.writeln(" }, testOn: '$groupTestOn');");
207 } else {
208 buffer.writeln(" });");
209 }
210
211 buffer.writeln("}");
212
213 new File(p.join(_sandbox, filename)).writeAsStringSync(buffer.toString());
115 } 214 }
116 215
117 ProcessResult _runUnittest(List<String> args) => 216 ProcessResult _runUnittest(List<String> args) =>
118 runUnittest(args, workingDirectory: _sandbox); 217 runUnittest(args, workingDirectory: _sandbox);
OLDNEW
« no previous file with comments | « test/runner/isolate_listener_test.dart ('k') | test/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698