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.computer.element; | 5 library test.computer.element; |
6 | 6 |
7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
8 | 8 |
9 import 'package:analysis_server/src/constants.dart'; | 9 import 'package:analysis_server/src/constants.dart'; |
10 import 'package:analysis_server/src/protocol_server.dart'; | 10 import 'package:analysis_server/src/protocol_server.dart'; |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 expect(location.offset, 20); | 267 expect(location.offset, 20); |
268 expect(location.length, 'myConstructor'.length); | 268 expect(location.length, 'myConstructor'.length); |
269 expect(location.startLine, 2); | 269 expect(location.startLine, 2); |
270 expect(location.startColumn, 11); | 270 expect(location.startColumn, 11); |
271 } | 271 } |
272 expect(element.parameters, '(int a, [String b])'); | 272 expect(element.parameters, '(int a, [String b])'); |
273 expect(element.returnType, 'A'); | 273 expect(element.returnType, 'A'); |
274 expect(element.flags, Element.FLAG_CONST); | 274 expect(element.flags, Element.FLAG_CONST); |
275 } | 275 } |
276 | 276 |
| 277 void test_fromElement_dynamic() { |
| 278 var engineElement = engine.DynamicElementImpl.instance; |
| 279 // create notification Element |
| 280 Element element = newElement_fromEngine(engineElement); |
| 281 expect(element.kind, ElementKind.UNKNOWN); |
| 282 expect(element.name, 'dynamic'); |
| 283 expect(element.location, isNull); |
| 284 expect(element.parameters, isNull); |
| 285 expect(element.returnType, isNull); |
| 286 expect(element.flags, 0); |
| 287 } |
| 288 |
277 void test_fromElement_FIELD() { | 289 void test_fromElement_FIELD() { |
278 engine.Source source = addSource('/test.dart', ''' | 290 engine.Source source = addSource('/test.dart', ''' |
279 class A { | 291 class A { |
280 static const myField = 42; | 292 static const myField = 42; |
281 }'''); | 293 }'''); |
282 engine.CompilationUnit unit = resolveLibraryUnit(source); | 294 engine.CompilationUnit unit = resolveLibraryUnit(source); |
283 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | 295 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); |
284 // create notification Element | 296 // create notification Element |
285 Element element = newElement_fromEngine(engineElement); | 297 Element element = newElement_fromEngine(engineElement); |
286 expect(element.kind, ElementKind.FIELD); | 298 expect(element.kind, ElementKind.FIELD); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 expect(location.file, '/test.dart'); | 406 expect(location.file, '/test.dart'); |
395 expect(location.offset, 16); | 407 expect(location.offset, 16); |
396 expect(location.length, 'mySetter'.length); | 408 expect(location.length, 'mySetter'.length); |
397 expect(location.startLine, 2); | 409 expect(location.startLine, 2); |
398 expect(location.startColumn, 7); | 410 expect(location.startColumn, 7); |
399 } | 411 } |
400 expect(element.parameters, '(String x)'); | 412 expect(element.parameters, '(String x)'); |
401 expect(element.returnType, isNull); | 413 expect(element.returnType, isNull); |
402 expect(element.flags, 0); | 414 expect(element.flags, 0); |
403 } | 415 } |
404 | |
405 void test_fromElement_dynamic() { | |
406 var engineElement = engine.DynamicElementImpl.instance; | |
407 // create notification Element | |
408 Element element = newElement_fromEngine(engineElement); | |
409 expect(element.kind, ElementKind.UNKNOWN); | |
410 expect(element.name, 'dynamic'); | |
411 expect(element.location, isNull); | |
412 expect(element.parameters, isNull); | |
413 expect(element.returnType, isNull); | |
414 expect(element.flags, 0); | |
415 } | |
416 } | 416 } |
417 | 417 |
418 /** | |
419 * Helper class for testing the correspondence between an analysis engine enum | |
420 * and an analysis server API enum. | |
421 */ | |
422 class EnumTester<EngineEnum, ApiEnum extends Enum> { | |
423 /** | |
424 * Test that the function [convert] properly converts all possible values of | |
425 * [EngineEnum] to an [ApiEnum] with the same name, with the exceptions noted | |
426 * in [exceptions]. For each key in [exceptions], if the corresponding value | |
427 * is null, then we check that converting the given key results in an error. | |
428 * If the corresponding value is an [ApiEnum], then we check that converting | |
429 * the given key results in the given value. | |
430 */ | |
431 void run(ApiEnum convert(EngineEnum value), {Map<EngineEnum, | |
432 ApiEnum> exceptions: const {}}) { | |
433 ClassMirror engineClass = reflectClass(EngineEnum); | |
434 engineClass.staticMembers.forEach((Symbol symbol, MethodMirror method) { | |
435 if (symbol == #values) { | |
436 return; | |
437 } | |
438 if (!method.isGetter) { | |
439 return; | |
440 } | |
441 String enumName = MirrorSystem.getName(symbol); | |
442 EngineEnum engineValue = engineClass.getField(symbol).reflectee; | |
443 expect(engineValue, new isInstanceOf<EngineEnum>()); | |
444 if (exceptions.containsKey(engineValue)) { | |
445 ApiEnum expectedResult = exceptions[engineValue]; | |
446 if (expectedResult == null) { | |
447 expect(() { | |
448 convert(engineValue); | |
449 }, throws); | |
450 } else { | |
451 ApiEnum apiValue = convert(engineValue); | |
452 expect(apiValue, equals(expectedResult)); | |
453 } | |
454 } else { | |
455 ApiEnum apiValue = convert(engineValue); | |
456 expect(apiValue.name, equals(enumName)); | |
457 } | |
458 }); | |
459 } | |
460 } | |
461 | |
462 | |
463 @ReflectiveTestCase() | 418 @ReflectiveTestCase() |
464 class EnumTest { | 419 class EnumTest { |
465 void test_AnalysisErrorSeverity() { | 420 void test_AnalysisErrorSeverity() { |
466 new EnumTester<engine.ErrorSeverity, AnalysisErrorSeverity>().run( | 421 new EnumTester<engine.ErrorSeverity, AnalysisErrorSeverity>().run( |
467 (engine.ErrorSeverity engineErrorSeverity) => | 422 (engine.ErrorSeverity engineErrorSeverity) => |
468 new AnalysisErrorSeverity(engineErrorSeverity.name), | 423 new AnalysisErrorSeverity(engineErrorSeverity.name), |
469 exceptions: { | 424 exceptions: { |
470 engine.ErrorSeverity.NONE: null | 425 engine.ErrorSeverity.NONE: null |
471 }); | 426 }); |
472 } | 427 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 engine.ElementKind.UNIVERSE: ElementKind.UNKNOWN | 459 engine.ElementKind.UNIVERSE: ElementKind.UNKNOWN |
505 }); | 460 }); |
506 } | 461 } |
507 | 462 |
508 void test_SearchResultKind() { | 463 void test_SearchResultKind() { |
509 // TODO(paulberry): why does the MatchKind class exist at all? Can't we | 464 // TODO(paulberry): why does the MatchKind class exist at all? Can't we |
510 // use SearchResultKind inside the analysis server? | 465 // use SearchResultKind inside the analysis server? |
511 new EnumTester<MatchKind, SearchResultKind>().run( | 466 new EnumTester<MatchKind, SearchResultKind>().run( |
512 newSearchResultKind_fromEngine, | 467 newSearchResultKind_fromEngine, |
513 exceptions: { | 468 exceptions: { |
514 // TODO(paulberry): do any of the exceptions below constitute bugs? | 469 // TODO(paulberry): do any of the exceptions below constitute bugs? |
515 MatchKind.ANGULAR_REFERENCE: SearchResultKind.UNKNOWN, | 470 MatchKind.ANGULAR_REFERENCE: SearchResultKind.UNKNOWN, |
516 MatchKind.ANGULAR_CLOSING_TAG_REFERENCE: SearchResultKind.UNKNOWN | 471 MatchKind.ANGULAR_CLOSING_TAG_REFERENCE: SearchResultKind.UNKNOWN |
517 }); | 472 }); |
518 } | 473 } |
519 } | 474 } |
| 475 |
| 476 |
| 477 /** |
| 478 * Helper class for testing the correspondence between an analysis engine enum |
| 479 * and an analysis server API enum. |
| 480 */ |
| 481 class EnumTester<EngineEnum, ApiEnum extends Enum> { |
| 482 /** |
| 483 * Test that the function [convert] properly converts all possible values of |
| 484 * [EngineEnum] to an [ApiEnum] with the same name, with the exceptions noted |
| 485 * in [exceptions]. For each key in [exceptions], if the corresponding value |
| 486 * is null, then we check that converting the given key results in an error. |
| 487 * If the corresponding value is an [ApiEnum], then we check that converting |
| 488 * the given key results in the given value. |
| 489 */ |
| 490 void run(ApiEnum convert(EngineEnum value), {Map<EngineEnum, |
| 491 ApiEnum> exceptions: const {}}) { |
| 492 ClassMirror engineClass = reflectClass(EngineEnum); |
| 493 engineClass.staticMembers.forEach((Symbol symbol, MethodMirror method) { |
| 494 if (symbol == #values) { |
| 495 return; |
| 496 } |
| 497 if (!method.isGetter) { |
| 498 return; |
| 499 } |
| 500 String enumName = MirrorSystem.getName(symbol); |
| 501 EngineEnum engineValue = engineClass.getField(symbol).reflectee; |
| 502 expect(engineValue, new isInstanceOf<EngineEnum>()); |
| 503 if (exceptions.containsKey(engineValue)) { |
| 504 ApiEnum expectedResult = exceptions[engineValue]; |
| 505 if (expectedResult == null) { |
| 506 expect(() { |
| 507 convert(engineValue); |
| 508 }, throws); |
| 509 } else { |
| 510 ApiEnum apiValue = convert(engineValue); |
| 511 expect(apiValue, equals(expectedResult)); |
| 512 } |
| 513 } else { |
| 514 ApiEnum apiValue = convert(engineValue); |
| 515 expect(apiValue.name, equals(enumName)); |
| 516 } |
| 517 }); |
| 518 } |
| 519 } |
OLD | NEW |