OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.src.task.incremental_element_builder_test; | 5 library test.src.task.incremental_element_builder_test; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
10 import 'package:analyzer/src/task/incremental_element_builder.dart'; | 10 import 'package:analyzer/src/task/incremental_element_builder.dart'; |
11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
12 | 12 |
13 import '../../reflective_tests.dart'; | 13 import '../../reflective_tests.dart'; |
14 import '../context/abstract_context.dart'; | 14 import '../context/abstract_context.dart'; |
15 | 15 |
16 main() { | 16 main() { |
17 groupSep = ' | '; | 17 groupSep = ' | '; |
18 runReflectiveTests(IncrementalCompilationUnitElementBuilderTest); | 18 runReflectiveTests(IncrementalCompilationUnitElementBuilderTest); |
19 } | 19 } |
20 | 20 |
21 @reflectiveTest | 21 @reflectiveTest |
22 class IncrementalCompilationUnitElementBuilderTest extends AbstractContextTest { | 22 class IncrementalCompilationUnitElementBuilderTest extends AbstractContextTest { |
23 Source source; | 23 Source source; |
24 String oldCode; | 24 String oldCode; |
25 CompilationUnit oldUnit; | 25 CompilationUnit oldUnit; |
| 26 CompilationUnitElement unitElement; |
26 | 27 |
27 String newCode; | 28 String newCode; |
28 CompilationUnit newUnit; | 29 CompilationUnit newUnit; |
29 | 30 |
30 String getNodeText(AstNode node) { | 31 String getNodeText(AstNode node) { |
31 return newCode.substring(node.offset, node.end); | 32 return newCode.substring(node.offset, node.end); |
32 } | 33 } |
33 | 34 |
34 test_directives_add() { | 35 test_directives_add() { |
35 _buildOldUnit(r''' | 36 _buildOldUnit(r''' |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 { | 211 { |
211 Directive newNode = newDirectives[2]; | 212 Directive newNode = newDirectives[2]; |
212 expect(newNode, same(oldDirectives[2])); | 213 expect(newNode, same(oldDirectives[2])); |
213 expect(getNodeText(newNode), "import 'dart:math';"); | 214 expect(getNodeText(newNode), "import 'dart:math';"); |
214 ImportElement element = newNode.element; | 215 ImportElement element = newNode.element; |
215 expect(element, isNotNull); | 216 expect(element, isNotNull); |
216 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); | 217 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); |
217 } | 218 } |
218 } | 219 } |
219 | 220 |
| 221 test_unitMembers_accessor_add() { |
| 222 _buildOldUnit(r''' |
| 223 get a => 1; |
| 224 '''); |
| 225 _buildNewUnit(r''' |
| 226 get a => 1; |
| 227 get b => 2; |
| 228 '''); |
| 229 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 230 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 231 // nodes |
| 232 FunctionDeclaration node1 = newNodes[0]; |
| 233 FunctionDeclaration node2 = newNodes[1]; |
| 234 expect(node1, same(oldNodes[0])); |
| 235 // elements |
| 236 PropertyAccessorElement elementA = node1.element; |
| 237 PropertyAccessorElement elementB = node2.element; |
| 238 expect(elementA, isNotNull); |
| 239 expect(elementB, isNotNull); |
| 240 expect(elementA.name, 'a'); |
| 241 expect(elementB.name, 'b'); |
| 242 // unit.types |
| 243 expect(unitElement.topLevelVariables, |
| 244 unorderedEquals([elementA.variable, elementB.variable])); |
| 245 expect(unitElement.accessors, unorderedEquals([elementA, elementB])); |
| 246 } |
| 247 |
| 248 test_unitMembers_class_add() { |
| 249 _buildOldUnit(r''' |
| 250 class A {} |
| 251 '''); |
| 252 _buildNewUnit(r''' |
| 253 class A {} |
| 254 class B {} |
| 255 '''); |
| 256 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 257 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 258 // nodes |
| 259 CompilationUnitMember nodeA = newNodes[0]; |
| 260 CompilationUnitMember nodeB = newNodes[1]; |
| 261 expect(nodeA, same(oldNodes[0])); |
| 262 // elements |
| 263 ClassElement elementA = nodeA.element; |
| 264 ClassElement elementB = nodeB.element; |
| 265 expect(elementA, isNotNull); |
| 266 expect(elementB, isNotNull); |
| 267 expect(elementA.name, 'A'); |
| 268 expect(elementB.name, 'B'); |
| 269 // unit.types |
| 270 expect(unitElement.types, unorderedEquals([elementA, elementB])); |
| 271 } |
| 272 |
| 273 test_unitMembers_class_comments() { |
| 274 _buildOldUnit(r''' |
| 275 /// reference [bool] type. |
| 276 class A {} |
| 277 /// reference [int] type. |
| 278 class B {} |
| 279 /// reference [double] and [B] types. |
| 280 class C {} |
| 281 '''); |
| 282 _buildNewUnit(r''' |
| 283 /// reference [double] and [B] types. |
| 284 class C {} |
| 285 /// reference [bool] type. |
| 286 class A {} |
| 287 /// reference [int] type. |
| 288 class B {} |
| 289 '''); |
| 290 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 291 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 292 { |
| 293 CompilationUnitMember newNode = newNodes[0]; |
| 294 expect(newNode, same(oldNodes[2])); |
| 295 expect(getNodeText(newNode), r''' |
| 296 /// reference [double] and [B] types. |
| 297 class C {}'''); |
| 298 ClassElement element = newNode.element; |
| 299 expect(element, isNotNull); |
| 300 expect(element.name, 'C'); |
| 301 expect(element.nameOffset, newCode.indexOf('C {}')); |
| 302 // [double] and [B] are still resolved |
| 303 { |
| 304 var docReferences = newNode.documentationComment.references; |
| 305 expect(docReferences, hasLength(2)); |
| 306 expect(docReferences[0].identifier.staticElement.name, 'double'); |
| 307 expect(docReferences[1].identifier.staticElement, |
| 308 same(newNodes[2].element)); |
| 309 } |
| 310 } |
| 311 { |
| 312 CompilationUnitMember newNode = newNodes[1]; |
| 313 expect(newNode, same(oldNodes[0])); |
| 314 expect(getNodeText(newNode), r''' |
| 315 /// reference [bool] type. |
| 316 class A {}'''); |
| 317 ClassElement element = newNode.element; |
| 318 expect(element, isNotNull); |
| 319 expect(element.name, 'A'); |
| 320 expect(element.nameOffset, newCode.indexOf('A {}')); |
| 321 // [bool] is still resolved |
| 322 { |
| 323 var docReferences = newNode.documentationComment.references; |
| 324 expect(docReferences, hasLength(1)); |
| 325 expect(docReferences[0].identifier.staticElement.name, 'bool'); |
| 326 } |
| 327 } |
| 328 { |
| 329 CompilationUnitMember newNode = newNodes[2]; |
| 330 expect(newNode, same(oldNodes[1])); |
| 331 expect(getNodeText(newNode), r''' |
| 332 /// reference [int] type. |
| 333 class B {}'''); |
| 334 ClassElement element = newNode.element; |
| 335 expect(element, isNotNull); |
| 336 expect(element.name, 'B'); |
| 337 expect(element.nameOffset, newCode.indexOf('B {}')); |
| 338 // [int] is still resolved |
| 339 { |
| 340 var docReferences = newNode.documentationComment.references; |
| 341 expect(docReferences, hasLength(1)); |
| 342 expect(docReferences[0].identifier.staticElement.name, 'int'); |
| 343 } |
| 344 } |
| 345 } |
| 346 |
| 347 test_unitMembers_class_reorder() { |
| 348 _buildOldUnit(r''' |
| 349 class A {} |
| 350 class B {} |
| 351 class C {} |
| 352 '''); |
| 353 _buildNewUnit(r''' |
| 354 class C {} |
| 355 class A {} |
| 356 class B {} |
| 357 '''); |
| 358 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 359 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 360 { |
| 361 CompilationUnitMember newNode = newNodes[0]; |
| 362 expect(newNode, same(oldNodes[2])); |
| 363 expect(getNodeText(newNode), 'class C {}'); |
| 364 ClassElement element = newNode.element; |
| 365 expect(element, isNotNull); |
| 366 expect(element.name, 'C'); |
| 367 expect(element.nameOffset, newCode.indexOf('C {}')); |
| 368 } |
| 369 { |
| 370 CompilationUnitMember newNode = newNodes[1]; |
| 371 expect(newNode, same(oldNodes[0])); |
| 372 expect(getNodeText(newNode), 'class A {}'); |
| 373 ClassElement element = newNode.element; |
| 374 expect(element, isNotNull); |
| 375 expect(element.name, 'A'); |
| 376 expect(element.nameOffset, newCode.indexOf('A {}')); |
| 377 } |
| 378 { |
| 379 CompilationUnitMember newNode = newNodes[2]; |
| 380 expect(newNode, same(oldNodes[1])); |
| 381 expect(getNodeText(newNode), 'class B {}'); |
| 382 ClassElement element = newNode.element; |
| 383 expect(element, isNotNull); |
| 384 expect(element.name, 'B'); |
| 385 expect(element.nameOffset, newCode.indexOf('B {}')); |
| 386 } |
| 387 } |
| 388 |
| 389 test_unitMembers_enum_add() { |
| 390 _buildOldUnit(r''' |
| 391 enum A {A1, A2} |
| 392 '''); |
| 393 _buildNewUnit(r''' |
| 394 enum A {A1, A2} |
| 395 enum B {B1, B2} |
| 396 '''); |
| 397 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 398 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 399 // nodes |
| 400 CompilationUnitMember nodeA = newNodes[0]; |
| 401 CompilationUnitMember nodeB = newNodes[1]; |
| 402 expect(nodeA, same(oldNodes[0])); |
| 403 // elements |
| 404 ClassElement elementA = nodeA.element; |
| 405 ClassElement elementB = nodeB.element; |
| 406 expect(elementA, isNotNull); |
| 407 expect(elementB, isNotNull); |
| 408 expect(elementA.name, 'A'); |
| 409 expect(elementB.name, 'B'); |
| 410 // unit.types |
| 411 expect(unitElement.enums, unorderedEquals([elementA, elementB])); |
| 412 } |
| 413 |
| 414 test_unitMembers_function_add() { |
| 415 _buildOldUnit(r''' |
| 416 a() {} |
| 417 '''); |
| 418 _buildNewUnit(r''' |
| 419 a() {} |
| 420 b() {} |
| 421 '''); |
| 422 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 423 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 424 // nodes |
| 425 CompilationUnitMember nodeA = newNodes[0]; |
| 426 CompilationUnitMember nodeB = newNodes[1]; |
| 427 expect(nodeA, same(oldNodes[0])); |
| 428 // elements |
| 429 FunctionElement elementA = nodeA.element; |
| 430 FunctionElement elementB = nodeB.element; |
| 431 expect(elementA, isNotNull); |
| 432 expect(elementB, isNotNull); |
| 433 expect(elementA.name, 'a'); |
| 434 expect(elementB.name, 'b'); |
| 435 // unit.types |
| 436 expect(unitElement.functions, unorderedEquals([elementA, elementB])); |
| 437 } |
| 438 |
| 439 test_unitMembers_functionTypeAlias_add() { |
| 440 _buildOldUnit(r''' |
| 441 typedef A(); |
| 442 '''); |
| 443 _buildNewUnit(r''' |
| 444 typedef A(); |
| 445 typedef B(); |
| 446 '''); |
| 447 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 448 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 449 // nodes |
| 450 CompilationUnitMember nodeA = newNodes[0]; |
| 451 CompilationUnitMember nodeB = newNodes[1]; |
| 452 expect(nodeA, same(oldNodes[0])); |
| 453 // elements |
| 454 FunctionTypeAliasElement elementA = nodeA.element; |
| 455 FunctionTypeAliasElement elementB = nodeB.element; |
| 456 expect(elementA, isNotNull); |
| 457 expect(elementB, isNotNull); |
| 458 expect(elementA.name, 'A'); |
| 459 expect(elementB.name, 'B'); |
| 460 // unit.types |
| 461 expect( |
| 462 unitElement.functionTypeAliases, unorderedEquals([elementA, elementB])); |
| 463 } |
| 464 |
| 465 test_unitMembers_topLevelVariable_add() { |
| 466 _buildOldUnit(r''' |
| 467 int a, b; |
| 468 '''); |
| 469 _buildNewUnit(r''' |
| 470 int a, b; |
| 471 int c, d; |
| 472 '''); |
| 473 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 474 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 475 // nodes |
| 476 TopLevelVariableDeclaration node1 = newNodes[0]; |
| 477 TopLevelVariableDeclaration node2 = newNodes[1]; |
| 478 expect(node1, same(oldNodes[0])); |
| 479 // elements |
| 480 TopLevelVariableElement elementA = node1.variables.variables[0].element; |
| 481 TopLevelVariableElement elementB = node1.variables.variables[1].element; |
| 482 TopLevelVariableElement elementC = node2.variables.variables[0].element; |
| 483 TopLevelVariableElement elementD = node2.variables.variables[1].element; |
| 484 expect(elementA, isNotNull); |
| 485 expect(elementB, isNotNull); |
| 486 expect(elementC, isNotNull); |
| 487 expect(elementD, isNotNull); |
| 488 expect(elementA.name, 'a'); |
| 489 expect(elementB.name, 'b'); |
| 490 expect(elementC.name, 'c'); |
| 491 expect(elementD.name, 'd'); |
| 492 // unit.types |
| 493 expect(unitElement.topLevelVariables, |
| 494 unorderedEquals([elementA, elementB, elementC, elementD])); |
| 495 expect(unitElement.accessors, unorderedEquals([ |
| 496 elementA.getter, |
| 497 elementA.setter, |
| 498 elementB.getter, |
| 499 elementB.setter, |
| 500 elementC.getter, |
| 501 elementC.setter, |
| 502 elementD.getter, |
| 503 elementD.setter |
| 504 ])); |
| 505 } |
| 506 |
| 507 test_unitMembers_topLevelVariableDeclaration() { |
| 508 _buildOldUnit(r''' |
| 509 bool a = 1, b = 2; |
| 510 int c = 3; |
| 511 '''); |
| 512 _buildNewUnit(r''' |
| 513 int c = 3; |
| 514 |
| 515 bool a =1, b = 2; |
| 516 '''); |
| 517 List<CompilationUnitMember> oldNodes = oldUnit.declarations; |
| 518 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 519 { |
| 520 TopLevelVariableDeclaration newNode = newNodes[0]; |
| 521 expect(newNode, same(oldNodes[1])); |
| 522 expect(getNodeText(newNode), 'int c = 3;'); |
| 523 { |
| 524 TopLevelVariableElement element = |
| 525 newNode.variables.variables[0].element; |
| 526 expect(element, isNotNull); |
| 527 expect(element.name, 'c'); |
| 528 expect(element.nameOffset, newCode.indexOf('c = 3')); |
| 529 } |
| 530 } |
| 531 { |
| 532 TopLevelVariableDeclaration newNode = newNodes[1]; |
| 533 expect(newNode, same(oldNodes[0])); |
| 534 expect(getNodeText(newNode), 'bool a =1, b = 2;'); |
| 535 { |
| 536 TopLevelVariableElement element = |
| 537 newNode.variables.variables[0].element; |
| 538 expect(element, isNotNull); |
| 539 expect(element.name, 'a'); |
| 540 expect(element.nameOffset, newCode.indexOf('a =1')); |
| 541 } |
| 542 { |
| 543 TopLevelVariableElement element = |
| 544 newNode.variables.variables[1].element; |
| 545 expect(element, isNotNull); |
| 546 expect(element.name, 'b'); |
| 547 expect(element.nameOffset, newCode.indexOf('b = 2')); |
| 548 } |
| 549 } |
| 550 } |
| 551 |
220 void _buildNewUnit(String newCode) { | 552 void _buildNewUnit(String newCode) { |
221 this.newCode = newCode; | 553 this.newCode = newCode; |
222 context.setContents(source, newCode); | 554 context.setContents(source, newCode); |
223 newUnit = context.parseCompilationUnit(source); | 555 newUnit = context.parseCompilationUnit(source); |
224 new IncrementalCompilationUnitElementBuilder(oldUnit, newUnit).build(); | 556 new IncrementalCompilationUnitElementBuilder(oldUnit, newUnit).build(); |
| 557 expect(newUnit.element, unitElement); |
225 } | 558 } |
226 | 559 |
227 void _buildOldUnit(String oldCode) { | 560 void _buildOldUnit(String oldCode) { |
228 this.oldCode = oldCode; | 561 this.oldCode = oldCode; |
229 source = newSource('/test.dart', oldCode); | 562 source = newSource('/test.dart', oldCode); |
230 oldUnit = context.resolveCompilationUnit2(source, source); | 563 oldUnit = context.resolveCompilationUnit2(source, source); |
| 564 unitElement = oldUnit.element; |
| 565 expect(unitElement, isNotNull); |
231 } | 566 } |
232 } | 567 } |
OLD | NEW |