Chromium Code Reviews| 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 { |
|
scheglov
2017/08/01 15:16:34
I would add a test for using a class defined in th
Brian Wilkerson
2017/08/01 15:31:43
Done
| |
| 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_comment() async { | |
| 136 String selection = 'comment'; | |
| 137 String content = """ | |
| 138 // Method $selection. | |
| 139 blankLine() { | |
| 140 print(''); | |
| 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_partialNames() async { | |
| 149 String selection = 'x + y'; | |
| 150 String content = """ | |
| 151 plusThree(int xx) { | |
| 152 int yy = 2; | |
| 153 print(x${selection}y); | |
| 154 } | |
| 155 """; | |
| 156 List<ImportedElements> elementsList = await _computeElements( | |
| 157 content, content.indexOf(selection), selection.length); | |
| 158 expect(elementsList, hasLength(0)); | |
| 159 } | |
| 160 | |
| 161 test_none_wholeNames() async { | |
| 31 String selection = 'x + y + 1'; | 162 String selection = 'x + y + 1'; |
| 32 String content = """ | 163 String content = """ |
| 33 plusThree(int x) { | 164 plusThree(int x) { |
| 34 int y = 2; | 165 int y = 2; |
| 35 print($selection); | 166 print($selection); |
| 36 } | 167 } |
| 37 """; | 168 """; |
| 38 List<ImportedElements> elements = await _computeElements( | 169 List<ImportedElements> elementsList = await _computeElements( |
| 39 content, content.indexOf(selection), selection.length); | 170 content, content.indexOf(selection), selection.length); |
| 40 expect(elements, hasLength(0)); | 171 expect(elementsList, hasLength(0)); |
| 172 } | |
| 173 | |
| 174 test_package_multipleInSame() async { | |
| 175 addPackageSource('foo', 'foo.dart', ''' | |
| 176 class A { | |
| 177 static String a = ''; | |
| 178 } | |
| 179 class B { | |
| 180 static String b = ''; | |
| 181 } | |
| 182 '''); | |
| 183 String selection = "A.a + B.b"; | |
| 184 String content = """ | |
| 185 import 'package:foo/foo.dart'; | |
| 186 blankLine() { | |
| 187 print($selection); | |
| 188 } | |
| 189 """; | |
| 190 List<ImportedElements> elementsList = await _computeElements( | |
| 191 content, content.indexOf(selection), selection.length); | |
| 192 expect(elementsList, hasLength(1)); | |
| 193 ImportedElements elements = elementsList[0]; | |
| 194 expect(elements, isNotNull); | |
| 195 expect(elements.path, '/pubcache/foo/lib/foo.dart'); | |
| 196 expect(elements.prefix, ''); | |
| 197 expect(elements.elements, unorderedEquals(['A', 'B'])); | |
| 198 } | |
| 199 | |
| 200 test_package_noPrefix() async { | |
| 201 addPackageSource('foo', 'foo.dart', ''' | |
| 202 class Foo { | |
| 203 static String first = ''; | |
| 204 } | |
| 205 '''); | |
| 206 String selection = "Foo.first"; | |
| 207 String content = """ | |
| 208 import 'package:foo/foo.dart'; | |
| 209 blankLine() { | |
| 210 print($selection); | |
| 211 } | |
| 212 """; | |
| 213 List<ImportedElements> elementsList = await _computeElements( | |
| 214 content, content.indexOf(selection), selection.length); | |
| 215 expect(elementsList, hasLength(1)); | |
| 216 ImportedElements elements = elementsList[0]; | |
| 217 expect(elements, isNotNull); | |
| 218 expect(elements.path, '/pubcache/foo/lib/foo.dart'); | |
| 219 expect(elements.prefix, ''); | |
| 220 expect(elements.elements, unorderedEquals(['Foo'])); | |
| 221 } | |
| 222 | |
| 223 test_package_prefix_selected() async { | |
| 224 addPackageSource('foo', 'foo.dart', ''' | |
| 225 class Foo { | |
| 226 static String first = ''; | |
| 227 } | |
| 228 '''); | |
| 229 String selection = "f.Foo.first"; | |
| 230 String content = """ | |
| 231 import 'package:foo/foo.dart' as f; | |
| 232 blankLine() { | |
| 233 print($selection); | |
| 234 } | |
| 235 """; | |
| 236 List<ImportedElements> elementsList = await _computeElements( | |
| 237 content, content.indexOf(selection), selection.length); | |
| 238 expect(elementsList, hasLength(1)); | |
| 239 ImportedElements elements = elementsList[0]; | |
| 240 expect(elements, isNotNull); | |
| 241 expect(elements.path, '/pubcache/foo/lib/foo.dart'); | |
| 242 expect(elements.prefix, 'f'); | |
| 243 expect(elements.elements, unorderedEquals(['Foo'])); | |
| 244 } | |
| 245 | |
| 246 test_package_prefix_unselected() async { | |
| 247 addPackageSource('foo', 'foo.dart', ''' | |
| 248 class Foo { | |
| 249 static String first = ''; | |
| 250 } | |
| 251 '''); | |
| 252 String selection = "Foo.first"; | |
| 253 String content = """ | |
| 254 import 'package:foo/foo.dart' as f; | |
| 255 blankLine() { | |
| 256 print(f.$selection); | |
| 257 } | |
| 258 """; | |
| 259 List<ImportedElements> elementsList = await _computeElements( | |
| 260 content, content.indexOf(selection), selection.length); | |
| 261 expect(elementsList, hasLength(1)); | |
| 262 ImportedElements elements = elementsList[0]; | |
| 263 expect(elements, isNotNull); | |
| 264 expect(elements.path, '/pubcache/foo/lib/foo.dart'); | |
| 265 expect(elements.prefix, ''); | |
| 266 expect(elements.elements, unorderedEquals(['Foo'])); | |
| 267 } | |
| 268 | |
| 269 test_package_prefixedAndNot() async { | |
| 270 addPackageSource('foo', 'foo.dart', ''' | |
| 271 class Foo { | |
| 272 static String first = ''; | |
| 273 static String second = ''; | |
| 274 } | |
| 275 '''); | |
| 276 String selection = "f.Foo.first + Foo.second"; | |
| 277 String content = """ | |
| 278 import 'package:foo/foo.dart'; | |
| 279 import 'package:foo/foo.dart' as f; | |
| 280 blankLine() { | |
| 281 print($selection); | |
| 282 } | |
| 283 """; | |
| 284 List<ImportedElements> elementsList = await _computeElements( | |
| 285 content, content.indexOf(selection), selection.length); | |
| 286 | |
| 287 expect(elementsList, hasLength(2)); | |
| 288 ImportedElements elements1 = elementsList[0]; | |
| 289 ImportedElements elements2 = elementsList[1]; | |
| 290 ImportedElements notPrefixedElements; | |
| 291 ImportedElements prefixedElements; | |
| 292 if (elements1.prefix == '') { | |
| 293 prefixedElements = elements2; | |
| 294 notPrefixedElements = elements1; | |
| 295 } else { | |
| 296 prefixedElements = elements1; | |
| 297 notPrefixedElements = elements2; | |
| 298 } | |
| 299 | |
| 300 expect(notPrefixedElements, isNotNull); | |
| 301 expect(notPrefixedElements.path, '/pubcache/foo/lib/foo.dart'); | |
| 302 expect(notPrefixedElements.prefix, ''); | |
| 303 expect(notPrefixedElements.elements, unorderedEquals(['Foo'])); | |
| 304 | |
| 305 expect(prefixedElements, isNotNull); | |
| 306 expect(prefixedElements.path, '/pubcache/foo/lib/foo.dart'); | |
| 307 expect(prefixedElements.prefix, 'f'); | |
| 308 expect(prefixedElements.elements, unorderedEquals(['Foo'])); | |
| 41 } | 309 } |
| 42 | 310 |
| 43 Future<List<ImportedElements>> _computeElements( | 311 Future<List<ImportedElements>> _computeElements( |
| 44 String sourceContent, int offset, int length) async { | 312 String sourceContent, int offset, int length) async { |
| 45 provider.newFile(sourcePath, sourceContent); | 313 provider.newFile(sourcePath, sourceContent); |
| 46 ResolveResult result = await driver.getResult(sourcePath); | 314 ResolveResult result = await driver.getResult(sourcePath); |
| 47 ImportedElementsComputer computer = | 315 ImportedElementsComputer computer = |
| 48 new ImportedElementsComputer(result.unit, offset, length); | 316 new ImportedElementsComputer(result.unit, offset, length); |
| 49 return computer.compute(); | 317 return computer.compute(); |
| 50 } | 318 } |
| 51 } | 319 } |
| OLD | NEW |