| 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 analyzer.test.src.dart.ast.utilities_test; | 5 library analyzer.test.src.dart.ast.utilities_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; | 8 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; |
| 9 import 'package:analyzer/dart/ast/token.dart'; | 9 import 'package:analyzer/dart/ast/token.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 | 363 |
| 364 Object _getConstantValue(String source) => | 364 Object _getConstantValue(String source) => |
| 365 parseExpression(source).accept(new ConstantEvaluator()); | 365 parseExpression(source).accept(new ConstantEvaluator()); |
| 366 } | 366 } |
| 367 | 367 |
| 368 @reflectiveTest | 368 @reflectiveTest |
| 369 class NodeLocator2Test extends ParserTestCase { | 369 class NodeLocator2Test extends ParserTestCase { |
| 370 void test_onlyStartOffset() { | 370 void test_onlyStartOffset() { |
| 371 String code = ' int vv; '; | 371 String code = ' int vv; '; |
| 372 // 012345678 | 372 // 012345678 |
| 373 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code); | 373 CompilationUnit unit = parseCompilationUnit(code); |
| 374 TopLevelVariableDeclaration declaration = unit.declarations[0]; | 374 TopLevelVariableDeclaration declaration = unit.declarations[0]; |
| 375 VariableDeclarationList variableList = declaration.variables; | 375 VariableDeclarationList variableList = declaration.variables; |
| 376 Identifier typeName = (variableList.type as TypeName).name; | 376 Identifier typeName = (variableList.type as TypeName).name; |
| 377 SimpleIdentifier varName = variableList.variables[0].name; | 377 SimpleIdentifier varName = variableList.variables[0].name; |
| 378 expect(new NodeLocator2(0).searchWithin(unit), same(unit)); | 378 expect(new NodeLocator2(0).searchWithin(unit), same(unit)); |
| 379 expect(new NodeLocator2(1).searchWithin(unit), same(typeName)); | 379 expect(new NodeLocator2(1).searchWithin(unit), same(typeName)); |
| 380 expect(new NodeLocator2(2).searchWithin(unit), same(typeName)); | 380 expect(new NodeLocator2(2).searchWithin(unit), same(typeName)); |
| 381 expect(new NodeLocator2(3).searchWithin(unit), same(typeName)); | 381 expect(new NodeLocator2(3).searchWithin(unit), same(typeName)); |
| 382 expect(new NodeLocator2(4).searchWithin(unit), same(variableList)); | 382 expect(new NodeLocator2(4).searchWithin(unit), same(variableList)); |
| 383 expect(new NodeLocator2(5).searchWithin(unit), same(varName)); | 383 expect(new NodeLocator2(5).searchWithin(unit), same(varName)); |
| 384 expect(new NodeLocator2(6).searchWithin(unit), same(varName)); | 384 expect(new NodeLocator2(6).searchWithin(unit), same(varName)); |
| 385 expect(new NodeLocator2(7).searchWithin(unit), same(declaration)); | 385 expect(new NodeLocator2(7).searchWithin(unit), same(declaration)); |
| 386 expect(new NodeLocator2(8).searchWithin(unit), same(unit)); | 386 expect(new NodeLocator2(8).searchWithin(unit), same(unit)); |
| 387 expect(new NodeLocator2(9).searchWithin(unit), isNull); | 387 expect(new NodeLocator2(9).searchWithin(unit), isNull); |
| 388 expect(new NodeLocator2(100).searchWithin(unit), isNull); | 388 expect(new NodeLocator2(100).searchWithin(unit), isNull); |
| 389 } | 389 } |
| 390 | 390 |
| 391 void test_startEndOffset() { | 391 void test_startEndOffset() { |
| 392 String code = ' int vv; '; | 392 String code = ' int vv; '; |
| 393 // 012345678 | 393 // 012345678 |
| 394 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code); | 394 CompilationUnit unit = parseCompilationUnit(code); |
| 395 TopLevelVariableDeclaration declaration = unit.declarations[0]; | 395 TopLevelVariableDeclaration declaration = unit.declarations[0]; |
| 396 VariableDeclarationList variableList = declaration.variables; | 396 VariableDeclarationList variableList = declaration.variables; |
| 397 Identifier typeName = (variableList.type as TypeName).name; | 397 Identifier typeName = (variableList.type as TypeName).name; |
| 398 SimpleIdentifier varName = variableList.variables[0].name; | 398 SimpleIdentifier varName = variableList.variables[0].name; |
| 399 expect(new NodeLocator2(-1, 2).searchWithin(unit), isNull); | 399 expect(new NodeLocator2(-1, 2).searchWithin(unit), isNull); |
| 400 expect(new NodeLocator2(0, 2).searchWithin(unit), same(unit)); | 400 expect(new NodeLocator2(0, 2).searchWithin(unit), same(unit)); |
| 401 expect(new NodeLocator2(1, 2).searchWithin(unit), same(typeName)); | 401 expect(new NodeLocator2(1, 2).searchWithin(unit), same(typeName)); |
| 402 expect(new NodeLocator2(1, 3).searchWithin(unit), same(typeName)); | 402 expect(new NodeLocator2(1, 3).searchWithin(unit), same(typeName)); |
| 403 expect(new NodeLocator2(1, 4).searchWithin(unit), same(variableList)); | 403 expect(new NodeLocator2(1, 4).searchWithin(unit), same(variableList)); |
| 404 expect(new NodeLocator2(5, 6).searchWithin(unit), same(varName)); | 404 expect(new NodeLocator2(5, 6).searchWithin(unit), same(varName)); |
| 405 expect(new NodeLocator2(5, 7).searchWithin(unit), same(declaration)); | 405 expect(new NodeLocator2(5, 7).searchWithin(unit), same(declaration)); |
| 406 expect(new NodeLocator2(5, 8).searchWithin(unit), same(unit)); | 406 expect(new NodeLocator2(5, 8).searchWithin(unit), same(unit)); |
| 407 expect(new NodeLocator2(5, 100).searchWithin(unit), isNull); | 407 expect(new NodeLocator2(5, 100).searchWithin(unit), isNull); |
| 408 expect(new NodeLocator2(100, 200).searchWithin(unit), isNull); | 408 expect(new NodeLocator2(100, 200).searchWithin(unit), isNull); |
| 409 } | 409 } |
| 410 } | 410 } |
| 411 | 411 |
| 412 @reflectiveTest | 412 @reflectiveTest |
| 413 class NodeLocatorTest extends ParserTestCase { | 413 class NodeLocatorTest extends ParserTestCase { |
| 414 void test_range() { | 414 void test_range() { |
| 415 CompilationUnit unit = | 415 CompilationUnit unit = parseCompilationUnit("library myLib;"); |
| 416 ParserTestCase.parseCompilationUnit("library myLib;"); | |
| 417 _assertLocate( | 416 _assertLocate( |
| 418 unit, 4, 10, (node) => node is LibraryDirective, LibraryDirective); | 417 unit, 4, 10, (node) => node is LibraryDirective, LibraryDirective); |
| 419 } | 418 } |
| 420 | 419 |
| 421 void test_searchWithin_null() { | 420 void test_searchWithin_null() { |
| 422 NodeLocator locator = new NodeLocator(0, 0); | 421 NodeLocator locator = new NodeLocator(0, 0); |
| 423 expect(locator.searchWithin(null), isNull); | 422 expect(locator.searchWithin(null), isNull); |
| 424 } | 423 } |
| 425 | 424 |
| 426 void test_searchWithin_offset() { | 425 void test_searchWithin_offset() { |
| 427 CompilationUnit unit = | 426 CompilationUnit unit = parseCompilationUnit("library myLib;"); |
| 428 ParserTestCase.parseCompilationUnit("library myLib;"); | |
| 429 _assertLocate( | 427 _assertLocate( |
| 430 unit, 10, 10, (node) => node is SimpleIdentifier, SimpleIdentifier); | 428 unit, 10, 10, (node) => node is SimpleIdentifier, SimpleIdentifier); |
| 431 } | 429 } |
| 432 | 430 |
| 433 void test_searchWithin_offsetAfterNode() { | 431 void test_searchWithin_offsetAfterNode() { |
| 434 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r''' | 432 CompilationUnit unit = parseCompilationUnit(r''' |
| 435 class A {} | 433 class A {} |
| 436 class B {}'''); | 434 class B {}'''); |
| 437 NodeLocator locator = new NodeLocator(1024, 1024); | 435 NodeLocator locator = new NodeLocator(1024, 1024); |
| 438 AstNode node = locator.searchWithin(unit.declarations[0]); | 436 AstNode node = locator.searchWithin(unit.declarations[0]); |
| 439 expect(node, isNull); | 437 expect(node, isNull); |
| 440 } | 438 } |
| 441 | 439 |
| 442 void test_searchWithin_offsetBeforeNode() { | 440 void test_searchWithin_offsetBeforeNode() { |
| 443 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r''' | 441 CompilationUnit unit = parseCompilationUnit(r''' |
| 444 class A {} | 442 class A {} |
| 445 class B {}'''); | 443 class B {}'''); |
| 446 NodeLocator locator = new NodeLocator(0, 0); | 444 NodeLocator locator = new NodeLocator(0, 0); |
| 447 AstNode node = locator.searchWithin(unit.declarations[1]); | 445 AstNode node = locator.searchWithin(unit.declarations[1]); |
| 448 expect(node, isNull); | 446 expect(node, isNull); |
| 449 } | 447 } |
| 450 | 448 |
| 451 void _assertLocate(CompilationUnit unit, int start, int end, | 449 void _assertLocate(CompilationUnit unit, int start, int end, |
| 452 Predicate<Object> predicate, Type expectedClass) { | 450 Predicate<Object> predicate, Type expectedClass) { |
| 453 NodeLocator locator = new NodeLocator(start, end); | 451 NodeLocator locator = new NodeLocator(start, end); |
| (...skipping 5388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5842 /** | 5840 /** |
| 5843 * Assert that a `ToSourceVisitor` will produce the [expectedSource] when | 5841 * Assert that a `ToSourceVisitor` will produce the [expectedSource] when |
| 5844 * visiting the given [node]. | 5842 * visiting the given [node]. |
| 5845 */ | 5843 */ |
| 5846 void _assertSource(String expectedSource, AstNode node) { | 5844 void _assertSource(String expectedSource, AstNode node) { |
| 5847 PrintStringWriter writer = new PrintStringWriter(); | 5845 PrintStringWriter writer = new PrintStringWriter(); |
| 5848 node.accept(new ToSourceVisitor(writer)); | 5846 node.accept(new ToSourceVisitor(writer)); |
| 5849 expect(writer.toString(), expectedSource); | 5847 expect(writer.toString(), expectedSource); |
| 5850 } | 5848 } |
| 5851 } | 5849 } |
| OLD | NEW |