| 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.services.src.index.abstract_single_file; | 5 library test.services.src.index.abstract_single_file; |
| 6 | 6 |
| 7 import 'abstract_context.dart'; | |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart'; | 9 import 'package:analyzer/src/generated/error.dart'; |
| 11 import 'package:analyzer/src/generated/java_engine.dart'; | 10 import 'package:analyzer/src/generated/java_engine.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 14 | 13 |
| 14 import 'abstract_context.dart'; |
| 15 |
| 15 | 16 |
| 16 class AbstractSingleUnitTest extends AbstractContextTest { | 17 class AbstractSingleUnitTest extends AbstractContextTest { |
| 17 bool verifyNoTestUnitErrors = true; | 18 bool verifyNoTestUnitErrors = true; |
| 18 | 19 |
| 19 String testCode; | 20 String testCode; |
| 20 String testFile = '/test.dart'; | 21 String testFile = '/test.dart'; |
| 21 Source testSource; | 22 Source testSource; |
| 22 CompilationUnit testUnit; | 23 CompilationUnit testUnit; |
| 23 CompilationUnitElement testUnitElement; | 24 CompilationUnitElement testUnitElement; |
| 24 LibraryElement testLibraryElement; | 25 LibraryElement testLibraryElement; |
| 25 | 26 |
| 27 void addTestSource(String code) { |
| 28 testCode = code; |
| 29 testSource = addSource(testFile, code); |
| 30 } |
| 31 |
| 26 void assertNoErrorsInSource(Source source) { | 32 void assertNoErrorsInSource(Source source) { |
| 27 List<AnalysisError> errors = context.getErrors(source).errors; | 33 List<AnalysisError> errors = context.getErrors(source).errors; |
| 28 expect(errors, isEmpty); | 34 expect(errors, isEmpty); |
| 29 } | 35 } |
| 30 | 36 |
| 31 Element findElement(String name, [ElementKind kind]) { | 37 Element findElement(String name, [ElementKind kind]) { |
| 32 return findChildElement(testUnitElement, name, kind); | 38 return findChildElement(testUnitElement, name, kind); |
| 33 } | 39 } |
| 34 | 40 |
| 41 int findEnd(String search) { |
| 42 return findOffset(search) + search.length; |
| 43 } |
| 44 |
| 35 /** | 45 /** |
| 36 * Returns the [SimpleIdentifier] at the given search pattern. | 46 * Returns the [SimpleIdentifier] at the given search pattern. |
| 37 */ | 47 */ |
| 38 SimpleIdentifier findIdentifier(String search) { | 48 SimpleIdentifier findIdentifier(String search) { |
| 39 return findNodeAtString(search, (node) => node is SimpleIdentifier); | 49 return findNodeAtString(search, (node) => node is SimpleIdentifier); |
| 40 } | 50 } |
| 41 | 51 |
| 42 AstNode findNodeAtOffset(int offset, [Predicate<AstNode> predicate]) { | 52 AstNode findNodeAtOffset(int offset, [Predicate<AstNode> predicate]) { |
| 43 AstNode result = new NodeLocator.con1(offset).searchWithin(testUnit); | 53 AstNode result = new NodeLocator.con1(offset).searchWithin(testUnit); |
| 44 if (result != null && predicate != null) { | 54 if (result != null && predicate != null) { |
| 45 result = result.getAncestor(predicate); | 55 result = result.getAncestor(predicate); |
| 46 } | 56 } |
| 47 return result; | 57 return result; |
| 48 } | 58 } |
| 49 | 59 |
| 50 AstNode findNodeAtString(String search, [Predicate<AstNode> predicate]) { | 60 AstNode findNodeAtString(String search, [Predicate<AstNode> predicate]) { |
| 51 int offset = findOffset(search); | 61 int offset = findOffset(search); |
| 52 return findNodeAtOffset(offset, predicate); | 62 return findNodeAtOffset(offset, predicate); |
| 53 } | 63 } |
| 54 | 64 |
| 55 Element findNodeElementAtString(String search, | 65 Element findNodeElementAtString(String search, |
| 56 [Predicate<AstNode> predicate]) { | 66 [Predicate<AstNode> predicate]) { |
| 57 AstNode node = findNodeAtString(search, predicate); | 67 AstNode node = findNodeAtString(search, predicate); |
| 58 if (node == null) { | 68 if (node == null) { |
| 59 return null; | 69 return null; |
| 60 } | 70 } |
| 61 return ElementLocator.locate(node); | 71 return ElementLocator.locate(node); |
| 62 } | 72 } |
| 63 | 73 |
| 64 int findEnd(String search) { | |
| 65 return findOffset(search) + search.length; | |
| 66 } | |
| 67 | |
| 68 int findOffset(String search) { | 74 int findOffset(String search) { |
| 69 int offset = testCode.indexOf(search); | 75 int offset = testCode.indexOf(search); |
| 70 expect(offset, isNonNegative, reason: "Not found '$search' in\n$testCode"); | 76 expect(offset, isNonNegative, reason: "Not found '$search' in\n$testCode"); |
| 71 return offset; | 77 return offset; |
| 72 } | 78 } |
| 73 | 79 |
| 74 int getLeadingIdentifierLength(String search) { | 80 int getLeadingIdentifierLength(String search) { |
| 75 int length = 0; | 81 int length = 0; |
| 76 while (length < search.length) { | 82 while (length < search.length) { |
| 77 int c = search.codeUnitAt(length); | 83 int c = search.codeUnitAt(length); |
| 78 if (c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0)) { | 84 if (c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0)) { |
| 79 length++; | 85 length++; |
| 80 continue; | 86 continue; |
| 81 } | 87 } |
| 82 if (c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0)) { | 88 if (c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0)) { |
| 83 length++; | 89 length++; |
| 84 continue; | 90 continue; |
| 85 } | 91 } |
| 86 if (c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0)) { | 92 if (c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0)) { |
| 87 length++; | 93 length++; |
| 88 continue; | 94 continue; |
| 89 } | 95 } |
| 90 break; | 96 break; |
| 91 } | 97 } |
| 92 return length; | 98 return length; |
| 93 } | 99 } |
| 94 | 100 |
| 95 void resolveTestUnit(String code) { | 101 void resolveTestUnit(String code) { |
| 96 testCode = code; | 102 addTestSource(code); |
| 97 testSource = addSource(testFile, code); | |
| 98 testUnit = resolveLibraryUnit(testSource); | 103 testUnit = resolveLibraryUnit(testSource); |
| 99 if (verifyNoTestUnitErrors) { | 104 if (verifyNoTestUnitErrors) { |
| 100 assertNoErrorsInSource(testSource); | 105 assertNoErrorsInSource(testSource); |
| 101 } | 106 } |
| 102 testUnitElement = testUnit.element; | 107 testUnitElement = testUnit.element; |
| 103 testLibraryElement = testUnitElement.library; | 108 testLibraryElement = testUnitElement.library; |
| 104 } | 109 } |
| 105 } | 110 } |
| OLD | NEW |