| 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/plugin/protocol/protocol.dart'; | 7 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 8 import 'package:analysis_server/plugin/protocol/protocol_dart.dart'; | 8 import 'package:analysis_server/plugin/protocol/protocol_dart.dart'; |
| 9 import 'package:analyzer/dart/ast/ast.dart' as engine; | 9 import 'package:analyzer/dart/ast/ast.dart' as engine; |
| 10 import 'package:analyzer/dart/ast/visitor.dart' as engine; | 10 import 'package:analyzer/dart/ast/visitor.dart' as engine; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 @reflectiveTest | 98 @reflectiveTest |
| 99 class ElementTest extends AbstractContextTest { | 99 class ElementTest extends AbstractContextTest { |
| 100 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, | 100 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, |
| 101 [engine.ElementKind kind]) { | 101 [engine.ElementKind kind]) { |
| 102 return findChildElement(unit.element, name, kind); | 102 return findChildElement(unit.element, name, kind); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void test_fromElement_CLASS() { | 105 test_fromElement_CLASS() async { |
| 106 engine.Source source = addSource( | 106 engine.Source source = addSource( |
| 107 '/test.dart', | 107 '/test.dart', |
| 108 ''' | 108 ''' |
| 109 @deprecated | 109 @deprecated |
| 110 abstract class _A {} | 110 abstract class _A {} |
| 111 class B<K, V> {}'''); | 111 class B<K, V> {}'''); |
| 112 engine.CompilationUnit unit = resolveLibraryUnit(source); | 112 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 113 { | 113 { |
| 114 engine.ClassElement engineElement = findElementInUnit(unit, '_A'); | 114 engine.ClassElement engineElement = findElementInUnit(unit, '_A'); |
| 115 // create notification Element | 115 // create notification Element |
| 116 Element element = convertElement(engineElement); | 116 Element element = convertElement(engineElement); |
| 117 expect(element.kind, ElementKind.CLASS); | 117 expect(element.kind, ElementKind.CLASS); |
| 118 expect(element.name, '_A'); | 118 expect(element.name, '_A'); |
| 119 expect(element.typeParameters, isNull); | 119 expect(element.typeParameters, isNull); |
| 120 { | 120 { |
| 121 Location location = element.location; | 121 Location location = element.location; |
| 122 expect(location.file, '/test.dart'); | 122 expect(location.file, '/test.dart'); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 136 engine.ClassElement engineElement = findElementInUnit(unit, 'B'); | 136 engine.ClassElement engineElement = findElementInUnit(unit, 'B'); |
| 137 // create notification Element | 137 // create notification Element |
| 138 Element element = convertElement(engineElement); | 138 Element element = convertElement(engineElement); |
| 139 expect(element.kind, ElementKind.CLASS); | 139 expect(element.kind, ElementKind.CLASS); |
| 140 expect(element.name, 'B'); | 140 expect(element.name, 'B'); |
| 141 expect(element.typeParameters, '<K, V>'); | 141 expect(element.typeParameters, '<K, V>'); |
| 142 expect(element.flags, 0); | 142 expect(element.flags, 0); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 void test_fromElement_CONSTRUCTOR() { | 146 test_fromElement_CONSTRUCTOR() async { |
| 147 engine.Source source = addSource( | 147 engine.Source source = addSource( |
| 148 '/test.dart', | 148 '/test.dart', |
| 149 ''' | 149 ''' |
| 150 class A { | 150 class A { |
| 151 const A.myConstructor(int a, [String b]); | 151 const A.myConstructor(int a, [String b]); |
| 152 }'''); | 152 }'''); |
| 153 engine.CompilationUnit unit = resolveLibraryUnit(source); | 153 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 154 engine.ConstructorElement engineElement = | 154 engine.ConstructorElement engineElement = |
| 155 findElementInUnit(unit, 'myConstructor'); | 155 findElementInUnit(unit, 'myConstructor'); |
| 156 // create notification Element | 156 // create notification Element |
| 157 Element element = convertElement(engineElement); | 157 Element element = convertElement(engineElement); |
| 158 expect(element.kind, ElementKind.CONSTRUCTOR); | 158 expect(element.kind, ElementKind.CONSTRUCTOR); |
| 159 expect(element.name, 'myConstructor'); | 159 expect(element.name, 'myConstructor'); |
| 160 expect(element.typeParameters, isNull); | 160 expect(element.typeParameters, isNull); |
| 161 { | 161 { |
| 162 Location location = element.location; | 162 Location location = element.location; |
| 163 expect(location.file, '/test.dart'); | 163 expect(location.file, '/test.dart'); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 176 // create notification Element | 176 // create notification Element |
| 177 Element element = convertElement(engineElement); | 177 Element element = convertElement(engineElement); |
| 178 expect(element.kind, ElementKind.UNKNOWN); | 178 expect(element.kind, ElementKind.UNKNOWN); |
| 179 expect(element.name, 'dynamic'); | 179 expect(element.name, 'dynamic'); |
| 180 expect(element.location, isNull); | 180 expect(element.location, isNull); |
| 181 expect(element.parameters, isNull); | 181 expect(element.parameters, isNull); |
| 182 expect(element.returnType, isNull); | 182 expect(element.returnType, isNull); |
| 183 expect(element.flags, 0); | 183 expect(element.flags, 0); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void test_fromElement_ENUM() { | 186 test_fromElement_ENUM() async { |
| 187 engine.Source source = addSource( | 187 engine.Source source = addSource( |
| 188 '/test.dart', | 188 '/test.dart', |
| 189 ''' | 189 ''' |
| 190 @deprecated | 190 @deprecated |
| 191 enum _E1 { one, two } | 191 enum _E1 { one, two } |
| 192 enum E2 { three, four }'''); | 192 enum E2 { three, four }'''); |
| 193 engine.CompilationUnit unit = resolveLibraryUnit(source); | 193 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 194 { | 194 { |
| 195 engine.ClassElement engineElement = findElementInUnit(unit, '_E1'); | 195 engine.ClassElement engineElement = findElementInUnit(unit, '_E1'); |
| 196 expect(engineElement.isDeprecated, isTrue); | 196 expect(engineElement.isDeprecated, isTrue); |
| 197 // create notification Element | 197 // create notification Element |
| 198 Element element = convertElement(engineElement); | 198 Element element = convertElement(engineElement); |
| 199 expect(element.kind, ElementKind.ENUM); | 199 expect(element.kind, ElementKind.ENUM); |
| 200 expect(element.name, '_E1'); | 200 expect(element.name, '_E1'); |
| 201 expect(element.typeParameters, isNull); | 201 expect(element.typeParameters, isNull); |
| 202 { | 202 { |
| 203 Location location = element.location; | 203 Location location = element.location; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 217 engine.ClassElement engineElement = findElementInUnit(unit, 'E2'); | 217 engine.ClassElement engineElement = findElementInUnit(unit, 'E2'); |
| 218 // create notification Element | 218 // create notification Element |
| 219 Element element = convertElement(engineElement); | 219 Element element = convertElement(engineElement); |
| 220 expect(element.kind, ElementKind.ENUM); | 220 expect(element.kind, ElementKind.ENUM); |
| 221 expect(element.name, 'E2'); | 221 expect(element.name, 'E2'); |
| 222 expect(element.typeParameters, isNull); | 222 expect(element.typeParameters, isNull); |
| 223 expect(element.flags, 0); | 223 expect(element.flags, 0); |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 | 226 |
| 227 void test_fromElement_ENUM_CONSTANT() { | 227 test_fromElement_ENUM_CONSTANT() async { |
| 228 engine.Source source = addSource( | 228 engine.Source source = addSource( |
| 229 '/test.dart', | 229 '/test.dart', |
| 230 ''' | 230 ''' |
| 231 @deprecated | 231 @deprecated |
| 232 enum _E1 { one, two } | 232 enum _E1 { one, two } |
| 233 enum E2 { three, four }'''); | 233 enum E2 { three, four }'''); |
| 234 engine.CompilationUnit unit = resolveLibraryUnit(source); | 234 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 235 { | 235 { |
| 236 engine.FieldElement engineElement = findElementInUnit(unit, 'one'); | 236 engine.FieldElement engineElement = findElementInUnit(unit, 'one'); |
| 237 // create notification Element | 237 // create notification Element |
| 238 Element element = convertElement(engineElement); | 238 Element element = convertElement(engineElement); |
| 239 expect(element.kind, ElementKind.ENUM_CONSTANT); | 239 expect(element.kind, ElementKind.ENUM_CONSTANT); |
| 240 expect(element.name, 'one'); | 240 expect(element.name, 'one'); |
| 241 { | 241 { |
| 242 Location location = element.location; | 242 Location location = element.location; |
| 243 expect(location.file, '/test.dart'); | 243 expect(location.file, '/test.dart'); |
| 244 expect(location.offset, 23); | 244 expect(location.offset, 23); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 expect(location.length, 'values'.length); | 305 expect(location.length, 'values'.length); |
| 306 expect(location.startLine, 1); | 306 expect(location.startLine, 1); |
| 307 expect(location.startColumn, 0); | 307 expect(location.startColumn, 0); |
| 308 } | 308 } |
| 309 expect(element.parameters, isNull); | 309 expect(element.parameters, isNull); |
| 310 expect(element.returnType, 'List<E2>'); | 310 expect(element.returnType, 'List<E2>'); |
| 311 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 311 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 312 } | 312 } |
| 313 } | 313 } |
| 314 | 314 |
| 315 void test_fromElement_FIELD() { | 315 test_fromElement_FIELD() async { |
| 316 engine.Source source = addSource( | 316 engine.Source source = addSource( |
| 317 '/test.dart', | 317 '/test.dart', |
| 318 ''' | 318 ''' |
| 319 class A { | 319 class A { |
| 320 static const myField = 42; | 320 static const myField = 42; |
| 321 }'''); | 321 }'''); |
| 322 engine.CompilationUnit unit = resolveLibraryUnit(source); | 322 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 323 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | 323 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); |
| 324 // create notification Element | 324 // create notification Element |
| 325 Element element = convertElement(engineElement); | 325 Element element = convertElement(engineElement); |
| 326 expect(element.kind, ElementKind.FIELD); | 326 expect(element.kind, ElementKind.FIELD); |
| 327 expect(element.name, 'myField'); | 327 expect(element.name, 'myField'); |
| 328 { | 328 { |
| 329 Location location = element.location; | 329 Location location = element.location; |
| 330 expect(location.file, '/test.dart'); | 330 expect(location.file, '/test.dart'); |
| 331 expect(location.offset, 25); | 331 expect(location.offset, 25); |
| 332 expect(location.length, 'myField'.length); | 332 expect(location.length, 'myField'.length); |
| 333 expect(location.startLine, 2); | 333 expect(location.startLine, 2); |
| 334 expect(location.startColumn, 16); | 334 expect(location.startColumn, 16); |
| 335 } | 335 } |
| 336 expect(element.parameters, isNull); | 336 expect(element.parameters, isNull); |
| 337 expect(element.returnType, 'dynamic'); | 337 expect(element.returnType, 'dynamic'); |
| 338 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 338 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
| 339 } | 339 } |
| 340 | 340 |
| 341 void test_fromElement_FUNCTION_TYPE_ALIAS() { | 341 test_fromElement_FUNCTION_TYPE_ALIAS() async { |
| 342 engine.Source source = addSource( | 342 engine.Source source = addSource( |
| 343 '/test.dart', | 343 '/test.dart', |
| 344 ''' | 344 ''' |
| 345 typedef int F<T>(String x); | 345 typedef int F<T>(String x); |
| 346 '''); | 346 '''); |
| 347 engine.CompilationUnit unit = resolveLibraryUnit(source); | 347 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 348 engine.FunctionTypeAliasElement engineElement = | 348 engine.FunctionTypeAliasElement engineElement = |
| 349 findElementInUnit(unit, 'F'); | 349 findElementInUnit(unit, 'F'); |
| 350 // create notification Element | 350 // create notification Element |
| 351 Element element = convertElement(engineElement); | 351 Element element = convertElement(engineElement); |
| 352 expect(element.kind, ElementKind.FUNCTION_TYPE_ALIAS); | 352 expect(element.kind, ElementKind.FUNCTION_TYPE_ALIAS); |
| 353 expect(element.name, 'F'); | 353 expect(element.name, 'F'); |
| 354 expect(element.typeParameters, '<T>'); | 354 expect(element.typeParameters, '<T>'); |
| 355 { | 355 { |
| 356 Location location = element.location; | 356 Location location = element.location; |
| 357 expect(location.file, '/test.dart'); | 357 expect(location.file, '/test.dart'); |
| 358 expect(location.offset, 12); | 358 expect(location.offset, 12); |
| 359 expect(location.length, 'F'.length); | 359 expect(location.length, 'F'.length); |
| 360 expect(location.startLine, 1); | 360 expect(location.startLine, 1); |
| 361 expect(location.startColumn, 13); | 361 expect(location.startColumn, 13); |
| 362 } | 362 } |
| 363 expect(element.parameters, '(String x)'); | 363 expect(element.parameters, '(String x)'); |
| 364 expect(element.returnType, 'int'); | 364 expect(element.returnType, 'int'); |
| 365 expect(element.flags, 0); | 365 expect(element.flags, 0); |
| 366 } | 366 } |
| 367 | 367 |
| 368 void test_fromElement_GETTER() { | 368 test_fromElement_GETTER() async { |
| 369 engine.Source source = addSource( | 369 engine.Source source = addSource( |
| 370 '/test.dart', | 370 '/test.dart', |
| 371 ''' | 371 ''' |
| 372 class A { | 372 class A { |
| 373 String get myGetter => 42; | 373 String get myGetter => 42; |
| 374 }'''); | 374 }'''); |
| 375 engine.CompilationUnit unit = resolveLibraryUnit(source); | 375 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 376 engine.PropertyAccessorElement engineElement = | 376 engine.PropertyAccessorElement engineElement = |
| 377 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); | 377 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); |
| 378 // create notification Element | 378 // create notification Element |
| 379 Element element = convertElement(engineElement); | 379 Element element = convertElement(engineElement); |
| 380 expect(element.kind, ElementKind.GETTER); | 380 expect(element.kind, ElementKind.GETTER); |
| 381 expect(element.name, 'myGetter'); | 381 expect(element.name, 'myGetter'); |
| 382 { | 382 { |
| 383 Location location = element.location; | 383 Location location = element.location; |
| 384 expect(location.file, '/test.dart'); | 384 expect(location.file, '/test.dart'); |
| 385 expect(location.offset, 23); | 385 expect(location.offset, 23); |
| 386 expect(location.length, 'myGetter'.length); | 386 expect(location.length, 'myGetter'.length); |
| 387 expect(location.startLine, 2); | 387 expect(location.startLine, 2); |
| 388 expect(location.startColumn, 14); | 388 expect(location.startColumn, 14); |
| 389 } | 389 } |
| 390 expect(element.parameters, isNull); | 390 expect(element.parameters, isNull); |
| 391 expect(element.returnType, 'String'); | 391 expect(element.returnType, 'String'); |
| 392 expect(element.flags, 0); | 392 expect(element.flags, 0); |
| 393 } | 393 } |
| 394 | 394 |
| 395 void test_fromElement_LABEL() { | 395 test_fromElement_LABEL() async { |
| 396 engine.Source source = addSource( | 396 engine.Source source = addSource( |
| 397 '/test.dart', | 397 '/test.dart', |
| 398 ''' | 398 ''' |
| 399 main() { | 399 main() { |
| 400 myLabel: | 400 myLabel: |
| 401 while (true) { | 401 while (true) { |
| 402 break myLabel; | 402 break myLabel; |
| 403 } | 403 } |
| 404 }'''); | 404 }'''); |
| 405 engine.CompilationUnit unit = resolveLibraryUnit(source); | 405 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 406 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); | 406 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); |
| 407 // create notification Element | 407 // create notification Element |
| 408 Element element = convertElement(engineElement); | 408 Element element = convertElement(engineElement); |
| 409 expect(element.kind, ElementKind.LABEL); | 409 expect(element.kind, ElementKind.LABEL); |
| 410 expect(element.name, 'myLabel'); | 410 expect(element.name, 'myLabel'); |
| 411 { | 411 { |
| 412 Location location = element.location; | 412 Location location = element.location; |
| 413 expect(location.file, '/test.dart'); | 413 expect(location.file, '/test.dart'); |
| 414 expect(location.offset, 9); | 414 expect(location.offset, 9); |
| 415 expect(location.length, 'myLabel'.length); | 415 expect(location.length, 'myLabel'.length); |
| 416 expect(location.startLine, 2); | 416 expect(location.startLine, 2); |
| 417 expect(location.startColumn, 1); | 417 expect(location.startColumn, 1); |
| 418 } | 418 } |
| 419 expect(element.parameters, isNull); | 419 expect(element.parameters, isNull); |
| 420 expect(element.returnType, isNull); | 420 expect(element.returnType, isNull); |
| 421 expect(element.flags, 0); | 421 expect(element.flags, 0); |
| 422 } | 422 } |
| 423 | 423 |
| 424 void test_fromElement_METHOD() { | 424 test_fromElement_METHOD() async { |
| 425 engine.Source source = addSource( | 425 engine.Source source = addSource( |
| 426 '/test.dart', | 426 '/test.dart', |
| 427 ''' | 427 ''' |
| 428 class A { | 428 class A { |
| 429 static List<String> myMethod(int a, {String b, int c}) { | 429 static List<String> myMethod(int a, {String b, int c}) { |
| 430 return null; | 430 return null; |
| 431 } | 431 } |
| 432 }'''); | 432 }'''); |
| 433 engine.CompilationUnit unit = resolveLibraryUnit(source); | 433 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 434 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | 434 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); |
| 435 // create notification Element | 435 // create notification Element |
| 436 Element element = convertElement(engineElement); | 436 Element element = convertElement(engineElement); |
| 437 expect(element.kind, ElementKind.METHOD); | 437 expect(element.kind, ElementKind.METHOD); |
| 438 expect(element.name, 'myMethod'); | 438 expect(element.name, 'myMethod'); |
| 439 { | 439 { |
| 440 Location location = element.location; | 440 Location location = element.location; |
| 441 expect(location.file, '/test.dart'); | 441 expect(location.file, '/test.dart'); |
| 442 expect(location.offset, 32); | 442 expect(location.offset, 32); |
| 443 expect(location.length, 'myGetter'.length); | 443 expect(location.length, 'myGetter'.length); |
| 444 expect(location.startLine, 2); | 444 expect(location.startLine, 2); |
| 445 expect(location.startColumn, 23); | 445 expect(location.startColumn, 23); |
| 446 } | 446 } |
| 447 expect(element.parameters, '(int a, {String b, int c})'); | 447 expect(element.parameters, '(int a, {String b, int c})'); |
| 448 expect(element.returnType, 'List<String>'); | 448 expect(element.returnType, 'List<String>'); |
| 449 expect(element.flags, Element.FLAG_STATIC); | 449 expect(element.flags, Element.FLAG_STATIC); |
| 450 } | 450 } |
| 451 | 451 |
| 452 void test_fromElement_SETTER() { | 452 test_fromElement_SETTER() async { |
| 453 engine.Source source = addSource( | 453 engine.Source source = addSource( |
| 454 '/test.dart', | 454 '/test.dart', |
| 455 ''' | 455 ''' |
| 456 class A { | 456 class A { |
| 457 set mySetter(String x) {} | 457 set mySetter(String x) {} |
| 458 }'''); | 458 }'''); |
| 459 engine.CompilationUnit unit = resolveLibraryUnit(source); | 459 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
| 460 engine.FieldElement engineFieldElement = | 460 engine.FieldElement engineFieldElement = |
| 461 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); | 461 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); |
| 462 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; | 462 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; |
| 463 // create notification Element | 463 // create notification Element |
| 464 Element element = convertElement(engineElement); | 464 Element element = convertElement(engineElement); |
| 465 expect(element.kind, ElementKind.SETTER); | 465 expect(element.kind, ElementKind.SETTER); |
| 466 expect(element.name, 'mySetter'); | 466 expect(element.name, 'mySetter'); |
| 467 { | 467 { |
| 468 Location location = element.location; | 468 Location location = element.location; |
| 469 expect(location.file, '/test.dart'); | 469 expect(location.file, '/test.dart'); |
| 470 expect(location.offset, 16); | 470 expect(location.offset, 16); |
| 471 expect(location.length, 'mySetter'.length); | 471 expect(location.length, 'mySetter'.length); |
| 472 expect(location.startLine, 2); | 472 expect(location.startLine, 2); |
| 473 expect(location.startColumn, 7); | 473 expect(location.startColumn, 7); |
| 474 } | 474 } |
| 475 expect(element.parameters, '(String x)'); | 475 expect(element.parameters, '(String x)'); |
| 476 expect(element.returnType, isNull); | 476 expect(element.returnType, isNull); |
| 477 expect(element.flags, 0); | 477 expect(element.flags, 0); |
| 478 } | 478 } |
| 479 } | 479 } |
| OLD | NEW |