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