| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test.computer.element; | |
| 6 | |
| 7 import 'package:analysis_server/src/protocol_server.dart'; | |
| 8 import 'package:analyzer/src/generated/ast.dart'; | |
| 9 import 'package:analyzer/src/generated/element.dart' as engine; | |
| 10 import 'package:analyzer/src/generated/source.dart'; | |
| 11 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 import '../abstract_context.dart'; | |
| 15 import '../reflective_tests.dart'; | |
| 16 | |
| 17 | |
| 18 | |
| 19 main() { | |
| 20 groupSep = ' | '; | |
| 21 runReflectiveTests(ElementTest); | |
| 22 runReflectiveTests(ElementKindTest); | |
| 23 } | |
| 24 | |
| 25 | |
| 26 class ElementKindTest { | |
| 27 void test_fromEngine() { | |
| 28 expect( | |
| 29 newElementKind_fromEngine(engine.ElementKind.CLASS), | |
| 30 ElementKind.CLASS); | |
| 31 expect( | |
| 32 newElementKind_fromEngine(engine.ElementKind.COMPILATION_UNIT), | |
| 33 ElementKind.COMPILATION_UNIT); | |
| 34 expect( | |
| 35 newElementKind_fromEngine(engine.ElementKind.CONSTRUCTOR), | |
| 36 ElementKind.CONSTRUCTOR); | |
| 37 expect( | |
| 38 newElementKind_fromEngine(engine.ElementKind.FIELD), | |
| 39 ElementKind.FIELD); | |
| 40 expect( | |
| 41 newElementKind_fromEngine(engine.ElementKind.FUNCTION), | |
| 42 ElementKind.FUNCTION); | |
| 43 expect( | |
| 44 newElementKind_fromEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), | |
| 45 ElementKind.FUNCTION_TYPE_ALIAS); | |
| 46 expect( | |
| 47 newElementKind_fromEngine(engine.ElementKind.GETTER), | |
| 48 ElementKind.GETTER); | |
| 49 expect( | |
| 50 newElementKind_fromEngine(engine.ElementKind.LABEL), | |
| 51 ElementKind.LABEL); | |
| 52 expect( | |
| 53 newElementKind_fromEngine(engine.ElementKind.LIBRARY), | |
| 54 ElementKind.LIBRARY); | |
| 55 expect( | |
| 56 newElementKind_fromEngine(engine.ElementKind.LOCAL_VARIABLE), | |
| 57 ElementKind.LOCAL_VARIABLE); | |
| 58 expect( | |
| 59 newElementKind_fromEngine(engine.ElementKind.METHOD), | |
| 60 ElementKind.METHOD); | |
| 61 expect( | |
| 62 newElementKind_fromEngine(engine.ElementKind.PARAMETER), | |
| 63 ElementKind.PARAMETER); | |
| 64 expect( | |
| 65 newElementKind_fromEngine(engine.ElementKind.SETTER), | |
| 66 ElementKind.SETTER); | |
| 67 expect( | |
| 68 newElementKind_fromEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), | |
| 69 ElementKind.TOP_LEVEL_VARIABLE); | |
| 70 expect( | |
| 71 newElementKind_fromEngine(engine.ElementKind.TYPE_PARAMETER), | |
| 72 ElementKind.TYPE_PARAMETER); | |
| 73 expect( | |
| 74 newElementKind_fromEngine(engine.ElementKind.ANGULAR_COMPONENT), | |
| 75 ElementKind.UNKNOWN); | |
| 76 } | |
| 77 | |
| 78 void test_string_constructor() { | |
| 79 expect(new ElementKind(ElementKind.CLASS.name), ElementKind.CLASS); | |
| 80 expect( | |
| 81 new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), | |
| 82 ElementKind.CLASS_TYPE_ALIAS); | |
| 83 expect( | |
| 84 new ElementKind(ElementKind.COMPILATION_UNIT.name), | |
| 85 ElementKind.COMPILATION_UNIT); | |
| 86 expect( | |
| 87 new ElementKind(ElementKind.CONSTRUCTOR.name), | |
| 88 ElementKind.CONSTRUCTOR); | |
| 89 expect(new ElementKind(ElementKind.FIELD.name), ElementKind.FIELD); | |
| 90 expect(new ElementKind(ElementKind.FUNCTION.name), ElementKind.FUNCTION); | |
| 91 expect( | |
| 92 new ElementKind(ElementKind.FUNCTION_TYPE_ALIAS.name), | |
| 93 ElementKind.FUNCTION_TYPE_ALIAS); | |
| 94 expect(new ElementKind(ElementKind.GETTER.name), ElementKind.GETTER); | |
| 95 expect(new ElementKind(ElementKind.LIBRARY.name), ElementKind.LIBRARY); | |
| 96 expect( | |
| 97 new ElementKind(ElementKind.LOCAL_VARIABLE.name), | |
| 98 ElementKind.LOCAL_VARIABLE); | |
| 99 expect(new ElementKind(ElementKind.METHOD.name), ElementKind.METHOD); | |
| 100 expect(new ElementKind(ElementKind.PARAMETER.name), ElementKind.PARAMETER); | |
| 101 expect(new ElementKind(ElementKind.SETTER.name), ElementKind.SETTER); | |
| 102 expect( | |
| 103 new ElementKind(ElementKind.TOP_LEVEL_VARIABLE.name), | |
| 104 ElementKind.TOP_LEVEL_VARIABLE); | |
| 105 expect( | |
| 106 new ElementKind(ElementKind.TYPE_PARAMETER.name), | |
| 107 ElementKind.TYPE_PARAMETER); | |
| 108 expect( | |
| 109 new ElementKind(ElementKind.UNIT_TEST_TEST.name), | |
| 110 ElementKind.UNIT_TEST_TEST); | |
| 111 expect( | |
| 112 new ElementKind(ElementKind.UNIT_TEST_GROUP.name), | |
| 113 ElementKind.UNIT_TEST_GROUP); | |
| 114 expect(new ElementKind(ElementKind.UNKNOWN.name), ElementKind.UNKNOWN); | |
| 115 expect(() { | |
| 116 new ElementKind('no-such-kind'); | |
| 117 }, throws); | |
| 118 } | |
| 119 | |
| 120 void test_toString() { | |
| 121 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); | |
| 122 expect( | |
| 123 ElementKind.COMPILATION_UNIT.toString(), | |
| 124 'ElementKind.COMPILATION_UNIT'); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 | |
| 129 @ReflectiveTestCase() | |
| 130 class ElementTest extends AbstractContextTest { | |
| 131 engine.Element findElementInUnit(CompilationUnit unit, String name, | |
| 132 [engine.ElementKind kind]) { | |
| 133 return findChildElement(unit.element, name, kind); | |
| 134 } | |
| 135 | |
| 136 void test_fromElement_CLASS() { | |
| 137 Source source = addSource('/test.dart', ''' | |
| 138 @deprecated | |
| 139 abstract class _MyClass {}'''); | |
| 140 CompilationUnit unit = resolveLibraryUnit(source); | |
| 141 engine.ClassElement engineElement = findElementInUnit(unit, '_MyClass'); | |
| 142 // create notification Element | |
| 143 Element element = newElement_fromEngine(engineElement); | |
| 144 expect(element.kind, ElementKind.CLASS); | |
| 145 expect(element.name, '_MyClass'); | |
| 146 { | |
| 147 Location location = element.location; | |
| 148 expect(location.file, '/test.dart'); | |
| 149 expect(location.offset, 27); | |
| 150 expect(location.length, '_MyClass'.length); | |
| 151 expect(location.startLine, 2); | |
| 152 expect(location.startColumn, 16); | |
| 153 } | |
| 154 expect( | |
| 155 element.flags, | |
| 156 Element.FLAG_ABSTRACT | Element.FLAG_DEPRECATED | Element.FLAG_PRIVATE); | |
| 157 } | |
| 158 | |
| 159 void test_fromElement_CONSTRUCTOR() { | |
| 160 Source source = addSource('/test.dart', ''' | |
| 161 class A { | |
| 162 const A.myConstructor(int a, [String b]); | |
| 163 }'''); | |
| 164 CompilationUnit unit = resolveLibraryUnit(source); | |
| 165 engine.ConstructorElement engineElement = | |
| 166 findElementInUnit(unit, 'myConstructor'); | |
| 167 // create notification Element | |
| 168 Element element = newElement_fromEngine(engineElement); | |
| 169 expect(element.kind, ElementKind.CONSTRUCTOR); | |
| 170 expect(element.name, 'myConstructor'); | |
| 171 { | |
| 172 Location location = element.location; | |
| 173 expect(location.file, '/test.dart'); | |
| 174 expect(location.offset, 20); | |
| 175 expect(location.length, 'myConstructor'.length); | |
| 176 expect(location.startLine, 2); | |
| 177 expect(location.startColumn, 11); | |
| 178 } | |
| 179 expect(element.parameters, '(int a, [String b])'); | |
| 180 expect(element.returnType, 'A'); | |
| 181 expect(element.flags, Element.FLAG_CONST); | |
| 182 } | |
| 183 | |
| 184 void test_fromElement_FIELD() { | |
| 185 Source source = addSource('/test.dart', ''' | |
| 186 class A { | |
| 187 static const myField = 42; | |
| 188 }'''); | |
| 189 CompilationUnit unit = resolveLibraryUnit(source); | |
| 190 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | |
| 191 // create notification Element | |
| 192 Element element = newElement_fromEngine(engineElement); | |
| 193 expect(element.kind, ElementKind.FIELD); | |
| 194 expect(element.name, 'myField'); | |
| 195 { | |
| 196 Location location = element.location; | |
| 197 expect(location.file, '/test.dart'); | |
| 198 expect(location.offset, 25); | |
| 199 expect(location.length, 'myField'.length); | |
| 200 expect(location.startLine, 2); | |
| 201 expect(location.startColumn, 16); | |
| 202 } | |
| 203 expect(element.parameters, isNull); | |
| 204 expect(element.returnType, isNull); | |
| 205 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | |
| 206 } | |
| 207 | |
| 208 void test_fromElement_GETTER() { | |
| 209 Source source = addSource('/test.dart', ''' | |
| 210 class A { | |
| 211 String myGetter => 42; | |
| 212 }'''); | |
| 213 CompilationUnit unit = resolveLibraryUnit(source); | |
| 214 engine.PropertyAccessorElement engineElement = | |
| 215 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); | |
| 216 // create notification Element | |
| 217 Element element = newElement_fromEngine(engineElement); | |
| 218 expect(element.kind, ElementKind.GETTER); | |
| 219 expect(element.name, 'myGetter'); | |
| 220 { | |
| 221 Location location = element.location; | |
| 222 expect(location.file, '/test.dart'); | |
| 223 expect(location.offset, 19); | |
| 224 expect(location.length, 'myGetter'.length); | |
| 225 expect(location.startLine, 2); | |
| 226 expect(location.startColumn, 10); | |
| 227 } | |
| 228 expect(element.parameters, '()'); | |
| 229 expect(element.returnType, 'String'); | |
| 230 expect(element.flags, 0); | |
| 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 = newElement_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 | |
| 260 void test_fromElement_METHOD() { | |
| 261 Source source = addSource('/test.dart', ''' | |
| 262 class A { | |
| 263 static List<String> myMethod(int a, {String b}) { | |
| 264 return null; | |
| 265 } | |
| 266 }'''); | |
| 267 CompilationUnit unit = resolveLibraryUnit(source); | |
| 268 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | |
| 269 // create notification Element | |
| 270 Element element = newElement_fromEngine(engineElement); | |
| 271 expect(element.kind, ElementKind.METHOD); | |
| 272 expect(element.name, 'myMethod'); | |
| 273 { | |
| 274 Location location = element.location; | |
| 275 expect(location.file, '/test.dart'); | |
| 276 expect(location.offset, 32); | |
| 277 expect(location.length, 'myGetter'.length); | |
| 278 expect(location.startLine, 2); | |
| 279 expect(location.startColumn, 23); | |
| 280 } | |
| 281 expect(element.parameters, '(int a, {String b})'); | |
| 282 expect(element.returnType, 'List<String>'); | |
| 283 expect(element.flags, Element.FLAG_STATIC); | |
| 284 } | |
| 285 | |
| 286 void test_fromElement_SETTER() { | |
| 287 Source source = addSource('/test.dart', ''' | |
| 288 class A { | |
| 289 set mySetter(String x) {} | |
| 290 }'''); | |
| 291 CompilationUnit unit = resolveLibraryUnit(source); | |
| 292 engine.FieldElement engineFieldElement = | |
| 293 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); | |
| 294 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; | |
| 295 // create notification Element | |
| 296 Element element = newElement_fromEngine(engineElement); | |
| 297 expect(element.kind, ElementKind.SETTER); | |
| 298 expect(element.name, 'mySetter'); | |
| 299 { | |
| 300 Location location = element.location; | |
| 301 expect(location.file, '/test.dart'); | |
| 302 expect(location.offset, 16); | |
| 303 expect(location.length, 'mySetter'.length); | |
| 304 expect(location.startLine, 2); | |
| 305 expect(location.startColumn, 7); | |
| 306 } | |
| 307 expect(element.parameters, '(String x)'); | |
| 308 expect(element.returnType, isNull); | |
| 309 expect(element.flags, 0); | |
| 310 } | |
| 311 | |
| 312 void test_fromElement_dynamic() { | |
| 313 var engineElement = engine.DynamicElementImpl.instance; | |
| 314 // create notification Element | |
| 315 Element element = newElement_fromEngine(engineElement); | |
| 316 expect(element.kind, ElementKind.UNKNOWN); | |
| 317 expect(element.name, 'dynamic'); | |
| 318 expect(element.location, isNull); | |
| 319 expect(element.parameters, isNull); | |
| 320 expect(element.returnType, isNull); | |
| 321 expect(element.flags, 0); | |
| 322 } | |
| 323 } | |
| OLD | NEW |