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