| 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 'package:analysis_server/src/protocol.dart'; | 7 import 'package:analysis_server/src/protocol.dart'; |
| 8 import '../abstract_context.dart'; | |
| 9 import '../reflective_tests.dart'; | |
| 10 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 11 import 'package:analyzer/src/generated/element.dart' as engine; | 9 import 'package:analyzer/src/generated/element.dart' as engine; |
| 12 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; | 11 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; |
| 14 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 15 | 13 |
| 14 import '../abstract_context.dart'; |
| 15 import '../reflective_tests.dart'; |
| 16 |
| 16 | 17 |
| 17 | 18 |
| 18 main() { | 19 main() { |
| 19 groupSep = ' | '; | 20 groupSep = ' | '; |
| 20 runReflectiveTests(ElementTest); | 21 runReflectiveTests(ElementTest); |
| 21 runReflectiveTests(ElementKindTest); | 22 runReflectiveTests(ElementKindTest); |
| 22 } | 23 } |
| 23 | 24 |
| 24 | 25 |
| 25 class ElementKindTest { | 26 class ElementKindTest { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 39 expect( | 40 expect( |
| 40 new ElementKind.fromEngine(engine.ElementKind.FUNCTION), | 41 new ElementKind.fromEngine(engine.ElementKind.FUNCTION), |
| 41 ElementKind.FUNCTION); | 42 ElementKind.FUNCTION); |
| 42 expect( | 43 expect( |
| 43 new ElementKind.fromEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), | 44 new ElementKind.fromEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), |
| 44 ElementKind.FUNCTION_TYPE_ALIAS); | 45 ElementKind.FUNCTION_TYPE_ALIAS); |
| 45 expect( | 46 expect( |
| 46 new ElementKind.fromEngine(engine.ElementKind.GETTER), | 47 new ElementKind.fromEngine(engine.ElementKind.GETTER), |
| 47 ElementKind.GETTER); | 48 ElementKind.GETTER); |
| 48 expect( | 49 expect( |
| 50 new ElementKind.fromEngine(engine.ElementKind.LABEL), |
| 51 ElementKind.LABEL); |
| 52 expect( |
| 49 new ElementKind.fromEngine(engine.ElementKind.LIBRARY), | 53 new ElementKind.fromEngine(engine.ElementKind.LIBRARY), |
| 50 ElementKind.LIBRARY); | 54 ElementKind.LIBRARY); |
| 51 expect( | 55 expect( |
| 52 new ElementKind.fromEngine(engine.ElementKind.LOCAL_VARIABLE), | 56 new ElementKind.fromEngine(engine.ElementKind.LOCAL_VARIABLE), |
| 53 ElementKind.LOCAL_VARIABLE); | 57 ElementKind.LOCAL_VARIABLE); |
| 54 expect( | 58 expect( |
| 55 new ElementKind.fromEngine(engine.ElementKind.METHOD), | 59 new ElementKind.fromEngine(engine.ElementKind.METHOD), |
| 56 ElementKind.METHOD); | 60 ElementKind.METHOD); |
| 57 expect( | 61 expect( |
| 58 new ElementKind.fromEngine(engine.ElementKind.PARAMETER), | 62 new ElementKind.fromEngine(engine.ElementKind.PARAMETER), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 76 expect( | 80 expect( |
| 77 new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), | 81 new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), |
| 78 ElementKind.CLASS_TYPE_ALIAS); | 82 ElementKind.CLASS_TYPE_ALIAS); |
| 79 expect( | 83 expect( |
| 80 new ElementKind(ElementKind.COMPILATION_UNIT.name), | 84 new ElementKind(ElementKind.COMPILATION_UNIT.name), |
| 81 ElementKind.COMPILATION_UNIT); | 85 ElementKind.COMPILATION_UNIT); |
| 82 expect( | 86 expect( |
| 83 new ElementKind(ElementKind.CONSTRUCTOR.name), | 87 new ElementKind(ElementKind.CONSTRUCTOR.name), |
| 84 ElementKind.CONSTRUCTOR); | 88 ElementKind.CONSTRUCTOR); |
| 85 expect(new ElementKind(ElementKind.FIELD.name), ElementKind.FIELD); | 89 expect(new ElementKind(ElementKind.FIELD.name), ElementKind.FIELD); |
| 86 expect( | 90 expect(new ElementKind(ElementKind.FUNCTION.name), ElementKind.FUNCTION); |
| 87 new ElementKind(ElementKind.FUNCTION.name), | |
| 88 ElementKind.FUNCTION); | |
| 89 expect( | 91 expect( |
| 90 new ElementKind(ElementKind.FUNCTION_TYPE_ALIAS.name), | 92 new ElementKind(ElementKind.FUNCTION_TYPE_ALIAS.name), |
| 91 ElementKind.FUNCTION_TYPE_ALIAS); | 93 ElementKind.FUNCTION_TYPE_ALIAS); |
| 92 expect(new ElementKind(ElementKind.GETTER.name), ElementKind.GETTER); | 94 expect(new ElementKind(ElementKind.GETTER.name), ElementKind.GETTER); |
| 93 expect(new ElementKind(ElementKind.LIBRARY.name), ElementKind.LIBRARY); | 95 expect(new ElementKind(ElementKind.LIBRARY.name), ElementKind.LIBRARY); |
| 94 expect( | 96 expect( |
| 95 new ElementKind(ElementKind.LOCAL_VARIABLE.name), | 97 new ElementKind(ElementKind.LOCAL_VARIABLE.name), |
| 96 ElementKind.LOCAL_VARIABLE); | 98 ElementKind.LOCAL_VARIABLE); |
| 97 expect(new ElementKind(ElementKind.METHOD.name), ElementKind.METHOD); | 99 expect(new ElementKind(ElementKind.METHOD.name), ElementKind.METHOD); |
| 98 expect( | 100 expect(new ElementKind(ElementKind.PARAMETER.name), ElementKind.PARAMETER); |
| 99 new ElementKind(ElementKind.PARAMETER.name), | |
| 100 ElementKind.PARAMETER); | |
| 101 expect(new ElementKind(ElementKind.SETTER.name), ElementKind.SETTER); | 101 expect(new ElementKind(ElementKind.SETTER.name), ElementKind.SETTER); |
| 102 expect( | 102 expect( |
| 103 new ElementKind(ElementKind.TOP_LEVEL_VARIABLE.name), | 103 new ElementKind(ElementKind.TOP_LEVEL_VARIABLE.name), |
| 104 ElementKind.TOP_LEVEL_VARIABLE); | 104 ElementKind.TOP_LEVEL_VARIABLE); |
| 105 expect( | 105 expect( |
| 106 new ElementKind(ElementKind.TYPE_PARAMETER.name), | 106 new ElementKind(ElementKind.TYPE_PARAMETER.name), |
| 107 ElementKind.TYPE_PARAMETER); | 107 ElementKind.TYPE_PARAMETER); |
| 108 expect( | 108 expect( |
| 109 new ElementKind(ElementKind.UNIT_TEST_TEST.name), | 109 new ElementKind(ElementKind.UNIT_TEST_TEST.name), |
| 110 ElementKind.UNIT_TEST_TEST); | 110 ElementKind.UNIT_TEST_TEST); |
| 111 expect( | 111 expect( |
| 112 new ElementKind(ElementKind.UNIT_TEST_GROUP.name), | 112 new ElementKind(ElementKind.UNIT_TEST_GROUP.name), |
| 113 ElementKind.UNIT_TEST_GROUP); | 113 ElementKind.UNIT_TEST_GROUP); |
| 114 expect(new ElementKind(ElementKind.UNKNOWN.name), ElementKind.UNKNOWN); | 114 expect(new ElementKind(ElementKind.UNKNOWN.name), ElementKind.UNKNOWN); |
| 115 expect(() { | 115 expect(() { |
| 116 new ElementKind('no-such-kind'); | 116 new ElementKind('no-such-kind'); |
| 117 }, throws); | 117 }, throws); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void test_toString() { | 120 void test_toString() { |
| 121 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); | 121 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); |
| 122 expect(ElementKind.COMPILATION_UNIT.toString(), | 122 expect( |
| 123 ElementKind.COMPILATION_UNIT.toString(), |
| 123 'ElementKind.COMPILATION_UNIT'); | 124 'ElementKind.COMPILATION_UNIT'); |
| 124 } | 125 } |
| 125 } | 126 } |
| 126 | 127 |
| 127 | 128 |
| 128 @ReflectiveTestCase() | 129 @ReflectiveTestCase() |
| 129 class ElementTest extends AbstractContextTest { | 130 class ElementTest extends AbstractContextTest { |
| 130 engine.Element findElementInUnit(CompilationUnit unit, String name, | 131 engine.Element findElementInUnit(CompilationUnit unit, String name, |
| 131 [engine.ElementKind kind]) { | 132 [engine.ElementKind kind]) { |
| 132 return findChildElement(unit.element, name, kind); | 133 return findChildElement(unit.element, name, kind); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 expect(location.offset, 19); | 223 expect(location.offset, 19); |
| 223 expect(location.length, 'myGetter'.length); | 224 expect(location.length, 'myGetter'.length); |
| 224 expect(location.startLine, 2); | 225 expect(location.startLine, 2); |
| 225 expect(location.startColumn, 10); | 226 expect(location.startColumn, 10); |
| 226 } | 227 } |
| 227 expect(element.parameters, '()'); | 228 expect(element.parameters, '()'); |
| 228 expect(element.returnType, 'String'); | 229 expect(element.returnType, 'String'); |
| 229 expect(element.flags, 0); | 230 expect(element.flags, 0); |
| 230 } | 231 } |
| 231 | 232 |
| 233 void test_fromElement_LABEL() { |
| 234 Source source = addSource('/test.dart', ''' |
| 235 main() { |
| 236 myLabel: |
| 237 while (true) { |
| 238 break myLabel; |
| 239 } |
| 240 }'''); |
| 241 CompilationUnit unit = resolveLibraryUnit(source); |
| 242 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); |
| 243 // create notification Element |
| 244 Element element = new Element.fromEngine(engineElement); |
| 245 expect(element.kind, ElementKind.LABEL); |
| 246 expect(element.name, 'myLabel'); |
| 247 { |
| 248 Location location = element.location; |
| 249 expect(location.file, '/test.dart'); |
| 250 expect(location.offset, 9); |
| 251 expect(location.length, 'myLabel'.length); |
| 252 expect(location.startLine, 2); |
| 253 expect(location.startColumn, 1); |
| 254 } |
| 255 expect(element.parameters, isNull); |
| 256 expect(element.returnType, isNull); |
| 257 expect(element.flags, 0); |
| 258 } |
| 259 |
| 232 void test_fromElement_METHOD() { | 260 void test_fromElement_METHOD() { |
| 233 Source source = addSource('/test.dart', ''' | 261 Source source = addSource('/test.dart', ''' |
| 234 class A { | 262 class A { |
| 235 static List<String> myMethod(int a, {String b}) { | 263 static List<String> myMethod(int a, {String b}) { |
| 236 return null; | 264 return null; |
| 237 } | 265 } |
| 238 }'''); | 266 }'''); |
| 239 CompilationUnit unit = resolveLibraryUnit(source); | 267 CompilationUnit unit = resolveLibraryUnit(source); |
| 240 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | 268 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); |
| 241 // create notification Element | 269 // create notification Element |
| (...skipping 18 matching lines...) Expand all Loading... |
| 260 // create notification Element | 288 // create notification Element |
| 261 Element element = new Element.fromEngine(engineElement); | 289 Element element = new Element.fromEngine(engineElement); |
| 262 expect(element.kind, ElementKind.UNKNOWN); | 290 expect(element.kind, ElementKind.UNKNOWN); |
| 263 expect(element.name, 'dynamic'); | 291 expect(element.name, 'dynamic'); |
| 264 expect(element.location, isNull); | 292 expect(element.location, isNull); |
| 265 expect(element.parameters, isNull); | 293 expect(element.parameters, isNull); |
| 266 expect(element.returnType, isNull); | 294 expect(element.returnType, isNull); |
| 267 expect(element.flags, 0); | 295 expect(element.flags, 0); |
| 268 } | 296 } |
| 269 } | 297 } |
| OLD | NEW |