Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1932)

Side by Side Diff: pkg/analysis_server/test/index/index_test.dart

Issue 351813002: A local file-based Index implementation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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:analysis_server/src/resource.dart';
12 import 'package:analyzer/src/generated/ast.dart';
13 import 'package:analyzer/src/generated/element.dart';
14 import 'package:analyzer/src/generated/engine.dart';
15 import 'package:analyzer/src/generated/html.dart';
16 import 'package:analyzer/src/generated/index.dart';
17 import 'package:analyzer/src/generated/sdk.dart';
18 import 'package:analyzer/src/generated/source_io.dart';
19 import 'package:unittest/unittest.dart';
20
21 import '../mocks.dart';
22 import '../reflective_tests.dart';
23 import 'store/single_source_container.dart';
24
25
26 main() {
27 groupSep = ' | ';
28 group('ServerIndex', () {
29 runReflectiveTests(_LocalSplitIndexTest);
30 });
31 }
32
33
34 void _assertElementNames(List<Location> locations, List expected) {
35 expect(_toElementNames(locations), unorderedEquals(expected));
36 }
37
38
39 Iterable<String> _toElementNames(List<Location> locations) {
40 return locations.map((loc) => loc.element.name);
41 }
42
43
44 @ReflectiveTestCase()
45 class _LocalSplitIndexTest {
46 static final DartSdk SDK = new MockSdk();
47
48 AnalysisContext context;
49 LocalSplitIndex index;
50 Directory indexDirectory;
51 MemoryResourceProvider provider = new MemoryResourceProvider();
Paul Berry 2014/06/24 15:49:59 This should be initialized in setUp().
scheglov 2014/06/24 18:07:46 Why? It does not reference any other field, so can
52
53 void setUp() {
54 // prepare Index
55 indexDirectory = Directory.systemTemp.createTempSync(
56 'AnalysisServer_index');
57 index = new LocalSplitIndex(indexDirectory);
58 // prepare AnalysisContext
59 context = AnalysisEngine.instance.createAnalysisContext();
60 context.sourceFactory = new SourceFactory(<UriResolver>[new DartUriResolver(
61 SDK), new ResourceUriResolver(provider)]);
62 }
63
64 void tearDown() {
65 indexDirectory.delete(recursive: true);
66 index = null;
67 context = null;
68 provider = null;
69 }
70
71 test_clear() {
Paul Berry 2014/06/24 15:49:59 Since it is a common mistake to forget to return n
scheglov 2014/06/24 18:07:46 I'm not 100% sure that declaring Future explicitly
72 _indexTest('main() {}');
73 return _getDefinedFunctions().then((locations) {
74 _assertElementNames(locations, ['main']);
75 // clear
76 index.clear();
77 return _getDefinedFunctions().then((locations) {
78 expect(locations, isEmpty);
79 });
80 });
81 }
82
83 void test_getRelationships() {
84 var callback = new _RecordingRelationshipCallback();
85 Element element = UniverseElement.INSTANCE;
86 index.getRelationships(element, IndexConstants.DEFINES_CLASS, callback);
87 expect(callback.locations, isEmpty);
88 }
Paul Berry 2014/06/24 15:49:59 We should also test that getRelationships() passes
scheglov 2014/06/24 18:07:46 Actually we cannot. getRelationships() implements
89
90 test_indexHtmlUnit_nullUnit() {
Paul Berry 2014/06/24 15:49:59 Similarly, I'd prefer to see this function's retur
scheglov 2014/06/24 18:07:46 Done, thank you for catching this.
91 index.indexHtmlUnit(context, null);
92 }
93
94 test_indexHtmlUnit_nullUnitElement() {
95 HtmlUnit unit = new HtmlUnit(null, [], null);
96 index.indexHtmlUnit(context, unit);
97 }
98
99 test_indexUnit() {
100 _indexTest('main() {}');
101 return _getDefinedFunctions().then((locations) {
102 _assertElementNames(locations, ['main']);
103 });
104 }
105
106 test_indexUnit_nullUnit() {
107 index.indexUnit(context, null);
108 }
109
110 test_indexUnit_nullUnitElement() {
111 CompilationUnit unit = new CompilationUnit(null, null, [], [], null);
112 index.indexUnit(context, unit);
113 }
114
115 test_removeContext() {
116 _indexTest('main() {}');
117 return _getDefinedFunctions().then((locations) {
118 // OK, there is a location
119 _assertElementNames(locations, ['main']);
120 // remove context
121 index.removeContext(context);
122 return _getDefinedFunctions().then((locations) {
123 expect(locations, isEmpty);
124 });
125 });
126 }
127
128 test_removeSource() {
129 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}');
130 _indexLibraryUnit('/testB.dart', 'fb() {}');
131 return _getDefinedFunctions().then((locations) {
132 // OK, there are 2 functions
133 _assertElementNames(locations, ['fa', 'fb']);
134 // remove source
135 index.removeSource(context, sourceA);
136 return _getDefinedFunctions().then((locations) {
137 _assertElementNames(locations, ['fb']);
138 });
139 });
140 }
141
142 test_removeSources() {
143 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}');
144 _indexLibraryUnit('/testB.dart', 'fb() {}');
145 return _getDefinedFunctions().then((locations) {
146 // OK, there are 2 functions
147 _assertElementNames(locations, ['fa', 'fb']);
148 // remove source(s)
149 index.removeSources(context, new SingleSourceContainer(sourceA));
150 return _getDefinedFunctions().then((locations) {
151 _assertElementNames(locations, ['fb']);
152 });
153 });
154 }
155
156 void test_statistics() {
157 expect(index.statistics, '[0 locations, 0 sources, 0 names]');
158 }
159
160 Source _addSource(String path, String content) {
161 File file = provider.newFile(path, content);
162 Source source = file.createSource(UriKind.FILE_URI);
163 ChangeSet changeSet = new ChangeSet();
164 changeSet.addedSource(source);
165 context.applyChanges(changeSet);
166 context.setContents(source, content);
167 return source;
168 }
169
170 Future<List<Location>> _getDefinedFunctions() {
171 return index.getRelationshipsAsync(UniverseElement.INSTANCE,
172 IndexConstants.DEFINES_FUNCTION);
173 }
174
175 Source _indexLibraryUnit(String path, String content) {
176 Source source = _addSource(path, content);
177 CompilationUnit dartUnit = _resolveLibraryUnit(source);
178 index.indexUnit(context, dartUnit);
179 return source;
180 }
181
182 void _indexTest(String content) {
183 _indexLibraryUnit('/test.dart', content);
184 }
185
186 CompilationUnit _resolveLibraryUnit(Source source) {
187 return context.resolveCompilationUnit2(source, source);
188 }
189 }
190
191
192 /**
193 * A [RelationshipCallback] that remembers [Location]s.
194 */
195 class _RecordingRelationshipCallback extends RelationshipCallback {
196 List<Location> locations;
197
198 @override
199 void hasRelationships(Element element, Relationship relationship,
200 List<Location> locations) {
201 this.locations = locations;
202 }
203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698