Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, 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 import 'dart:convert'; | |
| 6 | |
| 7 import 'package:analyzer/dart/element/element.dart'; | |
| 8 import 'package:analyzer/src/summary/format.dart'; | |
| 9 import 'package:analyzer/src/summary/idl.dart'; | |
| 10 import 'package:analyzer/src/summary/index_unit.dart'; | |
| 11 import 'package:unittest/unittest.dart'; | |
| 12 | |
| 13 import '../../reflective_tests.dart'; | |
| 14 import '../abstract_single_unit.dart'; | |
| 15 | |
| 16 main() { | |
| 17 groupSep = ' | '; | |
| 18 runReflectiveTests(PackageIndexAssemblerTest); | |
| 19 } | |
| 20 | |
| 21 class ExpectedLocation { | |
| 22 CompilationUnitElement unitElement; | |
| 23 int offset; | |
| 24 int length; | |
| 25 bool isQualified; | |
| 26 bool isResolved; | |
| 27 | |
| 28 ExpectedLocation(this.unitElement, this.offset, this.length, this.isQualified, | |
| 29 this.isResolved); | |
| 30 | |
| 31 @override | |
| 32 String toString() { | |
| 33 return '(unit=$unitElement; offset=$offset; length=$length;' | |
| 34 ' isQualified=$isQualified isResolved=$isResolved)'; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * TODO(scheglov) add 'resolved' and 'qualified' flags. | |
| 40 */ | |
| 41 class LocationImpl { | |
|
Paul Berry
2016/02/25 23:05:13
What is this class for? I don't see where it's us
scheglov
2016/02/26 04:54:03
Removed.
| |
| 42 final int offset; | |
| 43 final int length; | |
| 44 | |
| 45 LocationImpl(this.offset, this.length); | |
| 46 } | |
| 47 | |
| 48 @reflectiveTest | |
| 49 class PackageIndexAssemblerTest extends AbstractSingleUnitTest { | |
|
Paul Berry
2016/02/25 23:05:13
It seems like a lot of code in IndexContributor is
scheglov
2016/02/26 04:54:03
Yes, of course!
Just not in this CL.
| |
| 50 PackageIndex packageIndex; | |
| 51 UnitIndex unitIndex; | |
| 52 | |
| 53 void test_isExtendedBy_ClassDeclaration() { | |
| 54 _indexTestUnit(''' | |
| 55 class A {} // 1 | |
| 56 class B extends A {} // 2 | |
| 57 '''); | |
| 58 ClassElement classElementA = findElement('A'); | |
| 59 // verify | |
| 60 _assertHasRelation(classElementA, IndexRelationKind.IS_EXTENDED_BY, | |
| 61 _expectedLocation('A {} // 2')); | |
| 62 } | |
| 63 | |
| 64 void test_isReferencedBy_ClassElement() { | |
| 65 _indexTestUnit(''' | |
| 66 class A { | |
| 67 static var field; | |
| 68 } | |
| 69 main(A p) { | |
| 70 A v; | |
| 71 new A(); // 2 | |
| 72 A.field = 1; | |
| 73 print(A.field); // 3 | |
| 74 } | |
| 75 '''); | |
| 76 ClassElement element = findElement("A"); | |
| 77 // verify | |
| 78 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | |
| 79 _expectedLocation('A p) {')); | |
| 80 _assertHasRelation( | |
| 81 element, IndexRelationKind.IS_REFERENCED_BY, _expectedLocation('A v;')); | |
| 82 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | |
| 83 _expectedLocation('A(); // 2')); | |
| 84 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | |
| 85 _expectedLocation('A.field = 1;')); | |
| 86 _assertHasRelation(element, IndexRelationKind.IS_REFERENCED_BY, | |
| 87 _expectedLocation('A.field); // 3')); | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * Asserts that [unitIndex] has an item with the expected properties. | |
| 92 */ | |
| 93 void _assertHasRelation( | |
| 94 Element element, | |
| 95 IndexRelationKind expectedRelationship, | |
| 96 ExpectedLocation expectedLocation) { | |
| 97 int elementId = _findElementId(element); | |
| 98 for (int i = 0; i < unitIndex.locationOffsets.length; i++) { | |
| 99 if (unitIndex.elements[i] == elementId && | |
| 100 unitIndex.locationOffsets[i] == expectedLocation.offset && | |
| 101 unitIndex.locationLengths[i] == expectedLocation.length) { | |
| 102 return; | |
| 103 } | |
| 104 } | |
| 105 _failWithIndexDump( | |
| 106 'not found\n$element $expectedRelationship at $expectedLocation'); | |
| 107 } | |
| 108 | |
| 109 ExpectedLocation _expectedLocation(String search, | |
| 110 {int length: -1, bool isQualified: false, bool isResolved: true}) { | |
| 111 int offset = findOffset(search); | |
| 112 if (length == -1) { | |
| 113 length = getLeadingIdentifierLength(search); | |
| 114 } | |
| 115 return new ExpectedLocation( | |
| 116 testUnitElement, offset, length, isQualified, isResolved); | |
| 117 } | |
| 118 | |
| 119 void _failWithIndexDump(String msg) { | |
| 120 String packageIndexJsonString = | |
| 121 new JsonEncoder.withIndent(' ').convert(packageIndex.toJson()); | |
| 122 fail('$msg in\n' + packageIndexJsonString); | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Return the [element] identifier in [packageIndex] or fail. | |
| 127 */ | |
| 128 int _findElementId(Element element) { | |
| 129 int elementUnitId = _getElementUnitId(element); | |
| 130 for (int elementId = 0; | |
| 131 elementId < packageIndex.elementUnits.length; | |
| 132 elementId++) { | |
| 133 if (packageIndex.elementUnits[elementId] == elementUnitId && | |
| 134 packageIndex.elementOffsets[elementId] == element.nameOffset) { | |
| 135 return elementId; | |
| 136 } | |
| 137 } | |
| 138 _failWithIndexDump('Element $element is not referenced'); | |
| 139 return 0; | |
| 140 } | |
| 141 | |
| 142 int _getElementUnitId(Element element) { | |
| 143 CompilationUnitElement unitElement = | |
| 144 PackageIndexAssembler.getUnitElement(element); | |
| 145 int libraryUriId = _getUriId(unitElement.library.source.uri); | |
| 146 int unitUriId = _getUriId(unitElement.library.source.uri); | |
| 147 for (int i = 0; i < packageIndex.elementLibraryUris.length; i++) { | |
| 148 if (packageIndex.elementLibraryUris[i] == libraryUriId && | |
| 149 packageIndex.elementUnitUris[i] == unitUriId) { | |
| 150 return i; | |
| 151 } | |
| 152 } | |
| 153 fail('Unit $unitElement of $element is not referenced in the index.'); | |
| 154 return -1; | |
| 155 } | |
| 156 | |
| 157 int _getUriId(Uri uri) { | |
| 158 String str = uri.toString(); | |
| 159 int id = packageIndex.uris.indexOf(str); | |
| 160 expect(id, isNonNegative); | |
| 161 return id; | |
| 162 } | |
| 163 | |
| 164 void _indexTestUnit(String code) { | |
| 165 resolveTestUnit(code); | |
| 166 PackageIndexAssembler assembler = new PackageIndexAssembler(); | |
| 167 assembler.index(testUnit); | |
| 168 // assemble, write and read | |
| 169 PackageIndexBuilder indexBuilder = assembler.assemble(); | |
| 170 List<int> indexBytes = indexBuilder.toBuffer(); | |
| 171 packageIndex = new PackageIndex.fromBuffer(indexBytes); | |
| 172 // prepare the only unit index | |
| 173 expect(packageIndex.units, hasLength(1)); | |
| 174 unitIndex = packageIndex.units[0]; | |
| 175 } | |
| 176 } | |
| OLD | NEW |