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

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

Issue 349103003: Migrate protocol_test.dart to reflective tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 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
« no previous file with comments | « pkg/analysis_server/test/declarative_tests.dart ('k') | no next file » | 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) 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:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
11 11
12 import 'declarative_tests.dart'; 12 import 'reflective_tests.dart';
13
14
15 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>());
16
13 17
14 main() { 18 main() {
15 addTestSuite(NotificationTest); 19 groupSep = ' | ';
16 addTestSuite(RequestTest); 20 group('Notification', () {
17 addTestSuite(RequestErrorTest); 21 runReflectiveTests(NotificationTest);
18 addTestSuite(RequestDatumTest); 22 });
19 addTestSuite(ResponseTest); 23 group('Request', () {
20 } 24 runReflectiveTests(RequestTest);
21 25 });
26 group('RequestError', () {
27 runReflectiveTests(RequestErrorTest);
28 });
29 group('RequestDatum', () {
30 runReflectiveTests(RequestDatumTest);
31 });
32 group('Response', () {
33 runReflectiveTests(ResponseTest);
34 });
35 }
36
37
38 @ReflectiveTestCase()
39 class InvalidParameterResponseMatcher extends Matcher {
40 static const int ERROR_CODE = -2;
41
42 @override
43 Description describe(Description description) => description.add(
44 "an 'invalid parameter' response (code $ERROR_CODE)");
45
46 @override
47 bool matches(item, Map matchState) {
48 if (item is! RequestFailure) {
49 return false;
50 }
51 var response = item.response;
52 if (response is! Response) {
53 return false;
54 }
55 if (response.error is! RequestError) {
56 return false;
57 }
58 RequestError requestError = response.error;
59 if (requestError.code != ERROR_CODE) {
60 return false;
61 }
62 return true;
63 }
64 }
65
66
67 @ReflectiveTestCase()
22 class NotificationTest { 68 class NotificationTest {
23 @runTest 69 void test_fromJson() {
24 static void getParameter_defined() { 70 Notification original = new Notification('foo');
71 Notification notification = new Notification.fromJson(original.toJson());
72 expect(notification.event, equals('foo'));
73 expect(notification.params.length, equals(0));
74 expect(notification.getParameter('x'), isNull);
75 }
76
77 void test_fromJson_withParams() {
78 Notification original = new Notification('foo');
79 original.setParameter('x', 'y');
80 Notification notification = new Notification.fromJson(original.toJson());
81 expect(notification.event, equals('foo'));
82 expect(notification.params.length, equals(1));
83 expect(notification.getParameter('x'), equals('y'));
84 }
85
86 void test_getParameter_defined() {
25 Notification notification = new Notification('foo'); 87 Notification notification = new Notification('foo');
26 notification.setParameter('x', 'y'); 88 notification.setParameter('x', 'y');
27 expect(notification.event, equals('foo')); 89 expect(notification.event, equals('foo'));
28 expect(notification.params.length, equals(1)); 90 expect(notification.params.length, equals(1));
29 expect(notification.getParameter('x'), equals('y')); 91 expect(notification.getParameter('x'), equals('y'));
30 expect(notification.toJson(), equals({ 92 expect(notification.toJson(), equals({
31 'event' : 'foo', 93 'event': 'foo',
32 'params' : {'x' : 'y'} 94 'params': {
95 'x': 'y'
96 }
33 })); 97 }));
34 } 98 }
35 99
36 @runTest 100 void test_getParameter_undefined() {
37 static void getParameter_undefined() {
38 Notification notification = new Notification('foo'); 101 Notification notification = new Notification('foo');
39 expect(notification.event, equals('foo')); 102 expect(notification.event, equals('foo'));
40 expect(notification.params.length, equals(0)); 103 expect(notification.params.length, equals(0));
41 expect(notification.getParameter('x'), isNull); 104 expect(notification.getParameter('x'), isNull);
42 expect(notification.toJson(), equals({ 105 expect(notification.toJson(), equals({
43 'event' : 'foo' 106 'event': 'foo'
44 })); 107 }));
45 } 108 }
46 109 }
47 @runTest 110
48 static void fromJson() { 111
49 Notification original = new Notification('foo'); 112 @ReflectiveTestCase()
50 Notification notification = new Notification.fromJson(original.toJson()); 113 class RequestDatumTest {
51 expect(notification.event, equals('foo')); 114 static Matcher isRequestDatum = new isInstanceOf<RequestDatum>(
52 expect(notification.params.length, equals(0)); 115 "RequestDatum");
53 expect(notification.getParameter('x'), isNull); 116
54 } 117 static Request request;
55 118 static Matcher _throwsInvalidParameter = throwsA(
56 @runTest 119 new InvalidParameterResponseMatcher());
57 static void fromJson_withParams() { 120
58 Notification original = new Notification('foo'); 121 void setUp() {
59 original.setParameter('x', 'y'); 122 request = new Request('myId', 'myMethod');
60 Notification notification = new Notification.fromJson(original.toJson()); 123 }
61 expect(notification.event, equals('foo')); 124
62 expect(notification.params.length, equals(1)); 125 void test_asBool() {
63 expect(notification.getParameter('x'), equals('y')); 126 expect(makeDatum(true).asBool(), isTrue);
64 } 127 expect(makeDatum(false).asBool(), isFalse);
65 } 128 expect(makeDatum('true').asBool(), isTrue);
66 129 expect(makeDatum('false').asBool(), isFalse);
67 class RequestTest { 130 expect(() => makeDatum('abc').asBool(), _throwsInvalidParameter);
68 @runTest 131 }
69 static void getParameter_defined() { 132
70 String name = 'name'; 133 void test_asInt() {
71 String value = 'value'; 134 expect(makeDatum(1).asInt(), equals(1));
72 Request request = new Request('0', ''); 135 expect(makeDatum('2').asInt(), equals(2));
73 request.setParameter(name, value); 136 expect(() => makeDatum('xxx').asInt(), _throwsInvalidParameter);
74 expect(request.getParameter(name, null).datum, equals(value)); 137 expect(() => makeDatum(true).asInt(), _throwsInvalidParameter);
75 } 138 }
76 139
77 @runTest 140 void test_asList_emptyList() {
78 static void getParameter_undefined() { 141 expect(makeDatum([]).asList((datum) => datum.asString()), equals([]));
79 String name = 'name'; 142 }
80 String defaultValue = 'default value'; 143
81 Request request = new Request('0', ''); 144 void test_asList_nonEmptyList() {
82 expect(request.getParameter(name, defaultValue).datum, equals(defaultValue)) ; 145 expect(makeDatum(['foo', 'bar']).asList((datum) => datum.asString()),
83 } 146 equals(['foo', 'bar']));
84 147 }
85 @runTest 148
86 static void getRequiredParameter_defined() { 149 void test_asList_nonList() {
87 String name = 'name'; 150 expect(() => makeDatum(3).asList((datum) => null), _throwsInvalidParameter);
88 String value = 'value'; 151 }
89 Request request = new Request('0', ''); 152
90 request.setParameter(name, value); 153 void test_asString() {
91 expect(request.getRequiredParameter(name).datum, equals(value)); 154 expect(makeDatum('foo').asString(), equals('foo'));
92 } 155 expect(() => makeDatum(3).asString(), _throwsInvalidParameter);
93 156 }
94 @runTest 157
95 static void getRequiredParameter_undefined() { 158 void test_asStringList() {
96 String name = 'name'; 159 expect(makeDatum(['foo', 'bar']).asStringList(), equals(['foo', 'bar']));
97 Request request = new Request('0', ''); 160 expect(makeDatum([]).asStringList(), equals([]));
98 expect(() => request.getRequiredParameter(name), _throwsRequestFailure); 161 expect(() => makeDatum(['foo', 1]).asStringList(), _throwsInvalidParameter);
99 } 162 expect(() => makeDatum({}).asStringList(), _throwsInvalidParameter);
100 163 }
101 @runTest 164
102 static void fromJson() { 165 void test_asStringListMap() {
103 Request original = new Request('one', 'aMethod'); 166 {
104 String json = JSON.encode(original.toJson()); 167 var map = {
105 Request request = new Request.fromString(json); 168 'key1': ['value11', 'value12'],
106 expect(request.id, equals('one')); 169 'key2': ['value21', 'value22']
107 expect(request.method, equals('aMethod')); 170 };
108 } 171 expect(makeDatum(map).asStringListMap(), map);
109 172 }
110 @runTest 173 {
111 static void fromJson_invalidId() { 174 var map = {
112 String json = '{"id":{"one":"two"},"method":"aMethod","params":{"foo":"bar"} }'; 175 'key1': 10,
113 Request request = new Request.fromString(json); 176 'key2': 20
114 expect(request, isNull); 177 };
115 } 178 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter);
116 179 }
117 @runTest 180 {
118 static void fromJson_invalidMethod() { 181 var map = {
119 String json = '{"id":"one","method":{"boo":"aMethod"},"params":{"foo":"bar"} }'; 182 'key1': [11, 12],
120 Request request = new Request.fromString(json); 183 'key2': [21, 22]
121 expect(request, isNull); 184 };
122 } 185 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter);
123 186 }
124 @runTest 187 }
125 static void fromJson_invalidParams() { 188
126 String json = '{"id":"one","method":"aMethod","params":"foobar"}'; 189 void test_asStringMap() {
127 Request request = new Request.fromString(json); 190 expect(makeDatum({
128 expect(request, isNull); 191 'key1': 'value1',
129 } 192 'key2': 'value2'
130 193 }).asStringMap(), equals({
131 @runTest 194 'key1': 'value1',
132 static void fromJson_withParams() { 195 'key2': 'value2'
133 Request original = new Request('one', 'aMethod');
134 original.setParameter('foo', 'bar');
135 String json = JSON.encode(original.toJson());
136 Request request = new Request.fromString(json);
137 expect(request.id, equals('one'));
138 expect(request.method, equals('aMethod'));
139 expect(request.getParameter('foo', null).asString(), equals('bar'));
140 }
141
142 @runTest
143 static void toJson() {
144 Request request = new Request('one', 'aMethod');
145 expect(request.toJson(), equals({
146 Request.ID : 'one',
147 Request.METHOD : 'aMethod'
148 })); 196 }));
149 } 197 expect(makeDatum({}).asStringMap(), equals({}));
150 198 expect(() => makeDatum({
151 @runTest 199 'key1': 'value1',
152 static void toJson_withParams() { 200 'key2': 2
153 Request request = new Request('one', 'aMethod'); 201 }).asStringMap(), _throwsInvalidParameter);
154 request.setParameter('foo', 'bar'); 202 expect(() => makeDatum({
155 expect(request.toJson(), equals({ 203 'key1': 1,
156 Request.ID : 'one', 204 'key2': 2
157 Request.METHOD : 'aMethod', 205 }).asStringMap(), _throwsInvalidParameter);
158 Request.PARAMS : {'foo' : 'bar'} 206 expect(() => makeDatum([]).asStringMap(), _throwsInvalidParameter);
159 })); 207 }
160 } 208
161 } 209 void test_forEachMap_emptyMap() {
162 210 makeDatum({}).forEachMap((key, value) {
211 fail('Empty map should not be iterated');
212 });
213 }
214
215 void test_forEachMap_nonMap() {
216 expect(() => makeDatum(1).forEachMap((key, value) {
217 fail('Non-map should not be iterated');
218 }), _throwsInvalidParameter);
219 }
220
221 void test_forEachMap_oneElementMap() {
222 int callCount = 0;
223 makeDatum({
224 'key': 'value'
225 }).forEachMap((key, value) {
226 callCount++;
227 expect(key, equals('key'));
228 expect(value, isRequestDatum);
229 expect(value.datum, equals('value'));
230 });
231 expect(callCount, equals(1));
232 }
233
234 void test_forEachMap_twoElementMap() {
235 int callCount = 0;
236 Map<String, String> map = {
237 'key1': 'value1',
238 'key2': 'value2'
239 };
240 Map iterationResult = {};
241 makeDatum(map).forEachMap((key, value) {
242 callCount++;
243 expect(value, isRequestDatum);
244 iterationResult[key] = value.datum;
245 });
246 expect(callCount, equals(2));
247 expect(iterationResult, equals(map));
248 }
249
250 void test_hasKey() {
251 var datum = makeDatum({
252 'foo': 'bar'
253 });
254 expect(datum.hasKey('foo'), isTrue);
255 expect(datum.hasKey('bar'), isFalse);
256 expect(datum.hasKey('baz'), isFalse);
257 }
258
259 void test_indexOperator_hasKey() {
260 var indexResult = makeDatum({
261 'foo': 'bar'
262 })['foo'];
263 expect(indexResult, isRequestDatum);
264 expect(indexResult.datum, equals('bar'));
265 expect(indexResult.path, equals('myPath.foo'));
266 }
267
268 void test_indexOperator_missingKey() {
269 expect(() => makeDatum({
270 'foo': 'bar'
271 })['baz'], _throwsInvalidParameter);
272 }
273
274 void test_indexOperator_nonMap() {
275 expect(() => makeDatum(1)['foo'], _throwsInvalidParameter);
276 }
277
278 static RequestDatum makeDatum(dynamic datum) {
279 return new RequestDatum(request, 'myPath', datum);
280 }
281 }
282
283
284 @ReflectiveTestCase()
163 class RequestErrorTest { 285 class RequestErrorTest {
164 @runTest 286 void test_create() {
165 static void create() {
166 RequestError error = new RequestError(42, 'msg'); 287 RequestError error = new RequestError(42, 'msg');
167 expect(error.code, 42); 288 expect(error.code, 42);
168 expect(error.message, "msg"); 289 expect(error.message, "msg");
169 expect(error.toJson(), equals({ 290 expect(error.toJson(), equals({
170 RequestError.CODE: 42, 291 RequestError.CODE: 42,
171 RequestError.MESSAGE: "msg" 292 RequestError.MESSAGE: "msg"
172 })); 293 }));
173 } 294 }
174 295
175 @runTest 296 void test_create_internalError() {
176 static void create_parseError() { 297 RequestError error = new RequestError.internalError();
298 expect(error.code, RequestError.CODE_INTERNAL_ERROR);
299 expect(error.message, "Internal error");
300 }
301
302 void test_create_invalidParameters() {
303 RequestError error = new RequestError.invalidParameters();
304 expect(error.code, RequestError.CODE_INVALID_PARAMS);
305 expect(error.message, "Invalid parameters");
306 }
307
308 void test_create_invalidRequest() {
309 RequestError error = new RequestError.invalidRequest();
310 expect(error.code, RequestError.CODE_INVALID_REQUEST);
311 expect(error.message, "Invalid request");
312 }
313
314 void test_create_methodNotFound() {
315 RequestError error = new RequestError.methodNotFound();
316 expect(error.code, RequestError.CODE_METHOD_NOT_FOUND);
317 expect(error.message, "Method not found");
318 }
319
320 void test_create_parseError() {
177 RequestError error = new RequestError.parseError(); 321 RequestError error = new RequestError.parseError();
178 expect(error.code, RequestError.CODE_PARSE_ERROR); 322 expect(error.code, RequestError.CODE_PARSE_ERROR);
179 expect(error.message, "Parse error"); 323 expect(error.message, "Parse error");
180 } 324 }
181 325
182 @runTest 326 void test_create_serverAlreadyStarted() {
183 static void create_methodNotFound() {
184 RequestError error = new RequestError.methodNotFound();
185 expect(error.code, RequestError.CODE_METHOD_NOT_FOUND);
186 expect(error.message, "Method not found");
187 }
188
189 @runTest
190 static void create_invalidParameters() {
191 RequestError error = new RequestError.invalidParameters();
192 expect(error.code, RequestError.CODE_INVALID_PARAMS);
193 expect(error.message, "Invalid parameters");
194 }
195
196 @runTest
197 static void create_invalidRequest() {
198 RequestError error = new RequestError.invalidRequest();
199 expect(error.code, RequestError.CODE_INVALID_REQUEST);
200 expect(error.message, "Invalid request");
201 }
202
203 @runTest
204 static void create_internalError() {
205 RequestError error = new RequestError.internalError();
206 expect(error.code, RequestError.CODE_INTERNAL_ERROR);
207 expect(error.message, "Internal error");
208 }
209
210 @runTest
211 static void create_serverAlreadyStarted() {
212 RequestError error = new RequestError.serverAlreadyStarted(); 327 RequestError error = new RequestError.serverAlreadyStarted();
213 expect(error.code, RequestError.CODE_SERVER_ALREADY_STARTED); 328 expect(error.code, RequestError.CODE_SERVER_ALREADY_STARTED);
214 expect(error.message, "Server already started"); 329 expect(error.message, "Server already started");
215 } 330 }
216 331
217 @runTest 332 void test_fromJson() {
218 static void fromJson() {
219 var json = { 333 var json = {
220 RequestError.CODE: RequestError.CODE_PARSE_ERROR, 334 RequestError.CODE: RequestError.CODE_PARSE_ERROR,
221 RequestError.MESSAGE: 'foo', 335 RequestError.MESSAGE: 'foo',
222 RequestError.DATA: {'ints': [1, 2, 3]} 336 RequestError.DATA: {
337 'ints': [1, 2, 3]
338 }
223 }; 339 };
224 RequestError error = new RequestError.fromJson(json); 340 RequestError error = new RequestError.fromJson(json);
225 expect(error.code, RequestError.CODE_PARSE_ERROR); 341 expect(error.code, RequestError.CODE_PARSE_ERROR);
226 expect(error.message, "foo"); 342 expect(error.message, "foo");
227 expect(error.data['ints'], [1, 2, 3]); 343 expect(error.data['ints'], [1, 2, 3]);
228 expect(error.getData('ints'), [1, 2, 3]); 344 expect(error.getData('ints'), [1, 2, 3]);
229 } 345 }
230 346
231 @runTest 347 void test_toJson() {
232 static void toJson() {
233 RequestError error = new RequestError(0, 'msg'); 348 RequestError error = new RequestError(0, 'msg');
234 error.setData('answer', 42); 349 error.setData('answer', 42);
235 error.setData('question', 'unknown'); 350 error.setData('question', 'unknown');
236 expect(error.toJson(), { 351 expect(error.toJson(), {
237 RequestError.CODE: 0, 352 RequestError.CODE: 0,
238 RequestError.MESSAGE: 'msg', 353 RequestError.MESSAGE: 'msg',
239 RequestError.DATA: {'answer': 42, 'question': 'unknown'} 354 RequestError.DATA: {
355 'answer': 42,
356 'question': 'unknown'
357 }
240 }); 358 });
241 } 359 }
242 } 360 }
243 361
244 class InvalidParameterResponseMatcher extends Matcher { 362
245 static const int ERROR_CODE = -2; 363 @ReflectiveTestCase()
246 364 class RequestTest {
247 @override 365 void test_fromJson() {
248 Description describe(Description description) => 366 Request original = new Request('one', 'aMethod');
249 description.add("an 'invalid parameter' response (code $ERROR_CODE)"); 367 String json = JSON.encode(original.toJson());
250 368 Request request = new Request.fromString(json);
251 @override 369 expect(request.id, equals('one'));
252 bool matches(item, Map matchState) { 370 expect(request.method, equals('aMethod'));
253 if (item is! RequestFailure) { 371 }
254 return false; 372
255 } 373 void test_fromJson_invalidId() {
256 var response = item.response; 374 String json =
257 if (response is! Response) { 375 '{"id":{"one":"two"},"method":"aMethod","params":{"foo":"bar"}}';
258 return false; 376 Request request = new Request.fromString(json);
259 } 377 expect(request, isNull);
260 if (response.error is! RequestError) { 378 }
261 return false; 379
262 } 380 void test_fromJson_invalidMethod() {
263 RequestError requestError = response.error; 381 String json =
264 if (requestError.code != ERROR_CODE) { 382 '{"id":"one","method":{"boo":"aMethod"},"params":{"foo":"bar"}}';
265 return false; 383 Request request = new Request.fromString(json);
266 } 384 expect(request, isNull);
267 return true; 385 }
386
387 void test_fromJson_invalidParams() {
388 String json = '{"id":"one","method":"aMethod","params":"foobar"}';
389 Request request = new Request.fromString(json);
390 expect(request, isNull);
391 }
392
393 void test_fromJson_withParams() {
394 Request original = new Request('one', 'aMethod');
395 original.setParameter('foo', 'bar');
396 String json = JSON.encode(original.toJson());
397 Request request = new Request.fromString(json);
398 expect(request.id, equals('one'));
399 expect(request.method, equals('aMethod'));
400 expect(request.getParameter('foo', null).asString(), equals('bar'));
401 }
402
403 void test_getParameter_defined() {
404 String name = 'name';
405 String value = 'value';
406 Request request = new Request('0', '');
407 request.setParameter(name, value);
408 expect(request.getParameter(name, null).datum, equals(value));
409 }
410
411 void test_getParameter_undefined() {
412 String name = 'name';
413 String defaultValue = 'default value';
414 Request request = new Request('0', '');
415 expect(request.getParameter(name, defaultValue).datum, equals(
416 defaultValue));
417 }
418
419 void test_getRequiredParameter_defined() {
420 String name = 'name';
421 String value = 'value';
422 Request request = new Request('0', '');
423 request.setParameter(name, value);
424 expect(request.getRequiredParameter(name).datum, equals(value));
425 }
426
427 void test_getRequiredParameter_undefined() {
428 String name = 'name';
429 Request request = new Request('0', '');
430 expect(() => request.getRequiredParameter(name), _throwsRequestFailure);
431 }
432
433 void test_toJson() {
434 Request request = new Request('one', 'aMethod');
435 expect(request.toJson(), equals({
436 Request.ID: 'one',
437 Request.METHOD: 'aMethod'
438 }));
439 }
440
441 void test_toJson_withParams() {
442 Request request = new Request('one', 'aMethod');
443 request.setParameter('foo', 'bar');
444 expect(request.toJson(), equals({
445 Request.ID: 'one',
446 Request.METHOD: 'aMethod',
447 Request.PARAMS: {
448 'foo': 'bar'
449 }
450 }));
268 } 451 }
269 } 452 }
270 453
271 class RequestDatumTest { 454
272 static Request request; 455 @ReflectiveTestCase()
273
274 static Matcher _throwsInvalidParameter = throwsA(
275 new InvalidParameterResponseMatcher());
276 static Matcher isRequestDatum = new isInstanceOf<RequestDatum>("RequestDatum"
277 );
278
279 static void setUp() {
280 request = new Request('myId', 'myMethod');
281 }
282
283 static RequestDatum makeDatum(dynamic datum) {
284 return new RequestDatum(request, 'myPath', datum);
285 }
286
287 @runTest
288 static void indexOperator_nonMap() {
289 setUp();
290 expect(() => makeDatum(1)['foo'], _throwsInvalidParameter);
291 }
292
293 @runTest
294 static void indexOperator_missingKey() {
295 setUp();
296 expect(() => makeDatum({
297 'foo': 'bar'
298 })['baz'], _throwsInvalidParameter);
299 }
300
301 @runTest
302 static void indexOperator_hasKey() {
303 setUp();
304 var indexResult = makeDatum({
305 'foo': 'bar'
306 })['foo'];
307 expect(indexResult, isRequestDatum);
308 expect(indexResult.datum, equals('bar'));
309 expect(indexResult.path, equals('myPath.foo'));
310 }
311
312 @runTest
313 static void hasKey() {
314 setUp();
315 var datum = makeDatum({
316 'foo': 'bar'
317 });
318 expect(datum.hasKey('foo'), isTrue);
319 expect(datum.hasKey('bar'), isFalse);
320 expect(datum.hasKey('baz'), isFalse);
321 }
322
323 @runTest
324 static void forEachMap_nonMap() {
325 setUp();
326 expect(() => makeDatum(1).forEachMap((key, value) {
327 fail('Non-map should not be iterated');
328 }), _throwsInvalidParameter);
329 }
330
331 @runTest
332 static void forEachMap_emptyMap() {
333 setUp();
334 makeDatum({}).forEachMap((key, value) {
335 fail('Empty map should not be iterated');
336 });
337 }
338
339 @runTest
340 static void forEachMap_oneElementMap() {
341 setUp();
342 int callCount = 0;
343 makeDatum({
344 'key': 'value'
345 }).forEachMap((key, value) {
346 callCount++;
347 expect(key, equals('key'));
348 expect(value, isRequestDatum);
349 expect(value.datum, equals('value'));
350 });
351 expect(callCount, equals(1));
352 }
353
354 @runTest
355 static void forEachMap_twoElementMap() {
356 setUp();
357 int callCount = 0;
358 Map<String, String> map = {
359 'key1': 'value1',
360 'key2': 'value2'
361 };
362 Map iterationResult = {};
363 makeDatum(map).forEachMap((key, value) {
364 callCount++;
365 expect(value, isRequestDatum);
366 iterationResult[key] = value.datum;
367 });
368 expect(callCount, equals(2));
369 expect(iterationResult, equals(map));
370 }
371
372 @runTest
373 static void asBool() {
374 setUp();
375 expect(makeDatum(true).asBool(), isTrue);
376 expect(makeDatum(false).asBool(), isFalse);
377 expect(makeDatum('true').asBool(), isTrue);
378 expect(makeDatum('false').asBool(), isFalse);
379 expect(() => makeDatum('abc').asBool(), _throwsInvalidParameter);
380 }
381
382 @runTest
383 static void asInt() {
384 setUp();
385 expect(makeDatum(1).asInt(), equals(1));
386 expect(makeDatum('2').asInt(), equals(2));
387 expect(() => makeDatum('xxx').asInt(), _throwsInvalidParameter);
388 expect(() => makeDatum(true).asInt(), _throwsInvalidParameter);
389 }
390
391 @runTest
392 static void asList_nonList() {
393 setUp();
394 expect(() => makeDatum(3).asList((datum) => null), _throwsInvalidParameter);
395 }
396
397 @runTest
398 static void asList_emptyList() {
399 setUp();
400 expect(makeDatum([]).asList((datum) => datum.asString()), equals([]));
401 }
402
403 @runTest
404 static void asList_nonEmptyList() {
405 setUp();
406 expect(makeDatum(['foo', 'bar']).asList((datum) => datum.asString()), equals (['foo', 'bar']));
407 }
408
409 @runTest
410 static void asString() {
411 setUp();
412 expect(makeDatum('foo').asString(), equals('foo'));
413 expect(() => makeDatum(3).asString(), _throwsInvalidParameter);
414 }
415
416 @runTest
417 static void asStringList() {
418 setUp();
419 expect(makeDatum(['foo', 'bar']).asStringList(), equals(['foo', 'bar']));
420 expect(makeDatum([]).asStringList(), equals([]));
421 expect(() => makeDatum(['foo', 1]).asStringList(), _throwsInvalidParameter);
422 expect(() => makeDatum({}).asStringList(), _throwsInvalidParameter);
423 }
424
425 @runTest
426 static void asStringMap() {
427 setUp();
428 expect(makeDatum({
429 'key1': 'value1',
430 'key2': 'value2'
431 }).asStringMap(), equals({
432 'key1': 'value1',
433 'key2': 'value2'
434 }));
435 expect(makeDatum({}).asStringMap(), equals({}));
436 expect(() => makeDatum({
437 'key1': 'value1',
438 'key2': 2
439 }).asStringMap(), _throwsInvalidParameter);
440 expect(() => makeDatum({
441 'key1': 1,
442 'key2': 2
443 }).asStringMap(), _throwsInvalidParameter);
444 expect(() => makeDatum([]).asStringMap(), _throwsInvalidParameter);
445 }
446
447 @runTest
448 static void asStringListMap() {
449 setUp();
450 {
451 var map = {
452 'key1': ['value11', 'value12'],
453 'key2': ['value21', 'value22']
454 };
455 expect(makeDatum(map).asStringListMap(), map);
456 }
457 {
458 var map = {
459 'key1': 10,
460 'key2': 20
461 };
462 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter);
463 }
464 {
465 var map = {
466 'key1': [11, 12],
467 'key2': [21, 22]
468 };
469 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter);
470 }
471 }
472 }
473
474 class ResponseTest { 456 class ResponseTest {
475 @runTest 457 void test_create_contextDoesNotExist() {
476 static void create_contextDoesNotExist() {
477 Response response = new Response.contextDoesNotExist(new Request('0', '')); 458 Response response = new Response.contextDoesNotExist(new Request('0', ''));
478 expect(response.id, equals('0')); 459 expect(response.id, equals('0'));
479 expect(response.error, isNotNull); 460 expect(response.error, isNotNull);
480 expect(response.toJson(), equals({ 461 expect(response.toJson(), equals({
481 Response.ID: '0', 462 Response.ID: '0',
482 Response.ERROR: {'code': -1, 'message': 'Context does not exist'} 463 Response.ERROR: {
483 })); 464 'code': -1,
484 } 465 'message': 'Context does not exist'
485 466 }
486 @runTest 467 }));
487 static void create_invalidRequestFormat() { 468 }
469
470 void test_create_invalidRequestFormat() {
488 Response response = new Response.invalidRequestFormat(); 471 Response response = new Response.invalidRequestFormat();
489 expect(response.id, equals('')); 472 expect(response.id, equals(''));
490 expect(response.error, isNotNull); 473 expect(response.error, isNotNull);
491 expect(response.toJson(), equals({ 474 expect(response.toJson(), equals({
492 Response.ID: '', 475 Response.ID: '',
493 Response.ERROR: {'code': -4, 'message': 'Invalid request'} 476 Response.ERROR: {
494 })); 477 'code': -4,
495 } 478 'message': 'Invalid request'
496 479 }
497 @runTest 480 }));
498 static void create_missingRequiredParameter() { 481 }
499 Response response = new Response.missingRequiredParameter(new Request('0', ' '), 'x'); 482
500 expect(response.id, equals('0')); 483 void test_create_missingRequiredParameter() {
501 expect(response.error, isNotNull); 484 Response response = new Response.missingRequiredParameter(new Request('0',
502 expect(response.toJson(), equals({ 485 ''), 'x');
503 Response.ID: '0', 486 expect(response.id, equals('0'));
504 Response.ERROR: {'code': -5, 'message': 'Missing required parameter: x'} 487 expect(response.error, isNotNull);
505 })); 488 expect(response.toJson(), equals({
506 } 489 Response.ID: '0',
507 490 Response.ERROR: {
508 @runTest 491 'code': -5,
509 static void create_unknownAnalysisOption() { 492 'message': 'Missing required parameter: x'
510 Response response = new Response.unknownAnalysisOption(new Request('0', ''), 'x'); 493 }
511 expect(response.id, equals('0')); 494 }));
512 expect(response.error, isNotNull); 495 }
513 expect(response.toJson(), equals({ 496
514 Response.ID: '0', 497 void test_create_unanalyzedPriorityFiles() {
515 Response.ERROR: {'code': -6, 'message': 'Unknown analysis option: "x"'} 498 Response response = new Response.unanalyzedPriorityFiles(new Request('0',
516 })); 499 ''), 'file list');
517 } 500 expect(response.id, equals('0'));
518 501 expect(response.error, isNotNull);
519 @runTest 502 expect(response.toJson(), equals({
520 static void create_unknownRequest() { 503 Response.ID: '0',
504 Response.ERROR: {
505 'code': -11,
506 'message': "Unanalyzed files cannot be a priority: 'file list'"
507 }
508 }));
509 }
510
511 void test_create_unknownAnalysisOption() {
512 Response response = new Response.unknownAnalysisOption(new Request('0', ''),
513 'x');
514 expect(response.id, equals('0'));
515 expect(response.error, isNotNull);
516 expect(response.toJson(), equals({
517 Response.ID: '0',
518 Response.ERROR: {
519 'code': -6,
520 'message': 'Unknown analysis option: "x"'
521 }
522 }));
523 }
524
525 void test_create_unknownRequest() {
521 Response response = new Response.unknownRequest(new Request('0', '')); 526 Response response = new Response.unknownRequest(new Request('0', ''));
522 expect(response.id, equals('0')); 527 expect(response.id, equals('0'));
523 expect(response.error, isNotNull); 528 expect(response.error, isNotNull);
524 expect(response.toJson(), equals({ 529 expect(response.toJson(), equals({
525 Response.ID: '0', 530 Response.ID: '0',
526 Response.ERROR: {'code': -7, 'message': 'Unknown request'} 531 Response.ERROR: {
527 })); 532 'code': -7,
528 } 533 'message': 'Unknown request'
529 534 }
530 @runTest 535 }));
531 static void create_unanalyzedPriorityFiles() { 536 }
532 Response response = new Response.unanalyzedPriorityFiles(new Request('0', '' ), 'file list'); 537
533 expect(response.id, equals('0')); 538 void test_fromJson() {
534 expect(response.error, isNotNull);
535 expect(response.toJson(), equals({
536 Response.ID: '0',
537 Response.ERROR: {'code': -11, 'message': "Unanalyzed files cannot be a pri ority: 'file list'"}
538 }));
539 }
540
541 @runTest
542 static void setResult() {
543 String resultName = 'name';
544 String resultValue = 'value';
545 Response response = new Response('0');
546 response.setResult(resultName, resultValue);
547 expect(response.getResult(resultName), same(resultValue));
548 expect(response.toJson(), equals({
549 Response.ID: '0',
550 Response.RESULT: {
551 resultName: resultValue
552 }
553 }));
554 }
555
556 @runTest
557 static void fromJson() {
558 Response original = new Response('myId'); 539 Response original = new Response('myId');
559 Response response = new Response.fromJson(original.toJson()); 540 Response response = new Response.fromJson(original.toJson());
560 expect(response.id, equals('myId')); 541 expect(response.id, equals('myId'));
561 } 542 }
562 543
563 @runTest 544 void test_fromJson_withError() {
564 static void fromJson_withError() {
565 Response original = new Response.invalidRequestFormat(); 545 Response original = new Response.invalidRequestFormat();
566 Response response = new Response.fromJson(original.toJson()); 546 Response response = new Response.fromJson(original.toJson());
567 expect(response.id, equals('')); 547 expect(response.id, equals(''));
568 expect(response.error, isNotNull); 548 expect(response.error, isNotNull);
569 RequestError error = response.error; 549 RequestError error = response.error;
570 expect(error.code, equals(-4)); 550 expect(error.code, equals(-4));
571 expect(error.message, equals('Invalid request')); 551 expect(error.message, equals('Invalid request'));
572 } 552 }
573 553
574 @runTest 554 void test_fromJson_withResult() {
575 static void fromJson_withResult() {
576 Response original = new Response('myId'); 555 Response original = new Response('myId');
577 original.setResult('foo', 'bar'); 556 original.setResult('foo', 'bar');
578 Response response = new Response.fromJson(original.toJson()); 557 Response response = new Response.fromJson(original.toJson());
579 expect(response.id, equals('myId')); 558 expect(response.id, equals('myId'));
580 Map<String, Object> result = response.result; 559 Map<String, Object> result = response.result;
581 expect(result.length, equals(1)); 560 expect(result.length, equals(1));
582 expect(result['foo'], equals('bar')); 561 expect(result['foo'], equals('bar'));
583 } 562 }
563
564 void test_setResult() {
565 String resultName = 'name';
566 String resultValue = 'value';
567 Response response = new Response('0');
568 response.setResult(resultName, resultValue);
569 expect(response.getResult(resultName), same(resultValue));
570 expect(response.toJson(), equals({
571 Response.ID: '0',
572 Response.RESULT: {
573 resultName: resultValue
574 }
575 }));
576 }
584 } 577 }
585
586 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>());
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/declarative_tests.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698