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.services.src.index.local_index; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:analysis_services/index/index.dart'; | |
10 import 'package:analysis_services/index/local_memory_index.dart'; | |
11 import 'package:analysis_services/src/index/local_index.dart'; | |
12 import 'package:analysis_testing/abstract_context.dart'; | |
13 import 'package:analysis_testing/reflective_tests.dart'; | |
14 import 'package:analyzer/src/generated/ast.dart'; | |
15 import 'package:analyzer/src/generated/html.dart'; | |
16 import 'package:analyzer/src/generated/source_io.dart'; | |
17 import 'package:unittest/unittest.dart'; | |
18 | |
19 import 'store/single_source_container.dart'; | |
20 | |
21 | |
22 main() { | |
23 groupSep = ' | '; | |
24 runReflectiveTests(LocalIndexTest); | |
25 } | |
26 | |
27 | |
28 void _assertElementNames(List<Location> locations, List expected) { | |
29 expect(_toElementNames(locations), unorderedEquals(expected)); | |
30 } | |
31 | |
32 | |
33 Iterable<String> _toElementNames(List<Location> locations) { | |
34 return locations.map((loc) => loc.element.name); | |
35 } | |
36 | |
37 | |
38 @ReflectiveTestCase() | |
39 class LocalIndexTest extends AbstractContextTest { | |
40 LocalIndex index; | |
41 | |
42 void setUp() { | |
43 super.setUp(); | |
44 index = createLocalMemoryIndex(); | |
45 } | |
46 | |
47 void tearDown() { | |
48 super.tearDown(); | |
49 index = null; | |
50 } | |
51 | |
52 Future test_clear() { | |
53 _indexTest('main() {}'); | |
54 return _getDefinedFunctions().then((locations) { | |
55 _assertElementNames(locations, ['main']); | |
56 // clear | |
57 index.clear(); | |
58 return _getDefinedFunctions().then((locations) { | |
59 expect(locations, isEmpty); | |
60 }); | |
61 }); | |
62 } | |
63 | |
64 void test_indexHtmlUnit_nullUnit() { | |
65 index.indexHtmlUnit(context, null); | |
66 } | |
67 | |
68 void test_indexHtmlUnit_nullUnitElement() { | |
69 HtmlUnit unit = new HtmlUnit(null, [], null); | |
70 index.indexHtmlUnit(context, unit); | |
71 } | |
72 | |
73 Future test_indexUnit() { | |
74 _indexTest('main() {}'); | |
75 return _getDefinedFunctions().then((locations) { | |
76 _assertElementNames(locations, ['main']); | |
77 }); | |
78 } | |
79 | |
80 void test_indexUnit_nullUnit() { | |
81 index.indexUnit(context, null); | |
82 } | |
83 | |
84 void test_indexUnit_nullUnitElement() { | |
85 CompilationUnit unit = new CompilationUnit(null, null, [], [], null); | |
86 index.indexUnit(context, unit); | |
87 } | |
88 | |
89 Future test_removeContext() { | |
90 _indexTest('main() {}'); | |
91 return _getDefinedFunctions().then((locations) { | |
92 // OK, there is a location | |
93 _assertElementNames(locations, ['main']); | |
94 // remove context | |
95 index.removeContext(context); | |
96 return _getDefinedFunctions().then((locations) { | |
97 expect(locations, isEmpty); | |
98 }); | |
99 }); | |
100 } | |
101 | |
102 Future test_removeSource() { | |
103 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}'); | |
104 _indexLibraryUnit('/testB.dart', 'fb() {}'); | |
105 return _getDefinedFunctions().then((locations) { | |
106 // OK, there are 2 functions | |
107 _assertElementNames(locations, ['fa', 'fb']); | |
108 // remove source | |
109 index.removeSource(context, sourceA); | |
110 return _getDefinedFunctions().then((locations) { | |
111 _assertElementNames(locations, ['fb']); | |
112 }); | |
113 }); | |
114 } | |
115 | |
116 Future test_removeSources() { | |
117 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}'); | |
118 _indexLibraryUnit('/testB.dart', 'fb() {}'); | |
119 return _getDefinedFunctions().then((locations) { | |
120 // OK, there are 2 functions | |
121 _assertElementNames(locations, ['fa', 'fb']); | |
122 // remove source(s) | |
123 index.removeSources(context, new SingleSourceContainer(sourceA)); | |
124 return _getDefinedFunctions().then((locations) { | |
125 _assertElementNames(locations, ['fb']); | |
126 }); | |
127 }); | |
128 } | |
129 | |
130 void test_statistics() { | |
131 expect(index.statistics, '[0 locations, 0 sources, 0 names]'); | |
132 } | |
133 | |
134 Future<List<Location>> _getDefinedFunctions() { | |
135 return index.getRelationships( | |
136 UniverseElement.INSTANCE, | |
137 IndexConstants.DEFINES); | |
138 } | |
139 | |
140 Source _indexLibraryUnit(String path, String content) { | |
141 Source source = addSource(path, content); | |
142 CompilationUnit dartUnit = resolveLibraryUnit(source); | |
143 index.indexUnit(context, dartUnit); | |
144 return source; | |
145 } | |
146 | |
147 void _indexTest(String content) { | |
148 _indexLibraryUnit('/test.dart', content); | |
149 } | |
150 } | |
OLD | NEW |