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

Side by Side Diff: pkg/analysis_server/test/src/utilities/change_builder_dart_test.dart

Issue 1110583002: Add utilities for creating SourceChanges (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add missed files Created 5 years, 7 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 | Annotate | Revision Log
OLDNEW
(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 library analysis_server.test.src.utilities.change_builder_dart_test;
6
7 import 'package:analysis_server/src/protocol.dart';
8 import 'package:analysis_server/src/utilities/change_builder_core.dart';
9 import 'package:analysis_server/src/utilities/change_builder_dart.dart';
10 import 'package:analysis_server/utilities/change_builder_core.dart';
11 import 'package:analysis_server/utilities/change_builder_dart.dart';
12 import 'package:analyzer/src/generated/ast.dart';
13 import 'package:analyzer/src/generated/source.dart';
14 import 'package:test_reflective_loader/test_reflective_loader.dart';
15 import 'package:unittest/unittest.dart';
16
17 import '../../abstract_context.dart';
18 import '../../domain_execution_test.dart';
19
20 main() {
21 groupSep = ' | ';
22 defineReflectiveTests(DartChangeBuilderImplTest);
23 defineReflectiveTests(DartEditBuilderImplTest);
24 defineReflectiveTests(DartFileEditBuilderImplTest);
25 }
26
27 @reflectiveTest
28 class DartChangeBuilderImplTest extends AbstractContextTest {
29 void test_createFileEditBuilder() {
30 Source source = addSource('/test.dart', 'library test;');
31 resolveLibraryUnit(source);
32 int timeStamp = 54;
33 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
34 DartFileEditBuilderImpl fileEditBuilder =
35 builder.createFileEditBuilder(source, timeStamp);
36 expect(fileEditBuilder, new isInstanceOf<DartFileEditBuilder>());
37 SourceFileEdit fileEdit = fileEditBuilder.fileEdit;
38 expect(fileEdit.file, source.fullName);
39 expect(fileEdit.fileStamp, timeStamp);
40 }
41 }
42
43 @reflectiveTest
44 class DartEditBuilderImplTest extends AbstractContextTest {
45 SourceEdit getEdit(DartChangeBuilderImpl builder) {
46 SourceChange sourceChange = builder.sourceChange;
47 expect(sourceChange, isNotNull);
48 List<SourceFileEdit> fileEdits = sourceChange.edits;
49 expect(fileEdits, hasLength(1));
50 SourceFileEdit fileEdit = fileEdits[0];
51 expect(fileEdit, isNotNull);
52 List<SourceEdit> edits = fileEdit.edits;
53 expect(edits, hasLength(1));
54 return edits[0];
55 }
56
57 void test_writeClassDeclaration_isAbstract() {
58 Source source = addSource('/test.dart', '');
59 resolveLibraryUnit(source);
60
61 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
62 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
63 builder.addInsertion(0, (DartEditBuilder builder) {
64 builder.writeClassDeclaration('C', isAbstract: true);
65 });
66 });
67 SourceEdit edit = getEdit(builder);
68 expect(edit.replacement, equalsIgnoringWhitespace('abstract class C { }'));
69 }
70
71 void test_writeClassDeclaration_nameOnly() {
72 Source source = addSource('/test.dart', '');
73 resolveLibraryUnit(source);
74
75 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
76 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
77 builder.addInsertion(0, (DartEditBuilder builder) {
78 builder.writeClassDeclaration('C');
79 });
80 });
81 SourceEdit edit = getEdit(builder);
82 expect(edit.replacement, equalsIgnoringWhitespace('class C { }'));
83
84 List<LinkedEditGroup> linkedEditGroups =
85 builder.sourceChange.linkedEditGroups;
86 expect(linkedEditGroups, hasLength(1));
87 LinkedEditGroup group = linkedEditGroups[0];
88 expect(group.length, 1);
89 expect(group.positions, hasLength(1));
90 }
91
92 void test_writeClassDeclaration_superclass() {
93 Source source = addSource('/test.dart', 'class B {}');
94 CompilationUnit unit = resolveLibraryUnit(source);
95 ClassDeclaration declaration = unit.declarations[0];
96
97 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
98 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
99 builder.addInsertion(0, (DartEditBuilder builder) {
100 builder.writeClassDeclaration('C',
101 superclass: declaration.element.type);
102 });
103 });
104 SourceEdit edit = getEdit(builder);
105 expect(edit.replacement, equalsIgnoringWhitespace('class C extends B { }'));
106 }
107
108 void test_writeOverrideOfInheritedMember() {
109 String content = '''
110 class A {
111 A add(A a) => null;
112 }
113 class B extends A {
114 }''';
115 Source source = addSource('/test.dart', content);
116 CompilationUnit unit = resolveLibraryUnit(source);
117 ClassDeclaration declaration = unit.declarations[0];
118
119 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
120 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
121 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
122 builder.writeOverrideOfInheritedMember(declaration.element.methods[0]);
123 });
124 });
125 SourceEdit edit = getEdit(builder);
126 expect(edit.replacement, equalsIgnoringWhitespace('''
127 @override
128 A add(A a) {
129 // TODO: implement add
130 return null;
131 }'''));
132 }
133
134 void test_writeParameters_named() {
135 String content = 'f(int i, {String s}) {}';
136 Source source = addSource('/test.dart', content);
137 CompilationUnit unit = resolveLibraryUnit(source);
138 FunctionDeclaration f = unit.declarations[0];
139 FormalParameterList parameters = f.functionExpression.parameters;
140 Iterable elements = parameters.parameters
141 .map((FormalParameter parameter) => parameter.element);
142
143 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
144 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
145 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
146 builder.writeParameters(elements);
147 });
148 });
149 SourceEdit edit = getEdit(builder);
150 expect(edit.replacement, equalsIgnoringWhitespace('(int i, {String s})'));
151 }
152
153 void test_writeParameters_positional() {
154 String content = 'f(int i, [String s]) {}';
155 Source source = addSource('/test.dart', content);
156 CompilationUnit unit = resolveLibraryUnit(source);
157 FunctionDeclaration f = unit.declarations[0];
158 FormalParameterList parameters = f.functionExpression.parameters;
159 Iterable elements = parameters.parameters
160 .map((FormalParameter parameter) => parameter.element);
161
162 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
163 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
164 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
165 builder.writeParameters(elements);
166 });
167 });
168 SourceEdit edit = getEdit(builder);
169 expect(edit.replacement, equalsIgnoringWhitespace('(int i, [String s])'));
170 }
171
172 void test_writeParameters_required() {
173 String content = 'f(int i, String s) {}';
174 Source source = addSource('/test.dart', content);
175 CompilationUnit unit = resolveLibraryUnit(source);
176 FunctionDeclaration f = unit.declarations[0];
177 FormalParameterList parameters = f.functionExpression.parameters;
178 Iterable elements = parameters.parameters
179 .map((FormalParameter parameter) => parameter.element);
180
181 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
182 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
183 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
184 builder.writeParameters(elements);
185 });
186 });
187 SourceEdit edit = getEdit(builder);
188 expect(edit.replacement, equalsIgnoringWhitespace('(int i, String s)'));
189 }
190
191 void test_writeParametersMatchingArguments_named() {
192 String content = '''
193 f(int i, String s) {
194 g(s, index: i);
195 }''';
196 Source source = addSource('/test.dart', content);
197 CompilationUnit unit = resolveLibraryUnit(source);
198 FunctionDeclaration f = unit.declarations[0];
199 BlockFunctionBody body = f.functionExpression.body;
200 ExpressionStatement statement = body.block.statements[0];
201 MethodInvocation invocation = statement.expression;
202
203 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
204 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
205 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
206 builder.writeParametersMatchingArguments(invocation.argumentList);
207 });
208 });
209 SourceEdit edit = getEdit(builder);
210 expect(
211 edit.replacement, equalsIgnoringWhitespace('(String s, [int index])'));
212 }
213
214 void test_writeParametersMatchingArguments_required() {
215 String content = '''
216 f(int i, String s) {
217 g(s, i);
218 }''';
219 Source source = addSource('/test.dart', content);
220 CompilationUnit unit = resolveLibraryUnit(source);
221 FunctionDeclaration f = unit.declarations[0];
222 BlockFunctionBody body = f.functionExpression.body;
223 ExpressionStatement statement = body.block.statements[0];
224 MethodInvocation invocation = statement.expression;
225
226 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
227 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
228 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
229 builder.writeParametersMatchingArguments(invocation.argumentList);
230 });
231 });
232 SourceEdit edit = getEdit(builder);
233 expect(edit.replacement, equalsIgnoringWhitespace('(String s, int i)'));
234 }
235
236 void test_writeParameterSource() {
237 String content = 'class A {}';
238 Source source = addSource('/test.dart', content);
239 CompilationUnit unit = resolveLibraryUnit(source);
240 ClassDeclaration classA = unit.declarations[0];
241
242 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
243 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
244 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
245 builder.writeParameterSource(classA.element.type, 'a');
246 });
247 });
248 SourceEdit edit = getEdit(builder);
249 expect(edit.replacement, equalsIgnoringWhitespace('A a'));
250 }
251
252 void test_writeType_dymanic() {
253 String content = 'class A {}';
254 Source source = addSource('/test.dart', content);
255 CompilationUnit unit = resolveLibraryUnit(source);
256
257 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
258 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
259 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
260 builder.writeType(unit.element.context.typeProvider.dynamicType);
261 });
262 });
263 SourceEdit edit = getEdit(builder);
264 expect(edit.replacement, equalsIgnoringWhitespace(''));
265 }
266
267 void test_writeType_genericType() {
268 String content = 'class A {} class B<E> {}';
269 Source source = addSource('/test.dart', content);
270 CompilationUnit unit = resolveLibraryUnit(source);
271 ClassDeclaration classA = unit.declarations[0];
272 ClassDeclaration classB = unit.declarations[1];
273
274 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
275 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
276 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
277 builder
278 .writeType(classB.element.type.substitute4([classA.element.type]));
279 });
280 });
281 SourceEdit edit = getEdit(builder);
282 expect(edit.replacement, equalsIgnoringWhitespace('B<A>'));
283 }
284
285 void test_writeType_groupName() {
286 String content = 'class A {} class B extends A {} class C extends B {}';
287 Source source = addSource('/test.dart', content);
288 CompilationUnit unit = resolveLibraryUnit(source);
289 ClassDeclaration classC = unit.declarations[2];
290
291 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
292 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
293 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
294 builder.writeType(classC.element.type, groupName: 'type');
295 });
296 });
297 SourceEdit edit = getEdit(builder);
298 expect(edit.replacement, equalsIgnoringWhitespace('C'));
299
300 List<LinkedEditGroup> linkedEditGroups =
301 builder.sourceChange.linkedEditGroups;
302 expect(linkedEditGroups, hasLength(1));
303 LinkedEditGroup group = linkedEditGroups[0];
304 expect(group, isNotNull);
305 }
306
307 void test_writeType_groupName_addSupertypeProposals() {
308 String content = 'class A {} class B extends A {} class C extends B {}';
309 Source source = addSource('/test.dart', content);
310 CompilationUnit unit = resolveLibraryUnit(source);
311 ClassDeclaration classC = unit.declarations[2];
312
313 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
314 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
315 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
316 builder.writeType(classC.element.type,
317 addSupertypeProposals: true, groupName: 'type');
318 });
319 });
320 SourceEdit edit = getEdit(builder);
321 expect(edit.replacement, equalsIgnoringWhitespace('C'));
322
323 List<LinkedEditGroup> linkedEditGroups =
324 builder.sourceChange.linkedEditGroups;
325 expect(linkedEditGroups, hasLength(1));
326 LinkedEditGroup group = linkedEditGroups[0];
327 List<LinkedEditSuggestion> suggestions = group.suggestions;
328 expect(suggestions, hasLength(4));
329 Iterable<String> values = suggestions
330 .map((LinkedEditSuggestion suggestion) {
331 expect(suggestion.kind, LinkedEditSuggestionKind.TYPE);
332 return suggestion.value;
333 });
334 expect(values, contains('Object'));
335 expect(values, contains('A'));
336 expect(values, contains('B'));
337 expect(values, contains('C'));
338 }
339
340 void test_writeType_null() {
341 String content = 'class A {}';
342 Source source = addSource('/test.dart', content);
343 resolveLibraryUnit(source);
344
345 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
346 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
347 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
348 builder.writeType(null);
349 });
350 });
351 SourceEdit edit = getEdit(builder);
352 expect(edit.replacement, equalsIgnoringWhitespace(''));
353 }
354
355 void test_writeType_required_dymanic() {
356 String content = 'class A {}';
357 Source source = addSource('/test.dart', content);
358 CompilationUnit unit = resolveLibraryUnit(source);
359
360 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
361 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
362 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
363 builder.writeType(unit.element.context.typeProvider.dynamicType,
364 required: true);
365 });
366 });
367 SourceEdit edit = getEdit(builder);
368 expect(edit.replacement, equalsIgnoringWhitespace('var'));
369 }
370
371 void test_writeType_required_notNull() {
372 String content = 'class A {}';
373 Source source = addSource('/test.dart', content);
374 CompilationUnit unit = resolveLibraryUnit(source);
375 ClassDeclaration classA = unit.declarations[0];
376
377 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
378 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
379 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
380 builder.writeType(classA.element.type, required: true);
381 });
382 });
383 SourceEdit edit = getEdit(builder);
384 expect(edit.replacement, equalsIgnoringWhitespace('A'));
385 }
386
387 void test_writeType_required_null() {
388 String content = 'class A {}';
389 Source source = addSource('/test.dart', content);
390 resolveLibraryUnit(source);
391
392 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
393 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
394 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
395 builder.writeType(null, required: true);
396 });
397 });
398 SourceEdit edit = getEdit(builder);
399 expect(edit.replacement, equalsIgnoringWhitespace('var'));
400 }
401
402 void test_writeType_simpleType() {
403 String content = 'class A {}';
404 Source source = addSource('/test.dart', content);
405 CompilationUnit unit = resolveLibraryUnit(source);
406 ClassDeclaration classA = unit.declarations[0];
407
408 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
409 builder.addFileEdit(source, 1, (DartFileEditBuilderImpl builder) {
410 builder.addInsertion(content.length - 1, (DartEditBuilder builder) {
411 builder.writeType(classA.element.type);
412 });
413 });
414 SourceEdit edit = getEdit(builder);
415 expect(edit.replacement, equalsIgnoringWhitespace('A'));
416 }
417 }
418
419 @reflectiveTest
420 class DartFileEditBuilderImplTest extends AbstractContextTest {
421 void test_createEditBuilder() {
422 Source source = addSource('/test.dart', 'library test;');
423 resolveLibraryUnit(source);
424 int timeStamp = 65;
425 DartChangeBuilderImpl builder = new DartChangeBuilderImpl(context);
426 builder.addFileEdit(source, timeStamp, (DartFileEditBuilderImpl builder) {
427 int offset = 4;
428 int length = 5;
429 DartEditBuilderImpl editBuilder =
430 builder.createEditBuilder(offset, length);
431 expect(editBuilder, new isInstanceOf<DartEditBuilder>());
432 SourceEdit sourceEdit = editBuilder.sourceEdit;
433 expect(sourceEdit.length, length);
434 expect(sourceEdit.offset, offset);
435 expect(sourceEdit.replacement, isEmpty);
436 });
437 }
438 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698