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

Side by Side Diff: pkg/analysis_server/test/services/correction/util_test.dart

Issue 2532393008: Add libraries in corrections using SourceInfo instance of LibraryElement. (Closed)
Patch Set: Created 4 years 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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 library test.services.correction.util; 5 library test.services.correction.util;
6 6
7 import 'package:analysis_server/plugin/protocol/protocol.dart'; 7 import 'package:analysis_server/plugin/protocol/protocol.dart';
8 import 'package:analysis_server/src/services/correction/strings.dart'; 8 import 'package:analysis_server/src/services/correction/strings.dart';
9 import 'package:analysis_server/src/services/correction/util.dart'; 9 import 'package:analysis_server/src/services/correction/util.dart';
10 import 'package:analyzer/dart/element/element.dart';
11 import 'package:analyzer/src/generated/source.dart';
12 import 'package:test/test.dart'; 10 import 'package:test/test.dart';
13 import 'package:test_reflective_loader/test_reflective_loader.dart'; 11 import 'package:test_reflective_loader/test_reflective_loader.dart';
14 12
15 import '../../abstract_single_unit.dart'; 13 import '../../abstract_single_unit.dart';
16 14
17 main() { 15 main() {
18 defineReflectiveSuite(() { 16 defineReflectiveSuite(() {
19 defineReflectiveTests(UtilTest); 17 defineReflectiveTests(UtilTest);
20 }); 18 });
21 } 19 }
22 20
23 @reflectiveTest 21 @reflectiveTest
24 class UtilTest extends AbstractSingleUnitTest { 22 class UtilTest extends AbstractSingleUnitTest {
25 test_addLibraryImports_dart_hasImports_between() { 23 test_addLibraryImports_dart_hasImports_between() {
26 resolveTestUnit(''' 24 resolveTestUnit('''
27 import 'dart:async'; 25 import 'dart:async';
28 import 'dart:math'; 26 import 'dart:math';
29 '''); 27 ''');
30 LibraryElement newLibrary = _getDartLibrary('dart:collection'); 28 SourceInfo newLibrary = _getDartSourceInfo('dart:collection');
31 _assertAddLibraryImport( 29 _assertAddLibraryImport(
32 <LibraryElement>[newLibrary], 30 <SourceInfo>[newLibrary],
33 ''' 31 '''
34 import 'dart:async'; 32 import 'dart:async';
35 import 'dart:collection'; 33 import 'dart:collection';
36 import 'dart:math'; 34 import 'dart:math';
37 '''); 35 ''');
38 } 36 }
39 37
40 test_addLibraryImports_dart_hasImports_first() { 38 test_addLibraryImports_dart_hasImports_first() {
41 resolveTestUnit(''' 39 resolveTestUnit('''
42 import 'dart:collection'; 40 import 'dart:collection';
43 import 'dart:math'; 41 import 'dart:math';
44 '''); 42 ''');
45 LibraryElement newLibrary = _getDartLibrary('dart:async'); 43 SourceInfo newLibrary = _getDartSourceInfo('dart:async');
46 _assertAddLibraryImport( 44 _assertAddLibraryImport(
47 <LibraryElement>[newLibrary], 45 <SourceInfo>[newLibrary],
48 ''' 46 '''
49 import 'dart:async'; 47 import 'dart:async';
50 import 'dart:collection'; 48 import 'dart:collection';
51 import 'dart:math'; 49 import 'dart:math';
52 '''); 50 ''');
53 } 51 }
54 52
55 test_addLibraryImports_dart_hasImports_last() { 53 test_addLibraryImports_dart_hasImports_last() {
56 resolveTestUnit(''' 54 resolveTestUnit('''
57 import 'dart:async'; 55 import 'dart:async';
58 import 'dart:collection'; 56 import 'dart:collection';
59 '''); 57 ''');
60 LibraryElement newLibrary = _getDartLibrary('dart:math'); 58 SourceInfo newLibrary = _getDartSourceInfo('dart:math');
61 _assertAddLibraryImport( 59 _assertAddLibraryImport(
62 <LibraryElement>[newLibrary], 60 <SourceInfo>[newLibrary],
63 ''' 61 '''
64 import 'dart:async'; 62 import 'dart:async';
65 import 'dart:collection'; 63 import 'dart:collection';
66 import 'dart:math'; 64 import 'dart:math';
67 '''); 65 ''');
68 } 66 }
69 67
70 test_addLibraryImports_dart_hasImports_multiple() { 68 test_addLibraryImports_dart_hasImports_multiple() {
71 resolveTestUnit(''' 69 resolveTestUnit('''
72 import 'dart:collection'; 70 import 'dart:collection';
73 import 'dart:math'; 71 import 'dart:math';
74 '''); 72 ''');
75 LibraryElement newLibrary1 = _getDartLibrary('dart:async'); 73 SourceInfo newLibrary1 = _getDartSourceInfo('dart:async');
76 LibraryElement newLibrary2 = _getDartLibrary('dart:html'); 74 SourceInfo newLibrary2 = _getDartSourceInfo('dart:html');
77 _assertAddLibraryImport( 75 _assertAddLibraryImport(
78 <LibraryElement>[newLibrary1, newLibrary2], 76 <SourceInfo>[newLibrary1, newLibrary2],
79 ''' 77 '''
80 import 'dart:async'; 78 import 'dart:async';
81 import 'dart:collection'; 79 import 'dart:collection';
82 import 'dart:html'; 80 import 'dart:html';
83 import 'dart:math'; 81 import 'dart:math';
84 '''); 82 ''');
85 } 83 }
86 84
87 test_addLibraryImports_dart_hasImports_multiple_first() { 85 test_addLibraryImports_dart_hasImports_multiple_first() {
88 resolveTestUnit(''' 86 resolveTestUnit('''
89 import 'dart:html'; 87 import 'dart:html';
90 import 'dart:math'; 88 import 'dart:math';
91 '''); 89 ''');
92 LibraryElement newLibrary1 = _getDartLibrary('dart:async'); 90 SourceInfo newLibrary1 = _getDartSourceInfo('dart:async');
93 LibraryElement newLibrary2 = _getDartLibrary('dart:collection'); 91 SourceInfo newLibrary2 = _getDartSourceInfo('dart:collection');
94 _assertAddLibraryImport( 92 _assertAddLibraryImport(
95 <LibraryElement>[newLibrary1, newLibrary2], 93 <SourceInfo>[newLibrary1, newLibrary2],
96 ''' 94 '''
97 import 'dart:async'; 95 import 'dart:async';
98 import 'dart:collection'; 96 import 'dart:collection';
99 import 'dart:html'; 97 import 'dart:html';
100 import 'dart:math'; 98 import 'dart:math';
101 '''); 99 ''');
102 } 100 }
103 101
104 test_addLibraryImports_dart_hasImports_multiple_last() { 102 test_addLibraryImports_dart_hasImports_multiple_last() {
105 resolveTestUnit(''' 103 resolveTestUnit('''
106 import 'dart:async'; 104 import 'dart:async';
107 import 'dart:collection'; 105 import 'dart:collection';
108 '''); 106 ''');
109 LibraryElement newLibrary1 = _getDartLibrary('dart:html'); 107 SourceInfo newLibrary1 = _getDartSourceInfo('dart:html');
110 LibraryElement newLibrary2 = _getDartLibrary('dart:math'); 108 SourceInfo newLibrary2 = _getDartSourceInfo('dart:math');
111 _assertAddLibraryImport( 109 _assertAddLibraryImport(
112 <LibraryElement>[newLibrary1, newLibrary2], 110 <SourceInfo>[newLibrary1, newLibrary2],
113 ''' 111 '''
114 import 'dart:async'; 112 import 'dart:async';
115 import 'dart:collection'; 113 import 'dart:collection';
116 import 'dart:html'; 114 import 'dart:html';
117 import 'dart:math'; 115 import 'dart:math';
118 '''); 116 ''');
119 } 117 }
120 118
121 test_addLibraryImports_dart_hasLibraryDirective() { 119 test_addLibraryImports_dart_hasLibraryDirective() {
122 resolveTestUnit(''' 120 resolveTestUnit('''
123 library test; 121 library test;
124 122
125 class A {} 123 class A {}
126 '''); 124 ''');
127 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); 125 SourceInfo newLibrary1 = _getDartSourceInfo('dart:math');
128 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); 126 SourceInfo newLibrary2 = _getDartSourceInfo('dart:async');
129 _assertAddLibraryImport( 127 _assertAddLibraryImport(
130 <LibraryElement>[newLibrary1, newLibrary2], 128 <SourceInfo>[newLibrary1, newLibrary2],
131 ''' 129 '''
132 library test; 130 library test;
133 131
134 import 'dart:async'; 132 import 'dart:async';
135 import 'dart:math'; 133 import 'dart:math';
136 134
137 class A {} 135 class A {}
138 '''); 136 ''');
139 } 137 }
140 138
141 test_addLibraryImports_dart_noDirectives_hasComment() { 139 test_addLibraryImports_dart_noDirectives_hasComment() {
142 resolveTestUnit(''' 140 resolveTestUnit('''
143 /// Comment. 141 /// Comment.
144 142
145 class A {} 143 class A {}
146 '''); 144 ''');
147 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); 145 SourceInfo newLibrary1 = _getDartSourceInfo('dart:math');
148 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); 146 SourceInfo newLibrary2 = _getDartSourceInfo('dart:async');
149 _assertAddLibraryImport( 147 _assertAddLibraryImport(
150 <LibraryElement>[newLibrary1, newLibrary2], 148 <SourceInfo>[newLibrary1, newLibrary2],
151 ''' 149 '''
152 /// Comment. 150 /// Comment.
153 151
154 import 'dart:async'; 152 import 'dart:async';
155 import 'dart:math'; 153 import 'dart:math';
156 154
157 class A {} 155 class A {}
158 '''); 156 ''');
159 } 157 }
160 158
161 test_addLibraryImports_dart_noDirectives_hasShebang() { 159 test_addLibraryImports_dart_noDirectives_hasShebang() {
162 resolveTestUnit(''' 160 resolveTestUnit('''
163 #!/bin/dart 161 #!/bin/dart
164 162
165 class A {} 163 class A {}
166 '''); 164 ''');
167 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); 165 SourceInfo newLibrary1 = _getDartSourceInfo('dart:math');
168 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); 166 SourceInfo newLibrary2 = _getDartSourceInfo('dart:async');
169 _assertAddLibraryImport( 167 _assertAddLibraryImport(
170 <LibraryElement>[newLibrary1, newLibrary2], 168 <SourceInfo>[newLibrary1, newLibrary2],
171 ''' 169 '''
172 #!/bin/dart 170 #!/bin/dart
173 171
174 import 'dart:async'; 172 import 'dart:async';
175 import 'dart:math'; 173 import 'dart:math';
176 174
177 class A {} 175 class A {}
178 '''); 176 ''');
179 } 177 }
180 178
181 test_addLibraryImports_dart_noDirectives_noShebang() { 179 test_addLibraryImports_dart_noDirectives_noShebang() {
182 resolveTestUnit(''' 180 resolveTestUnit('''
183 class A {} 181 class A {}
184 '''); 182 ''');
185 LibraryElement newLibrary1 = _getDartLibrary('dart:math'); 183 SourceInfo newLibrary1 = _getDartSourceInfo('dart:math');
186 LibraryElement newLibrary2 = _getDartLibrary('dart:async'); 184 SourceInfo newLibrary2 = _getDartSourceInfo('dart:async');
187 _assertAddLibraryImport( 185 _assertAddLibraryImport(
188 <LibraryElement>[newLibrary1, newLibrary2], 186 <SourceInfo>[newLibrary1, newLibrary2],
189 ''' 187 '''
190 import 'dart:async'; 188 import 'dart:async';
191 import 'dart:math'; 189 import 'dart:math';
192 190
193 class A {} 191 class A {}
194 '''); 192 ''');
195 } 193 }
196 194
197 test_addLibraryImports_package_hasDart_hasPackages_insertAfter() { 195 test_addLibraryImports_package_hasDart_hasPackages_insertAfter() {
198 resolveTestUnit(''' 196 resolveTestUnit('''
199 import 'dart:async'; 197 import 'dart:async';
200 198
201 import 'package:aaa/aaa.dart'; 199 import 'package:aaa/aaa.dart';
202 '''); 200 ''');
203 LibraryElement newLibrary = 201 SourceInfo newLibrary =
204 _mockLibraryElement('/lib/bbb.dart', 'package:bbb/bbb.dart'); 202 _getSourceInfo('/lib/bbb.dart', 'package:bbb/bbb.dart');
205 _assertAddLibraryImport( 203 _assertAddLibraryImport(
206 <LibraryElement>[newLibrary], 204 <SourceInfo>[newLibrary],
207 ''' 205 '''
208 import 'dart:async'; 206 import 'dart:async';
209 207
210 import 'package:aaa/aaa.dart'; 208 import 'package:aaa/aaa.dart';
211 import 'package:bbb/bbb.dart'; 209 import 'package:bbb/bbb.dart';
212 '''); 210 ''');
213 } 211 }
214 212
215 test_addLibraryImports_package_hasDart_hasPackages_insertBefore() { 213 test_addLibraryImports_package_hasDart_hasPackages_insertBefore() {
216 resolveTestUnit(''' 214 resolveTestUnit('''
217 import 'dart:async'; 215 import 'dart:async';
218 216
219 import 'package:bbb/bbb.dart'; 217 import 'package:bbb/bbb.dart';
220 '''); 218 ''');
221 LibraryElement newLibrary = 219 SourceInfo newLibrary =
222 _mockLibraryElement('/lib/aaa.dart', 'package:aaa/aaa.dart'); 220 _getSourceInfo('/lib/aaa.dart', 'package:aaa/aaa.dart');
223 _assertAddLibraryImport( 221 _assertAddLibraryImport(
224 <LibraryElement>[newLibrary], 222 <SourceInfo>[newLibrary],
225 ''' 223 '''
226 import 'dart:async'; 224 import 'dart:async';
227 225
228 import 'package:aaa/aaa.dart'; 226 import 'package:aaa/aaa.dart';
229 import 'package:bbb/bbb.dart'; 227 import 'package:bbb/bbb.dart';
230 '''); 228 ''');
231 } 229 }
232 230
233 test_addLibraryImports_package_hasImports_between() { 231 test_addLibraryImports_package_hasImports_between() {
234 resolveTestUnit(''' 232 resolveTestUnit('''
235 import 'package:aaa/aaa.dart'; 233 import 'package:aaa/aaa.dart';
236 import 'package:ddd/ddd.dart'; 234 import 'package:ddd/ddd.dart';
237 '''); 235 ''');
238 LibraryElement newLibrary1 = 236 SourceInfo newLibrary1 =
239 _mockLibraryElement('/lib/bbb.dart', 'package:bbb/bbb.dart'); 237 _getSourceInfo('/lib/bbb.dart', 'package:bbb/bbb.dart');
240 LibraryElement newLibrary2 = 238 SourceInfo newLibrary2 =
241 _mockLibraryElement('/lib/ccc.dart', 'package:ccc/ccc.dart'); 239 _getSourceInfo('/lib/ccc.dart', 'package:ccc/ccc.dart');
242 _assertAddLibraryImport( 240 _assertAddLibraryImport(
243 <LibraryElement>[newLibrary1, newLibrary2], 241 <SourceInfo>[newLibrary1, newLibrary2],
244 ''' 242 '''
245 import 'package:aaa/aaa.dart'; 243 import 'package:aaa/aaa.dart';
246 import 'package:bbb/bbb.dart'; 244 import 'package:bbb/bbb.dart';
247 import 'package:ccc/ccc.dart'; 245 import 'package:ccc/ccc.dart';
248 import 'package:ddd/ddd.dart'; 246 import 'package:ddd/ddd.dart';
249 '''); 247 ''');
250 } 248 }
251 249
252 void _assertAddLibraryImport( 250 void _assertAddLibraryImport(
253 List<LibraryElement> newLibraries, String expectedCode) { 251 List<SourceInfo> newLibraries, String expectedCode) {
254 SourceChange change = new SourceChange(''); 252 SourceChange change = new SourceChange('');
255 addLibraryImports(change, testLibraryElement, newLibraries.toSet()); 253 addLibraryImports(change, testLibraryElement, newLibraries.toSet());
256 SourceFileEdit testEdit = change.getFileEdit(testFile); 254 SourceFileEdit testEdit = change.getFileEdit(testFile);
257 expect(testEdit, isNotNull); 255 expect(testEdit, isNotNull);
258 String resultCode = SourceEdit.applySequence(testCode, testEdit.edits); 256 String resultCode = SourceEdit.applySequence(testCode, testEdit.edits);
259 expect(resultCode, expectedCode); 257 expect(resultCode, expectedCode);
260 } 258 }
261 259
262 LibraryElement _getDartLibrary(String uri) { 260 SourceInfo _getDartSourceInfo(String uri) {
263 String path = removeStart(uri, 'dart:'); 261 String path = removeStart(uri, 'dart:');
264 Source newSource = new _SourceMock('/sdk/lib/$path.dart', Uri.parse(uri)); 262 return new SourceInfo('/sdk/lib/$path.dart', Uri.parse(uri));
265 return new _LibraryElementMock(newSource);
266 } 263 }
267 264
268 LibraryElement _mockLibraryElement(String path, String uri) { 265 SourceInfo _getSourceInfo(String path, String uri) {
269 Source newSource = new _SourceMock(path, Uri.parse(uri)); 266 return new SourceInfo(path, Uri.parse(uri));
270 return new _LibraryElementMock(newSource);
271 } 267 }
272 } 268 }
273
274 class _LibraryElementMock implements LibraryElement {
275 @override
276 final Source source;
277
278 _LibraryElementMock(this.source);
279
280 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
281 }
282
283 class _SourceMock implements Source {
284 @override
285 final String fullName;
286
287 @override
288 final Uri uri;
289
290 _SourceMock(this.fullName, this.uri);
291
292 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
293 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698