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 Source testSource; |
| 21 CompilationUnit testUnit; |
| 22 CompilationUnitElement testUnitElement; |
| 23 LibraryElement testLibraryElement; |
| 24 |
| 25 void assertNoErrorsInSource(Source source) { |
| 26 List<AnalysisError> errors = context.getErrors(source).errors; |
| 27 expect(errors, isEmpty); |
| 28 } |
| 29 |
| 30 Element findElement(String name, [ElementKind kind]) { |
| 31 return findChildElement(testUnitElement, name, kind); |
| 32 } |
| 33 |
| 34 AstNode findNodeAtOffset(int offset, [Predicate<AstNode> predicate]) { |
| 35 AstNode result = new NodeLocator.con1(offset).searchWithin(testUnit); |
| 36 if (result != null && predicate != null) { |
| 37 result = result.getAncestor(predicate); |
| 38 } |
| 39 return result; |
| 40 } |
| 41 |
| 42 AstNode findNodeAtString(String search, [Predicate<AstNode> predicate]) { |
| 43 int offset = findOffset(search); |
| 44 return findNodeAtOffset(offset, predicate); |
| 45 } |
| 46 |
| 47 Element findNodeElementAtString(String search, |
| 48 [Predicate<AstNode> predicate]) { |
| 49 AstNode node = findNodeAtString(search, predicate); |
| 50 if (node == null) { |
| 51 return null; |
| 52 } |
| 53 return ElementLocator.locate(node); |
| 54 } |
| 55 |
| 56 int findOffset(String search) { |
| 57 int offset = testCode.indexOf(search); |
| 58 expect(offset, isNonNegative, reason: "Not found '$search' in\n$testCode"); |
| 59 return offset; |
| 60 } |
| 61 |
| 62 int getLeadingIdentifierLength(String search) { |
| 63 int length = 0; |
| 64 while (length < search.length) { |
| 65 int c = search.codeUnitAt(length); |
| 66 if (c >= 'a'.codeUnitAt(0) && c <= 'z'.codeUnitAt(0)) { |
| 67 length++; |
| 68 continue; |
| 69 } |
| 70 if (c >= 'A'.codeUnitAt(0) && c <= 'Z'.codeUnitAt(0)) { |
| 71 length++; |
| 72 continue; |
| 73 } |
| 74 if (c >= '0'.codeUnitAt(0) && c <= '9'.codeUnitAt(0)) { |
| 75 length++; |
| 76 continue; |
| 77 } |
| 78 break; |
| 79 } |
| 80 return length; |
| 81 } |
| 82 |
| 83 void resolveTestUnit(String code) { |
| 84 testCode = code; |
| 85 testSource = addSource('/test.dart', code); |
| 86 testUnit = resolveLibraryUnit(testSource); |
| 87 if (verifyNoTestUnitErrors) { |
| 88 assertNoErrorsInSource(testSource); |
| 89 } |
| 90 testUnitElement = testUnit.element; |
| 91 testLibraryElement = testUnitElement.library; |
| 92 } |
| 93 } |
OLD | NEW |