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:async'; |
| 6 |
| 7 import 'package:analysis_server/src/services/index2/index2.dart'; |
| 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:analyzer/src/summary/format.dart'; |
| 12 import 'package:analyzer/src/summary/idl.dart'; |
| 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 14 import 'package:unittest/unittest.dart'; |
| 15 |
| 16 import '../../abstract_single_unit.dart'; |
| 17 import '../../utils.dart'; |
| 18 |
| 19 main() { |
| 20 initializeTestEnvironment(); |
| 21 defineReflectiveTests(Index2Test); |
| 22 } |
| 23 |
| 24 @reflectiveTest |
| 25 class Index2Test extends AbstractSingleUnitTest { |
| 26 PackageIndexStore indexStore = new _MemoryPackageIndexStore(); |
| 27 Index2 index; |
| 28 |
| 29 /** |
| 30 * Return the [Location] with given properties, or fail. |
| 31 */ |
| 32 Location findLocation(List<Location> locations, String libraryUri, |
| 33 String unitUri, int offset, int length) { |
| 34 for (Location location in locations) { |
| 35 if (location.libraryUri == libraryUri && |
| 36 location.unitUri == unitUri && |
| 37 location.offset == offset && |
| 38 location.length == length) { |
| 39 return location; |
| 40 } |
| 41 } |
| 42 fail('No at $offset with length $length in\n${locations.join('\n')}'); |
| 43 return null; |
| 44 } |
| 45 |
| 46 /** |
| 47 * Return the [Location] with given properties, or fail. |
| 48 */ |
| 49 Location findLocationSource( |
| 50 List<Location> locations, Source source, String search, |
| 51 {int length}) { |
| 52 String code = source.contents.data; |
| 53 int offset = code.indexOf(search); |
| 54 expect(offset, isNonNegative, reason: 'Not found "$search" in\n$code'); |
| 55 length ??= getLeadingIdentifierLength(search); |
| 56 String uri = source.uri.toString(); |
| 57 return findLocation(locations, uri, uri, offset, length); |
| 58 } |
| 59 |
| 60 /** |
| 61 * Return the [Location] with given properties, or fail. |
| 62 */ |
| 63 Location findLocationTest(List<Location> locations, String search, |
| 64 {int length}) { |
| 65 int offset = findOffset(search); |
| 66 length ??= getLeadingIdentifierLength(search); |
| 67 String testUri = testSource.uri.toString(); |
| 68 return findLocation(locations, testUri, testUri, offset, length); |
| 69 } |
| 70 |
| 71 void setUp() { |
| 72 super.setUp(); |
| 73 index = new Index2(indexStore); |
| 74 } |
| 75 |
| 76 void tearDown() { |
| 77 super.tearDown(); |
| 78 index = null; |
| 79 indexStore = null; |
| 80 } |
| 81 |
| 82 test_getRelations_isExtendedBy() async { |
| 83 _indexTestUnit(r''' |
| 84 class A {} |
| 85 class B extends A {} // B |
| 86 '''); |
| 87 Source source2 = _indexUnit( |
| 88 '/test2.dart', |
| 89 r''' |
| 90 import 'test.dart'; |
| 91 class C extends A {} // C |
| 92 '''); |
| 93 ClassElement elementA = testUnitElement.getType('A'); |
| 94 List<Location> locations = |
| 95 await index.getRelations(elementA, IndexRelationKind.IS_EXTENDED_BY); |
| 96 findLocationTest(locations, 'A {} // B'); |
| 97 findLocationSource(locations, source2, 'A {} // C'); |
| 98 } |
| 99 |
| 100 test_getRelations_isReferencedBy() async { |
| 101 _indexTestUnit(r''' |
| 102 main(int a, int b) { |
| 103 } |
| 104 '''); |
| 105 ClassElement intElement = context.typeProvider.intType.element; |
| 106 List<Location> locations = await index.getRelations( |
| 107 intElement, IndexRelationKind.IS_REFERENCED_BY); |
| 108 findLocationTest(locations, 'int a'); |
| 109 findLocationTest(locations, 'int b'); |
| 110 } |
| 111 |
| 112 void _indexTestUnit(String code) { |
| 113 resolveTestUnit(code); |
| 114 index.indexUnit(testUnit); |
| 115 } |
| 116 |
| 117 Source _indexUnit(String path, String code) { |
| 118 Source source = addSource(path, code); |
| 119 CompilationUnit unit = resolveLibraryUnit(source); |
| 120 index.indexUnit(unit); |
| 121 return source; |
| 122 } |
| 123 } |
| 124 |
| 125 /** |
| 126 * A [PackageIndexId] for [_MemoryPackageIndexStore]. |
| 127 */ |
| 128 class _MemoryPackageIndexId implements PackageIndexId { |
| 129 final String key; |
| 130 |
| 131 _MemoryPackageIndexId(this.key); |
| 132 } |
| 133 |
| 134 /** |
| 135 * A [PackageIndexStore] that keeps objects in memory; |
| 136 */ |
| 137 class _MemoryPackageIndexStore implements PackageIndexStore { |
| 138 final Map<String, PackageIndex> indexMap = <String, PackageIndex>{}; |
| 139 |
| 140 @override |
| 141 Future<Iterable<PackageIndexId>> getIds() async { |
| 142 return indexMap.keys.map((key) => new _MemoryPackageIndexId(key)); |
| 143 } |
| 144 |
| 145 @override |
| 146 Future<PackageIndex> getIndex(PackageIndexId id) async { |
| 147 return indexMap[(id as _MemoryPackageIndexId).key]; |
| 148 } |
| 149 |
| 150 @override |
| 151 putIndex(String unitLibraryUri, String unitUnitUri, |
| 152 PackageIndexBuilder indexBuilder) { |
| 153 List<int> indexBytes = indexBuilder.toBuffer(); |
| 154 PackageIndex index = new PackageIndex.fromBuffer(indexBytes); |
| 155 String key = '$unitLibraryUri;$unitUnitUri'; |
| 156 indexMap[key] = index; |
| 157 } |
| 158 } |
OLD | NEW |