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