| 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'; |
| 11 import 'package:analysis_server/src/services/search/search_engine.dart'; | 11 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 12 import 'package:analyzer/src/generated/ast.dart' as engine; | 12 import 'package:analyzer/src/generated/ast.dart' as engine; |
| 13 import 'package:analyzer/src/generated/element.dart' as engine; | 13 import 'package:analyzer/src/generated/element.dart' as engine; |
| 14 import 'package:analyzer/src/generated/error.dart' as engine; | 14 import 'package:analyzer/src/generated/error.dart' as engine; |
| 15 import 'package:analyzer/src/generated/source.dart' as engine; | 15 import 'package:analyzer/src/generated/source.dart' as engine; |
| 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 17 import 'package:typed_mock/typed_mock.dart'; | 17 import 'package:typed_mock/typed_mock.dart'; |
| 18 import 'package:unittest/unittest.dart'; | 18 import 'package:unittest/unittest.dart'; |
| 19 | 19 |
| 20 import 'abstract_context.dart'; | |
| 21 import 'mocks.dart'; | 20 import 'mocks.dart'; |
| 22 import 'utils.dart'; | 21 import 'utils.dart'; |
| 23 | 22 |
| 24 main() { | 23 main() { |
| 25 initializeTestEnvironment(); | 24 initializeTestEnvironment(); |
| 26 defineReflectiveTests(AnalysisErrorTest); | 25 defineReflectiveTests(AnalysisErrorTest); |
| 27 defineReflectiveTests(ElementTest); | |
| 28 defineReflectiveTests(ElementKindTest); | |
| 29 defineReflectiveTests(EnumTest); | 26 defineReflectiveTests(EnumTest); |
| 30 } | 27 } |
| 31 | 28 |
| 32 class AnalysisErrorMock extends TypedMock implements engine.AnalysisError { | 29 class AnalysisErrorMock extends TypedMock implements engine.AnalysisError { |
| 33 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 30 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 34 } | 31 } |
| 35 | 32 |
| 36 @reflectiveTest | 33 @reflectiveTest |
| 37 class AnalysisErrorTest { | 34 class AnalysisErrorTest { |
| 38 engine.Source source = new MockSource(); | 35 engine.Source source = new MockSource(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 66 TYPE: 'COMPILE_TIME_ERROR', | 63 TYPE: 'COMPILE_TIME_ERROR', |
| 67 LOCATION: { | 64 LOCATION: { |
| 68 FILE: 'foo.dart', | 65 FILE: 'foo.dart', |
| 69 OFFSET: 10, | 66 OFFSET: 10, |
| 70 LENGTH: 20, | 67 LENGTH: 20, |
| 71 START_LINE: 3, | 68 START_LINE: 3, |
| 72 START_COLUMN: 2 | 69 START_COLUMN: 2 |
| 73 }, | 70 }, |
| 74 MESSAGE: 'my message', | 71 MESSAGE: 'my message', |
| 75 CORRECTION: 'my correction', | 72 CORRECTION: 'my correction', |
| 76 HAS_FIX : false | 73 HAS_FIX: false |
| 77 }); | 74 }); |
| 78 } | 75 } |
| 79 | 76 |
| 80 void test_fromEngine_noCorrection() { | 77 void test_fromEngine_noCorrection() { |
| 81 when(engineError.correction).thenReturn(null); | 78 when(engineError.correction).thenReturn(null); |
| 82 AnalysisError error = newAnalysisError_fromEngine(lineInfo, engineError); | 79 AnalysisError error = newAnalysisError_fromEngine(lineInfo, engineError); |
| 83 expect(error.toJson(), { | 80 expect(error.toJson(), { |
| 84 SEVERITY: 'ERROR', | 81 SEVERITY: 'ERROR', |
| 85 TYPE: 'COMPILE_TIME_ERROR', | 82 TYPE: 'COMPILE_TIME_ERROR', |
| 86 LOCATION: { | 83 LOCATION: { |
| 87 FILE: 'foo.dart', | 84 FILE: 'foo.dart', |
| 88 OFFSET: 10, | 85 OFFSET: 10, |
| 89 LENGTH: 20, | 86 LENGTH: 20, |
| 90 START_LINE: 3, | 87 START_LINE: 3, |
| 91 START_COLUMN: 2 | 88 START_COLUMN: 2 |
| 92 }, | 89 }, |
| 93 MESSAGE: 'my message', | 90 MESSAGE: 'my message', |
| 94 HAS_FIX : false | 91 HAS_FIX: false |
| 95 }); | 92 }); |
| 96 } | 93 } |
| 97 | 94 |
| 98 void test_fromEngine_noLineInfo() { | 95 void test_fromEngine_noLineInfo() { |
| 99 when(engineError.correction).thenReturn(null); | 96 when(engineError.correction).thenReturn(null); |
| 100 AnalysisError error = newAnalysisError_fromEngine(null, engineError); | 97 AnalysisError error = newAnalysisError_fromEngine(null, engineError); |
| 101 expect(error.toJson(), { | 98 expect(error.toJson(), { |
| 102 SEVERITY: 'ERROR', | 99 SEVERITY: 'ERROR', |
| 103 TYPE: 'COMPILE_TIME_ERROR', | 100 TYPE: 'COMPILE_TIME_ERROR', |
| 104 LOCATION: { | 101 LOCATION: { |
| 105 FILE: 'foo.dart', | 102 FILE: 'foo.dart', |
| 106 OFFSET: 10, | 103 OFFSET: 10, |
| 107 LENGTH: 20, | 104 LENGTH: 20, |
| 108 START_LINE: -1, | 105 START_LINE: -1, |
| 109 START_COLUMN: -1 | 106 START_COLUMN: -1 |
| 110 }, | 107 }, |
| 111 MESSAGE: 'my message', | 108 MESSAGE: 'my message', |
| 112 HAS_FIX : false | 109 HAS_FIX: false |
| 113 }); | 110 }); |
| 114 } | 111 } |
| 115 } | 112 } |
| 116 | 113 |
| 117 @reflectiveTest | 114 @reflectiveTest |
| 118 class ElementKindTest { | |
| 119 void test_fromEngine() { | |
| 120 expect( | |
| 121 newElementKind_fromEngine(engine.ElementKind.CLASS), ElementKind.CLASS); | |
| 122 expect(newElementKind_fromEngine(engine.ElementKind.COMPILATION_UNIT), | |
| 123 ElementKind.COMPILATION_UNIT); | |
| 124 expect(newElementKind_fromEngine(engine.ElementKind.CONSTRUCTOR), | |
| 125 ElementKind.CONSTRUCTOR); | |
| 126 expect( | |
| 127 newElementKind_fromEngine(engine.ElementKind.FIELD), ElementKind.FIELD); | |
| 128 expect(newElementKind_fromEngine(engine.ElementKind.FUNCTION), | |
| 129 ElementKind.FUNCTION); | |
| 130 expect(newElementKind_fromEngine(engine.ElementKind.FUNCTION_TYPE_ALIAS), | |
| 131 ElementKind.FUNCTION_TYPE_ALIAS); | |
| 132 expect(newElementKind_fromEngine(engine.ElementKind.GETTER), | |
| 133 ElementKind.GETTER); | |
| 134 expect( | |
| 135 newElementKind_fromEngine(engine.ElementKind.LABEL), ElementKind.LABEL); | |
| 136 expect(newElementKind_fromEngine(engine.ElementKind.LIBRARY), | |
| 137 ElementKind.LIBRARY); | |
| 138 expect(newElementKind_fromEngine(engine.ElementKind.LOCAL_VARIABLE), | |
| 139 ElementKind.LOCAL_VARIABLE); | |
| 140 expect(newElementKind_fromEngine(engine.ElementKind.METHOD), | |
| 141 ElementKind.METHOD); | |
| 142 expect(newElementKind_fromEngine(engine.ElementKind.PARAMETER), | |
| 143 ElementKind.PARAMETER); | |
| 144 expect(newElementKind_fromEngine(engine.ElementKind.SETTER), | |
| 145 ElementKind.SETTER); | |
| 146 expect(newElementKind_fromEngine(engine.ElementKind.TOP_LEVEL_VARIABLE), | |
| 147 ElementKind.TOP_LEVEL_VARIABLE); | |
| 148 expect(newElementKind_fromEngine(engine.ElementKind.TYPE_PARAMETER), | |
| 149 ElementKind.TYPE_PARAMETER); | |
| 150 } | |
| 151 | |
| 152 void test_string_constructor() { | |
| 153 expect(new ElementKind(ElementKind.CLASS.name), ElementKind.CLASS); | |
| 154 expect(new ElementKind(ElementKind.CLASS_TYPE_ALIAS.name), | |
| 155 ElementKind.CLASS_TYPE_ALIAS); | |
| 156 expect(new ElementKind(ElementKind.COMPILATION_UNIT.name), | |
| 157 ElementKind.COMPILATION_UNIT); | |
| 158 expect( | |
| 159 new ElementKind(ElementKind.CONSTRUCTOR.name), ElementKind.CONSTRUCTOR); | |
| 160 expect(new ElementKind(ElementKind.FIELD.name), ElementKind.FIELD); | |
| 161 expect(new ElementKind(ElementKind.FUNCTION.name), ElementKind.FUNCTION); | |
| 162 expect(new ElementKind(ElementKind.FUNCTION_TYPE_ALIAS.name), | |
| 163 ElementKind.FUNCTION_TYPE_ALIAS); | |
| 164 expect(new ElementKind(ElementKind.GETTER.name), ElementKind.GETTER); | |
| 165 expect(new ElementKind(ElementKind.LIBRARY.name), ElementKind.LIBRARY); | |
| 166 expect(new ElementKind(ElementKind.LOCAL_VARIABLE.name), | |
| 167 ElementKind.LOCAL_VARIABLE); | |
| 168 expect(new ElementKind(ElementKind.METHOD.name), ElementKind.METHOD); | |
| 169 expect(new ElementKind(ElementKind.PARAMETER.name), ElementKind.PARAMETER); | |
| 170 expect(new ElementKind(ElementKind.SETTER.name), ElementKind.SETTER); | |
| 171 expect(new ElementKind(ElementKind.TOP_LEVEL_VARIABLE.name), | |
| 172 ElementKind.TOP_LEVEL_VARIABLE); | |
| 173 expect(new ElementKind(ElementKind.TYPE_PARAMETER.name), | |
| 174 ElementKind.TYPE_PARAMETER); | |
| 175 expect(new ElementKind(ElementKind.UNIT_TEST_TEST.name), | |
| 176 ElementKind.UNIT_TEST_TEST); | |
| 177 expect(new ElementKind(ElementKind.UNIT_TEST_GROUP.name), | |
| 178 ElementKind.UNIT_TEST_GROUP); | |
| 179 expect(new ElementKind(ElementKind.UNKNOWN.name), ElementKind.UNKNOWN); | |
| 180 expect(() { | |
| 181 new ElementKind('no-such-kind'); | |
| 182 }, throws); | |
| 183 } | |
| 184 | |
| 185 void test_toString() { | |
| 186 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); | |
| 187 expect(ElementKind.COMPILATION_UNIT.toString(), | |
| 188 'ElementKind.COMPILATION_UNIT'); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 @reflectiveTest | |
| 193 class ElementTest extends AbstractContextTest { | |
| 194 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, | |
| 195 [engine.ElementKind kind]) { | |
| 196 return findChildElement(unit.element, name, kind); | |
| 197 } | |
| 198 | |
| 199 void test_fromElement_CLASS() { | |
| 200 engine.Source source = addSource( | |
| 201 '/test.dart', | |
| 202 ''' | |
| 203 @deprecated | |
| 204 abstract class _A {} | |
| 205 class B<K, V> {}'''); | |
| 206 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 207 { | |
| 208 engine.ClassElement engineElement = findElementInUnit(unit, '_A'); | |
| 209 // create notification Element | |
| 210 Element element = newElement_fromEngine(engineElement); | |
| 211 expect(element.kind, ElementKind.CLASS); | |
| 212 expect(element.name, '_A'); | |
| 213 expect(element.typeParameters, isNull); | |
| 214 { | |
| 215 Location location = element.location; | |
| 216 expect(location.file, '/test.dart'); | |
| 217 expect(location.offset, 27); | |
| 218 expect(location.length, '_A'.length); | |
| 219 expect(location.startLine, 2); | |
| 220 expect(location.startColumn, 16); | |
| 221 } | |
| 222 expect(element.parameters, isNull); | |
| 223 expect( | |
| 224 element.flags, | |
| 225 Element.FLAG_ABSTRACT | | |
| 226 Element.FLAG_DEPRECATED | | |
| 227 Element.FLAG_PRIVATE); | |
| 228 } | |
| 229 { | |
| 230 engine.ClassElement engineElement = findElementInUnit(unit, 'B'); | |
| 231 // create notification Element | |
| 232 Element element = newElement_fromEngine(engineElement); | |
| 233 expect(element.kind, ElementKind.CLASS); | |
| 234 expect(element.name, 'B'); | |
| 235 expect(element.typeParameters, '<K, V>'); | |
| 236 expect(element.flags, 0); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 void test_fromElement_CONSTRUCTOR() { | |
| 241 engine.Source source = addSource( | |
| 242 '/test.dart', | |
| 243 ''' | |
| 244 class A { | |
| 245 const A.myConstructor(int a, [String b]); | |
| 246 }'''); | |
| 247 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 248 engine.ConstructorElement engineElement = | |
| 249 findElementInUnit(unit, 'myConstructor'); | |
| 250 // create notification Element | |
| 251 Element element = newElement_fromEngine(engineElement); | |
| 252 expect(element.kind, ElementKind.CONSTRUCTOR); | |
| 253 expect(element.name, 'myConstructor'); | |
| 254 expect(element.typeParameters, isNull); | |
| 255 { | |
| 256 Location location = element.location; | |
| 257 expect(location.file, '/test.dart'); | |
| 258 expect(location.offset, 20); | |
| 259 expect(location.length, 'myConstructor'.length); | |
| 260 expect(location.startLine, 2); | |
| 261 expect(location.startColumn, 11); | |
| 262 } | |
| 263 expect(element.parameters, '(int a, [String b])'); | |
| 264 expect(element.returnType, 'A'); | |
| 265 expect(element.flags, Element.FLAG_CONST); | |
| 266 } | |
| 267 | |
| 268 void test_fromElement_dynamic() { | |
| 269 var engineElement = engine.DynamicElementImpl.instance; | |
| 270 // create notification Element | |
| 271 Element element = newElement_fromEngine(engineElement); | |
| 272 expect(element.kind, ElementKind.UNKNOWN); | |
| 273 expect(element.name, 'dynamic'); | |
| 274 expect(element.location, isNull); | |
| 275 expect(element.parameters, isNull); | |
| 276 expect(element.returnType, isNull); | |
| 277 expect(element.flags, 0); | |
| 278 } | |
| 279 | |
| 280 void test_fromElement_ENUM() { | |
| 281 engine.Source source = addSource( | |
| 282 '/test.dart', | |
| 283 ''' | |
| 284 @deprecated | |
| 285 enum _E1 { one, two } | |
| 286 enum E2 { three, four }'''); | |
| 287 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 288 { | |
| 289 engine.ClassElement engineElement = findElementInUnit(unit, '_E1'); | |
| 290 expect(engineElement.isDeprecated, isTrue); | |
| 291 // create notification Element | |
| 292 Element element = newElement_fromEngine(engineElement); | |
| 293 expect(element.kind, ElementKind.ENUM); | |
| 294 expect(element.name, '_E1'); | |
| 295 expect(element.typeParameters, isNull); | |
| 296 { | |
| 297 Location location = element.location; | |
| 298 expect(location.file, '/test.dart'); | |
| 299 expect(location.offset, 17); | |
| 300 expect(location.length, '_E1'.length); | |
| 301 expect(location.startLine, 2); | |
| 302 expect(location.startColumn, 6); | |
| 303 } | |
| 304 expect(element.parameters, isNull); | |
| 305 expect( | |
| 306 element.flags, | |
| 307 (engineElement.isDeprecated ? Element.FLAG_DEPRECATED : 0) | | |
| 308 Element.FLAG_PRIVATE); | |
| 309 } | |
| 310 { | |
| 311 engine.ClassElement engineElement = findElementInUnit(unit, 'E2'); | |
| 312 // create notification Element | |
| 313 Element element = newElement_fromEngine(engineElement); | |
| 314 expect(element.kind, ElementKind.ENUM); | |
| 315 expect(element.name, 'E2'); | |
| 316 expect(element.typeParameters, isNull); | |
| 317 expect(element.flags, 0); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 void test_fromElement_ENUM_CONSTANT() { | |
| 322 engine.Source source = addSource( | |
| 323 '/test.dart', | |
| 324 ''' | |
| 325 @deprecated | |
| 326 enum _E1 { one, two } | |
| 327 enum E2 { three, four }'''); | |
| 328 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 329 { | |
| 330 engine.FieldElement engineElement = findElementInUnit(unit, 'one'); | |
| 331 // create notification Element | |
| 332 Element element = newElement_fromEngine(engineElement); | |
| 333 expect(element.kind, ElementKind.ENUM_CONSTANT); | |
| 334 expect(element.name, 'one'); | |
| 335 { | |
| 336 Location location = element.location; | |
| 337 expect(location.file, '/test.dart'); | |
| 338 expect(location.offset, 23); | |
| 339 expect(location.length, 'one'.length); | |
| 340 expect(location.startLine, 2); | |
| 341 expect(location.startColumn, 12); | |
| 342 } | |
| 343 expect(element.parameters, isNull); | |
| 344 expect(element.returnType, '_E1'); | |
| 345 // TODO(danrubel) determine why enum constant is not marked as deprecated | |
| 346 //engine.ClassElement classElement = engineElement.enclosingElement; | |
| 347 //expect(classElement.isDeprecated, isTrue); | |
| 348 expect( | |
| 349 element.flags, | |
| 350 // Element.FLAG_DEPRECATED | | |
| 351 Element.FLAG_CONST | Element.FLAG_STATIC); | |
| 352 } | |
| 353 { | |
| 354 engine.FieldElement engineElement = findElementInUnit(unit, 'three'); | |
| 355 // create notification Element | |
| 356 Element element = newElement_fromEngine(engineElement); | |
| 357 expect(element.kind, ElementKind.ENUM_CONSTANT); | |
| 358 expect(element.name, 'three'); | |
| 359 { | |
| 360 Location location = element.location; | |
| 361 expect(location.file, '/test.dart'); | |
| 362 expect(location.offset, 44); | |
| 363 expect(location.length, 'three'.length); | |
| 364 expect(location.startLine, 3); | |
| 365 expect(location.startColumn, 11); | |
| 366 } | |
| 367 expect(element.parameters, isNull); | |
| 368 expect(element.returnType, 'E2'); | |
| 369 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | |
| 370 } | |
| 371 { | |
| 372 engine.FieldElement engineElement = findElementInUnit(unit, 'index'); | |
| 373 // create notification Element | |
| 374 Element element = newElement_fromEngine(engineElement); | |
| 375 expect(element.kind, ElementKind.FIELD); | |
| 376 expect(element.name, 'index'); | |
| 377 { | |
| 378 Location location = element.location; | |
| 379 expect(location.file, '/test.dart'); | |
| 380 expect(location.offset, -1); | |
| 381 expect(location.length, 'index'.length); | |
| 382 expect(location.startLine, 1); | |
| 383 expect(location.startColumn, 0); | |
| 384 } | |
| 385 expect(element.parameters, isNull); | |
| 386 expect(element.returnType, 'int'); | |
| 387 expect(element.flags, Element.FLAG_FINAL); | |
| 388 } | |
| 389 { | |
| 390 engine.FieldElement engineElement = findElementInUnit(unit, 'values'); | |
| 391 // create notification Element | |
| 392 Element element = newElement_fromEngine(engineElement); | |
| 393 expect(element.kind, ElementKind.FIELD); | |
| 394 expect(element.name, 'values'); | |
| 395 { | |
| 396 Location location = element.location; | |
| 397 expect(location.file, '/test.dart'); | |
| 398 expect(location.offset, -1); | |
| 399 expect(location.length, 'values'.length); | |
| 400 expect(location.startLine, 1); | |
| 401 expect(location.startColumn, 0); | |
| 402 } | |
| 403 expect(element.parameters, isNull); | |
| 404 expect(element.returnType, 'List<E2>'); | |
| 405 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 void test_fromElement_FIELD() { | |
| 410 engine.Source source = addSource( | |
| 411 '/test.dart', | |
| 412 ''' | |
| 413 class A { | |
| 414 static const myField = 42; | |
| 415 }'''); | |
| 416 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 417 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | |
| 418 // create notification Element | |
| 419 Element element = newElement_fromEngine(engineElement); | |
| 420 expect(element.kind, ElementKind.FIELD); | |
| 421 expect(element.name, 'myField'); | |
| 422 { | |
| 423 Location location = element.location; | |
| 424 expect(location.file, '/test.dart'); | |
| 425 expect(location.offset, 25); | |
| 426 expect(location.length, 'myField'.length); | |
| 427 expect(location.startLine, 2); | |
| 428 expect(location.startColumn, 16); | |
| 429 } | |
| 430 expect(element.parameters, isNull); | |
| 431 expect(element.returnType, 'dynamic'); | |
| 432 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | |
| 433 } | |
| 434 | |
| 435 void test_fromElement_FUNCTION_TYPE_ALIAS() { | |
| 436 engine.Source source = addSource( | |
| 437 '/test.dart', | |
| 438 ''' | |
| 439 typedef int F<T>(String x); | |
| 440 '''); | |
| 441 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 442 engine.FunctionTypeAliasElement engineElement = | |
| 443 findElementInUnit(unit, 'F'); | |
| 444 // create notification Element | |
| 445 Element element = newElement_fromEngine(engineElement); | |
| 446 expect(element.kind, ElementKind.FUNCTION_TYPE_ALIAS); | |
| 447 expect(element.name, 'F'); | |
| 448 expect(element.typeParameters, '<T>'); | |
| 449 { | |
| 450 Location location = element.location; | |
| 451 expect(location.file, '/test.dart'); | |
| 452 expect(location.offset, 12); | |
| 453 expect(location.length, 'F'.length); | |
| 454 expect(location.startLine, 1); | |
| 455 expect(location.startColumn, 13); | |
| 456 } | |
| 457 expect(element.parameters, '(String x)'); | |
| 458 expect(element.returnType, 'int'); | |
| 459 expect(element.flags, 0); | |
| 460 } | |
| 461 | |
| 462 void test_fromElement_GETTER() { | |
| 463 engine.Source source = addSource( | |
| 464 '/test.dart', | |
| 465 ''' | |
| 466 class A { | |
| 467 String get myGetter => 42; | |
| 468 }'''); | |
| 469 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 470 engine.PropertyAccessorElement engineElement = | |
| 471 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); | |
| 472 // create notification Element | |
| 473 Element element = newElement_fromEngine(engineElement); | |
| 474 expect(element.kind, ElementKind.GETTER); | |
| 475 expect(element.name, 'myGetter'); | |
| 476 { | |
| 477 Location location = element.location; | |
| 478 expect(location.file, '/test.dart'); | |
| 479 expect(location.offset, 23); | |
| 480 expect(location.length, 'myGetter'.length); | |
| 481 expect(location.startLine, 2); | |
| 482 expect(location.startColumn, 14); | |
| 483 } | |
| 484 expect(element.parameters, isNull); | |
| 485 expect(element.returnType, 'String'); | |
| 486 expect(element.flags, 0); | |
| 487 } | |
| 488 | |
| 489 void test_fromElement_LABEL() { | |
| 490 engine.Source source = addSource( | |
| 491 '/test.dart', | |
| 492 ''' | |
| 493 main() { | |
| 494 myLabel: | |
| 495 while (true) { | |
| 496 break myLabel; | |
| 497 } | |
| 498 }'''); | |
| 499 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 500 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); | |
| 501 // create notification Element | |
| 502 Element element = newElement_fromEngine(engineElement); | |
| 503 expect(element.kind, ElementKind.LABEL); | |
| 504 expect(element.name, 'myLabel'); | |
| 505 { | |
| 506 Location location = element.location; | |
| 507 expect(location.file, '/test.dart'); | |
| 508 expect(location.offset, 9); | |
| 509 expect(location.length, 'myLabel'.length); | |
| 510 expect(location.startLine, 2); | |
| 511 expect(location.startColumn, 1); | |
| 512 } | |
| 513 expect(element.parameters, isNull); | |
| 514 expect(element.returnType, isNull); | |
| 515 expect(element.flags, 0); | |
| 516 } | |
| 517 | |
| 518 void test_fromElement_METHOD() { | |
| 519 engine.Source source = addSource( | |
| 520 '/test.dart', | |
| 521 ''' | |
| 522 class A { | |
| 523 static List<String> myMethod(int a, {String b, int c}) { | |
| 524 return null; | |
| 525 } | |
| 526 }'''); | |
| 527 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 528 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | |
| 529 // create notification Element | |
| 530 Element element = newElement_fromEngine(engineElement); | |
| 531 expect(element.kind, ElementKind.METHOD); | |
| 532 expect(element.name, 'myMethod'); | |
| 533 { | |
| 534 Location location = element.location; | |
| 535 expect(location.file, '/test.dart'); | |
| 536 expect(location.offset, 32); | |
| 537 expect(location.length, 'myGetter'.length); | |
| 538 expect(location.startLine, 2); | |
| 539 expect(location.startColumn, 23); | |
| 540 } | |
| 541 expect(element.parameters, '(int a, {String b, int c})'); | |
| 542 expect(element.returnType, 'List<String>'); | |
| 543 expect(element.flags, Element.FLAG_STATIC); | |
| 544 } | |
| 545 | |
| 546 void test_fromElement_SETTER() { | |
| 547 engine.Source source = addSource( | |
| 548 '/test.dart', | |
| 549 ''' | |
| 550 class A { | |
| 551 set mySetter(String x) {} | |
| 552 }'''); | |
| 553 engine.CompilationUnit unit = resolveLibraryUnit(source); | |
| 554 engine.FieldElement engineFieldElement = | |
| 555 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); | |
| 556 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; | |
| 557 // create notification Element | |
| 558 Element element = newElement_fromEngine(engineElement); | |
| 559 expect(element.kind, ElementKind.SETTER); | |
| 560 expect(element.name, 'mySetter'); | |
| 561 { | |
| 562 Location location = element.location; | |
| 563 expect(location.file, '/test.dart'); | |
| 564 expect(location.offset, 16); | |
| 565 expect(location.length, 'mySetter'.length); | |
| 566 expect(location.startLine, 2); | |
| 567 expect(location.startColumn, 7); | |
| 568 } | |
| 569 expect(element.parameters, '(String x)'); | |
| 570 expect(element.returnType, isNull); | |
| 571 expect(element.flags, 0); | |
| 572 } | |
| 573 } | |
| 574 | |
| 575 @reflectiveTest | |
| 576 class EnumTest { | 115 class EnumTest { |
| 577 void test_AnalysisErrorSeverity() { | 116 void test_AnalysisErrorSeverity() { |
| 578 new EnumTester<engine.ErrorSeverity, AnalysisErrorSeverity>().run( | 117 new EnumTester<engine.ErrorSeverity, AnalysisErrorSeverity>().run( |
| 579 (engine.ErrorSeverity engineErrorSeverity) => | 118 (engine.ErrorSeverity engineErrorSeverity) => |
| 580 new AnalysisErrorSeverity(engineErrorSeverity.name), | 119 new AnalysisErrorSeverity(engineErrorSeverity.name), |
| 581 exceptions: {engine.ErrorSeverity.NONE: null}); | 120 exceptions: {engine.ErrorSeverity.NONE: null}); |
| 582 } | 121 } |
| 583 | 122 |
| 584 void test_AnalysisErrorType() { | 123 void test_AnalysisErrorType() { |
| 585 new EnumTester<engine.ErrorType, AnalysisErrorType>().run( | 124 new EnumTester<engine.ErrorType, AnalysisErrorType>().run( |
| 586 (engine.ErrorType engineErrorType) => | 125 (engine.ErrorType engineErrorType) => |
| 587 new AnalysisErrorType(engineErrorType.name)); | 126 new AnalysisErrorType(engineErrorType.name)); |
| 588 } | 127 } |
| 589 | 128 |
| 590 void test_ElementKind() { | 129 void test_ElementKind() { |
| 591 new EnumTester<engine.ElementKind, ElementKind>() | 130 new EnumTester<engine.ElementKind, ElementKind>() |
| 592 .run(newElementKind_fromEngine, exceptions: { | 131 .run(convertElementKind, exceptions: { |
| 593 // TODO(paulberry): do any of the exceptions below constitute bugs? | 132 // TODO(paulberry): do any of the exceptions below constitute bugs? |
| 594 engine.ElementKind.DYNAMIC: ElementKind.UNKNOWN, | 133 engine.ElementKind.DYNAMIC: ElementKind.UNKNOWN, |
| 595 engine.ElementKind.EMBEDDED_HTML_SCRIPT: ElementKind.UNKNOWN, | 134 engine.ElementKind.EMBEDDED_HTML_SCRIPT: ElementKind.UNKNOWN, |
| 596 engine.ElementKind.ERROR: ElementKind.UNKNOWN, | 135 engine.ElementKind.ERROR: ElementKind.UNKNOWN, |
| 597 engine.ElementKind.EXPORT: ElementKind.UNKNOWN, | 136 engine.ElementKind.EXPORT: ElementKind.UNKNOWN, |
| 598 engine.ElementKind.EXTERNAL_HTML_SCRIPT: ElementKind.UNKNOWN, | 137 engine.ElementKind.EXTERNAL_HTML_SCRIPT: ElementKind.UNKNOWN, |
| 599 engine.ElementKind.HTML: ElementKind.UNKNOWN, | 138 engine.ElementKind.HTML: ElementKind.UNKNOWN, |
| 600 engine.ElementKind.IMPORT: ElementKind.UNKNOWN, | 139 engine.ElementKind.IMPORT: ElementKind.UNKNOWN, |
| 601 engine.ElementKind.NAME: ElementKind.UNKNOWN, | 140 engine.ElementKind.NAME: ElementKind.UNKNOWN, |
| 602 engine.ElementKind.UNIVERSE: ElementKind.UNKNOWN | 141 engine.ElementKind.UNIVERSE: ElementKind.UNKNOWN |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 ApiEnum apiValue = convert(engineValue); | 186 ApiEnum apiValue = convert(engineValue); |
| 648 expect(apiValue, equals(expectedResult)); | 187 expect(apiValue, equals(expectedResult)); |
| 649 } | 188 } |
| 650 } else { | 189 } else { |
| 651 ApiEnum apiValue = convert(engineValue); | 190 ApiEnum apiValue = convert(engineValue); |
| 652 expect(apiValue.name, equals(enumName)); | 191 expect(apiValue.name, equals(enumName)); |
| 653 } | 192 } |
| 654 }); | 193 }); |
| 655 } | 194 } |
| 656 } | 195 } |
| OLD | NEW |