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.index; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io' show Directory; | |
9 | |
10 import 'package:analysis_server/src/index/index.dart'; | |
11 import 'package:analyzer/src/generated/ast.dart'; | |
12 import 'package:analyzer/src/generated/element.dart'; | |
13 import 'package:analyzer/src/generated/html.dart'; | |
14 import 'package:analyzer/src/generated/index.dart'; | |
15 import 'package:analyzer/src/generated/source_io.dart'; | |
16 import 'package:unittest/unittest.dart'; | |
17 | |
18 import '../abstract_context.dart'; | |
19 import '../reflective_tests.dart'; | |
20 import 'store/memory_node_manager.dart'; | |
21 import 'store/single_source_container.dart'; | |
22 | |
23 | |
24 main() { | |
25 groupSep = ' | '; | |
26 group('LocalIndex', () { | |
27 runReflectiveTests(LocalIndexTest); | |
28 }); | |
29 } | |
30 | |
31 | |
32 void _assertElementNames(List<Location> locations, List expected) { | |
33 expect(_toElementNames(locations), unorderedEquals(expected)); | |
34 } | |
35 | |
36 | |
37 Iterable<String> _toElementNames(List<Location> locations) { | |
38 return locations.map((loc) => loc.element.name); | |
39 } | |
40 | |
41 | |
42 @ReflectiveTestCase() | |
43 class LocalIndexTest extends AbstractContextTest { | |
44 Directory indexDirectory; | |
45 LocalIndex index; | |
46 | |
47 void setUp() { | |
48 super.setUp(); | |
49 // prepare Index | |
50 indexDirectory = Directory.systemTemp.createTempSync( | |
51 'AnalysisServer_index'); | |
52 index = new LocalIndex(new MemoryNodeManager()); | |
53 } | |
54 | |
55 void tearDown() { | |
56 super.tearDown(); | |
57 indexDirectory.delete(recursive: true); | |
58 index = null; | |
59 } | |
60 | |
61 Future test_clear() { | |
62 _indexTest('main() {}'); | |
63 return _getDefinedFunctions().then((locations) { | |
64 _assertElementNames(locations, ['main']); | |
65 // clear | |
66 index.clear(); | |
67 return _getDefinedFunctions().then((locations) { | |
68 expect(locations, isEmpty); | |
69 }); | |
70 }); | |
71 } | |
72 | |
73 void test_getRelationships() { | |
74 var callback = new _RecordingRelationshipCallback(); | |
75 Element element = UniverseElement.INSTANCE; | |
76 index.getRelationships(element, IndexConstants.DEFINES_CLASS, callback); | |
77 expect(callback.locations, isEmpty); | |
78 } | |
79 | |
80 void test_indexHtmlUnit_nullUnit() { | |
81 index.indexHtmlUnit(context, null); | |
82 } | |
83 | |
84 void test_indexHtmlUnit_nullUnitElement() { | |
85 HtmlUnit unit = new HtmlUnit(null, [], null); | |
86 index.indexHtmlUnit(context, unit); | |
87 } | |
88 | |
89 Future test_indexUnit() { | |
90 _indexTest('main() {}'); | |
91 return _getDefinedFunctions().then((locations) { | |
92 _assertElementNames(locations, ['main']); | |
93 }); | |
94 } | |
95 | |
96 void test_indexUnit_nullUnit() { | |
97 index.indexUnit(context, null); | |
98 } | |
99 | |
100 void test_indexUnit_nullUnitElement() { | |
101 CompilationUnit unit = new CompilationUnit(null, null, [], [], null); | |
102 index.indexUnit(context, unit); | |
103 } | |
104 | |
105 Future test_removeContext() { | |
106 _indexTest('main() {}'); | |
107 return _getDefinedFunctions().then((locations) { | |
108 // OK, there is a location | |
109 _assertElementNames(locations, ['main']); | |
110 // remove context | |
111 index.removeContext(context); | |
112 return _getDefinedFunctions().then((locations) { | |
113 expect(locations, isEmpty); | |
114 }); | |
115 }); | |
116 } | |
117 | |
118 Future test_removeSource() { | |
119 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}'); | |
120 _indexLibraryUnit('/testB.dart', 'fb() {}'); | |
121 return _getDefinedFunctions().then((locations) { | |
122 // OK, there are 2 functions | |
123 _assertElementNames(locations, ['fa', 'fb']); | |
124 // remove source | |
125 index.removeSource(context, sourceA); | |
126 return _getDefinedFunctions().then((locations) { | |
127 _assertElementNames(locations, ['fb']); | |
128 }); | |
129 }); | |
130 } | |
131 | |
132 Future test_removeSources() { | |
133 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}'); | |
134 _indexLibraryUnit('/testB.dart', 'fb() {}'); | |
135 return _getDefinedFunctions().then((locations) { | |
136 // OK, there are 2 functions | |
137 _assertElementNames(locations, ['fa', 'fb']); | |
138 // remove source(s) | |
139 index.removeSources(context, new SingleSourceContainer(sourceA)); | |
140 return _getDefinedFunctions().then((locations) { | |
141 _assertElementNames(locations, ['fb']); | |
142 }); | |
143 }); | |
144 } | |
145 | |
146 void test_statistics() { | |
147 expect(index.statistics, '[0 locations, 0 sources, 0 names]'); | |
148 } | |
149 | |
150 Future<List<Location>> _getDefinedFunctions() { | |
151 return index.getRelationshipsAsync(UniverseElement.INSTANCE, | |
152 IndexConstants.DEFINES_FUNCTION); | |
153 } | |
154 | |
155 Source _indexLibraryUnit(String path, String content) { | |
156 Source source = addSource(path, content); | |
157 CompilationUnit dartUnit = resolveLibraryUnit(source); | |
158 index.indexUnit(context, dartUnit); | |
159 return source; | |
160 } | |
161 | |
162 void _indexTest(String content) { | |
163 _indexLibraryUnit('/test.dart', content); | |
164 } | |
165 } | |
166 | |
167 | |
168 /** | |
169 * A [RelationshipCallback] that remembers [Location]s. | |
170 */ | |
171 class _RecordingRelationshipCallback extends RelationshipCallback { | |
172 List<Location> locations; | |
173 | |
174 @override | |
175 void hasRelationships(Element element, Relationship relationship, | |
176 List<Location> locations) { | |
177 this.locations = locations; | |
178 } | |
179 } | |
OLD | NEW |