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

Side by Side Diff: pkg/analysis_server/test/protocol_test.dart

Issue 492563002: Make more use of generated classes in analysis server. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 library test.protocol; 5 library test.protocol;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'package:analysis_server/src/protocol.dart'; 9 import 'package:analysis_server/src/protocol.dart';
10 import 'package:analysis_server/src/services/json.dart'; 10 import 'package:analysis_server/src/services/json.dart';
11 import 'package:analysis_testing/reflective_tests.dart'; 11 import 'package:analysis_testing/reflective_tests.dart';
12 import 'package:unittest/unittest.dart'; 12 import 'package:unittest/unittest.dart';
13 13
14 14
15 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>()); 15 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>());
16 16
17 17
18 main() { 18 main() {
19 groupSep = ' | '; 19 groupSep = ' | ';
20 runReflectiveTests(NotificationTest); 20 runReflectiveTests(NotificationTest);
21 runReflectiveTests(RequestTest); 21 runReflectiveTests(RequestTest);
22 runReflectiveTests(RequestErrorTest); 22 runReflectiveTests(RequestErrorTest);
23 runReflectiveTests(RequestDatumTest);
24 runReflectiveTests(ResponseTest); 23 runReflectiveTests(ResponseTest);
25 } 24 }
26 25
27 26
28 @ReflectiveTestCase() 27 @ReflectiveTestCase()
29 class InvalidParameterResponseMatcher extends Matcher { 28 class InvalidParameterResponseMatcher extends Matcher {
30 static const String ERROR_CODE = 'INVALID_PARAMETER'; 29 static const String ERROR_CODE = 'INVALID_PARAMETER';
31 30
32 @override 31 @override
33 Description describe(Description description) => description.add( 32 Description describe(Description description) => description.add(
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 126
128 127
129 class _MyHasToJsonObject implements HasToJson { 128 class _MyHasToJsonObject implements HasToJson {
130 int offset; 129 int offset;
131 _MyHasToJsonObject(this.offset); 130 _MyHasToJsonObject(this.offset);
132 Map<String, Object> toJson() => {'offset': offset}; 131 Map<String, Object> toJson() => {'offset': offset};
133 } 132 }
134 133
135 134
136 @ReflectiveTestCase() 135 @ReflectiveTestCase()
137 class RequestDatumTest {
138 static Matcher isRequestDatum = new isInstanceOf<RequestDatum>(
139 "RequestDatum");
140
141 static Request request;
142 static Matcher _throwsInvalidParameter = throwsA(
143 new InvalidParameterResponseMatcher());
144
145 void setUp() {
146 request = new Request('myId', 'myMethod');
147 }
148
149 void test_asBool() {
150 expect(makeDatum(true).asBool(), isTrue);
151 expect(makeDatum(false).asBool(), isFalse);
152 expect(makeDatum('true').asBool(), isTrue);
153 expect(makeDatum('false').asBool(), isFalse);
154 expect(() => makeDatum('abc').asBool(), _throwsInvalidParameter);
155 }
156
157 void test_asInt() {
158 expect(makeDatum(1).asInt(), equals(1));
159 expect(makeDatum('2').asInt(), equals(2));
160 expect(() => makeDatum('xxx').asInt(), _throwsInvalidParameter);
161 expect(() => makeDatum(true).asInt(), _throwsInvalidParameter);
162 }
163
164 void test_asList_emptyList() {
165 expect(makeDatum([]).asList((datum) => datum.asString()), equals([]));
166 }
167
168 void test_asList_nonEmptyList() {
169 expect(makeDatum(['foo', 'bar']).asList((datum) => datum.asString()),
170 equals(['foo', 'bar']));
171 }
172
173 void test_asList_nonList() {
174 expect(() => makeDatum(3).asList((datum) => null), _throwsInvalidParameter);
175 }
176
177 void test_asList_null() {
178 expect(makeDatum(null).asList((datum) => datum.asString()), equals([]));
179 }
180
181 void test_asString() {
182 expect(makeDatum('foo').asString(), equals('foo'));
183 expect(() => makeDatum(3).asString(), _throwsInvalidParameter);
184 }
185
186 void test_asStringList() {
187 expect(makeDatum(['foo', 'bar']).asStringList(), equals(['foo', 'bar']));
188 expect(makeDatum([]).asStringList(), equals([]));
189 expect(makeDatum(null).asStringList(), equals([]));
190 expect(() => makeDatum(['foo', 1]).asStringList(), _throwsInvalidParameter);
191 expect(() => makeDatum({}).asStringList(), _throwsInvalidParameter);
192 }
193
194 void test_asStringListMap() {
195 {
196 var map = {
197 'key1': ['value11', 'value12'],
198 'key2': ['value21', 'value22']
199 };
200 expect(makeDatum(map).asStringListMap(), map);
201 }
202 {
203 var map = {
204 'key1': 10,
205 'key2': 20
206 };
207 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter);
208 }
209 {
210 var map = {
211 'key1': [11, 12],
212 'key2': [21, 22]
213 };
214 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter);
215 }
216 }
217
218 void test_asStringMap() {
219 expect(makeDatum({
220 'key1': 'value1',
221 'key2': 'value2'
222 }).asStringMap(), equals({
223 'key1': 'value1',
224 'key2': 'value2'
225 }));
226 expect(makeDatum({}).asStringMap(), equals({}));
227 expect(() => makeDatum({
228 'key1': 'value1',
229 'key2': 2
230 }).asStringMap(), _throwsInvalidParameter);
231 expect(() => makeDatum({
232 'key1': 1,
233 'key2': 2
234 }).asStringMap(), _throwsInvalidParameter);
235 expect(() => makeDatum([]).asStringMap(), _throwsInvalidParameter);
236 }
237
238 void test_forEachMap_emptyMap() {
239 makeDatum({}).forEachMap((key, value) {
240 fail('Empty map should not be iterated');
241 });
242 }
243
244 void test_forEachMap_nonMap() {
245 expect(() => makeDatum(1).forEachMap((key, value) {
246 fail('Non-map should not be iterated');
247 }), _throwsInvalidParameter);
248 }
249
250 void test_forEachMap_null() {
251 makeDatum(null).forEachMap((key, value) {
252 fail('Empty map should not be iterated');
253 });
254 }
255
256 void test_forEachMap_oneElementMap() {
257 int callCount = 0;
258 makeDatum({
259 'key': 'value'
260 }).forEachMap((key, value) {
261 callCount++;
262 expect(key, equals('key'));
263 expect(value, isRequestDatum);
264 expect(value.datum, equals('value'));
265 });
266 expect(callCount, equals(1));
267 }
268
269 void test_forEachMap_twoElementMap() {
270 int callCount = 0;
271 Map<String, String> map = {
272 'key1': 'value1',
273 'key2': 'value2'
274 };
275 Map iterationResult = {};
276 makeDatum(map).forEachMap((key, value) {
277 callCount++;
278 expect(value, isRequestDatum);
279 iterationResult[key] = value.datum;
280 });
281 expect(callCount, equals(2));
282 expect(iterationResult, equals(map));
283 }
284
285 void test_hasKey() {
286 var datum = makeDatum({
287 'foo': 'bar'
288 });
289 expect(datum.hasKey('foo'), isTrue);
290 expect(datum.hasKey('bar'), isFalse);
291 expect(datum.hasKey('baz'), isFalse);
292 }
293
294 void test_hasKey_null() {
295 expect(makeDatum(null).hasKey('foo'), isFalse);
296 }
297
298 void test_indexOperator_hasKey() {
299 var indexResult = makeDatum({
300 'foo': 'bar'
301 })['foo'];
302 expect(indexResult, isRequestDatum);
303 expect(indexResult.datum, equals('bar'));
304 expect(indexResult.path, equals('myPath.foo'));
305 }
306
307 void test_indexOperator_missingKey() {
308 expect(() => makeDatum({
309 'foo': 'bar'
310 })['baz'], _throwsInvalidParameter);
311 }
312
313 void test_indexOperator_nonMap() {
314 expect(() => makeDatum(1)['foo'], _throwsInvalidParameter);
315 }
316
317 void test_indexOperator_null() {
318 expect(() => makeDatum(null)['foo'], _throwsInvalidParameter);
319 }
320
321 void test_isList() {
322 expect(makeDatum(3).isList, isFalse);
323 expect(makeDatum(null).isList, isTrue);
324 expect(makeDatum([]).isList, isTrue);
325 expect(makeDatum(['foo', 'bar']).isList, isTrue);
326 }
327
328 void test_isMap() {
329 expect(makeDatum({
330 'key1': 'value1',
331 'key2': 'value2'
332 }).isMap, isTrue);
333 expect(makeDatum({}).isMap, isTrue);
334 expect(makeDatum(null).isMap, isTrue);
335 expect(makeDatum({
336 'key1': 'value1',
337 'key2': 2
338 }).isMap, isTrue);
339 expect(makeDatum({
340 'key1': 1,
341 'key2': 2
342 }).isMap, isTrue);
343 expect(makeDatum([]).isMap, isFalse);
344 }
345
346 void test_isStringList() {
347 expect(makeDatum(['foo', 'bar']).isStringList, isTrue);
348 expect(makeDatum([]).isStringList, isTrue);
349 expect(makeDatum(null).isStringList, isTrue);
350 expect(makeDatum(['foo', 1]).isStringList, isFalse);
351 expect(makeDatum({}).isStringList, isFalse);
352 }
353
354 void test_isStringListMap() {
355 expect(makeDatum({
356 'key1': ['value11', 'value12'],
357 'key2': ['value21', 'value22']
358 }).isStringListMap, isTrue);
359 expect(makeDatum({
360 'key1': 10,
361 'key2': 20
362 }).isStringListMap, isFalse);
363 expect(makeDatum({
364 'key1': [11, 12],
365 'key2': [21, 22]
366 }).isStringListMap, isFalse);
367 expect(makeDatum({}).isStringListMap, isTrue);
368 expect(makeDatum(null).isStringListMap, isTrue);
369 expect(makeDatum(3).isStringListMap, isFalse);
370 }
371
372 void test_isStringMap() {
373 expect(makeDatum({
374 'key1': 'value1',
375 'key2': 'value2'
376 }).isStringMap, isTrue);
377 expect(makeDatum({}).isStringMap, isTrue);
378 expect(makeDatum(null).isStringMap, isTrue);
379 expect(makeDatum({
380 'key1': 'value1',
381 'key2': 2
382 }).isStringMap, isFalse);
383 expect(makeDatum({
384 'key1': 1,
385 'key2': 2
386 }).isStringMap, isFalse);
387 expect(makeDatum([]).isMap, isFalse);
388 }
389
390 static RequestDatum makeDatum(dynamic datum) {
391 return new RequestDatum(request, 'myPath', datum);
392 }
393 }
394
395
396 @ReflectiveTestCase()
397 class RequestErrorTest { 136 class RequestErrorTest {
398 void test_create() { 137 void test_create() {
399 RequestError error = new RequestError('ERROR_CODE', 'msg'); 138 RequestError error = new RequestError('ERROR_CODE', 'msg');
400 expect(error.code, 'ERROR_CODE'); 139 expect(error.code, 'ERROR_CODE');
401 expect(error.message, "msg"); 140 expect(error.message, "msg");
402 expect(error.toJson(), equals({ 141 expect(error.toJson(), equals({
403 RequestError.CODE: 'ERROR_CODE', 142 RequestError.CODE: 'ERROR_CODE',
404 RequestError.MESSAGE: "msg" 143 RequestError.MESSAGE: "msg"
405 })); 144 }));
406 } 145 }
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 243
505 void test_fromJson_withParams() { 244 void test_fromJson_withParams() {
506 Request original = new Request('one', 'aMethod', {'foo': 'bar'}); 245 Request original = new Request('one', 'aMethod', {'foo': 'bar'});
507 String json = JSON.encode(original.toJson()); 246 String json = JSON.encode(original.toJson());
508 Request request = new Request.fromString(json); 247 Request request = new Request.fromString(json);
509 expect(request.id, equals('one')); 248 expect(request.id, equals('one'));
510 expect(request.method, equals('aMethod')); 249 expect(request.method, equals('aMethod'));
511 expect(request.params, equals({'foo': 'bar'})); 250 expect(request.params, equals({'foo': 'bar'}));
512 } 251 }
513 252
514 void test_getRequiredParameter_defined() {
515 String name = 'name';
516 String value = 'value';
517 Request request = new Request('0', '', {name: value});
518 expect(request.getRequiredParameter(name).datum, equals(value));
519 }
520
521 void test_getRequiredParameter_null() {
522 String name = 'name';
523 Request request = new Request('0', '', {name: null});
524 expect(request.getRequiredParameter(name).datum, equals(null));
525 }
526
527 void test_getRequiredParameter_undefined() {
528 String name = 'name';
529 Request request = new Request('0', '');
530 expect(() => request.getRequiredParameter(name), _throwsRequestFailure);
531 }
532
533 void test_toJson() { 253 void test_toJson() {
534 Request request = new Request('one', 'aMethod'); 254 Request request = new Request('one', 'aMethod');
535 expect(request.toJson(), equals({ 255 expect(request.toJson(), equals({
536 Request.ID: 'one', 256 Request.ID: 'one',
537 Request.METHOD: 'aMethod' 257 Request.METHOD: 'aMethod'
538 })); 258 }));
539 } 259 }
540 260
541 void test_toJson_withParams() { 261 void test_toJson_withParams() {
542 Request request = new Request('one', 'aMethod', {'foo': 'bar'}); 262 Request request = new Request('one', 'aMethod', {'foo': 'bar'});
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 Map<String, Object> result = response.result; 378 Map<String, Object> result = response.result;
659 expect(result.length, equals(1)); 379 expect(result.length, equals(1));
660 expect(result['foo'], equals('bar')); 380 expect(result['foo'], equals('bar'));
661 } 381 }
662 382
663 void test_setResult() { 383 void test_setResult() {
664 String resultName = 'name'; 384 String resultName = 'name';
665 String resultValue = 'value'; 385 String resultValue = 'value';
666 Response response = new Response('0'); 386 Response response = new Response('0');
667 response.setResult(resultName, resultValue); 387 response.setResult(resultName, resultValue);
668 expect(response.getResult(resultName), same(resultValue)); 388 expect(response.result[resultName], same(resultValue));
669 expect(response.toJson(), equals({ 389 expect(response.toJson(), equals({
670 Response.ID: '0', 390 Response.ID: '0',
671 Response.RESULT: { 391 Response.RESULT: {
672 resultName: resultValue 392 resultName: resultValue
673 } 393 }
674 })); 394 }));
675 } 395 }
676 } 396 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698