OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, 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 library analyzer_cli.test.built_mode; | |
6 | |
7 import 'dart:convert'; | |
8 | |
9 import 'package:analyzer_cli/src/build_mode.dart'; | |
10 import 'package:analyzer_cli/src/driver.dart'; | |
11 import 'package:analyzer_cli/src/options.dart'; | |
12 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
13 import 'package:typed_mock/typed_mock.dart'; | |
14 import 'package:unittest/unittest.dart'; | |
15 | |
16 main() { | |
17 defineReflectiveTests(WorkerLoopTest); | |
18 defineReflectiveTests(WorkInputTest); | |
19 defineReflectiveTests(WorkRequestTest); | |
20 } | |
21 | |
22 typedef _TestWorkerLoopAnalyze(CommandLineOptions options); | |
Paul Berry
2016/03/30 19:10:43
Nit: return type should be void.
| |
23 | |
24 @reflectiveTest | |
25 class WorkerLoopTest { | |
26 final _TestWorkerConnection connection = new _TestWorkerConnection(); | |
27 | |
28 void setUp() {} | |
29 | |
30 test_run() { | |
31 _setInputLine(JSON.encode({ | |
32 'arguments': [ | |
33 '--build-summary-input=/tmp/1.sum', | |
34 '--build-summary-input=/tmp/2.sum', | |
35 'package:foo/foo.dart|/inputs/foo/lib/foo.dart', | |
36 'package:foo/bar.dart|/inputs/foo/lib/bar.dart' | |
37 ], | |
38 })); | |
39 new _TestWorkerLoop(connection, (CommandLineOptions options) { | |
40 expect(options.buildSummaryInputs, | |
41 unorderedEquals(['/tmp/1.sum', '/tmp/2.sum'])); | |
42 expect( | |
43 options.sourceFiles, | |
44 unorderedEquals([ | |
45 'package:foo/foo.dart|/inputs/foo/lib/foo.dart', | |
46 'package:foo/bar.dart|/inputs/foo/lib/bar.dart' | |
47 ])); | |
48 outSink.writeln('outSink a'); | |
49 errorSink.writeln('errorSink a'); | |
50 outSink.writeln('outSink b'); | |
51 errorSink.writeln('errorSink b'); | |
52 }).run(); | |
53 expect(connection.jsonList, hasLength(1)); | |
54 expect(connection.jsonList[0], { | |
55 'exit_code': WorkerLoop.EXIT_CODE_OK, | |
56 'output': allOf(contains('errorSink a'), contains('errorSink a'), | |
57 contains('outSink a'), contains('outSink b')) | |
58 }); | |
59 } | |
60 | |
61 test_run_invalidOptions() { | |
62 _setInputLine(JSON.encode({ | |
63 'arguments': ['--unknown-option', '/foo.dart', '/bar.dart',], | |
64 })); | |
65 new _TestWorkerLoop(connection).run(); | |
66 expect(connection.jsonList, hasLength(1)); | |
67 expect(connection.jsonList[0], | |
68 {'exit_code': WorkerLoop.EXIT_CODE_ERROR, 'output': anything}); | |
69 } | |
70 | |
71 test_run_invalidRequest_noArgumentsInputs() { | |
72 _setInputLine('{}'); | |
73 new _TestWorkerLoop(connection).run(); | |
74 expect(connection.jsonList, hasLength(1)); | |
75 expect(connection.jsonList[0], | |
76 {'exit_code': WorkerLoop.EXIT_CODE_ERROR, 'output': anything}); | |
77 } | |
78 | |
79 test_run_invalidRequest_notJson() { | |
80 _setInputLine('not a JSON string'); | |
81 new _TestWorkerLoop(connection).run(); | |
82 expect(connection.jsonList, hasLength(1)); | |
83 expect(connection.jsonList[0], | |
84 {'exit_code': WorkerLoop.EXIT_CODE_ERROR, 'output': anything}); | |
85 } | |
86 | |
87 test_run_stopAtEOF() { | |
88 when(connection.readLineSync()).thenReturnList([null]); | |
89 new _TestWorkerLoop(connection).run(); | |
90 } | |
91 | |
92 void _setInputLine(String line) { | |
93 when(connection.readLineSync()).thenReturnList([line, null]); | |
94 } | |
95 } | |
96 | |
97 @reflectiveTest | |
98 class WorkInputTest { | |
99 test_fromJson() { | |
100 WorkInput input = new WorkInput.fromJson({ | |
101 'path': '/my/path', | |
102 'digest': [1, 2, 3, 4, 5] | |
103 }); | |
104 expect(input.path, '/my/path'); | |
105 expect(input.digest, <int>[1, 2, 3, 4, 5]); | |
106 } | |
107 | |
108 test_fromJson_digest_isMissing() { | |
109 WorkInput input = new WorkInput.fromJson({'path': '/my/path',}); | |
110 expect(input.path, '/my/path'); | |
111 expect(input.digest, <int>[]); | |
112 } | |
113 | |
114 test_fromJson_digest_isNotList() { | |
115 expect(() { | |
116 new WorkInput.fromJson({'path': '/my/path', 'digest': 0}); | |
117 }, throwsArgumentError); | |
118 } | |
119 | |
120 test_fromJson_digest_isNotListOfInt() { | |
121 expect(() { | |
122 new WorkInput.fromJson({ | |
123 'path': '/my/path', | |
124 'digest': ['a', 'b', 'c'] | |
125 }); | |
126 }, throwsArgumentError); | |
127 } | |
128 | |
129 test_fromJson_path_isMissing() { | |
130 expect(() { | |
131 new WorkInput.fromJson({ | |
132 'digest': [1, 2, 3, 4, 5] | |
133 }); | |
134 }, throwsArgumentError); | |
135 } | |
136 | |
137 test_fromJson_path_isNotString() { | |
138 expect(() { | |
139 new WorkInput.fromJson({ | |
140 'path': 0, | |
141 'digest': [1, 2, 3, 4, 5] | |
142 }); | |
143 }, throwsArgumentError); | |
144 } | |
145 | |
146 test_toJson() { | |
147 WorkInput input = new WorkInput('/my/path', <int>[1, 2, 3, 4, 5]); | |
148 Map<String, Object> json = input.toJson(); | |
149 expect(json, { | |
150 'path': '/my/path', | |
151 'digest': [1, 2, 3, 4, 5] | |
152 }); | |
153 } | |
154 | |
155 test_toJson_withoutDigest() { | |
156 WorkInput input = new WorkInput('/my/path', null); | |
157 Map<String, Object> json = input.toJson(); | |
158 expect(json, {'path': '/my/path'}); | |
159 } | |
160 } | |
161 | |
162 @reflectiveTest | |
163 class WorkRequestTest { | |
164 test_fromJson() { | |
165 WorkRequest request = new WorkRequest.fromJson({ | |
166 'arguments': ['--arg1', '--arg2', '--arg3'], | |
167 'inputs': [ | |
168 { | |
169 'path': '/my/path1', | |
170 'digest': [11, 12, 13] | |
171 }, | |
172 { | |
173 'path': '/my/path2', | |
174 'digest': [21, 22, 23] | |
175 } | |
176 ] | |
177 }); | |
178 expect(request.arguments, ['--arg1', '--arg2', '--arg3']); | |
179 expect(request.inputs, hasLength(2)); | |
180 expect(request.inputs[0].path, '/my/path1'); | |
181 expect(request.inputs[0].digest, <int>[11, 12, 13]); | |
182 expect(request.inputs[1].path, '/my/path2'); | |
183 expect(request.inputs[1].digest, <int>[21, 22, 23]); | |
184 } | |
185 | |
186 test_fromJson_arguments_isMissing() { | |
187 WorkRequest request = new WorkRequest.fromJson({ | |
188 'inputs': [ | |
189 { | |
190 'path': '/my/path1', | |
191 'digest': [11, 12, 13] | |
192 }, | |
193 ] | |
194 }); | |
195 expect(request.arguments, isEmpty); | |
196 expect(request.inputs, hasLength(1)); | |
197 expect(request.inputs[0].path, '/my/path1'); | |
198 expect(request.inputs[0].digest, <int>[11, 12, 13]); | |
199 } | |
200 | |
201 test_fromJson_arguments_isNotList() { | |
202 expect(() { | |
203 new WorkRequest.fromJson({'arguments': 0, 'inputs': []}); | |
204 }, throwsArgumentError); | |
205 } | |
206 | |
207 test_fromJson_arguments_isNotListOfString() { | |
208 expect(() { | |
209 new WorkRequest.fromJson({ | |
210 'arguments': [0, 1, 2], | |
211 'inputs': [] | |
212 }); | |
213 }, throwsArgumentError); | |
214 } | |
215 | |
216 test_fromJson_inputs_isMissing() { | |
217 WorkRequest request = new WorkRequest.fromJson({ | |
218 'arguments': ['--arg1', '--arg2', '--arg3'], | |
219 }); | |
220 expect(request.arguments, ['--arg1', '--arg2', '--arg3']); | |
221 expect(request.inputs, hasLength(0)); | |
222 } | |
223 | |
224 test_fromJson_inputs_isNotList() { | |
225 expect(() { | |
226 new WorkRequest.fromJson({ | |
227 'arguments': ['--arg1', '--arg2', '--arg3'], | |
228 'inputs': 0 | |
229 }); | |
230 }, throwsArgumentError); | |
231 } | |
232 | |
233 test_fromJson_inputs_isNotListOfObject() { | |
234 expect(() { | |
235 new WorkRequest.fromJson({ | |
236 'arguments': ['--arg1', '--arg2', '--arg3'], | |
237 'inputs': [0, 1, 2] | |
238 }); | |
239 }, throwsArgumentError); | |
240 } | |
241 | |
242 test_fromJson_noArgumentsInputs() { | |
243 expect(() { | |
244 new WorkRequest.fromJson({}); | |
245 }, throwsArgumentError); | |
246 } | |
247 | |
248 test_toJson() { | |
249 WorkRequest request = new WorkRequest(<String>[ | |
250 '--arg1', | |
251 '--arg2', | |
252 '--arg3' | |
253 ], <WorkInput>[ | |
254 new WorkInput('/my/path1', <int>[11, 12, 13]), | |
255 new WorkInput('/my/path2', <int>[21, 22, 23]) | |
256 ]); | |
257 Map<String, Object> json = request.toJson(); | |
258 expect(json, { | |
259 'arguments': ['--arg1', '--arg2', '--arg3'], | |
260 'inputs': [ | |
261 { | |
262 'path': '/my/path1', | |
263 'digest': [11, 12, 13] | |
264 }, | |
265 { | |
266 'path': '/my/path2', | |
267 'digest': [21, 22, 23] | |
268 } | |
269 ] | |
270 }); | |
271 } | |
272 } | |
273 | |
274 /** | |
275 * [WorkerConnection] mock. | |
276 */ | |
277 class _TestWorkerConnection extends TypedMock implements WorkerConnection { | |
278 final jsonList = <Map<String, Object>>[]; | |
Paul Berry
2016/03/30 19:10:42
It's not immediately obvious at the usage sites wh
| |
279 | |
280 @override | |
281 void writeJson(Map<String, Object> json) { | |
282 jsonList.add(json); | |
283 } | |
284 } | |
285 | |
286 /** | |
287 * [WorkerLoop] for testing. | |
288 */ | |
289 class _TestWorkerLoop extends WorkerLoop { | |
290 final _TestWorkerLoopAnalyze _analyze; | |
291 | |
292 _TestWorkerLoop(WorkerConnection connection, [this._analyze]) | |
293 : super(connection); | |
294 | |
295 @override | |
296 void analyze(CommandLineOptions options) { | |
297 if (_analyze != null) { | |
298 _analyze(options); | |
299 } | |
300 } | |
301 } | |
OLD | NEW |