| 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 'package:analysis_server/src/services/index/index_contributor.dart'; | |
| 8 import 'package:analysis_server/src/services/index/local_index.dart'; | |
| 9 import 'package:analysis_server/src/services/index/local_memory_index.dart'; | |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | |
| 11 import 'package:analyzer/dart/element/element.dart'; | |
| 12 import 'package:analyzer/src/generated/source_io.dart'; | |
| 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| 14 import 'package:unittest/unittest.dart'; | |
| 15 | |
| 16 import '../../abstract_context.dart'; | |
| 17 import '../../utils.dart'; | |
| 18 import 'store/single_source_container.dart'; | |
| 19 | |
| 20 main() { | |
| 21 initializeTestEnvironment(); | |
| 22 defineReflectiveTests(LocalIndexTest); | |
| 23 } | |
| 24 | |
| 25 void _assertElementNames(List<Element> elements, List expected) { | |
| 26 expect(_toElementNames(elements), unorderedEquals(expected)); | |
| 27 } | |
| 28 | |
| 29 Iterable<String> _toElementNames(List<Element> elements) { | |
| 30 return elements.map((element) => element.name); | |
| 31 } | |
| 32 | |
| 33 @reflectiveTest | |
| 34 class LocalIndexTest extends AbstractContextTest { | |
| 35 LocalIndex index; | |
| 36 | |
| 37 void setUp() { | |
| 38 super.setUp(); | |
| 39 index = createLocalMemoryIndex(); | |
| 40 index.contributors = [new DartIndexContributor()]; | |
| 41 } | |
| 42 | |
| 43 void tearDown() { | |
| 44 super.tearDown(); | |
| 45 index = null; | |
| 46 } | |
| 47 | |
| 48 void test_clear() { | |
| 49 _indexTest('main() {}'); | |
| 50 _assertElementNames(_getTopElements(), ['main']); | |
| 51 // clear | |
| 52 index.clear(); | |
| 53 expect(_getTopElements(), isEmpty); | |
| 54 } | |
| 55 | |
| 56 void test_index() { | |
| 57 _indexTest('main() {}'); | |
| 58 _assertElementNames(_getTopElements(), ['main']); | |
| 59 } | |
| 60 | |
| 61 void test_index_nullObject() { | |
| 62 index.index(context, null); | |
| 63 } | |
| 64 | |
| 65 void test_index_nullUnitElement() { | |
| 66 CompilationUnit unit = new CompilationUnit(null, null, [], [], null); | |
| 67 index.index(context, unit); | |
| 68 } | |
| 69 | |
| 70 void test_removeContext() { | |
| 71 _indexTest('main() {}'); | |
| 72 // OK, there is an element | |
| 73 _assertElementNames(_getTopElements(), ['main']); | |
| 74 // remove context | |
| 75 index.removeContext(context); | |
| 76 expect(_getTopElements(), isEmpty); | |
| 77 } | |
| 78 | |
| 79 void test_removeSource() { | |
| 80 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}'); | |
| 81 _indexLibraryUnit('/testB.dart', 'fb() {}'); | |
| 82 // OK, there are 2 functions | |
| 83 _assertElementNames(_getTopElements(), ['fa', 'fb']); | |
| 84 // remove source | |
| 85 index.removeSource(context, sourceA); | |
| 86 _assertElementNames(_getTopElements(), ['fb']); | |
| 87 } | |
| 88 | |
| 89 void test_removeSources() { | |
| 90 Source sourceA = _indexLibraryUnit('/testA.dart', 'fa() {}'); | |
| 91 _indexLibraryUnit('/testB.dart', 'fb() {}'); | |
| 92 // OK, there are 2 functions | |
| 93 _assertElementNames(_getTopElements(), ['fa', 'fb']); | |
| 94 // remove source(s) | |
| 95 index.removeSources(context, new SingleSourceContainer(sourceA)); | |
| 96 _assertElementNames(_getTopElements(), ['fb']); | |
| 97 } | |
| 98 | |
| 99 void test_statistics() { | |
| 100 expect(index.statistics, '[0 locations, 0 sources, 0 names]'); | |
| 101 } | |
| 102 | |
| 103 List<Element> _getTopElements() { | |
| 104 return index.getTopLevelDeclarations((_) => true); | |
| 105 } | |
| 106 | |
| 107 Source _indexLibraryUnit(String path, String content) { | |
| 108 Source source = addSource(path, content); | |
| 109 CompilationUnit dartUnit = resolveLibraryUnit(source); | |
| 110 index.index(context, dartUnit); | |
| 111 return source; | |
| 112 } | |
| 113 | |
| 114 void _indexTest(String content) { | |
| 115 _indexLibraryUnit('/test.dart', content); | |
| 116 } | |
| 117 } | |
| OLD | NEW |