Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: pkg/analysis_server/test/integration/edit/import_elements_test.dart

Issue 3001733002: Correctly handle adding imports when pasting into part files (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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:analyzer/file_system/physical_file_system.dart';
9 import 'package:analyzer/src/dart/sdk/sdk.dart';
8 import 'package:analyzer_plugin/protocol/protocol_common.dart'; 10 import 'package:analyzer_plugin/protocol/protocol_common.dart';
9 import 'package:test/test.dart'; 11 import 'package:test/test.dart';
10 import 'package:test_reflective_loader/test_reflective_loader.dart'; 12 import 'package:test_reflective_loader/test_reflective_loader.dart';
11 13
12 import '../support/integration_tests.dart'; 14 import '../support/integration_tests.dart';
13 15
14 main() { 16 main() {
15 defineReflectiveSuite(() { 17 defineReflectiveSuite(() {
16 defineReflectiveTests(AnalysisGetImportElementsIntegrationTest); 18 defineReflectiveTests(AnalysisGetImportElementsIntegrationTest);
17 }); 19 });
18 } 20 }
19 21
20 @reflectiveTest 22 @reflectiveTest
21 class AnalysisGetImportElementsIntegrationTest 23 class AnalysisGetImportElementsIntegrationTest
22 extends AbstractAnalysisServerIntegrationTest { 24 extends AbstractAnalysisServerIntegrationTest {
23 /** 25 /**
24 * Pathname of the file containing Dart code. 26 * Pathname of the file containing Dart code.
25 */ 27 */
26 String pathname; 28 String pathname;
27 29
28 /** 30 /**
29 * Dart code under test.
30 */
31 final String text = r'''
32 ''';
33
34 /**
35 * Check that an edit.importElements request with the given list of [elements] 31 * Check that an edit.importElements request with the given list of [elements]
36 * produces the [expected] list of edits. 32 * produces the [expected] list of edits.
37 */ 33 */
38 checkEdits(List<ImportedElements> elements, List<SourceEdit> expected) async { 34 checkEdits(List<ImportedElements> elements, List<SourceEdit> expected,
35 {String expectedFile}) async {
36 bool equals(SourceEdit actualEdit, SourceEdit expectedEdit) {
37 return actualEdit.offset == expectedEdit.offset &&
38 actualEdit.length == expectedEdit.length &&
39 actualEdit.replacement == expectedEdit.replacement;
40 }
41
42 int find(List<SourceEdit> actual, SourceEdit expectedEdit) {
43 for (int i = 0; i < actual.length; i++) {
44 SourceEdit actualEdit = actual[i];
45 if (equals(actualEdit, expectedEdit)) {
46 return i;
47 }
48 }
49 return -1;
50 }
51
39 EditImportElementsResult result = 52 EditImportElementsResult result =
40 await sendEditImportElements(pathname, elements); 53 await sendEditImportElements(pathname, elements);
41 54
42 SourceFileEdit edit = result.edit; 55 SourceFileEdit edit = result.edit;
43 expect(edit, isNotNull); 56 expect(edit, isNotNull);
44 expect(edit.edits, hasLength(expected.length)); 57 if (expectedFile == null) {
45 // TODO(brianwilkerson) Finish implementing this. 58 expect(edit.file, pathname);
59 } else {
60 expect(edit.file, expectedFile);
61 }
62 List<SourceEdit> actual = edit.edits;
63 expect(actual, hasLength(expected.length));
64 for (SourceEdit expectedEdit in expected) {
65 int index = find(actual, expectedEdit);
66 if (index < 0) {
67 fail('Expected $expectedEdit; not found');
68 }
69 actual.removeAt(index);
70 }
46 } 71 }
47 72
48 /** 73 /**
49 * Check that an edit.importElements request with the given list of [elements] 74 * Check that an edit.importElements request with the given list of [elements]
50 * produces no edits. 75 * produces no edits.
51 */ 76 */
52 Future<Null> checkNoEdits(List<ImportedElements> elements) async { 77 Future<Null> checkNoEdits(List<ImportedElements> elements) async {
53 EditImportElementsResult result = 78 EditImportElementsResult result =
54 await sendEditImportElements(pathname, <ImportedElements>[]); 79 await sendEditImportElements(pathname, <ImportedElements>[]);
55 80
56 SourceFileEdit edit = result.edit; 81 SourceFileEdit edit = result.edit;
57 expect(edit, isNotNull); 82 expect(edit, isNotNull);
58 expect(edit.edits, hasLength(0)); 83 expect(edit.edits, hasLength(0));
59 } 84 }
60 85
61 setUp() { 86 Future setUp() async {
62 return super.setUp().then((_) { 87 await super.setUp();
63 pathname = sourcePath('test.dart'); 88 pathname = sourcePath('test.dart');
64 });
65 } 89 }
66 90
67 test_getImportedElements_noEdits() async { 91 test_importElements_definingUnit() async {
68 writeFile(pathname, text); 92 writeFile(pathname, 'main() {}');
93 standardAnalysisSetup();
94 await analysisFinished;
95 PhysicalResourceProvider provider = PhysicalResourceProvider.INSTANCE;
96 String sdkPath = FolderBasedDartSdk.defaultSdkDirectory(provider).path;
97 String mathPath =
98 provider.pathContext.join(sdkPath, 'lib', 'math', 'math.dart');
99
100 await checkEdits(<ImportedElements>[
101 new ImportedElements(mathPath, '', <String>['Random'])
102 ], [
103 new SourceEdit(0, 0, "import 'dart:math';\n\n")
104 ]);
105 }
106
107 test_importElements_noEdits() async {
108 writeFile(pathname, '');
69 standardAnalysisSetup(); 109 standardAnalysisSetup();
70 await analysisFinished; 110 await analysisFinished;
71 111
72 List<Future> tests = []; 112 await checkNoEdits(<ImportedElements>[]);
73 // Test that an empty list of elements produces no edits. 113 }
74 tests.add(checkNoEdits(<ImportedElements>[])); 114
75 return Future.wait(tests); 115 test_importElements_part() async {
116 String libName = sourcePath('lib.dart');
117 writeFile(libName, '''
118 part 'test.dart';
119 main() {}
120 ''');
121 writeFile(pathname, '''
122 part of 'lib.dart';
123
124 class C {}
125 ''');
126 standardAnalysisSetup();
127 await analysisFinished;
128 PhysicalResourceProvider provider = PhysicalResourceProvider.INSTANCE;
129 String sdkPath = FolderBasedDartSdk.defaultSdkDirectory(provider).path;
130 String mathPath =
131 provider.pathContext.join(sdkPath, 'lib', 'math', 'math.dart');
132
133 await checkEdits(<ImportedElements>[
134 new ImportedElements(mathPath, '', <String>['Random'])
135 ], [
136 new SourceEdit(0, 0, "import 'dart:math';\n\n")
137 ], expectedFile: libName);
76 } 138 }
77 } 139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698