| Index: pkg/analysis_server/test/src/computer/import_elements_computer_test.dart
|
| diff --git a/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart b/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart
|
| index cb7f56164bfe9784973218e279945d96deb3ceb2..36a0b1913128e6da8b6ad98bd513740398d486db 100644
|
| --- a/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart
|
| +++ b/pkg/analysis_server/test/src/computer/import_elements_computer_test.dart
|
| @@ -2,10 +2,13 @@
|
| // for details. All rights reserved. Use of this source code is governed by a
|
| // BSD-style license that can be found in the LICENSE file.
|
|
|
| +import 'dart:async';
|
| +
|
| import 'package:analysis_server/protocol/protocol_generated.dart';
|
| import 'package:analysis_server/src/computer/import_elements_computer.dart';
|
| -import 'package:analyzer/dart/analysis/results.dart';
|
| +import 'package:analyzer/src/dart/analysis/driver.dart';
|
| import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
| +import 'package:front_end/src/base/source.dart';
|
| import 'package:test/test.dart';
|
| import 'package:test_reflective_loader/test_reflective_loader.dart';
|
|
|
| @@ -17,60 +20,294 @@ main() {
|
| });
|
| }
|
|
|
| -/**
|
| - * Tests that the [ImportElementsComputer] will correctly update imports. The
|
| - * tests are generally labeled based on the kind of import in the source (from
|
| - * which the text was copied) and the kind of import in the target (into which
|
| - * the text was pasted). Kinds are a combination of "prefix", "hide", "show" and
|
| - * "deferred", or "bare" when there are none of the previous, or "none" when
|
| - * there is no import.
|
| - */
|
| @reflectiveTest
|
| class ImportElementsComputerTest extends AbstractContextTest {
|
| - String targetPath;
|
| - String targetCode;
|
| + String path;
|
| + String originalContent;
|
| + ImportElementsComputer computer;
|
| + SourceFileEdit sourceFileEdit;
|
|
|
| - ResolveResult result;
|
| + void assertChanges(String expectedContent) {
|
| + String resultCode =
|
| + SourceEdit.applySequence(originalContent, sourceFileEdit.edits);
|
| + expect(resultCode, expectedContent);
|
| + }
|
|
|
| - setUp() async {
|
| - super.setUp();
|
| - packageMap['p'] = [provider.newFolder(provider.convertPath('/p/lib'))];
|
| - targetPath = provider.convertPath('/p/lib/target.dart');
|
| - targetCode = '''
|
| -main() {}
|
| -''';
|
| - provider.newFile(targetPath, targetCode);
|
| - result = await driver.getResult(targetPath);
|
| - }
|
| -
|
| - @failingTest
|
| - test_bare_none() {
|
| - List<ImportedElements> elements = <ImportedElements>[
|
| - new ImportedElements(
|
| - provider.convertPath('/p/lib/a.dart'), '', <String>['A']),
|
| - ];
|
| - List<SourceEdit> edits = _computeEditsFor(elements);
|
| + void assertNoChanges() {
|
| + expect(sourceFileEdit.edits, isEmpty);
|
| + }
|
| +
|
| + Future<Null> computeChanges(List<ImportedElements> importedElements) async {
|
| + SourceChange change = await computer.createEdits(importedElements);
|
| + expect(change, isNotNull);
|
| + List<SourceFileEdit> edits = change.edits;
|
| expect(edits, hasLength(1));
|
| - SourceEdit edit = edits[0];
|
| - expect(edit, isNotNull);
|
| - expect(edit.offset, 0);
|
| - expect(edit.length, 0);
|
| - expect(edit.apply(targetCode), """
|
| -import 'source.dart';
|
| + sourceFileEdit = edits[0];
|
| + expect(sourceFileEdit, isNotNull);
|
| + }
|
| +
|
| + Future<Null> createBuilder(String content) async {
|
| + originalContent = content;
|
| + provider.newFile(path, content);
|
| + AnalysisResult result = await driver.getResult(path);
|
| + computer = new ImportElementsComputer(provider, result);
|
| + }
|
| +
|
| + void setUp() {
|
| + super.setUp();
|
| + path = provider.convertPath('/test.dart');
|
| + }
|
| +
|
| + test_createEdits_addImport_noPrefix() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' as foo;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' as foo;
|
| +import 'package:pkg/foo.dart';
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_addImport_prefix() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart';
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, 'foo', <String>['A'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart';
|
| +import 'package:pkg/foo.dart' as foo;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_addShow_multipleNames() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' show B;
|
| +import 'package:pkg/foo.dart' as foo;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A', 'C'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' show B, A, C;
|
| +import 'package:pkg/foo.dart' as foo;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_addShow_removeHide() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' show A, B hide C, D;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['C'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' show A, B, C hide D;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_addShow_singleName_noPrefix() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' show B;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' show B, A;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_addShow_singleName_prefix() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' show C;
|
| +import 'package:pkg/foo.dart' as foo show B;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, 'foo', <String>['A'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' show C;
|
| +import 'package:pkg/foo.dart' as foo show B, A;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_alreadyImported_noCombinators() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart';
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A', 'B'])
|
| + ]);
|
| + assertNoChanges();
|
| + }
|
| +
|
| + test_createEdits_alreadyImported_withPrefix() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' as foo;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, 'foo', <String>['A', 'B'])
|
| + ]);
|
| + assertNoChanges();
|
| + }
|
| +
|
| + test_createEdits_alreadyImported_withShow() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' show A;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A'])
|
| + ]);
|
| + assertNoChanges();
|
| + }
|
| +
|
| + test_createEdits_noElements() async {
|
| + await createBuilder('');
|
| + await computeChanges(<ImportedElements>[]);
|
| + assertNoChanges();
|
| + }
|
| +
|
| + test_createEdits_removeHide_firstInCombinator() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A, B, C;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide B, C;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_removeHide_lastInCombinator() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A, B, C;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['C'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide A, B;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_removeHide_middleInCombinator() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A, B, C;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['B'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide A, C;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_removeHide_multipleCombinators() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A, B, C hide A, B, C;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['B'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide A, C hide A, C;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_removeHide_multipleNames() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A, B, C hide D, E, F hide G, H, I;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A', 'E', 'I'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide B, C hide D, F hide G, H;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_removeHideCombinator_first() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A hide B hide C;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide B hide C;
|
| +''');
|
| + }
|
| +
|
| + test_createEdits_removeHideCombinator_last() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A hide B hide C;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['C'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide A hide B;
|
| +''');
|
| + }
|
|
|
| -main() {}
|
| -""");
|
| + test_createEdits_removeHideCombinator_middle() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A hide B hide C;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['B'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart' hide A hide C;
|
| +''');
|
| }
|
|
|
| - test_none_none() {
|
| - List<ImportedElements> elements = <ImportedElements>[];
|
| - List<SourceEdit> edits = _computeEditsFor(elements);
|
| - expect(edits, hasLength(0));
|
| + test_createEdits_removeHideCombinator_only() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart';
|
| +''');
|
| }
|
|
|
| - List<SourceEdit> _computeEditsFor(List<ImportedElements> elements) {
|
| - ImportElementsComputer computer =
|
| - new ImportElementsComputer(result, targetPath, elements);
|
| - return computer.compute();
|
| + test_createEdits_removeHideCombinator_only_multiple() async {
|
| + Source fooSource = addPackageSource('pkg', 'foo.dart', '');
|
| + await createBuilder('''
|
| +import 'package:pkg/foo.dart' hide A, B;
|
| +''');
|
| + await computeChanges(<ImportedElements>[
|
| + new ImportedElements(fooSource.fullName, '', <String>['A', 'B'])
|
| + ]);
|
| + assertChanges('''
|
| +import 'package:pkg/foo.dart';
|
| +''');
|
| }
|
| }
|
|
|