| OLD | NEW |
| 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'; |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 243 |
| 244 class InvalidParameterResponseMatcher extends Matcher { | 244 class InvalidParameterResponseMatcher extends Matcher { |
| 245 static const int ERROR_CODE = -2; | 245 static const int ERROR_CODE = -2; |
| 246 | 246 |
| 247 @override | 247 @override |
| 248 Description describe(Description description) => | 248 Description describe(Description description) => |
| 249 description.add("an 'invalid parameter' response (code $ERROR_CODE)"); | 249 description.add("an 'invalid parameter' response (code $ERROR_CODE)"); |
| 250 | 250 |
| 251 @override | 251 @override |
| 252 bool matches(item, Map matchState) { | 252 bool matches(item, Map matchState) { |
| 253 if (item is! Response) { | 253 if (item is! RequestFailure) { |
| 254 return false; | 254 return false; |
| 255 } | 255 } |
| 256 Response response = item; | 256 var response = item.response; |
| 257 if (response is! Response) { |
| 258 return false; |
| 259 } |
| 257 if (response.error is! RequestError) { | 260 if (response.error is! RequestError) { |
| 258 return false; | 261 return false; |
| 259 } | 262 } |
| 260 RequestError requestError = response.error; | 263 RequestError requestError = response.error; |
| 261 if (requestError.code != ERROR_CODE) { | 264 if (requestError.code != ERROR_CODE) { |
| 262 return false; | 265 return false; |
| 263 } | 266 } |
| 264 return true; | 267 return true; |
| 265 } | 268 } |
| 266 } | 269 } |
| 267 | 270 |
| 268 class RequestDatumTest { | 271 class RequestDatumTest { |
| 269 static Request request; | 272 static Request request; |
| 270 | 273 |
| 271 static Matcher _throwsInvalidParameter = throwsA( | 274 static Matcher _throwsInvalidParameter = throwsA( |
| 272 new InvalidParameterResponseMatcher()); | 275 new InvalidParameterResponseMatcher()); |
| 273 static Matcher isRequestDatum = new isInstanceOf<RequestDatum>("RequestDatum" | 276 static Matcher isRequestDatum = new isInstanceOf<RequestDatum>("RequestDatum" |
| 274 ); | 277 ); |
| 275 | 278 |
| 276 static void setUp() { | 279 static void setUp() { |
| 277 request = new Request('myId', 'myMethod'); | 280 request = new Request('myId', 'myMethod'); |
| 278 } | 281 } |
| 279 | 282 |
| 280 static RequestDatum makeDatum(dynamic datum) { | 283 static RequestDatum makeDatum(dynamic datum) { |
| 281 return new RequestDatum(request, 'myPath', datum); | 284 return new RequestDatum(request, 'myPath', datum); |
| 282 } | 285 } |
| 283 | 286 |
| 284 @runTest | 287 @runTest |
| 285 static void indexOperator_nonMap() { | 288 static void indexOperator_nonMap() { |
| 286 //TODO fails | 289 setUp(); |
| 287 // expect(() => makeDatum(1)['foo'], _throwsInvalidParameter); | 290 expect(() => makeDatum(1)['foo'], _throwsInvalidParameter); |
| 288 } | 291 } |
| 289 | 292 |
| 290 @runTest | 293 @runTest |
| 291 static void indexOperator_missingKey() { | 294 static void indexOperator_missingKey() { |
| 292 //TODO fails | 295 setUp(); |
| 293 // expect(() => makeDatum({ | 296 expect(() => makeDatum({ |
| 294 // 'foo': 'bar' | 297 'foo': 'bar' |
| 295 // })['baz'], _throwsInvalidParameter); | 298 })['baz'], _throwsInvalidParameter); |
| 296 } | 299 } |
| 297 | 300 |
| 298 @runTest | 301 @runTest |
| 299 static void indexOperator_hasKey() { | 302 static void indexOperator_hasKey() { |
| 303 setUp(); |
| 300 var indexResult = makeDatum({ | 304 var indexResult = makeDatum({ |
| 301 'foo': 'bar' | 305 'foo': 'bar' |
| 302 })['foo']; | 306 })['foo']; |
| 303 expect(indexResult, isRequestDatum); | 307 expect(indexResult, isRequestDatum); |
| 304 expect(indexResult.datum, equals('bar')); | 308 expect(indexResult.datum, equals('bar')); |
| 305 expect(indexResult.path, equals('myPath.foo')); | 309 expect(indexResult.path, equals('myPath.foo')); |
| 306 } | 310 } |
| 307 | 311 |
| 308 @runTest | 312 @runTest |
| 309 static void hasKey() { | 313 static void hasKey() { |
| 314 setUp(); |
| 310 var datum = makeDatum({ | 315 var datum = makeDatum({ |
| 311 'foo': 'bar' | 316 'foo': 'bar' |
| 312 }); | 317 }); |
| 313 expect(datum.hasKey('foo'), isTrue); | 318 expect(datum.hasKey('foo'), isTrue); |
| 314 expect(datum.hasKey('bar'), isFalse); | 319 expect(datum.hasKey('bar'), isFalse); |
| 315 expect(datum.hasKey('baz'), isFalse); | 320 expect(datum.hasKey('baz'), isFalse); |
| 316 } | 321 } |
| 317 | 322 |
| 318 @runTest | 323 @runTest |
| 319 static void forEachMap_nonMap() { | 324 static void forEachMap_nonMap() { |
| 320 //TODO fails | 325 setUp(); |
| 321 // expect(() => makeDatum(1).forEachMap((key, value) { | 326 expect(() => makeDatum(1).forEachMap((key, value) { |
| 322 // fail('Non-map should not be iterated'); | 327 fail('Non-map should not be iterated'); |
| 323 // }), _throwsInvalidParameter); | 328 }), _throwsInvalidParameter); |
| 324 } | 329 } |
| 325 | 330 |
| 326 @runTest | 331 @runTest |
| 327 static void forEachMap_emptyMap() { | 332 static void forEachMap_emptyMap() { |
| 333 setUp(); |
| 328 makeDatum({}).forEachMap((key, value) { | 334 makeDatum({}).forEachMap((key, value) { |
| 329 fail('Empty map should not be iterated'); | 335 fail('Empty map should not be iterated'); |
| 330 }); | 336 }); |
| 331 } | 337 } |
| 332 | 338 |
| 333 @runTest | 339 @runTest |
| 334 static void forEachMap_oneElementMap() { | 340 static void forEachMap_oneElementMap() { |
| 341 setUp(); |
| 335 int callCount = 0; | 342 int callCount = 0; |
| 336 makeDatum({ | 343 makeDatum({ |
| 337 'key': 'value' | 344 'key': 'value' |
| 338 }).forEachMap((key, value) { | 345 }).forEachMap((key, value) { |
| 339 callCount++; | 346 callCount++; |
| 340 expect(key, equals('key')); | 347 expect(key, equals('key')); |
| 341 expect(value, isRequestDatum); | 348 expect(value, isRequestDatum); |
| 342 expect(value.datum, equals('value')); | 349 expect(value.datum, equals('value')); |
| 343 }); | 350 }); |
| 344 expect(callCount, equals(1)); | 351 expect(callCount, equals(1)); |
| 345 } | 352 } |
| 346 | 353 |
| 347 @runTest | 354 @runTest |
| 348 static void forEachMap_twoElementMap() { | 355 static void forEachMap_twoElementMap() { |
| 356 setUp(); |
| 349 int callCount = 0; | 357 int callCount = 0; |
| 350 Map<String, String> map = { | 358 Map<String, String> map = { |
| 351 'key1': 'value1', | 359 'key1': 'value1', |
| 352 'key2': 'value2' | 360 'key2': 'value2' |
| 353 }; | 361 }; |
| 354 Map iterationResult = {}; | 362 Map iterationResult = {}; |
| 355 makeDatum(map).forEachMap((key, value) { | 363 makeDatum(map).forEachMap((key, value) { |
| 356 callCount++; | 364 callCount++; |
| 357 expect(value, isRequestDatum); | 365 expect(value, isRequestDatum); |
| 358 iterationResult[key] = value.datum; | 366 iterationResult[key] = value.datum; |
| 359 }); | 367 }); |
| 360 expect(callCount, equals(2)); | 368 expect(callCount, equals(2)); |
| 361 expect(iterationResult, equals(map)); | 369 expect(iterationResult, equals(map)); |
| 362 } | 370 } |
| 363 | 371 |
| 364 @runTest | 372 @runTest |
| 365 static void asBool() { | 373 static void asBool() { |
| 374 setUp(); |
| 366 expect(makeDatum(true).asBool(), isTrue); | 375 expect(makeDatum(true).asBool(), isTrue); |
| 367 expect(makeDatum(false).asBool(), isFalse); | 376 expect(makeDatum(false).asBool(), isFalse); |
| 368 expect(makeDatum('true').asBool(), isTrue); | 377 expect(makeDatum('true').asBool(), isTrue); |
| 369 expect(makeDatum('false').asBool(), isFalse); | 378 expect(makeDatum('false').asBool(), isFalse); |
| 370 //TODO fails | 379 expect(() => makeDatum('abc').asBool(), _throwsInvalidParameter); |
| 371 // expect(() => makeDatum('abc').asBool(), _throwsInvalidParameter); | |
| 372 } | 380 } |
| 373 | 381 |
| 374 @runTest | 382 @runTest |
| 375 static void asInt() { | 383 static void asInt() { |
| 384 setUp(); |
| 376 expect(makeDatum(1).asInt(), equals(1)); | 385 expect(makeDatum(1).asInt(), equals(1)); |
| 377 expect(makeDatum('2').asInt(), equals(2)); | 386 expect(makeDatum('2').asInt(), equals(2)); |
| 378 //TODO fails | 387 expect(() => makeDatum('xxx').asInt(), _throwsInvalidParameter); |
| 379 // expect(() => makeDatum('xxx').asInt(), _throwsInvalidParameter); | 388 expect(() => makeDatum(true).asInt(), _throwsInvalidParameter); |
| 380 // expect(() => makeDatum(true).asInt(), _throwsInvalidParameter); | |
| 381 } | 389 } |
| 382 | 390 |
| 383 @runTest | 391 @runTest |
| 384 static void asString() { | 392 static void asString() { |
| 393 setUp(); |
| 385 expect(makeDatum('foo').asString(), equals('foo')); | 394 expect(makeDatum('foo').asString(), equals('foo')); |
| 386 //TODO fails | 395 expect(() => makeDatum(3).asString(), _throwsInvalidParameter); |
| 387 // expect(() => makeDatum(3).asString(), _throwsInvalidParameter); | |
| 388 } | 396 } |
| 389 | 397 |
| 390 @runTest | 398 @runTest |
| 391 static void asStringList() { | 399 static void asStringList() { |
| 400 setUp(); |
| 392 expect(makeDatum(['foo', 'bar']).asStringList(), equals(['foo', 'bar'])); | 401 expect(makeDatum(['foo', 'bar']).asStringList(), equals(['foo', 'bar'])); |
| 393 expect(makeDatum([]).asStringList(), equals([])); | 402 expect(makeDatum([]).asStringList(), equals([])); |
| 394 //TODO fails | 403 expect(() => makeDatum(['foo', 1]).asStringList(), _throwsInvalidParameter); |
| 395 // expect(() => makeDatum(['foo', 1]).asStringList(), _throwsInvalidParameter
); | 404 expect(() => makeDatum({}).asStringList(), _throwsInvalidParameter); |
| 396 // expect(() => makeDatum({}).asStringList(), _throwsInvalidParameter); | |
| 397 } | 405 } |
| 398 | 406 |
| 399 @runTest | 407 @runTest |
| 400 static void asStringMap() { | 408 static void asStringMap() { |
| 409 setUp(); |
| 401 expect(makeDatum({ | 410 expect(makeDatum({ |
| 402 'key1': 'value1', | 411 'key1': 'value1', |
| 403 'key2': 'value2' | 412 'key2': 'value2' |
| 404 }).asStringMap(), equals({ | 413 }).asStringMap(), equals({ |
| 405 'key1': 'value1', | 414 'key1': 'value1', |
| 406 'key2': 'value2' | 415 'key2': 'value2' |
| 407 })); | 416 })); |
| 408 expect(makeDatum({}).asStringMap(), equals({})); | 417 expect(makeDatum({}).asStringMap(), equals({})); |
| 409 //TODO fails | 418 expect(() => makeDatum({ |
| 410 // expect(() => makeDatum({ | 419 'key1': 'value1', |
| 411 // 'key1': 'value1', | 420 'key2': 2 |
| 412 // 'key2': 2 | 421 }).asStringMap(), _throwsInvalidParameter); |
| 413 // }).asStringMap(), _throwsInvalidParameter); | 422 expect(() => makeDatum({ |
| 414 // expect(() => makeDatum({ | 423 'key1': 1, |
| 415 // 'key1': 1, | 424 'key2': 2 |
| 416 // 'key2': 2 | 425 }).asStringMap(), _throwsInvalidParameter); |
| 417 // }).asStringMap(), _throwsInvalidParameter); | 426 expect(() => makeDatum([]).asStringMap(), _throwsInvalidParameter); |
| 418 // expect(() => makeDatum({ | |
| 419 // 1: 'value1', | |
| 420 // 2: 'value2' | |
| 421 // }).asStringMap(), _throwsInvalidParameter); | |
| 422 // expect(() => makeDatum([]).asStringMap(), _throwsInvalidParameter); | |
| 423 } | 427 } |
| 424 } | 428 } |
| 425 | 429 |
| 426 class ResponseTest { | 430 class ResponseTest { |
| 427 @runTest | 431 @runTest |
| 428 static void create_contextDoesNotExist() { | 432 static void create_contextDoesNotExist() { |
| 429 Response response = new Response.contextDoesNotExist(new Request('0', '')); | 433 Response response = new Response.contextDoesNotExist(new Request('0', '')); |
| 430 expect(response.id, equals('0')); | 434 expect(response.id, equals('0')); |
| 431 expect(response.error, isNotNull); | 435 expect(response.error, isNotNull); |
| 432 expect(response.toJson(), equals({ | 436 expect(response.toJson(), equals({ |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 original.setResult('foo', 'bar'); | 522 original.setResult('foo', 'bar'); |
| 519 Response response = new Response.fromJson(original.toJson()); | 523 Response response = new Response.fromJson(original.toJson()); |
| 520 expect(response.id, equals('myId')); | 524 expect(response.id, equals('myId')); |
| 521 Map<String, Object> result = response.result; | 525 Map<String, Object> result = response.result; |
| 522 expect(result.length, equals(1)); | 526 expect(result.length, equals(1)); |
| 523 expect(result['foo'], equals('bar')); | 527 expect(result['foo'], equals('bar')); |
| 524 } | 528 } |
| 525 } | 529 } |
| 526 | 530 |
| 527 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>()); | 531 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>()); |
| OLD | NEW |