OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 test.services.correction.name_suggestion; | |
6 | |
7 import 'package:analysis_services/src/correction/name_suggestion.dart'; | |
8 import 'package:analysis_testing/abstract_single_unit.dart'; | |
9 import 'package:analysis_testing/reflective_tests.dart'; | |
10 import 'package:analyzer/src/generated/ast.dart'; | |
11 import 'package:analyzer/src/generated/element.dart'; | |
12 import 'package:unittest/unittest.dart'; | |
13 | |
14 | |
15 main() { | |
16 groupSep = ' | '; | |
17 runReflectiveTests(VariableNameSuggestionTest); | |
18 } | |
19 | |
20 | |
21 @ReflectiveTestCase() | |
22 class VariableNameSuggestionTest extends AbstractSingleUnitTest { | |
23 void test_forExpression_cast() { | |
24 resolveTestUnit(''' | |
25 main() { | |
26 var sortedNodes; | |
27 var res = sortedNodes as String; | |
28 } | |
29 '''); | |
30 var excluded = new Set.from([]); | |
31 var expr = findNodeAtString('as String', (node) => node is AsExpression); | |
32 expect( | |
33 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
34 unorderedEquals(['sortedNodes', 'nodes'])); | |
35 } | |
36 | |
37 void test_forExpression_expectedType() { | |
38 resolveTestUnit(''' | |
39 class TreeNode {} | |
40 main() { | |
41 TreeNode node = null; | |
42 } | |
43 '''); | |
44 Set excluded = new Set.from([]); | |
45 DartType expectedType = (findElement('node') as LocalVariableElement).type; | |
46 Expression assignedExpression = | |
47 findNodeAtString('null;', (node) => node is NullLiteral); | |
48 List<String> suggestions = | |
49 getVariableNameSuggestionsForExpression( | |
50 expectedType, | |
51 assignedExpression, | |
52 excluded); | |
53 expect(suggestions, unorderedEquals(['treeNode', 'node'])); | |
54 } | |
55 | |
56 void test_forExpression_expectedType_String() { | |
57 resolveTestUnit(''' | |
58 main() { | |
59 String res = 'abc'; | |
60 } | |
61 '''); | |
62 DartType expectedType = (findElement('res') as LocalVariableElement).type; | |
63 Expression assignedExpression = findNodeAtString("'abc';"); | |
64 // first choice for "String" is "s" | |
65 expect( | |
66 getVariableNameSuggestionsForExpression( | |
67 expectedType, | |
68 assignedExpression, | |
69 new Set.from([])), | |
70 unorderedEquals(['s'])); | |
71 } | |
72 | |
73 void test_forExpression_expectedType_double() { | |
74 resolveTestUnit(''' | |
75 main() { | |
76 double res = 0.0; | |
77 } | |
78 '''); | |
79 DartType expectedType = (findElement('res') as LocalVariableElement).type; | |
80 Expression assignedExpression = findNodeAtString('0.0;'); | |
81 // first choice for "double" is "d" | |
82 expect( | |
83 getVariableNameSuggestionsForExpression( | |
84 expectedType, | |
85 assignedExpression, | |
86 new Set.from([])), | |
87 unorderedEquals(['d'])); | |
88 // if "d" is used, try "e", "f", etc | |
89 expect( | |
90 getVariableNameSuggestionsForExpression( | |
91 expectedType, | |
92 assignedExpression, | |
93 new Set.from(['d', 'e'])), | |
94 unorderedEquals(['f'])); | |
95 } | |
96 | |
97 void test_forExpression_expectedType_int() { | |
98 resolveTestUnit(''' | |
99 main() { | |
100 int res = 0; | |
101 } | |
102 '''); | |
103 DartType expectedType = (findElement('res') as LocalVariableElement).type; | |
104 Expression assignedExpression = findNodeAtString('0;'); | |
105 // first choice for "int" is "i" | |
106 expect( | |
107 getVariableNameSuggestionsForExpression( | |
108 expectedType, | |
109 assignedExpression, | |
110 new Set.from([])), | |
111 unorderedEquals(['i'])); | |
112 // if "i" is used, try "j", "k", etc | |
113 expect( | |
114 getVariableNameSuggestionsForExpression( | |
115 expectedType, | |
116 assignedExpression, | |
117 new Set.from(['i', 'j'])), | |
118 unorderedEquals(['k'])); | |
119 } | |
120 | |
121 void test_forExpression_instanceCreation() { | |
122 verifyNoTestUnitErrors = false; | |
123 resolveTestUnit(''' | |
124 import 'dart:math' as p; | |
125 main(p) { | |
126 new NoSuchClass(); | |
127 new p.NoSuchClass(); | |
128 new NoSuchClass.named(); | |
129 } | |
130 '''); | |
131 var excluded = new Set.from([]); | |
132 expect( | |
133 getVariableNameSuggestionsForExpression( | |
134 null, | |
135 findNodeAtString('new NoSuchClass()'), | |
136 excluded), | |
137 unorderedEquals(['noSuchClass', 'suchClass', 'class'])); | |
138 expect( | |
139 getVariableNameSuggestionsForExpression( | |
140 null, | |
141 findNodeAtString('new NoSuchClass.named()'), | |
142 excluded), | |
143 unorderedEquals(['noSuchClass', 'suchClass', 'class'])); | |
144 // TODO(scheglov) This test does not work. | |
145 // In "p.NoSuchClass" the identifier "p" is not resolved to a PrefixElement. | |
146 // expect( | |
147 // getVariableNameSuggestionsForExpression( | |
148 // null, | |
149 // findNodeAtString('new p.NoSuchClass()'), | |
150 // excluded), | |
151 // unorderedEquals(['noSuchClass', 'suchClass', 'class'])); | |
152 } | |
153 | |
154 void test_forExpression_invocationArgument_named() { | |
155 resolveTestUnit(''' | |
156 foo({a, b, c}) {} | |
157 main() { | |
158 foo(a: 111, c: 333, b: 222); | |
159 } | |
160 '''); | |
161 var excluded = new Set.from([]); | |
162 { | |
163 var expr = findNodeAtString('111'); | |
164 expect( | |
165 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
166 unorderedEquals(['a'])); | |
167 } | |
168 { | |
169 var expr = findNodeAtString('222'); | |
170 expect( | |
171 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
172 unorderedEquals(['b'])); | |
173 } | |
174 { | |
175 var expr = findNodeAtString('333'); | |
176 expect( | |
177 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
178 unorderedEquals(['c'])); | |
179 } | |
180 } | |
181 | |
182 void test_forExpression_invocationArgument_optional() { | |
183 resolveTestUnit(''' | |
184 foo(a, [b = 2, c = 3]) {} | |
185 main() { | |
186 foo(111, 222, 333); | |
187 } | |
188 '''); | |
189 var excluded = new Set.from([]); | |
190 { | |
191 var expr = findNodeAtString('111'); | |
192 expect( | |
193 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
194 unorderedEquals(['a'])); | |
195 } | |
196 { | |
197 var expr = findNodeAtString('222'); | |
198 expect( | |
199 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
200 unorderedEquals(['b'])); | |
201 } | |
202 { | |
203 var expr = findNodeAtString('333'); | |
204 expect( | |
205 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
206 unorderedEquals(['c'])); | |
207 } | |
208 } | |
209 | |
210 void test_forExpression_invocationArgument_positional() { | |
211 resolveTestUnit(''' | |
212 foo(a, b) {} | |
213 main() { | |
214 foo(111, 222); | |
215 } | |
216 '''); | |
217 var excluded = new Set.from([]); | |
218 { | |
219 var expr = findNodeAtString('111'); | |
220 expect( | |
221 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
222 unorderedEquals(['a'])); | |
223 } | |
224 { | |
225 var expr = findNodeAtString('222'); | |
226 expect( | |
227 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
228 unorderedEquals(['b'])); | |
229 } | |
230 } | |
231 | |
232 void test_forExpression_methodInvocation() { | |
233 resolveTestUnit(''' | |
234 main(p) { | |
235 var res = p.getSortedNodes(); | |
236 } | |
237 '''); | |
238 var excluded = new Set.from([]); | |
239 var expr = findNodeAtString('p.get', (node) => node is MethodInvocation); | |
240 expect( | |
241 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
242 unorderedEquals(['sortedNodes', 'nodes'])); | |
243 } | |
244 | |
245 void test_forExpression_methodInvocation_noPrefix() { | |
246 resolveTestUnit(''' | |
247 main(p) { | |
248 var res = p.sortedNodes(); | |
249 } | |
250 '''); | |
251 var excluded = new Set.from([]); | |
252 var expr = findNodeAtString('p.sorted', (node) => node is MethodInvocation); | |
253 expect( | |
254 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
255 unorderedEquals(['sortedNodes', 'nodes'])); | |
256 } | |
257 | |
258 void test_forExpression_name_get() { | |
259 resolveTestUnit(''' | |
260 main(p) { | |
261 var res = p.get(); | |
262 } | |
263 '''); | |
264 var excluded = new Set.from([]); | |
265 var expr = findNodeAtString('p.get', (node) => node is MethodInvocation); | |
266 expect( | |
267 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
268 unorderedEquals([])); | |
269 } | |
270 | |
271 void test_forExpression_privateName() { | |
272 resolveTestUnit(''' | |
273 main(p) { | |
274 p._name; | |
275 p._computeSuffix(); | |
276 } | |
277 '''); | |
278 var excluded = new Set.from([]); | |
279 expect( | |
280 getVariableNameSuggestionsForExpression( | |
281 null, | |
282 findNodeAtString('p._name', (node) => node is PrefixedIdentifier), | |
283 excluded), | |
284 unorderedEquals(['name'])); | |
285 expect( | |
286 getVariableNameSuggestionsForExpression( | |
287 null, | |
288 findNodeAtString('p._compute', (node) => node is MethodInvocation), | |
289 excluded), | |
290 unorderedEquals(['computeSuffix', 'suffix'])); | |
291 } | |
292 | |
293 void test_forExpression_propertyAccess() { | |
294 resolveTestUnit(''' | |
295 main(p) { | |
296 var res = p.sortedNodes; | |
297 } | |
298 '''); | |
299 var excluded = new Set.from([]); | |
300 expect( | |
301 getVariableNameSuggestionsForExpression( | |
302 null, | |
303 findNodeAtString('p.sorted', (node) => node is PrefixedIdentifier), | |
304 excluded), | |
305 unorderedEquals(['sortedNodes', 'nodes'])); | |
306 } | |
307 | |
308 void test_forExpression_simpleName() { | |
309 resolveTestUnit(''' | |
310 main(p) { | |
311 var sortedNodes = null; | |
312 var res = sortedNodes; | |
313 } | |
314 '''); | |
315 var excluded = new Set.from([]); | |
316 var expr = findNodeAtString('sortedNodes;'); | |
317 expect( | |
318 getVariableNameSuggestionsForExpression(null, expr, excluded), | |
319 unorderedEquals(['sortedNodes', 'nodes'])); | |
320 } | |
321 | |
322 void test_forExpression_unqualifiedInvocation() { | |
323 resolveTestUnit(''' | |
324 getSortedNodes() => []; | |
325 main(p) { | |
326 var res = getSortedNodes(); | |
327 } | |
328 '''); | |
329 var excluded = new Set.from([]); | |
330 expect( | |
331 getVariableNameSuggestionsForExpression( | |
332 null, | |
333 findNodeAtString('getSortedNodes();', (node) => node is MethodInvoca
tion), | |
334 excluded), | |
335 unorderedEquals(['sortedNodes', 'nodes'])); | |
336 } | |
337 | |
338 void test_forText() { | |
339 { | |
340 Set excluded = new Set.from([]); | |
341 List<String> suggestions = | |
342 getVariableNameSuggestionsForText('Goodbye, cruel world!', excluded); | |
343 expect( | |
344 suggestions, | |
345 unorderedEquals(['goodbyeCruelWorld', 'cruelWorld', 'world'])); | |
346 } | |
347 { | |
348 Set excluded = new Set.from(['world']); | |
349 List<String> suggestions = | |
350 getVariableNameSuggestionsForText('Goodbye, cruel world!', excluded); | |
351 expect( | |
352 suggestions, | |
353 unorderedEquals(['goodbyeCruelWorld', 'cruelWorld', 'world2'])); | |
354 } | |
355 } | |
356 | |
357 void test_getCamelWords_empty() { | |
358 expect(getCamelWords(''), unorderedEquals([])); | |
359 } | |
360 | |
361 void test_getCamelWords_multipleUpper() { | |
362 expect( | |
363 getCamelWords('sortedHTMLNodes'), | |
364 unorderedEquals(['sorted', 'HTML', 'Nodes'])); | |
365 } | |
366 | |
367 void test_getCamelWords_simpleCamel() { | |
368 expect( | |
369 getCamelWords('mySimpleText'), | |
370 unorderedEquals(['my', 'Simple', 'Text'])); | |
371 } | |
372 | |
373 void test_getCamelWords_simpleName() { | |
374 expect(getCamelWords('name'), unorderedEquals(['name'])); | |
375 } | |
376 } | |
OLD | NEW |