OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:analysis_server/src/services/index2/index2.dart'; | 5 import 'package:analysis_server/src/services/index2/index2.dart'; |
6 import 'package:analyzer/dart/ast/ast.dart'; | 6 import 'package:analyzer/dart/ast/ast.dart'; |
7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
8 import 'package:analyzer/src/generated/source.dart'; | 8 import 'package:analyzer/src/generated/source.dart'; |
9 import 'package:analyzer/src/summary/idl.dart'; | 9 import 'package:analyzer/src/summary/idl.dart'; |
10 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 10 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
12 | 12 |
13 import '../../abstract_single_unit.dart'; | 13 import '../../abstract_single_unit.dart'; |
14 import '../../utils.dart'; | 14 import '../../utils.dart'; |
15 | 15 |
16 main() { | 16 main() { |
17 initializeTestEnvironment(); | 17 initializeTestEnvironment(); |
18 defineReflectiveTests(Index2Test); | 18 defineReflectiveTests(Index2Test); |
19 } | 19 } |
20 | 20 |
21 @reflectiveTest | 21 @reflectiveTest |
22 class Index2Test extends AbstractSingleUnitTest { | 22 class Index2Test extends AbstractSingleUnitTest { |
23 Index2 index = createMemoryIndex2(); | 23 Index2 index = createMemoryIndex2(); |
24 | 24 |
25 /** | 25 /** |
26 * Return the [Location] with given properties, or fail. | 26 * Return the [Location] with given properties, or fail. |
27 */ | 27 */ |
28 Location findLocation(List<Location> locations, String libraryUri, | 28 Location findLocation(List<Location> locations, String libraryUri, |
29 String unitUri, int offset, int length) { | 29 String unitUri, int offset, int length, bool isQualified) { |
30 for (Location location in locations) { | 30 for (Location location in locations) { |
31 if (location.libraryUri == libraryUri && | 31 if (location.libraryUri == libraryUri && |
32 location.unitUri == unitUri && | 32 location.unitUri == unitUri && |
33 location.offset == offset && | 33 location.offset == offset && |
34 location.length == length) { | 34 location.length == length && |
| 35 location.isQualified == isQualified) { |
35 return location; | 36 return location; |
36 } | 37 } |
37 } | 38 } |
38 fail('No at $offset with length $length in\n${locations.join('\n')}'); | 39 fail('No at $offset with length $length qualified=$isQualified in\n' |
| 40 '${locations.join('\n')}'); |
39 return null; | 41 return null; |
40 } | 42 } |
41 | 43 |
42 /** | 44 /** |
43 * Return the [Location] with given properties, or fail. | 45 * Return the [Location] with given properties, or fail. |
44 */ | 46 */ |
45 Location findLocationSource( | 47 Location findLocationSource( |
46 List<Location> locations, Source source, String search, | 48 List<Location> locations, Source source, String search, bool isQualified, |
47 {int length}) { | 49 {int length}) { |
48 String code = source.contents.data; | 50 String code = source.contents.data; |
49 int offset = code.indexOf(search); | 51 int offset = code.indexOf(search); |
50 expect(offset, isNonNegative, reason: 'Not found "$search" in\n$code'); | 52 expect(offset, isNonNegative, reason: 'Not found "$search" in\n$code'); |
51 length ??= getLeadingIdentifierLength(search); | 53 length ??= getLeadingIdentifierLength(search); |
52 String uri = source.uri.toString(); | 54 String uri = source.uri.toString(); |
53 return findLocation(locations, uri, uri, offset, length); | 55 return findLocation(locations, uri, uri, offset, length, isQualified); |
54 } | 56 } |
55 | 57 |
56 /** | 58 /** |
57 * Return the [Location] with given properties, or fail. | 59 * Return the [Location] with given properties, or fail. |
58 */ | 60 */ |
59 Location findLocationTest(List<Location> locations, String search, | 61 Location findLocationTest( |
| 62 List<Location> locations, String search, bool isQualified, |
60 {int length}) { | 63 {int length}) { |
61 int offset = findOffset(search); | 64 int offset = findOffset(search); |
62 length ??= getLeadingIdentifierLength(search); | 65 length ??= getLeadingIdentifierLength(search); |
63 String testUri = testSource.uri.toString(); | 66 String testUri = testSource.uri.toString(); |
64 return findLocation(locations, testUri, testUri, offset, length); | 67 return findLocation( |
| 68 locations, testUri, testUri, offset, length, isQualified); |
65 } | 69 } |
66 | 70 |
67 void setUp() { | 71 void setUp() { |
68 super.setUp(); | 72 super.setUp(); |
69 } | 73 } |
70 | 74 |
71 void tearDown() { | 75 void tearDown() { |
72 super.tearDown(); | 76 super.tearDown(); |
73 index = null; | 77 index = null; |
74 } | 78 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 '''); | 129 '''); |
126 Source source2 = _indexUnit( | 130 Source source2 = _indexUnit( |
127 '/test2.dart', | 131 '/test2.dart', |
128 r''' | 132 r''' |
129 import 'test.dart'; | 133 import 'test.dart'; |
130 class C extends A {} // C | 134 class C extends A {} // C |
131 '''); | 135 '''); |
132 ClassElement elementA = testUnitElement.getType('A'); | 136 ClassElement elementA = testUnitElement.getType('A'); |
133 List<Location> locations = | 137 List<Location> locations = |
134 await index.getRelations(elementA, IndexRelationKind.IS_EXTENDED_BY); | 138 await index.getRelations(elementA, IndexRelationKind.IS_EXTENDED_BY); |
135 findLocationTest(locations, 'A {} // B'); | 139 findLocationTest(locations, 'A {} // B', false); |
136 findLocationSource(locations, source2, 'A {} // C'); | 140 findLocationSource(locations, source2, 'A {} // C', false); |
137 } | 141 } |
138 | 142 |
139 test_getRelations_isReferencedBy() async { | 143 test_getRelations_isReferencedBy() async { |
140 _indexTestUnit(r''' | 144 _indexTestUnit(r''' |
141 main(int a, int b) { | 145 main(int a, int b) { |
142 } | 146 } |
143 '''); | 147 '''); |
144 ClassElement intElement = context.typeProvider.intType.element; | 148 ClassElement intElement = context.typeProvider.intType.element; |
145 List<Location> locations = await index.getRelations( | 149 List<Location> locations = await index.getRelations( |
146 intElement, IndexRelationKind.IS_REFERENCED_BY); | 150 intElement, IndexRelationKind.IS_REFERENCED_BY); |
147 findLocationTest(locations, 'int a'); | 151 findLocationTest(locations, 'int a', false); |
148 findLocationTest(locations, 'int b'); | 152 findLocationTest(locations, 'int b', false); |
149 } | 153 } |
150 | 154 |
151 test_getUnresolvedMemberReferences() async { | 155 test_getUnresolvedMemberReferences_qualified_resolved() async { |
152 _indexTestUnit(''' | 156 _indexTestUnit(''' |
153 class A { | 157 class A { |
154 var test; // A | 158 var test; // A |
155 mainA() { | 159 } |
156 test(); // a-inv-r-nq | 160 main(A a) { |
157 test = 1; // a-ref-r-nq | 161 print(a.test); |
158 test += 2; // a-ref-r-nq | 162 a.test = 1; |
159 print(test); // a-ref-r-nq | 163 a.test += 2; |
| 164 a.test(); |
| 165 } |
| 166 '''); |
| 167 List<Location> locations = |
| 168 await index.getUnresolvedMemberReferences('test'); |
| 169 expect(locations, isEmpty); |
160 } | 170 } |
| 171 |
| 172 test_getUnresolvedMemberReferences_qualified_unresolved() async { |
| 173 _indexTestUnit(''' |
| 174 class A { |
| 175 var test; // A |
161 } | 176 } |
162 main(A a, p) { | 177 main(p) { |
163 a.test(); // a-inv-r-q | 178 print(p.test); |
164 a.test = 1; // a-ref-r-q | 179 p.test = 1; |
165 a.test += 2; // a-ref-r-q | 180 p.test += 2; |
166 print(a.test); // a-ref-r-q | 181 p.test(); |
167 p.test(); // p-inv-ur-q | |
168 p.test = 1; // p-ref-ur-q | |
169 p.test += 2; // p-ref-ur-q | |
170 print(p.test); // p-ref-ur-q | |
171 print(p.test2); // not requested | 182 print(p.test2); // not requested |
172 } | 183 } |
173 '''); | 184 '''); |
174 List<Location> locations = | 185 List<Location> locations = |
175 await index.getUnresolvedMemberReferences('test'); | 186 await index.getUnresolvedMemberReferences('test'); |
176 expect(locations, hasLength(4)); | 187 expect(locations, hasLength(4)); |
177 findLocationTest(locations, 'test(); // p-inv-ur-q'); | 188 findLocationTest(locations, 'test);', true); |
178 findLocationTest(locations, 'test = 1; // p-ref-ur-q'); | 189 findLocationTest(locations, 'test = 1;', true); |
179 findLocationTest(locations, 'test += 2; // p-ref-ur-q'); | 190 findLocationTest(locations, 'test += 2;', true); |
180 findLocationTest(locations, 'test); // p-ref-ur-q'); | 191 findLocationTest(locations, 'test();', true); |
| 192 } |
| 193 |
| 194 test_getUnresolvedMemberReferences_unqualified_resolved() async { |
| 195 _indexTestUnit(''' |
| 196 class A { |
| 197 var test; |
| 198 m() { |
| 199 print(test); |
| 200 test = 1; |
| 201 test += 2; |
| 202 test(); |
| 203 } |
| 204 } |
| 205 '''); |
| 206 List<Location> locations = |
| 207 await index.getUnresolvedMemberReferences('test'); |
| 208 expect(locations, isEmpty); |
| 209 } |
| 210 |
| 211 test_getUnresolvedMemberReferences_unqualified_unresolved() async { |
| 212 verifyNoTestUnitErrors = false; |
| 213 _indexTestUnit(''' |
| 214 class A { |
| 215 m() { |
| 216 print(test); |
| 217 test = 1; |
| 218 test += 2; |
| 219 test(); |
| 220 print(test2); // not requested |
| 221 } |
| 222 } |
| 223 '''); |
| 224 List<Location> locations = |
| 225 await index.getUnresolvedMemberReferences('test'); |
| 226 expect(locations, hasLength(4)); |
| 227 findLocationTest(locations, 'test);', false); |
| 228 findLocationTest(locations, 'test = 1;', false); |
| 229 findLocationTest(locations, 'test += 2;', false); |
| 230 findLocationTest(locations, 'test();', false); |
181 } | 231 } |
182 | 232 |
183 test_indexUnit_nullUnit() async { | 233 test_indexUnit_nullUnit() async { |
184 index.indexUnit(null); | 234 index.indexUnit(null); |
185 } | 235 } |
186 | 236 |
187 test_indexUnit_nullUnitElement() async { | 237 test_indexUnit_nullUnitElement() async { |
188 resolveTestUnit(''); | 238 resolveTestUnit(''); |
189 testUnit.element = null; | 239 testUnit.element = null; |
190 index.indexUnit(testUnit); | 240 index.indexUnit(testUnit); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 index.indexUnit(testUnit); | 303 index.indexUnit(testUnit); |
254 } | 304 } |
255 | 305 |
256 Source _indexUnit(String path, String code) { | 306 Source _indexUnit(String path, String code) { |
257 Source source = addSource(path, code); | 307 Source source = addSource(path, code); |
258 CompilationUnit unit = resolveLibraryUnit(source); | 308 CompilationUnit unit = resolveLibraryUnit(source); |
259 index.indexUnit(unit); | 309 index.indexUnit(unit); |
260 return source; | 310 return source; |
261 } | 311 } |
262 } | 312 } |
OLD | NEW |