Index: pkg/analysis_server/test/services/index2/index2.dart |
diff --git a/pkg/analysis_server/test/services/index2/index2.dart b/pkg/analysis_server/test/services/index2/index2.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..322e9d82080caba134374b4025a802c15f00b36d |
--- /dev/null |
+++ b/pkg/analysis_server/test/services/index2/index2.dart |
@@ -0,0 +1,158 @@ |
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+import 'dart:async'; |
+ |
+import 'package:analysis_server/src/services/index2/index2.dart'; |
+import 'package:analyzer/dart/ast/ast.dart'; |
+import 'package:analyzer/dart/element/element.dart'; |
+import 'package:analyzer/src/generated/source.dart'; |
+import 'package:analyzer/src/summary/format.dart'; |
+import 'package:analyzer/src/summary/idl.dart'; |
+import 'package:test_reflective_loader/test_reflective_loader.dart'; |
+import 'package:unittest/unittest.dart'; |
+ |
+import '../../abstract_single_unit.dart'; |
+import '../../utils.dart'; |
+ |
+main() { |
+ initializeTestEnvironment(); |
+ defineReflectiveTests(Index2Test); |
+} |
+ |
+@reflectiveTest |
+class Index2Test extends AbstractSingleUnitTest { |
+ PackageIndexStore indexStore = new _MemoryPackageIndexStore(); |
+ Index2 index; |
+ |
+ /** |
+ * Return the [Location] with given properties, or fail. |
+ */ |
+ Location findLocation(List<Location> locations, String libraryUri, |
+ String unitUri, int offset, int length) { |
+ for (Location location in locations) { |
+ if (location.libraryUri == libraryUri && |
+ location.unitUri == unitUri && |
+ location.offset == offset && |
+ location.length == length) { |
+ return location; |
+ } |
+ } |
+ fail('No at $offset with length $length in\n${locations.join('\n')}'); |
+ return null; |
+ } |
+ |
+ /** |
+ * Return the [Location] with given properties, or fail. |
+ */ |
+ Location findLocationSource( |
+ List<Location> locations, Source source, String search, |
+ {int length}) { |
+ String code = source.contents.data; |
+ int offset = code.indexOf(search); |
+ expect(offset, isNonNegative, reason: 'Not found "$search" in\n$code'); |
+ length ??= getLeadingIdentifierLength(search); |
+ String uri = source.uri.toString(); |
+ return findLocation(locations, uri, uri, offset, length); |
+ } |
+ |
+ /** |
+ * Return the [Location] with given properties, or fail. |
+ */ |
+ Location findLocationTest(List<Location> locations, String search, |
+ {int length}) { |
+ int offset = findOffset(search); |
+ length ??= getLeadingIdentifierLength(search); |
+ String testUri = testSource.uri.toString(); |
+ return findLocation(locations, testUri, testUri, offset, length); |
+ } |
+ |
+ void setUp() { |
+ super.setUp(); |
+ index = new Index2(indexStore); |
+ } |
+ |
+ void tearDown() { |
+ super.tearDown(); |
+ index = null; |
+ indexStore = null; |
+ } |
+ |
+ test_getRelations_isExtendedBy() async { |
+ _indexTestUnit(r''' |
+class A {} |
+class B extends A {} // B |
+'''); |
+ Source source2 = _indexUnit( |
+ '/test2.dart', |
+ r''' |
+import 'test.dart'; |
+class C extends A {} // C |
+'''); |
+ ClassElement elementA = testUnitElement.getType('A'); |
+ List<Location> locations = |
+ await index.getRelations(elementA, IndexRelationKind.IS_EXTENDED_BY); |
+ findLocationTest(locations, 'A {} // B'); |
+ findLocationSource(locations, source2, 'A {} // C'); |
+ } |
+ |
+ test_getRelations_isReferencedBy() async { |
+ _indexTestUnit(r''' |
+main(int a, int b) { |
+} |
+'''); |
+ ClassElement intElement = context.typeProvider.intType.element; |
+ List<Location> locations = await index.getRelations( |
+ intElement, IndexRelationKind.IS_REFERENCED_BY); |
+ findLocationTest(locations, 'int a'); |
+ findLocationTest(locations, 'int b'); |
+ } |
+ |
+ void _indexTestUnit(String code) { |
+ resolveTestUnit(code); |
+ index.indexUnit(testUnit); |
+ } |
+ |
+ Source _indexUnit(String path, String code) { |
+ Source source = addSource(path, code); |
+ CompilationUnit unit = resolveLibraryUnit(source); |
+ index.indexUnit(unit); |
+ return source; |
+ } |
+} |
+ |
+/** |
+ * A [PackageIndexId] for [_MemoryPackageIndexStore]. |
+ */ |
+class _MemoryPackageIndexId implements PackageIndexId { |
+ final String key; |
+ |
+ _MemoryPackageIndexId(this.key); |
+} |
+ |
+/** |
+ * A [PackageIndexStore] that keeps objects in memory; |
+ */ |
+class _MemoryPackageIndexStore implements PackageIndexStore { |
+ final Map<String, PackageIndex> indexMap = <String, PackageIndex>{}; |
+ |
+ @override |
+ Future<Iterable<PackageIndexId>> getIds() async { |
+ return indexMap.keys.map((key) => new _MemoryPackageIndexId(key)); |
+ } |
+ |
+ @override |
+ Future<PackageIndex> getIndex(PackageIndexId id) async { |
+ return indexMap[(id as _MemoryPackageIndexId).key]; |
+ } |
+ |
+ @override |
+ putIndex(String unitLibraryUri, String unitUnitUri, |
+ PackageIndexBuilder indexBuilder) { |
+ List<int> indexBytes = indexBuilder.toBuffer(); |
+ PackageIndex index = new PackageIndex.fromBuffer(indexBytes); |
+ String key = '$unitLibraryUri;$unitUnitUri'; |
+ indexMap[key] = index; |
+ } |
+} |