| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, 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 import 'dart:async'; | |
| 6 | |
| 7 import 'package:analysis_server/protocol/protocol_generated.dart'; | |
| 8 import 'package:analysis_server/src/provisional/edit/utilities/change_builder_co
re.dart'; | |
| 9 import 'package:analysis_server/src/provisional/edit/utilities/change_builder_da
rt.dart'; | |
| 10 import 'package:analysis_server/src/utilities/change_builder_dart.dart'; | |
| 11 import 'package:analyzer/dart/ast/ast.dart'; | |
| 12 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | |
| 13 import 'package:analyzer/dart/element/element.dart'; | |
| 14 import 'package:analyzer/dart/element/type.dart'; | |
| 15 import 'package:analyzer/src/context/source.dart'; | |
| 16 import 'package:analyzer/src/dart/analysis/driver.dart'; | |
| 17 import 'package:analyzer/src/generated/engine.dart'; | |
| 18 import 'package:analyzer/src/generated/resolver.dart'; | |
| 19 import 'package:analyzer/src/generated/source.dart'; | |
| 20 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; | |
| 21 import 'package:test/test.dart'; | |
| 22 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| 23 | |
| 24 import '../../abstract_context.dart'; | |
| 25 | |
| 26 main() { | |
| 27 defineReflectiveSuite(() { | |
| 28 defineReflectiveTests(DartChangeBuilderImplTest); | |
| 29 defineReflectiveTests(DartEditBuilderImplTest); | |
| 30 defineReflectiveTests(DartFileEditBuilderImplTest); | |
| 31 defineReflectiveTests(DartLinkedEditBuilderImplTest); | |
| 32 }); | |
| 33 } | |
| 34 | |
| 35 abstract class BuilderTestMixin { | |
| 36 SourceEdit getEdit(DartChangeBuilderImpl builder) { | |
| 37 SourceChange sourceChange = builder.sourceChange; | |
| 38 expect(sourceChange, isNotNull); | |
| 39 List<SourceFileEdit> fileEdits = sourceChange.edits; | |
| 40 expect(fileEdits, hasLength(1)); | |
| 41 SourceFileEdit fileEdit = fileEdits[0]; | |
| 42 expect(fileEdit, isNotNull); | |
| 43 List<SourceEdit> edits = fileEdit.edits; | |
| 44 expect(edits, hasLength(1)); | |
| 45 return edits[0]; | |
| 46 } | |
| 47 | |
| 48 List<SourceEdit> getEdits(DartChangeBuilderImpl builder) { | |
| 49 SourceChange sourceChange = builder.sourceChange; | |
| 50 expect(sourceChange, isNotNull); | |
| 51 List<SourceFileEdit> fileEdits = sourceChange.edits; | |
| 52 expect(fileEdits, hasLength(1)); | |
| 53 SourceFileEdit fileEdit = fileEdits[0]; | |
| 54 expect(fileEdit, isNotNull); | |
| 55 return fileEdit.edits; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 @reflectiveTest | |
| 60 class DartChangeBuilderImplTest extends AbstractContextTest { | |
| 61 @override | |
| 62 bool get enableNewAnalysisDriver => true; | |
| 63 | |
| 64 test_createFileEditBuilder() async { | |
| 65 String path = '/test.dart'; | |
| 66 addSource(path, 'library test;'); | |
| 67 int timeStamp = 54; | |
| 68 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 69 DartFileEditBuilderImpl fileEditBuilder = | |
| 70 await builder.createFileEditBuilder(path, timeStamp); | |
| 71 expect(fileEditBuilder, new isInstanceOf<DartFileEditBuilder>()); | |
| 72 SourceFileEdit fileEdit = fileEditBuilder.fileEdit; | |
| 73 expect(fileEdit.file, path); | |
| 74 expect(fileEdit.fileStamp, timeStamp); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 @reflectiveTest | |
| 79 class DartEditBuilderImplTest extends AbstractContextTest | |
| 80 with BuilderTestMixin { | |
| 81 @override | |
| 82 bool get enableNewAnalysisDriver => true; | |
| 83 | |
| 84 test_writeClassDeclaration_interfaces() async { | |
| 85 String path = '/test.dart'; | |
| 86 addSource(path, 'class A {}'); | |
| 87 DartType typeA = await _getType(path, 'A'); | |
| 88 | |
| 89 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 90 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 91 builder.addInsertion(0, (EditBuilder builder) { | |
| 92 (builder as DartEditBuilder) | |
| 93 .writeClassDeclaration('C', interfaces: [typeA]); | |
| 94 }); | |
| 95 }); | |
| 96 SourceEdit edit = getEdit(builder); | |
| 97 expect( | |
| 98 edit.replacement, equalsIgnoringWhitespace('class C implements A { }')); | |
| 99 } | |
| 100 | |
| 101 test_writeClassDeclaration_isAbstract() async { | |
| 102 String path = '/test.dart'; | |
| 103 addSource(path, ''); | |
| 104 | |
| 105 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 106 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 107 builder.addInsertion(0, (EditBuilder builder) { | |
| 108 (builder as DartEditBuilder) | |
| 109 .writeClassDeclaration('C', isAbstract: true); | |
| 110 }); | |
| 111 }); | |
| 112 SourceEdit edit = getEdit(builder); | |
| 113 expect(edit.replacement, equalsIgnoringWhitespace('abstract class C { }')); | |
| 114 } | |
| 115 | |
| 116 test_writeClassDeclaration_memberWriter() async { | |
| 117 String path = '/test.dart'; | |
| 118 addSource(path, ''); | |
| 119 | |
| 120 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 121 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 122 builder.addInsertion(0, (EditBuilder builder) { | |
| 123 (builder as DartEditBuilder).writeClassDeclaration('C', | |
| 124 memberWriter: () { | |
| 125 builder.write('/**/'); | |
| 126 }); | |
| 127 }); | |
| 128 }); | |
| 129 SourceEdit edit = getEdit(builder); | |
| 130 expect(edit.replacement, equalsIgnoringWhitespace('class C { /**/ }')); | |
| 131 } | |
| 132 | |
| 133 test_writeClassDeclaration_mixins_noSuperclass() async { | |
| 134 String path = '/test.dart'; | |
| 135 addSource(path, 'class A {}'); | |
| 136 DartType typeA = await _getType(path, 'A'); | |
| 137 | |
| 138 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 139 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 140 builder.addInsertion(0, (EditBuilder builder) { | |
| 141 (builder as DartEditBuilder) | |
| 142 .writeClassDeclaration('C', mixins: [typeA]); | |
| 143 }); | |
| 144 }); | |
| 145 SourceEdit edit = getEdit(builder); | |
| 146 expect(edit.replacement, | |
| 147 equalsIgnoringWhitespace('class C extends Object with A { }')); | |
| 148 } | |
| 149 | |
| 150 test_writeClassDeclaration_mixins_superclass() async { | |
| 151 String path = '/test.dart'; | |
| 152 addSource(path, 'class A {} class B {}'); | |
| 153 DartType typeA = await _getType(path, 'A'); | |
| 154 DartType typeB = await _getType(path, 'B'); | |
| 155 | |
| 156 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 157 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 158 builder.addInsertion(0, (EditBuilder builder) { | |
| 159 (builder as DartEditBuilder) | |
| 160 .writeClassDeclaration('C', mixins: [typeB], superclass: typeA); | |
| 161 }); | |
| 162 }); | |
| 163 SourceEdit edit = getEdit(builder); | |
| 164 expect(edit.replacement, | |
| 165 equalsIgnoringWhitespace('class C extends A with B { }')); | |
| 166 } | |
| 167 | |
| 168 test_writeClassDeclaration_nameGroupName() async { | |
| 169 String path = '/test.dart'; | |
| 170 addSource(path, ''); | |
| 171 | |
| 172 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 173 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 174 builder.addInsertion(0, (EditBuilder builder) { | |
| 175 (builder as DartEditBuilder) | |
| 176 .writeClassDeclaration('C', nameGroupName: 'name'); | |
| 177 }); | |
| 178 }); | |
| 179 SourceEdit edit = getEdit(builder); | |
| 180 expect(edit.replacement, equalsIgnoringWhitespace('class C { }')); | |
| 181 | |
| 182 List<LinkedEditGroup> linkedEditGroups = | |
| 183 builder.sourceChange.linkedEditGroups; | |
| 184 expect(linkedEditGroups, hasLength(1)); | |
| 185 LinkedEditGroup group = linkedEditGroups[0]; | |
| 186 expect(group.length, 1); | |
| 187 expect(group.positions, hasLength(1)); | |
| 188 } | |
| 189 | |
| 190 test_writeClassDeclaration_superclass() async { | |
| 191 String path = '/test.dart'; | |
| 192 addSource(path, 'class B {}'); | |
| 193 DartType typeB = await _getType(path, 'B'); | |
| 194 | |
| 195 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 196 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 197 builder.addInsertion(0, (EditBuilder builder) { | |
| 198 (builder as DartEditBuilder).writeClassDeclaration('C', | |
| 199 superclass: typeB, superclassGroupName: 'superclass'); | |
| 200 }); | |
| 201 }); | |
| 202 SourceEdit edit = getEdit(builder); | |
| 203 expect(edit.replacement, equalsIgnoringWhitespace('class C extends B { }')); | |
| 204 | |
| 205 List<LinkedEditGroup> linkedEditGroups = | |
| 206 builder.sourceChange.linkedEditGroups; | |
| 207 expect(linkedEditGroups, hasLength(1)); | |
| 208 LinkedEditGroup group = linkedEditGroups[0]; | |
| 209 expect(group.length, 1); | |
| 210 expect(group.positions, hasLength(1)); | |
| 211 } | |
| 212 | |
| 213 test_writeFieldDeclaration_initializerWriter() async { | |
| 214 String path = '/test.dart'; | |
| 215 String content = 'class A {}'; | |
| 216 addSource(path, content); | |
| 217 | |
| 218 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 219 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 220 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 221 (builder as DartEditBuilder).writeFieldDeclaration('f', | |
| 222 initializerWriter: () { | |
| 223 builder.write('e'); | |
| 224 }); | |
| 225 }); | |
| 226 }); | |
| 227 SourceEdit edit = getEdit(builder); | |
| 228 expect(edit.replacement, equalsIgnoringWhitespace('var f = e;')); | |
| 229 } | |
| 230 | |
| 231 test_writeFieldDeclaration_isConst() async { | |
| 232 String path = '/test.dart'; | |
| 233 String content = 'class A {}'; | |
| 234 addSource(path, content); | |
| 235 | |
| 236 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 237 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 238 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 239 (builder as DartEditBuilder).writeFieldDeclaration('f', isConst: true); | |
| 240 }); | |
| 241 }); | |
| 242 SourceEdit edit = getEdit(builder); | |
| 243 expect(edit.replacement, equalsIgnoringWhitespace('const f;')); | |
| 244 } | |
| 245 | |
| 246 test_writeFieldDeclaration_isConst_isFinal() async { | |
| 247 String path = '/test.dart'; | |
| 248 String content = 'class A {}'; | |
| 249 addSource(path, content); | |
| 250 | |
| 251 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 252 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 253 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 254 (builder as DartEditBuilder) | |
| 255 .writeFieldDeclaration('f', isConst: true, isFinal: true); | |
| 256 }); | |
| 257 }); | |
| 258 SourceEdit edit = getEdit(builder); | |
| 259 expect(edit.replacement, equalsIgnoringWhitespace('const f;')); | |
| 260 } | |
| 261 | |
| 262 test_writeFieldDeclaration_isFinal() async { | |
| 263 String path = '/test.dart'; | |
| 264 String content = 'class A {}'; | |
| 265 addSource(path, content); | |
| 266 | |
| 267 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 268 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 269 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 270 (builder as DartEditBuilder).writeFieldDeclaration('f', isFinal: true); | |
| 271 }); | |
| 272 }); | |
| 273 SourceEdit edit = getEdit(builder); | |
| 274 expect(edit.replacement, equalsIgnoringWhitespace('final f;')); | |
| 275 } | |
| 276 | |
| 277 test_writeFieldDeclaration_isStatic() async { | |
| 278 String path = '/test.dart'; | |
| 279 String content = 'class A {}'; | |
| 280 addSource(path, content); | |
| 281 | |
| 282 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 283 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 284 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 285 (builder as DartEditBuilder).writeFieldDeclaration('f', isStatic: true); | |
| 286 }); | |
| 287 }); | |
| 288 SourceEdit edit = getEdit(builder); | |
| 289 expect(edit.replacement, equalsIgnoringWhitespace('static var f;')); | |
| 290 } | |
| 291 | |
| 292 test_writeFieldDeclaration_nameGroupName() async { | |
| 293 String path = '/test.dart'; | |
| 294 String content = 'class A {}'; | |
| 295 addSource(path, content); | |
| 296 | |
| 297 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 298 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 299 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 300 (builder as DartEditBuilder) | |
| 301 .writeFieldDeclaration('f', nameGroupName: 'name'); | |
| 302 }); | |
| 303 }); | |
| 304 SourceEdit edit = getEdit(builder); | |
| 305 expect(edit.replacement, equalsIgnoringWhitespace('var f;')); | |
| 306 | |
| 307 List<LinkedEditGroup> linkedEditGroups = | |
| 308 builder.sourceChange.linkedEditGroups; | |
| 309 expect(linkedEditGroups, hasLength(1)); | |
| 310 LinkedEditGroup group = linkedEditGroups[0]; | |
| 311 expect(group.length, 1); | |
| 312 expect(group.positions, hasLength(1)); | |
| 313 Position position = group.positions[0]; | |
| 314 expect(position.offset, equals(13)); | |
| 315 } | |
| 316 | |
| 317 test_writeFieldDeclaration_type_typeGroupName() async { | |
| 318 String path = '/test.dart'; | |
| 319 String content = 'class A {} class B {}'; | |
| 320 addSource(path, content); | |
| 321 DartType typeA = await _getType(path, 'A'); | |
| 322 | |
| 323 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 324 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 325 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 326 (builder as DartEditBuilder) | |
| 327 .writeFieldDeclaration('f', type: typeA, typeGroupName: 'type'); | |
| 328 }); | |
| 329 }); | |
| 330 SourceEdit edit = getEdit(builder); | |
| 331 expect(edit.replacement, equalsIgnoringWhitespace('A f;')); | |
| 332 | |
| 333 List<LinkedEditGroup> linkedEditGroups = | |
| 334 builder.sourceChange.linkedEditGroups; | |
| 335 expect(linkedEditGroups, hasLength(1)); | |
| 336 LinkedEditGroup group = linkedEditGroups[0]; | |
| 337 expect(group.length, 1); | |
| 338 expect(group.positions, hasLength(1)); | |
| 339 Position position = group.positions[0]; | |
| 340 expect(position.offset, equals(20)); | |
| 341 } | |
| 342 | |
| 343 test_writeFunctionDeclaration_noReturnType_noParams_body() async { | |
| 344 String path = '/test.dart'; | |
| 345 String content = ''; | |
| 346 addSource(path, content); | |
| 347 | |
| 348 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 349 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 350 builder.addInsertion(0, (EditBuilder builder) { | |
| 351 (builder as DartEditBuilder).writeFunctionDeclaration('fib', | |
| 352 bodyWriter: () { | |
| 353 builder.write('{ ... }'); | |
| 354 }); | |
| 355 }); | |
| 356 }); | |
| 357 SourceEdit edit = getEdit(builder); | |
| 358 expect(edit.replacement, equalsIgnoringWhitespace('fib() { ... }')); | |
| 359 } | |
| 360 | |
| 361 test_writeFunctionDeclaration_noReturnType_noParams_noBody() async { | |
| 362 String path = '/test.dart'; | |
| 363 String content = ''; | |
| 364 addSource(path, content); | |
| 365 | |
| 366 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 367 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 368 builder.addInsertion(0, (EditBuilder builder) { | |
| 369 (builder as DartEditBuilder) | |
| 370 .writeFunctionDeclaration('fib', nameGroupName: 'name'); | |
| 371 }); | |
| 372 }); | |
| 373 SourceEdit edit = getEdit(builder); | |
| 374 expect(edit.replacement, equalsIgnoringWhitespace('fib() {}')); | |
| 375 | |
| 376 List<LinkedEditGroup> linkedEditGroups = | |
| 377 builder.sourceChange.linkedEditGroups; | |
| 378 expect(linkedEditGroups, hasLength(1)); | |
| 379 LinkedEditGroup group = linkedEditGroups[0]; | |
| 380 expect(group.length, 3); | |
| 381 expect(group.positions, hasLength(1)); | |
| 382 } | |
| 383 | |
| 384 test_writeFunctionDeclaration_noReturnType_params_noBody() async { | |
| 385 String path = '/test.dart'; | |
| 386 String content = ''; | |
| 387 addSource(path, content); | |
| 388 | |
| 389 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 390 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 391 builder.addInsertion(0, (EditBuilder builder) { | |
| 392 (builder as DartEditBuilder).writeFunctionDeclaration('fib', | |
| 393 parameterWriter: () { | |
| 394 builder.write('p, q, r'); | |
| 395 }); | |
| 396 }); | |
| 397 }); | |
| 398 SourceEdit edit = getEdit(builder); | |
| 399 expect(edit.replacement, equalsIgnoringWhitespace('fib(p, q, r) {}')); | |
| 400 } | |
| 401 | |
| 402 test_writeFunctionDeclaration_returnType_noParams_noBody() async { | |
| 403 String path = '/test.dart'; | |
| 404 String content = 'class A {}'; | |
| 405 addSource(path, content); | |
| 406 | |
| 407 DartType typeA = await _getType(path, 'A'); | |
| 408 | |
| 409 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 410 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 411 builder.addInsertion(0, (EditBuilder builder) { | |
| 412 (builder as DartEditBuilder).writeFunctionDeclaration('fib', | |
| 413 returnType: typeA, returnTypeGroupName: 'type'); | |
| 414 }); | |
| 415 }); | |
| 416 SourceEdit edit = getEdit(builder); | |
| 417 expect(edit.replacement, equalsIgnoringWhitespace('A fib() => null;')); | |
| 418 | |
| 419 List<LinkedEditGroup> linkedEditGroups = | |
| 420 builder.sourceChange.linkedEditGroups; | |
| 421 expect(linkedEditGroups, hasLength(1)); | |
| 422 LinkedEditGroup group = linkedEditGroups[0]; | |
| 423 expect(group.length, 1); | |
| 424 expect(group.positions, hasLength(1)); | |
| 425 } | |
| 426 | |
| 427 test_writeGetterDeclaration_bodyWriter() async { | |
| 428 String path = '/test.dart'; | |
| 429 String content = 'class A {}'; | |
| 430 addSource(path, content); | |
| 431 | |
| 432 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 433 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 434 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 435 (builder as DartEditBuilder).writeGetterDeclaration('g', | |
| 436 bodyWriter: () { | |
| 437 builder.write('{}'); | |
| 438 }); | |
| 439 }); | |
| 440 }); | |
| 441 SourceEdit edit = getEdit(builder); | |
| 442 expect(edit.replacement, equalsIgnoringWhitespace('get g {}')); | |
| 443 } | |
| 444 | |
| 445 test_writeGetterDeclaration_isStatic() async { | |
| 446 String path = '/test.dart'; | |
| 447 String content = 'class A {}'; | |
| 448 addSource(path, content); | |
| 449 | |
| 450 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 451 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 452 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 453 (builder as DartEditBuilder) | |
| 454 .writeGetterDeclaration('g', isStatic: true); | |
| 455 }); | |
| 456 }); | |
| 457 SourceEdit edit = getEdit(builder); | |
| 458 expect(edit.replacement, equalsIgnoringWhitespace('static get g => null;')); | |
| 459 } | |
| 460 | |
| 461 test_writeGetterDeclaration_nameGroupName() async { | |
| 462 String path = '/test.dart'; | |
| 463 String content = 'class A {}'; | |
| 464 addSource(path, content); | |
| 465 | |
| 466 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 467 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 468 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 469 (builder as DartEditBuilder) | |
| 470 .writeGetterDeclaration('g', nameGroupName: 'name'); | |
| 471 }); | |
| 472 }); | |
| 473 SourceEdit edit = getEdit(builder); | |
| 474 expect(edit.replacement, equalsIgnoringWhitespace('get g => null;')); | |
| 475 | |
| 476 List<LinkedEditGroup> linkedEditGroups = | |
| 477 builder.sourceChange.linkedEditGroups; | |
| 478 expect(linkedEditGroups, hasLength(1)); | |
| 479 LinkedEditGroup group = linkedEditGroups[0]; | |
| 480 expect(group.length, 1); | |
| 481 expect(group.positions, hasLength(1)); | |
| 482 Position position = group.positions[0]; | |
| 483 expect(position.offset, equals(13)); | |
| 484 } | |
| 485 | |
| 486 test_writeGetterDeclaration_returnType() async { | |
| 487 String path = '/test.dart'; | |
| 488 String content = 'class A {} class B {}'; | |
| 489 addSource(path, content); | |
| 490 DartType typeA = await _getType(path, 'A'); | |
| 491 | |
| 492 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 493 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 494 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 495 (builder as DartEditBuilder).writeGetterDeclaration('g', | |
| 496 returnType: typeA, returnTypeGroupName: 'returnType'); | |
| 497 }); | |
| 498 }); | |
| 499 SourceEdit edit = getEdit(builder); | |
| 500 expect(edit.replacement, equalsIgnoringWhitespace('A get g => null;')); | |
| 501 | |
| 502 List<LinkedEditGroup> linkedEditGroups = | |
| 503 builder.sourceChange.linkedEditGroups; | |
| 504 expect(linkedEditGroups, hasLength(1)); | |
| 505 LinkedEditGroup group = linkedEditGroups[0]; | |
| 506 expect(group.length, 1); | |
| 507 expect(group.positions, hasLength(1)); | |
| 508 Position position = group.positions[0]; | |
| 509 expect(position.offset, equals(20)); | |
| 510 } | |
| 511 | |
| 512 test_writeLocalVariableDeclaration_noType_initializer() async { | |
| 513 String path = '/test.dart'; | |
| 514 String content = ''' | |
| 515 void f() { | |
| 516 | |
| 517 }'''; | |
| 518 addSource(path, content); | |
| 519 await driver.getResult(path); | |
| 520 | |
| 521 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 522 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 523 builder.addInsertion(11, (EditBuilder builder) { | |
| 524 (builder as DartEditBuilder).writeLocalVariableDeclaration('foo', | |
| 525 initializerWriter: () { | |
| 526 builder.write('null'); | |
| 527 }); | |
| 528 }); | |
| 529 }); | |
| 530 SourceEdit edit = getEdit(builder); | |
| 531 expect(edit.replacement, equalsIgnoringWhitespace('var foo = null;')); | |
| 532 } | |
| 533 | |
| 534 test_writeLocalVariableDeclaration_noType_noInitializer() async { | |
| 535 String path = '/test.dart'; | |
| 536 String content = ''' | |
| 537 void f() { | |
| 538 | |
| 539 }'''; | |
| 540 addSource(path, content); | |
| 541 await driver.getResult(path); | |
| 542 | |
| 543 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 544 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 545 builder.addInsertion(11, (EditBuilder builder) { | |
| 546 (builder as DartEditBuilder) | |
| 547 .writeLocalVariableDeclaration('foo', nameGroupName: 'name'); | |
| 548 }); | |
| 549 }); | |
| 550 SourceEdit edit = getEdit(builder); | |
| 551 expect(edit.replacement, equalsIgnoringWhitespace('var foo;')); | |
| 552 | |
| 553 List<LinkedEditGroup> linkedEditGroups = | |
| 554 builder.sourceChange.linkedEditGroups; | |
| 555 expect(linkedEditGroups, hasLength(1)); | |
| 556 LinkedEditGroup group = linkedEditGroups[0]; | |
| 557 expect(group.length, 3); | |
| 558 expect(group.positions, hasLength(1)); | |
| 559 } | |
| 560 | |
| 561 test_writeLocalVariableDeclaration_noType_noInitializer_const() async { | |
| 562 String path = '/test.dart'; | |
| 563 String content = ''' | |
| 564 void f() { | |
| 565 | |
| 566 }'''; | |
| 567 addSource(path, content); | |
| 568 await driver.getResult(path); | |
| 569 | |
| 570 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 571 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 572 builder.addInsertion(11, (EditBuilder builder) { | |
| 573 (builder as DartEditBuilder) | |
| 574 .writeLocalVariableDeclaration('foo', isConst: true); | |
| 575 }); | |
| 576 }); | |
| 577 SourceEdit edit = getEdit(builder); | |
| 578 expect(edit.replacement, equalsIgnoringWhitespace('const foo;')); | |
| 579 } | |
| 580 | |
| 581 test_writeLocalVariableDeclaration_noType_noInitializer_final() async { | |
| 582 String path = '/test.dart'; | |
| 583 String content = ''' | |
| 584 void f() { | |
| 585 | |
| 586 }'''; | |
| 587 addSource(path, content); | |
| 588 await driver.getResult(path); | |
| 589 | |
| 590 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 591 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 592 builder.addInsertion(11, (EditBuilder builder) { | |
| 593 (builder as DartEditBuilder) | |
| 594 .writeLocalVariableDeclaration('foo', isFinal: true); | |
| 595 }); | |
| 596 }); | |
| 597 SourceEdit edit = getEdit(builder); | |
| 598 expect(edit.replacement, equalsIgnoringWhitespace('final foo;')); | |
| 599 } | |
| 600 | |
| 601 test_writeLocalVariableDeclaration_type_initializer() async { | |
| 602 String path = '/test.dart'; | |
| 603 String content = ''' | |
| 604 void f() { | |
| 605 | |
| 606 } | |
| 607 class MyClass {}'''; | |
| 608 addSource(path, content); | |
| 609 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 610 | |
| 611 ClassDeclaration A = unit.declarations[1]; | |
| 612 | |
| 613 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 614 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 615 builder.addInsertion(11, (EditBuilder builder) { | |
| 616 (builder as DartEditBuilder).writeLocalVariableDeclaration('foo', | |
| 617 initializerWriter: () { | |
| 618 builder.write('null'); | |
| 619 }, type: A.element.type); | |
| 620 }); | |
| 621 }); | |
| 622 SourceEdit edit = getEdit(builder); | |
| 623 expect(edit.replacement, equalsIgnoringWhitespace('MyClass foo = null;')); | |
| 624 } | |
| 625 | |
| 626 test_writeLocalVariableDeclaration_type_noInitializer() async { | |
| 627 String path = '/test.dart'; | |
| 628 String content = ''' | |
| 629 void f() { | |
| 630 | |
| 631 } | |
| 632 class MyClass {}'''; | |
| 633 addSource(path, content); | |
| 634 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 635 | |
| 636 ClassDeclaration A = unit.declarations[1]; | |
| 637 | |
| 638 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 639 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 640 builder.addInsertion(11, (EditBuilder builder) { | |
| 641 (builder as DartEditBuilder).writeLocalVariableDeclaration('foo', | |
| 642 type: A.element.type, typeGroupName: 'type'); | |
| 643 }); | |
| 644 }); | |
| 645 SourceEdit edit = getEdit(builder); | |
| 646 expect(edit.replacement, equalsIgnoringWhitespace('MyClass foo;')); | |
| 647 | |
| 648 List<LinkedEditGroup> linkedEditGroups = | |
| 649 builder.sourceChange.linkedEditGroups; | |
| 650 expect(linkedEditGroups, hasLength(1)); | |
| 651 LinkedEditGroup group = linkedEditGroups[0]; | |
| 652 expect(group.length, 7); | |
| 653 expect(group.positions, hasLength(1)); | |
| 654 } | |
| 655 | |
| 656 test_writeLocalVariableDeclaration_type_noInitializer_final() async { | |
| 657 String path = '/test.dart'; | |
| 658 String content = ''' | |
| 659 void f() { | |
| 660 | |
| 661 } | |
| 662 class MyClass {}'''; | |
| 663 addSource(path, content); | |
| 664 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 665 | |
| 666 ClassDeclaration A = unit.declarations[1]; | |
| 667 | |
| 668 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 669 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 670 builder.addInsertion(11, (EditBuilder builder) { | |
| 671 (builder as DartEditBuilder).writeLocalVariableDeclaration('foo', | |
| 672 isFinal: true, type: A.element.type, typeGroupName: 'type'); | |
| 673 }); | |
| 674 }); | |
| 675 SourceEdit edit = getEdit(builder); | |
| 676 expect(edit.replacement, equalsIgnoringWhitespace('final MyClass foo;')); | |
| 677 | |
| 678 List<LinkedEditGroup> linkedEditGroups = | |
| 679 builder.sourceChange.linkedEditGroups; | |
| 680 expect(linkedEditGroups, hasLength(1)); | |
| 681 LinkedEditGroup group = linkedEditGroups[0]; | |
| 682 expect(group.length, 7); | |
| 683 expect(group.positions, hasLength(1)); | |
| 684 } | |
| 685 | |
| 686 test_writeOverrideOfInheritedMember() async { | |
| 687 String path = '/test.dart'; | |
| 688 String content = ''' | |
| 689 class A { | |
| 690 A add(A a) => null; | |
| 691 } | |
| 692 class B extends A { | |
| 693 }'''; | |
| 694 addSource(path, content); | |
| 695 ClassElement classA = await _getClassElement(path, 'A'); | |
| 696 | |
| 697 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 698 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 699 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 700 (builder as DartEditBuilder) | |
| 701 .writeOverrideOfInheritedMember(classA.methods[0]); | |
| 702 }); | |
| 703 }); | |
| 704 SourceEdit edit = getEdit(builder); | |
| 705 expect(edit.replacement, equalsIgnoringWhitespace(''' | |
| 706 @override | |
| 707 A add(A a) { | |
| 708 // TODO: implement add | |
| 709 return null; | |
| 710 }''')); | |
| 711 } | |
| 712 | |
| 713 test_writeParameterMatchingArgument() async { | |
| 714 String path = '/test.dart'; | |
| 715 String content = r''' | |
| 716 f() {} | |
| 717 g() { | |
| 718 f(new A()); | |
| 719 } | |
| 720 class A {} | |
| 721 '''; | |
| 722 addSource(path, content); | |
| 723 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 724 FunctionDeclaration g = unit.declarations[1]; | |
| 725 BlockFunctionBody body = g.functionExpression.body; | |
| 726 ExpressionStatement statement = body.block.statements[0]; | |
| 727 MethodInvocation invocation = statement.expression; | |
| 728 Expression argument = invocation.argumentList.arguments[0]; | |
| 729 | |
| 730 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 731 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 732 builder.addInsertion(2, (EditBuilder builder) { | |
| 733 (builder as DartEditBuilder) | |
| 734 .writeParameterMatchingArgument(argument, 0, new Set<String>()); | |
| 735 }); | |
| 736 }); | |
| 737 SourceEdit edit = getEdit(builder); | |
| 738 expect(edit.replacement, equalsIgnoringWhitespace('A a')); | |
| 739 } | |
| 740 | |
| 741 test_writeParameters_named() async { | |
| 742 String path = '/test.dart'; | |
| 743 String content = 'f(int i, {String s}) {}'; | |
| 744 addSource(path, content); | |
| 745 | |
| 746 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 747 FunctionDeclaration f = unit.declarations[0]; | |
| 748 FormalParameterList parameters = f.functionExpression.parameters; | |
| 749 Iterable<ParameterElement> elements = parameters.parameters | |
| 750 .map(resolutionMap.elementDeclaredByFormalParameter); | |
| 751 | |
| 752 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 753 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 754 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 755 (builder as DartEditBuilder).writeParameters(elements); | |
| 756 }); | |
| 757 }); | |
| 758 SourceEdit edit = getEdit(builder); | |
| 759 expect(edit.replacement, equalsIgnoringWhitespace('(int i, {String s})')); | |
| 760 } | |
| 761 | |
| 762 test_writeParameters_positional() async { | |
| 763 String path = '/test.dart'; | |
| 764 String content = 'f(int i, [String s]) {}'; | |
| 765 addSource(path, content); | |
| 766 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 767 FunctionDeclaration f = unit.declarations[0]; | |
| 768 FormalParameterList parameters = f.functionExpression.parameters; | |
| 769 Iterable<ParameterElement> elements = parameters.parameters | |
| 770 .map(resolutionMap.elementDeclaredByFormalParameter); | |
| 771 | |
| 772 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 773 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 774 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 775 (builder as DartEditBuilder).writeParameters(elements); | |
| 776 }); | |
| 777 }); | |
| 778 SourceEdit edit = getEdit(builder); | |
| 779 expect(edit.replacement, equalsIgnoringWhitespace('(int i, [String s])')); | |
| 780 } | |
| 781 | |
| 782 test_writeParameters_required() async { | |
| 783 String path = '/test.dart'; | |
| 784 String content = 'f(int i, String s) {}'; | |
| 785 addSource(path, content); | |
| 786 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 787 FunctionDeclaration f = unit.declarations[0]; | |
| 788 FormalParameterList parameters = f.functionExpression.parameters; | |
| 789 Iterable<ParameterElement> elements = parameters.parameters | |
| 790 .map(resolutionMap.elementDeclaredByFormalParameter); | |
| 791 | |
| 792 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 793 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 794 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 795 (builder as DartEditBuilder).writeParameters(elements); | |
| 796 }); | |
| 797 }); | |
| 798 SourceEdit edit = getEdit(builder); | |
| 799 expect(edit.replacement, equalsIgnoringWhitespace('(int i, String s)')); | |
| 800 } | |
| 801 | |
| 802 test_writeParametersMatchingArguments_named() async { | |
| 803 String path = '/test.dart'; | |
| 804 String content = ''' | |
| 805 f(int i, String s) { | |
| 806 g(s, index: i); | |
| 807 }'''; | |
| 808 addSource(path, content); | |
| 809 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 810 FunctionDeclaration f = unit.declarations[0]; | |
| 811 BlockFunctionBody body = f.functionExpression.body; | |
| 812 ExpressionStatement statement = body.block.statements[0]; | |
| 813 MethodInvocation invocation = statement.expression; | |
| 814 | |
| 815 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 816 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 817 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 818 (builder as DartEditBuilder) | |
| 819 .writeParametersMatchingArguments(invocation.argumentList); | |
| 820 }); | |
| 821 }); | |
| 822 SourceEdit edit = getEdit(builder); | |
| 823 expect(edit.replacement, equalsIgnoringWhitespace('String s, {int index}')); | |
| 824 } | |
| 825 | |
| 826 test_writeParametersMatchingArguments_required() async { | |
| 827 String path = '/test.dart'; | |
| 828 String content = ''' | |
| 829 f(int i, String s) { | |
| 830 g(s, i); | |
| 831 }'''; | |
| 832 addSource(path, content); | |
| 833 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 834 FunctionDeclaration f = unit.declarations[0]; | |
| 835 BlockFunctionBody body = f.functionExpression.body; | |
| 836 ExpressionStatement statement = body.block.statements[0]; | |
| 837 MethodInvocation invocation = statement.expression; | |
| 838 | |
| 839 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 840 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 841 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 842 (builder as DartEditBuilder) | |
| 843 .writeParametersMatchingArguments(invocation.argumentList); | |
| 844 }); | |
| 845 }); | |
| 846 SourceEdit edit = getEdit(builder); | |
| 847 expect(edit.replacement, equalsIgnoringWhitespace('String s, int i')); | |
| 848 } | |
| 849 | |
| 850 test_writeParameterSource() async { | |
| 851 String path = '/test.dart'; | |
| 852 String content = 'class A {}'; | |
| 853 addSource(path, content); | |
| 854 DartType typeA = await _getType(path, 'A'); | |
| 855 | |
| 856 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 857 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 858 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 859 (builder as DartEditBuilder).writeParameterSource(typeA, 'a'); | |
| 860 }); | |
| 861 }); | |
| 862 SourceEdit edit = getEdit(builder); | |
| 863 expect(edit.replacement, equalsIgnoringWhitespace('A a')); | |
| 864 } | |
| 865 | |
| 866 test_writeType_dynamic() async { | |
| 867 String path = '/test.dart'; | |
| 868 String content = 'class A {}'; | |
| 869 addSource(path, content); | |
| 870 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 871 | |
| 872 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 873 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 874 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 875 (builder as DartEditBuilder).writeType(resolutionMap | |
| 876 .elementDeclaredByCompilationUnit(unit) | |
| 877 .context | |
| 878 .typeProvider | |
| 879 .dynamicType); | |
| 880 }); | |
| 881 }); | |
| 882 SourceEdit edit = getEdit(builder); | |
| 883 expect(edit.replacement, equalsIgnoringWhitespace('')); | |
| 884 } | |
| 885 | |
| 886 test_writeType_genericType() async { | |
| 887 String path = '/test.dart'; | |
| 888 String content = 'class A {} class B<E> {}'; | |
| 889 addSource(path, content); | |
| 890 InterfaceType typeA = await _getType(path, 'A'); | |
| 891 InterfaceType typeB = await _getType(path, 'B'); | |
| 892 | |
| 893 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 894 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 895 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 896 (builder as DartEditBuilder).writeType(typeB.instantiate([typeA])); | |
| 897 }); | |
| 898 }); | |
| 899 SourceEdit edit = getEdit(builder); | |
| 900 expect(edit.replacement, equalsIgnoringWhitespace('B<A>')); | |
| 901 } | |
| 902 | |
| 903 test_writeType_groupName() async { | |
| 904 String path = '/test.dart'; | |
| 905 String content = 'class A {} class B extends A {} class C extends B {}'; | |
| 906 addSource(path, content); | |
| 907 DartType typeC = await _getType(path, 'C'); | |
| 908 | |
| 909 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 910 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 911 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 912 (builder as DartEditBuilder).writeType(typeC, groupName: 'type'); | |
| 913 }); | |
| 914 }); | |
| 915 SourceEdit edit = getEdit(builder); | |
| 916 expect(edit.replacement, equalsIgnoringWhitespace('C')); | |
| 917 | |
| 918 List<LinkedEditGroup> linkedEditGroups = | |
| 919 builder.sourceChange.linkedEditGroups; | |
| 920 expect(linkedEditGroups, hasLength(1)); | |
| 921 LinkedEditGroup group = linkedEditGroups[0]; | |
| 922 expect(group, isNotNull); | |
| 923 } | |
| 924 | |
| 925 test_writeType_groupName_addSupertypeProposals() async { | |
| 926 String path = '/test.dart'; | |
| 927 String content = 'class A {} class B extends A {} class C extends B {}'; | |
| 928 addSource(path, content); | |
| 929 DartType typeC = await _getType(path, 'C'); | |
| 930 | |
| 931 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 932 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 933 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 934 (builder as DartEditBuilder) | |
| 935 .writeType(typeC, addSupertypeProposals: true, groupName: 'type'); | |
| 936 }); | |
| 937 }); | |
| 938 SourceEdit edit = getEdit(builder); | |
| 939 expect(edit.replacement, equalsIgnoringWhitespace('C')); | |
| 940 | |
| 941 List<LinkedEditGroup> linkedEditGroups = | |
| 942 builder.sourceChange.linkedEditGroups; | |
| 943 expect(linkedEditGroups, hasLength(1)); | |
| 944 LinkedEditGroup group = linkedEditGroups[0]; | |
| 945 List<LinkedEditSuggestion> suggestions = group.suggestions; | |
| 946 expect(suggestions, hasLength(4)); | |
| 947 Iterable<String> values = | |
| 948 suggestions.map((LinkedEditSuggestion suggestion) { | |
| 949 expect(suggestion.kind, LinkedEditSuggestionKind.TYPE); | |
| 950 return suggestion.value; | |
| 951 }); | |
| 952 expect(values, contains('Object')); | |
| 953 expect(values, contains('A')); | |
| 954 expect(values, contains('B')); | |
| 955 expect(values, contains('C')); | |
| 956 } | |
| 957 | |
| 958 test_writeType_null() async { | |
| 959 String path = '/test.dart'; | |
| 960 String content = 'class A {}'; | |
| 961 addSource(path, content); | |
| 962 | |
| 963 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 964 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 965 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 966 (builder as DartEditBuilder).writeType(null); | |
| 967 }); | |
| 968 }); | |
| 969 SourceEdit edit = getEdit(builder); | |
| 970 expect(edit.replacement, equalsIgnoringWhitespace('')); | |
| 971 } | |
| 972 | |
| 973 test_writeType_required_dynamic() async { | |
| 974 String path = '/test.dart'; | |
| 975 String content = 'class A {}'; | |
| 976 addSource(path, content); | |
| 977 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 978 | |
| 979 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 980 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 981 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 982 (builder as DartEditBuilder).writeType( | |
| 983 resolutionMap | |
| 984 .elementDeclaredByCompilationUnit(unit) | |
| 985 .context | |
| 986 .typeProvider | |
| 987 .dynamicType, | |
| 988 required: true); | |
| 989 }); | |
| 990 }); | |
| 991 SourceEdit edit = getEdit(builder); | |
| 992 expect(edit.replacement, equalsIgnoringWhitespace('var')); | |
| 993 } | |
| 994 | |
| 995 test_writeType_required_notNull() async { | |
| 996 String path = '/test.dart'; | |
| 997 String content = 'class A {}'; | |
| 998 addSource(path, content); | |
| 999 DartType typeA = await _getType(path, 'A'); | |
| 1000 | |
| 1001 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1002 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1003 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 1004 (builder as DartEditBuilder).writeType(typeA, required: true); | |
| 1005 }); | |
| 1006 }); | |
| 1007 SourceEdit edit = getEdit(builder); | |
| 1008 expect(edit.replacement, equalsIgnoringWhitespace('A')); | |
| 1009 } | |
| 1010 | |
| 1011 test_writeType_required_null() async { | |
| 1012 String path = '/test.dart'; | |
| 1013 String content = 'class A {}'; | |
| 1014 addSource(path, content); | |
| 1015 | |
| 1016 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1017 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1018 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 1019 (builder as DartEditBuilder).writeType(null, required: true); | |
| 1020 }); | |
| 1021 }); | |
| 1022 SourceEdit edit = getEdit(builder); | |
| 1023 expect(edit.replacement, equalsIgnoringWhitespace('var')); | |
| 1024 } | |
| 1025 | |
| 1026 test_writeType_simpleType() async { | |
| 1027 String path = '/test.dart'; | |
| 1028 String content = 'class A {}'; | |
| 1029 addSource(path, content); | |
| 1030 DartType typeA = await _getType(path, 'A'); | |
| 1031 | |
| 1032 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1033 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1034 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 1035 (builder as DartEditBuilder).writeType(typeA); | |
| 1036 }); | |
| 1037 }); | |
| 1038 SourceEdit edit = getEdit(builder); | |
| 1039 expect(edit.replacement, equalsIgnoringWhitespace('A')); | |
| 1040 } | |
| 1041 | |
| 1042 test_writeTypes_empty() async { | |
| 1043 String path = '/test.dart'; | |
| 1044 String content = 'class A {}'; | |
| 1045 addSource(path, content); | |
| 1046 | |
| 1047 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1048 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1049 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 1050 (builder as DartEditBuilderImpl).writeTypes([]); | |
| 1051 }); | |
| 1052 }); | |
| 1053 SourceEdit edit = getEdit(builder); | |
| 1054 expect(edit.replacement, isEmpty); | |
| 1055 } | |
| 1056 | |
| 1057 test_writeTypes_noPrefix() async { | |
| 1058 String path = '/test.dart'; | |
| 1059 String content = 'class A {} class B {}'; | |
| 1060 addSource(path, content); | |
| 1061 DartType typeA = await _getType(path, 'A'); | |
| 1062 DartType typeB = await _getType(path, 'B'); | |
| 1063 | |
| 1064 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1065 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1066 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 1067 (builder as DartEditBuilderImpl).writeTypes([typeA, typeB]); | |
| 1068 }); | |
| 1069 }); | |
| 1070 SourceEdit edit = getEdit(builder); | |
| 1071 expect(edit.replacement, equalsIgnoringWhitespace('A, B')); | |
| 1072 } | |
| 1073 | |
| 1074 test_writeTypes_null() async { | |
| 1075 String path = '/test.dart'; | |
| 1076 String content = 'class A {}'; | |
| 1077 addSource(path, content); | |
| 1078 | |
| 1079 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1080 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1081 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 1082 (builder as DartEditBuilderImpl).writeTypes(null); | |
| 1083 }); | |
| 1084 }); | |
| 1085 SourceEdit edit = getEdit(builder); | |
| 1086 expect(edit.replacement, isEmpty); | |
| 1087 } | |
| 1088 | |
| 1089 test_writeTypes_prefix() async { | |
| 1090 String path = '/test.dart'; | |
| 1091 String content = 'class A {} class B {}'; | |
| 1092 addSource(path, content); | |
| 1093 DartType typeA = await _getType(path, 'A'); | |
| 1094 DartType typeB = await _getType(path, 'B'); | |
| 1095 | |
| 1096 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1097 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1098 builder.addInsertion(content.length - 1, (EditBuilder builder) { | |
| 1099 (builder as DartEditBuilderImpl) | |
| 1100 .writeTypes([typeA, typeB], prefix: 'implements '); | |
| 1101 }); | |
| 1102 }); | |
| 1103 SourceEdit edit = getEdit(builder); | |
| 1104 expect(edit.replacement, equalsIgnoringWhitespace('implements A, B')); | |
| 1105 } | |
| 1106 | |
| 1107 Future<ClassElement> _getClassElement(String path, String name) async { | |
| 1108 UnitElementResult result = await driver.getUnitElement(path); | |
| 1109 return result.element.getType(name); | |
| 1110 } | |
| 1111 | |
| 1112 Future<DartType> _getType(String path, String name) async { | |
| 1113 ClassElement classElement = await _getClassElement(path, name); | |
| 1114 return classElement.type; | |
| 1115 } | |
| 1116 } | |
| 1117 | |
| 1118 @reflectiveTest | |
| 1119 class DartFileEditBuilderImplTest extends AbstractContextTest | |
| 1120 with BuilderTestMixin { | |
| 1121 @override | |
| 1122 bool get enableNewAnalysisDriver => true; | |
| 1123 | |
| 1124 TypeProvider get typeProvider { | |
| 1125 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | |
| 1126 context.sourceFactory = new SourceFactoryImpl([new DartUriResolver(sdk)]); | |
| 1127 return new TestTypeProvider(context); | |
| 1128 } | |
| 1129 | |
| 1130 test_convertFunctionFromSyncToAsync() async { | |
| 1131 String path = '/test.dart'; | |
| 1132 addSource(path, 'String f() {}'); | |
| 1133 | |
| 1134 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 1135 FunctionDeclaration function = unit.declarations[0]; | |
| 1136 FunctionBody body = function.functionExpression.body; | |
| 1137 | |
| 1138 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1139 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1140 (builder as DartFileEditBuilder) | |
| 1141 .convertFunctionFromSyncToAsync(body, typeProvider); | |
| 1142 }); | |
| 1143 List<SourceEdit> edits = getEdits(builder); | |
| 1144 expect(edits, hasLength(3)); | |
| 1145 expect(edits[0].replacement, equalsIgnoringWhitespace('async')); | |
| 1146 expect( | |
| 1147 edits[1].replacement, equalsIgnoringWhitespace("import 'dart:async';")); | |
| 1148 expect(edits[2].replacement, equalsIgnoringWhitespace('Future<String>')); | |
| 1149 } | |
| 1150 | |
| 1151 test_createEditBuilder() async { | |
| 1152 String path = '/test.dart'; | |
| 1153 addSource(path, 'library test;'); | |
| 1154 int timeStamp = 65; | |
| 1155 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1156 await builder.addFileEdit(path, timeStamp, (FileEditBuilder builder) { | |
| 1157 int offset = 4; | |
| 1158 int length = 5; | |
| 1159 DartEditBuilderImpl editBuilder = (builder as DartFileEditBuilderImpl) | |
| 1160 .createEditBuilder(offset, length); | |
| 1161 expect(editBuilder, new isInstanceOf<DartEditBuilder>()); | |
| 1162 SourceEdit sourceEdit = editBuilder.sourceEdit; | |
| 1163 expect(sourceEdit.length, length); | |
| 1164 expect(sourceEdit.offset, offset); | |
| 1165 expect(sourceEdit.replacement, isEmpty); | |
| 1166 }); | |
| 1167 } | |
| 1168 | |
| 1169 test_replaceTypeWithFuture() async { | |
| 1170 String path = '/test.dart'; | |
| 1171 addSource(path, 'String f() {}'); | |
| 1172 | |
| 1173 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 1174 FunctionDeclaration function = unit.declarations[0]; | |
| 1175 TypeAnnotation type = function.returnType; | |
| 1176 | |
| 1177 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(driver); | |
| 1178 await builder.addFileEdit(path, 1, (FileEditBuilder builder) { | |
| 1179 (builder as DartFileEditBuilder) | |
| 1180 .replaceTypeWithFuture(type, typeProvider); | |
| 1181 }); | |
| 1182 List<SourceEdit> edits = getEdits(builder); | |
| 1183 expect(edits, hasLength(2)); | |
| 1184 expect( | |
| 1185 edits[0].replacement, equalsIgnoringWhitespace("import 'dart:async';")); | |
| 1186 expect(edits[1].replacement, equalsIgnoringWhitespace('Future<String>')); | |
| 1187 } | |
| 1188 } | |
| 1189 | |
| 1190 @reflectiveTest | |
| 1191 class DartLinkedEditBuilderImplTest extends AbstractContextTest { | |
| 1192 @override | |
| 1193 bool get enableNewAnalysisDriver => true; | |
| 1194 | |
| 1195 test_addSuperTypesAsSuggestions() async { | |
| 1196 String path = '/test.dart'; | |
| 1197 addSource( | |
| 1198 path, | |
| 1199 ''' | |
| 1200 class A {} | |
| 1201 class B extends A {} | |
| 1202 class C extends B {} | |
| 1203 '''); | |
| 1204 CompilationUnit unit = (await driver.getResult(path))?.unit; | |
| 1205 ClassDeclaration classC = unit.declarations[2]; | |
| 1206 DartLinkedEditBuilderImpl builder = new DartLinkedEditBuilderImpl(null); | |
| 1207 builder.addSuperTypesAsSuggestions(classC.element.type); | |
| 1208 List<LinkedEditSuggestion> suggestions = builder.suggestions; | |
| 1209 expect(suggestions, hasLength(4)); | |
| 1210 expect(suggestions.map((s) => s.value), | |
| 1211 unorderedEquals(['Object', 'A', 'B', 'C'])); | |
| 1212 } | |
| 1213 } | |
| OLD | NEW |