| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analysis_server/protocol/protocol_generated.dart'; | 7 import 'package:analysis_server/protocol/protocol_generated.dart'; |
| 8 import 'package:analysis_server/src/computer/imported_elements_computer.dart'; | 8 import 'package:analysis_server/src/computer/imported_elements_computer.dart'; |
| 9 import 'package:analyzer/dart/analysis/results.dart'; | 9 import 'package:analyzer/dart/analysis/results.dart'; |
| 10 import 'package:test/test.dart'; | 10 import 'package:test/test.dart'; |
| 11 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 11 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 12 | 12 |
| 13 import '../../abstract_context.dart'; | 13 import '../../abstract_context.dart'; |
| 14 | 14 |
| 15 main() { | 15 main() { |
| 16 defineReflectiveSuite(() { | 16 defineReflectiveSuite(() { |
| 17 defineReflectiveTests(ImportElementsComputerTest); | 17 defineReflectiveTests(ImportElementsComputerTest); |
| 18 }); | 18 }); |
| 19 } | 19 } |
| 20 | 20 |
| 21 @reflectiveTest | 21 @reflectiveTest |
| 22 class ImportElementsComputerTest extends AbstractContextTest { | 22 class ImportElementsComputerTest extends AbstractContextTest { |
| 23 String sourcePath; | 23 String sourcePath; |
| 24 | 24 |
| 25 setUp() { | 25 setUp() { |
| 26 super.setUp(); | 26 super.setUp(); |
| 27 sourcePath = provider.convertPath('/p/lib/source.dart'); | 27 sourcePath = provider.convertPath('/p/lib/source.dart'); |
| 28 } | 28 } |
| 29 | 29 |
| 30 test_none() async { | 30 test_dartAsync_noPrefix() async { |
| 31 String selection = "Future<String> f = null;"; |
| 32 String content = """ |
| 33 import 'dart:async'; |
| 34 printer() { |
| 35 $selection |
| 36 print(await f); |
| 37 } |
| 38 """; |
| 39 List<ImportedElements> elementsList = await _computeElements( |
| 40 content, content.indexOf(selection), selection.length); |
| 41 expect(elementsList, hasLength(2)); |
| 42 ImportedElements elements1 = elementsList[0]; |
| 43 ImportedElements elements2 = elementsList[1]; |
| 44 ImportedElements asyncElements; |
| 45 ImportedElements coreElements; |
| 46 if (elements1.path == '/lib/core/core.dart') { |
| 47 coreElements = elements1; |
| 48 asyncElements = elements2; |
| 49 } else { |
| 50 coreElements = elements2; |
| 51 asyncElements = elements1; |
| 52 } |
| 53 expect(coreElements, isNotNull); |
| 54 expect(coreElements.path, '/lib/core/core.dart'); |
| 55 expect(coreElements.prefix, ''); |
| 56 expect(coreElements.elements, unorderedEquals(['String'])); |
| 57 |
| 58 expect(asyncElements, isNotNull); |
| 59 expect(asyncElements.path, '/lib/async/async.dart'); |
| 60 expect(asyncElements.prefix, ''); |
| 61 expect(asyncElements.elements, unorderedEquals(['Future'])); |
| 62 } |
| 63 |
| 64 test_dartAsync_prefix() async { |
| 65 String selection = "a.Future<String> f = null;"; |
| 66 String content = """ |
| 67 import 'dart:async' as a; |
| 68 printer() { |
| 69 $selection |
| 70 print(await f); |
| 71 } |
| 72 """; |
| 73 List<ImportedElements> elementsList = await _computeElements( |
| 74 content, content.indexOf(selection), selection.length); |
| 75 expect(elementsList, hasLength(2)); |
| 76 ImportedElements elements1 = elementsList[0]; |
| 77 ImportedElements elements2 = elementsList[1]; |
| 78 ImportedElements asyncElements; |
| 79 ImportedElements coreElements; |
| 80 if (elements1.path == '/lib/core/core.dart') { |
| 81 coreElements = elements1; |
| 82 asyncElements = elements2; |
| 83 } else { |
| 84 coreElements = elements2; |
| 85 asyncElements = elements1; |
| 86 } |
| 87 expect(coreElements, isNotNull); |
| 88 expect(coreElements.path, '/lib/core/core.dart'); |
| 89 expect(coreElements.prefix, ''); |
| 90 expect(coreElements.elements, unorderedEquals(['String'])); |
| 91 |
| 92 expect(asyncElements, isNotNull); |
| 93 expect(asyncElements.path, '/lib/async/async.dart'); |
| 94 expect(asyncElements.prefix, 'a'); |
| 95 expect(asyncElements.elements, unorderedEquals(['Future'])); |
| 96 } |
| 97 |
| 98 test_dartCore_noPrefix() async { |
| 99 String selection = "String s = '';"; |
| 100 String content = """ |
| 101 blankLine() { |
| 102 $selection |
| 103 print(s); |
| 104 } |
| 105 """; |
| 106 List<ImportedElements> elementsList = await _computeElements( |
| 107 content, content.indexOf(selection), selection.length); |
| 108 expect(elementsList, hasLength(1)); |
| 109 ImportedElements elements = elementsList[0]; |
| 110 expect(elements, isNotNull); |
| 111 expect(elements.path, '/lib/core/core.dart'); |
| 112 expect(elements.prefix, ''); |
| 113 expect(elements.elements, unorderedEquals(['String'])); |
| 114 } |
| 115 |
| 116 test_dartCore_prefix() async { |
| 117 String selection = "core.String s = '';"; |
| 118 String content = """ |
| 119 import 'dart:core' as core; |
| 120 blankLine() { |
| 121 $selection |
| 122 print(s); |
| 123 } |
| 124 """; |
| 125 List<ImportedElements> elementsList = await _computeElements( |
| 126 content, content.indexOf(selection), selection.length); |
| 127 expect(elementsList, hasLength(1)); |
| 128 ImportedElements elements = elementsList[0]; |
| 129 expect(elements, isNotNull); |
| 130 expect(elements.path, '/lib/core/core.dart'); |
| 131 expect(elements.prefix, 'core'); |
| 132 expect(elements.elements, unorderedEquals(['String'])); |
| 133 } |
| 134 |
| 135 test_none_partialNames() async { |
| 136 String selection = 'x + y'; |
| 137 String content = """ |
| 138 plusThree(int xx) { |
| 139 int yy = 2; |
| 140 print(x${selection}y); |
| 141 } |
| 142 """; |
| 143 List<ImportedElements> elementsList = await _computeElements( |
| 144 content, content.indexOf(selection), selection.length); |
| 145 expect(elementsList, hasLength(0)); |
| 146 } |
| 147 |
| 148 test_none_wholeNames() async { |
| 31 String selection = 'x + y + 1'; | 149 String selection = 'x + y + 1'; |
| 32 String content = """ | 150 String content = """ |
| 33 plusThree(int x) { | 151 plusThree(int x) { |
| 34 int y = 2; | 152 int y = 2; |
| 35 print($selection); | 153 print($selection); |
| 36 } | 154 } |
| 37 """; | 155 """; |
| 38 List<ImportedElements> elements = await _computeElements( | 156 List<ImportedElements> elementsList = await _computeElements( |
| 39 content, content.indexOf(selection), selection.length); | 157 content, content.indexOf(selection), selection.length); |
| 40 expect(elements, hasLength(0)); | 158 expect(elementsList, hasLength(0)); |
| 159 } |
| 160 |
| 161 test_package_multipleInSame() async { |
| 162 addPackageSource('foo', 'foo.dart', ''' |
| 163 class A { |
| 164 static String a = ''; |
| 165 } |
| 166 class B { |
| 167 static String b = ''; |
| 168 } |
| 169 '''); |
| 170 String selection = "A.a + B.b"; |
| 171 String content = """ |
| 172 import 'package:foo/foo.dart'; |
| 173 blankLine() { |
| 174 print($selection); |
| 175 } |
| 176 """; |
| 177 List<ImportedElements> elementsList = await _computeElements( |
| 178 content, content.indexOf(selection), selection.length); |
| 179 expect(elementsList, hasLength(1)); |
| 180 ImportedElements elements = elementsList[0]; |
| 181 expect(elements, isNotNull); |
| 182 expect(elements.path, '/pubcache/foo/lib/foo.dart'); |
| 183 expect(elements.prefix, ''); |
| 184 expect(elements.elements, unorderedEquals(['A', 'B'])); |
| 185 } |
| 186 |
| 187 test_package_noPrefix() async { |
| 188 addPackageSource('foo', 'foo.dart', ''' |
| 189 class Foo { |
| 190 static String first = ''; |
| 191 } |
| 192 '''); |
| 193 String selection = "Foo.first"; |
| 194 String content = """ |
| 195 import 'package:foo/foo.dart'; |
| 196 blankLine() { |
| 197 print($selection); |
| 198 } |
| 199 """; |
| 200 List<ImportedElements> elementsList = await _computeElements( |
| 201 content, content.indexOf(selection), selection.length); |
| 202 expect(elementsList, hasLength(1)); |
| 203 ImportedElements elements = elementsList[0]; |
| 204 expect(elements, isNotNull); |
| 205 expect(elements.path, '/pubcache/foo/lib/foo.dart'); |
| 206 expect(elements.prefix, ''); |
| 207 expect(elements.elements, unorderedEquals(['Foo'])); |
| 208 } |
| 209 |
| 210 test_package_prefix_selected() async { |
| 211 addPackageSource('foo', 'foo.dart', ''' |
| 212 class Foo { |
| 213 static String first = ''; |
| 214 } |
| 215 '''); |
| 216 String selection = "f.Foo.first"; |
| 217 String content = """ |
| 218 import 'package:foo/foo.dart' as f; |
| 219 blankLine() { |
| 220 print($selection); |
| 221 } |
| 222 """; |
| 223 List<ImportedElements> elementsList = await _computeElements( |
| 224 content, content.indexOf(selection), selection.length); |
| 225 expect(elementsList, hasLength(1)); |
| 226 ImportedElements elements = elementsList[0]; |
| 227 expect(elements, isNotNull); |
| 228 expect(elements.path, '/pubcache/foo/lib/foo.dart'); |
| 229 expect(elements.prefix, 'f'); |
| 230 expect(elements.elements, unorderedEquals(['Foo'])); |
| 231 } |
| 232 |
| 233 test_package_prefix_unselected() async { |
| 234 addPackageSource('foo', 'foo.dart', ''' |
| 235 class Foo { |
| 236 static String first = ''; |
| 237 } |
| 238 '''); |
| 239 String selection = "Foo.first"; |
| 240 String content = """ |
| 241 import 'package:foo/foo.dart' as f; |
| 242 blankLine() { |
| 243 print(f.$selection); |
| 244 } |
| 245 """; |
| 246 List<ImportedElements> elementsList = await _computeElements( |
| 247 content, content.indexOf(selection), selection.length); |
| 248 expect(elementsList, hasLength(1)); |
| 249 ImportedElements elements = elementsList[0]; |
| 250 expect(elements, isNotNull); |
| 251 expect(elements.path, '/pubcache/foo/lib/foo.dart'); |
| 252 expect(elements.prefix, ''); |
| 253 expect(elements.elements, unorderedEquals(['Foo'])); |
| 254 } |
| 255 |
| 256 test_package_prefixedAndNot() async { |
| 257 addPackageSource('foo', 'foo.dart', ''' |
| 258 class Foo { |
| 259 static String first = ''; |
| 260 static String second = ''; |
| 261 } |
| 262 '''); |
| 263 String selection = "f.Foo.first + Foo.second"; |
| 264 String content = """ |
| 265 import 'package:foo/foo.dart'; |
| 266 import 'package:foo/foo.dart' as f; |
| 267 blankLine() { |
| 268 print($selection); |
| 269 } |
| 270 """; |
| 271 List<ImportedElements> elementsList = await _computeElements( |
| 272 content, content.indexOf(selection), selection.length); |
| 273 |
| 274 expect(elementsList, hasLength(2)); |
| 275 ImportedElements elements1 = elementsList[0]; |
| 276 ImportedElements elements2 = elementsList[1]; |
| 277 ImportedElements notPrefixedElements; |
| 278 ImportedElements prefixedElements; |
| 279 if (elements1.prefix == '') { |
| 280 prefixedElements = elements2; |
| 281 notPrefixedElements = elements1; |
| 282 } else { |
| 283 prefixedElements = elements1; |
| 284 notPrefixedElements = elements2; |
| 285 } |
| 286 |
| 287 expect(notPrefixedElements, isNotNull); |
| 288 expect(notPrefixedElements.path, '/pubcache/foo/lib/foo.dart'); |
| 289 expect(notPrefixedElements.prefix, ''); |
| 290 expect(notPrefixedElements.elements, unorderedEquals(['Foo'])); |
| 291 |
| 292 expect(prefixedElements, isNotNull); |
| 293 expect(prefixedElements.path, '/pubcache/foo/lib/foo.dart'); |
| 294 expect(prefixedElements.prefix, 'f'); |
| 295 expect(prefixedElements.elements, unorderedEquals(['Foo'])); |
| 41 } | 296 } |
| 42 | 297 |
| 43 Future<List<ImportedElements>> _computeElements( | 298 Future<List<ImportedElements>> _computeElements( |
| 44 String sourceContent, int offset, int length) async { | 299 String sourceContent, int offset, int length) async { |
| 45 provider.newFile(sourcePath, sourceContent); | 300 provider.newFile(sourcePath, sourceContent); |
| 46 ResolveResult result = await driver.getResult(sourcePath); | 301 ResolveResult result = await driver.getResult(sourcePath); |
| 47 ImportedElementsComputer computer = | 302 ImportedElementsComputer computer = |
| 48 new ImportedElementsComputer(result.unit, offset, length); | 303 new ImportedElementsComputer(result.unit, offset, length); |
| 49 return computer.compute(); | 304 return computer.compute(); |
| 50 } | 305 } |
| 51 } | 306 } |
| OLD | NEW |