OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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.src.task.incremental_element_builder_test; | 5 library test.src.task.incremental_element_builder_test; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
10 import 'package:analyzer/src/task/incremental_element_builder.dart'; | 10 import 'package:analyzer/src/task/incremental_element_builder.dart'; |
11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
12 | 12 |
13 import '../../reflective_tests.dart'; | 13 import '../../reflective_tests.dart'; |
14 import '../context/abstract_context.dart'; | 14 import '../context/abstract_context.dart'; |
15 | 15 |
16 main() { | 16 main() { |
17 groupSep = ' | '; | 17 groupSep = ' | '; |
18 runReflectiveTests(IncrementalCompilationUnitElementBuilderTest); | 18 runReflectiveTests(IncrementalCompilationUnitElementBuilderTest); |
19 } | 19 } |
20 | 20 |
21 @reflectiveTest | 21 @reflectiveTest |
22 class IncrementalCompilationUnitElementBuilderTest extends AbstractContextTest { | 22 class IncrementalCompilationUnitElementBuilderTest extends AbstractContextTest { |
23 Source source; | 23 Source source; |
| 24 |
24 String oldCode; | 25 String oldCode; |
25 CompilationUnit oldUnit; | 26 CompilationUnit oldUnit; |
26 CompilationUnitElement unitElement; | 27 CompilationUnitElement unitElement; |
27 | 28 |
28 String newCode; | 29 String newCode; |
29 CompilationUnit newUnit; | 30 CompilationUnit newUnit; |
30 | 31 |
| 32 CompilationUnitElementDelta unitDelta; |
| 33 |
31 String getNodeText(AstNode node) { | 34 String getNodeText(AstNode node) { |
32 return newCode.substring(node.offset, node.end); | 35 return newCode.substring(node.offset, node.end); |
33 } | 36 } |
34 | 37 |
35 test_directives_add() { | 38 test_directives_add() { |
36 _buildOldUnit(r''' | 39 _buildOldUnit(r''' |
37 library test; | 40 library test; |
38 import 'dart:math'; | 41 import 'dart:math'; |
39 '''); | 42 '''); |
| 43 List<Directive> oldDirectives = oldUnit.directives.toList(); |
40 _buildNewUnit(r''' | 44 _buildNewUnit(r''' |
41 library test; | 45 library test; |
42 import 'dart:async'; | 46 import 'dart:async'; |
43 import 'dart:math'; | 47 import 'dart:math'; |
44 '''); | 48 '''); |
45 var oldDirectives = oldUnit.directives; | 49 List<Directive> newDirectives = newUnit.directives; |
46 var newDirectives = newUnit.directives; | |
47 { | 50 { |
48 Directive newNode = newDirectives[0]; | 51 Directive newNode = newDirectives[0]; |
49 expect(newNode, same(oldDirectives[0])); | 52 expect(newNode, same(oldDirectives[0])); |
50 expect(getNodeText(newNode), "library test;"); | 53 expect(getNodeText(newNode), "library test;"); |
51 LibraryElement element = newNode.element; | 54 LibraryElement element = newNode.element; |
52 expect(element, isNotNull); | 55 expect(element, isNotNull); |
53 expect(element.nameOffset, newCode.indexOf('test;')); | 56 expect(element.nameOffset, newCode.indexOf('test;')); |
54 } | 57 } |
55 { | 58 { |
56 Directive newNode = newDirectives[1]; | 59 Directive newNode = newDirectives[1]; |
57 expect(getNodeText(newNode), "import 'dart:async';"); | 60 expect(getNodeText(newNode), "import 'dart:async';"); |
58 ImportElement element = newNode.element; | 61 ImportElement element = newNode.element; |
59 expect(element, isNull); | 62 expect(element, isNull); |
60 } | 63 } |
61 { | 64 { |
62 Directive newNode = newDirectives[2]; | 65 Directive newNode = newDirectives[2]; |
63 expect(newNode, same(oldDirectives[1])); | 66 expect(newNode, same(oldDirectives[1])); |
64 expect(getNodeText(newNode), "import 'dart:math';"); | 67 expect(getNodeText(newNode), "import 'dart:math';"); |
65 ImportElement element = newNode.element; | 68 ImportElement element = newNode.element; |
66 expect(element, isNotNull); | 69 expect(element, isNotNull); |
67 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); | 70 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); |
68 } | 71 } |
| 72 expect(unitDelta.hasDirectiveChange, isTrue); |
69 } | 73 } |
70 | 74 |
71 test_directives_keepOffset_partOf() { | 75 test_directives_keepOffset_partOf() { |
72 String libCode = ''' | 76 String libCode = ''' |
73 // comment to shift tokens | 77 // comment to shift tokens |
74 library my_lib; | 78 library my_lib; |
75 part 'test.dart'; | 79 part 'test.dart'; |
76 '''; | 80 '''; |
77 Source libSource = newSource('/lib.dart', libCode); | 81 Source libSource = newSource('/lib.dart', libCode); |
78 _buildOldUnit(r''' | 82 _buildOldUnit(r''' |
79 part of my_lib; | 83 part of my_lib; |
80 class A {} | 84 class A {} |
81 ''', libSource); | 85 ''', libSource); |
| 86 List<Directive> oldDirectives = oldUnit.directives.toList(); |
82 _buildNewUnit(r''' | 87 _buildNewUnit(r''' |
83 part of my_lib; | 88 part of my_lib; |
84 class A {} | 89 class A {} |
85 '''); | 90 '''); |
86 var oldDirectives = oldUnit.directives; | 91 List<Directive> newDirectives = newUnit.directives; |
87 var newDirectives = newUnit.directives; | |
88 { | 92 { |
89 Directive newNode = newDirectives[0]; | 93 Directive newNode = newDirectives[0]; |
90 expect(newNode, same(oldDirectives[0])); | 94 expect(newNode, same(oldDirectives[0])); |
91 expect(getNodeText(newNode), 'part of my_lib;'); | 95 expect(getNodeText(newNode), 'part of my_lib;'); |
92 LibraryElement element = newNode.element; | 96 LibraryElement element = newNode.element; |
93 expect(element, isNotNull); | 97 expect(element, isNotNull); |
94 expect(element.nameOffset, libCode.indexOf('my_lib;')); | 98 expect(element.nameOffset, libCode.indexOf('my_lib;')); |
95 } | 99 } |
96 } | 100 } |
97 | 101 |
98 test_directives_remove() { | 102 test_directives_remove() { |
99 _buildOldUnit(r''' | 103 _buildOldUnit(r''' |
100 library test; | 104 library test; |
101 import 'dart:async'; | 105 import 'dart:async'; |
102 import 'dart:math'; | 106 import 'dart:math'; |
103 '''); | 107 '''); |
| 108 List<Directive> oldDirectives = oldUnit.directives.toList(); |
104 _buildNewUnit(r''' | 109 _buildNewUnit(r''' |
105 library test; | 110 library test; |
106 import 'dart:math'; | 111 import 'dart:math'; |
107 '''); | 112 '''); |
108 var oldDirectives = oldUnit.directives; | 113 List<Directive> newDirectives = newUnit.directives; |
109 var newDirectives = newUnit.directives; | |
110 { | 114 { |
111 Directive newNode = newDirectives[0]; | 115 Directive newNode = newDirectives[0]; |
112 expect(newNode, same(oldDirectives[0])); | 116 expect(newNode, same(oldDirectives[0])); |
113 expect(getNodeText(newNode), "library test;"); | 117 expect(getNodeText(newNode), "library test;"); |
114 LibraryElement element = newNode.element; | 118 LibraryElement element = newNode.element; |
115 expect(element, isNotNull); | 119 expect(element, isNotNull); |
116 expect(element.nameOffset, newCode.indexOf('test;')); | 120 expect(element.nameOffset, newCode.indexOf('test;')); |
117 } | 121 } |
118 { | 122 { |
119 Directive newNode = newDirectives[1]; | 123 Directive newNode = newDirectives[1]; |
120 expect(newNode, same(oldDirectives[2])); | 124 expect(newNode, same(oldDirectives[2])); |
121 expect(getNodeText(newNode), "import 'dart:math';"); | 125 expect(getNodeText(newNode), "import 'dart:math';"); |
122 ImportElement element = newNode.element; | 126 ImportElement element = newNode.element; |
123 expect(element, isNotNull); | 127 expect(element, isNotNull); |
124 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); | 128 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); |
125 } | 129 } |
| 130 expect(unitDelta.hasDirectiveChange, isTrue); |
126 } | 131 } |
127 | 132 |
128 test_directives_reorder() { | 133 test_directives_reorder() { |
129 _buildOldUnit(r''' | 134 _buildOldUnit(r''' |
130 library test; | 135 library test; |
131 import 'dart:math' as m; | 136 import 'dart:math' as m; |
132 import 'dart:async'; | 137 import 'dart:async'; |
133 '''); | 138 '''); |
| 139 List<Directive> oldDirectives = oldUnit.directives.toList(); |
134 _buildNewUnit(r''' | 140 _buildNewUnit(r''' |
135 library test; | 141 library test; |
136 import 'dart:async'; | 142 import 'dart:async'; |
137 import 'dart:math' as m; | 143 import 'dart:math' as m; |
138 '''); | 144 '''); |
139 var oldDirectives = oldUnit.directives; | 145 List<Directive> newDirectives = newUnit.directives; |
140 var newDirectives = newUnit.directives; | |
141 { | 146 { |
142 Directive newNode = newDirectives[0]; | 147 Directive newNode = newDirectives[0]; |
143 expect(newNode, same(oldDirectives[0])); | 148 expect(newNode, same(oldDirectives[0])); |
144 expect(getNodeText(newNode), "library test;"); | 149 expect(getNodeText(newNode), "library test;"); |
145 LibraryElement element = newNode.element; | 150 LibraryElement element = newNode.element; |
146 expect(element, isNotNull); | 151 expect(element, isNotNull); |
147 expect(element.nameOffset, newCode.indexOf('test;')); | 152 expect(element.nameOffset, newCode.indexOf('test;')); |
148 } | 153 } |
149 { | 154 { |
150 Directive newNode = newDirectives[1]; | 155 Directive newNode = newDirectives[1]; |
151 expect(newNode, same(oldDirectives[2])); | 156 expect(newNode, same(oldDirectives[2])); |
152 expect(getNodeText(newNode), "import 'dart:async';"); | 157 expect(getNodeText(newNode), "import 'dart:async';"); |
153 ImportElement element = newNode.element; | 158 ImportElement element = newNode.element; |
154 expect(element, isNotNull); | 159 expect(element, isNotNull); |
155 expect(element.nameOffset, newCode.indexOf("import 'dart:async';")); | 160 expect(element.nameOffset, newCode.indexOf("import 'dart:async';")); |
156 } | 161 } |
157 { | 162 { |
158 Directive newNode = newDirectives[2]; | 163 Directive newNode = newDirectives[2]; |
159 expect(newNode, same(oldDirectives[1])); | 164 expect(newNode, same(oldDirectives[1])); |
160 expect(getNodeText(newNode), "import 'dart:math' as m;"); | 165 expect(getNodeText(newNode), "import 'dart:math' as m;"); |
161 ImportElement element = newNode.element; | 166 ImportElement element = newNode.element; |
162 expect(element, isNotNull); | 167 expect(element, isNotNull); |
163 expect(element.nameOffset, newCode.indexOf("import 'dart:math' as m;")); | 168 expect(element.nameOffset, newCode.indexOf("import 'dart:math' as m;")); |
164 expect(element.prefix.nameOffset, newCode.indexOf("m;")); | 169 expect(element.prefix.nameOffset, newCode.indexOf("m;")); |
165 } | 170 } |
| 171 expect(unitDelta.hasDirectiveChange, isFalse); |
166 } | 172 } |
167 | 173 |
168 test_directives_sameOrder_insertSpaces() { | 174 test_directives_sameOrder_insertSpaces() { |
169 _buildOldUnit(r''' | 175 _buildOldUnit(r''' |
170 library test; | 176 library test; |
171 import 'dart:async'; | 177 import 'dart:async'; |
172 import 'dart:math'; | 178 import 'dart:math'; |
173 '''); | 179 '''); |
| 180 List<Directive> oldDirectives = oldUnit.directives.toList(); |
174 _buildNewUnit(r''' | 181 _buildNewUnit(r''' |
175 library test; | 182 library test; |
176 | 183 |
177 import 'dart:async' ; | 184 import 'dart:async' ; |
178 import 'dart:math'; | 185 import 'dart:math'; |
179 '''); | 186 '''); |
180 var oldDirectives = oldUnit.directives; | 187 List<Directive> newDirectives = newUnit.directives; |
181 var newDirectives = newUnit.directives; | |
182 { | 188 { |
183 Directive newNode = newDirectives[0]; | 189 Directive newNode = newDirectives[0]; |
184 expect(newNode, same(oldDirectives[0])); | 190 expect(newNode, same(oldDirectives[0])); |
185 expect(getNodeText(newNode), "library test;"); | 191 expect(getNodeText(newNode), "library test;"); |
186 LibraryElement element = newNode.element; | 192 LibraryElement element = newNode.element; |
187 expect(element, isNotNull); | 193 expect(element, isNotNull); |
188 expect(element.nameOffset, newCode.indexOf('test;')); | 194 expect(element.nameOffset, newCode.indexOf('test;')); |
189 } | 195 } |
190 { | 196 { |
191 Directive newNode = newDirectives[1]; | 197 Directive newNode = newDirectives[1]; |
192 expect(newNode, same(oldDirectives[1])); | 198 expect(newNode, same(oldDirectives[1])); |
193 expect(getNodeText(newNode), "import 'dart:async' ;"); | 199 expect(getNodeText(newNode), "import 'dart:async' ;"); |
194 ImportElement element = newNode.element; | 200 ImportElement element = newNode.element; |
195 expect(element, isNotNull); | 201 expect(element, isNotNull); |
196 expect(element.nameOffset, newCode.indexOf("import 'dart:async' ;")); | 202 expect(element.nameOffset, newCode.indexOf("import 'dart:async' ;")); |
197 } | 203 } |
198 { | 204 { |
199 Directive newNode = newDirectives[2]; | 205 Directive newNode = newDirectives[2]; |
200 expect(newNode, same(oldDirectives[2])); | 206 expect(newNode, same(oldDirectives[2])); |
201 expect(getNodeText(newNode), "import 'dart:math';"); | 207 expect(getNodeText(newNode), "import 'dart:math';"); |
202 ImportElement element = newNode.element; | 208 ImportElement element = newNode.element; |
203 expect(element, isNotNull); | 209 expect(element, isNotNull); |
204 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); | 210 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); |
205 } | 211 } |
| 212 expect(unitDelta.hasDirectiveChange, isFalse); |
206 } | 213 } |
207 | 214 |
208 test_directives_sameOrder_removeSpaces() { | 215 test_directives_sameOrder_removeSpaces() { |
209 _buildOldUnit(r''' | 216 _buildOldUnit(r''' |
210 library test; | 217 library test; |
211 | 218 |
212 import 'dart:async' ; | 219 import 'dart:async' ; |
213 import 'dart:math'; | 220 import 'dart:math'; |
214 '''); | 221 '''); |
| 222 List<Directive> oldDirectives = oldUnit.directives.toList(); |
215 _buildNewUnit(r''' | 223 _buildNewUnit(r''' |
216 library test; | 224 library test; |
217 import 'dart:async'; | 225 import 'dart:async'; |
218 import 'dart:math'; | 226 import 'dart:math'; |
219 '''); | 227 '''); |
220 var oldDirectives = oldUnit.directives; | 228 List<Directive> newDirectives = newUnit.directives; |
221 var newDirectives = newUnit.directives; | |
222 { | 229 { |
223 Directive newNode = newDirectives[0]; | 230 Directive newNode = newDirectives[0]; |
224 expect(newNode, same(oldDirectives[0])); | 231 expect(newNode, same(oldDirectives[0])); |
225 expect(getNodeText(newNode), "library test;"); | 232 expect(getNodeText(newNode), "library test;"); |
226 LibraryElement element = newNode.element; | 233 LibraryElement element = newNode.element; |
227 expect(element, isNotNull); | 234 expect(element, isNotNull); |
228 expect(element.nameOffset, newCode.indexOf('test;')); | 235 expect(element.nameOffset, newCode.indexOf('test;')); |
229 } | 236 } |
230 { | 237 { |
231 Directive newNode = newDirectives[1]; | 238 Directive newNode = newDirectives[1]; |
232 expect(newNode, same(oldDirectives[1])); | 239 expect(newNode, same(oldDirectives[1])); |
233 expect(getNodeText(newNode), "import 'dart:async';"); | 240 expect(getNodeText(newNode), "import 'dart:async';"); |
234 ImportElement element = newNode.element; | 241 ImportElement element = newNode.element; |
235 expect(element, isNotNull); | 242 expect(element, isNotNull); |
236 expect(element.nameOffset, newCode.indexOf("import 'dart:async';")); | 243 expect(element.nameOffset, newCode.indexOf("import 'dart:async';")); |
237 } | 244 } |
238 { | 245 { |
239 Directive newNode = newDirectives[2]; | 246 Directive newNode = newDirectives[2]; |
240 expect(newNode, same(oldDirectives[2])); | 247 expect(newNode, same(oldDirectives[2])); |
241 expect(getNodeText(newNode), "import 'dart:math';"); | 248 expect(getNodeText(newNode), "import 'dart:math';"); |
242 ImportElement element = newNode.element; | 249 ImportElement element = newNode.element; |
243 expect(element, isNotNull); | 250 expect(element, isNotNull); |
244 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); | 251 expect(element.nameOffset, newCode.indexOf("import 'dart:math';")); |
245 } | 252 } |
| 253 expect(unitDelta.hasDirectiveChange, isFalse); |
246 } | 254 } |
247 | 255 |
248 test_unitMembers_accessor_add() { | 256 test_unitMembers_accessor_add() { |
249 _buildOldUnit(r''' | 257 _buildOldUnit(r''' |
250 get a => 1; | 258 get a => 1; |
251 '''); | 259 '''); |
| 260 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
252 _buildNewUnit(r''' | 261 _buildNewUnit(r''' |
253 get a => 1; | 262 get a => 1; |
254 get b => 2; | 263 get b => 2; |
255 '''); | 264 '''); |
256 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
257 List<CompilationUnitMember> newNodes = newUnit.declarations; | 265 List<CompilationUnitMember> newNodes = newUnit.declarations; |
258 // nodes | 266 // nodes |
259 FunctionDeclaration node1 = newNodes[0]; | 267 FunctionDeclaration node1 = newNodes[0]; |
260 FunctionDeclaration node2 = newNodes[1]; | 268 FunctionDeclaration node2 = newNodes[1]; |
261 expect(node1, same(oldNodes[0])); | 269 expect(node1, same(oldNodes[0])); |
262 // elements | 270 // elements |
263 PropertyAccessorElement elementA = node1.element; | 271 PropertyAccessorElement elementA = node1.element; |
264 PropertyAccessorElement elementB = node2.element; | 272 PropertyAccessorElement elementB = node2.element; |
265 expect(elementA, isNotNull); | 273 expect(elementA, isNotNull); |
266 expect(elementB, isNotNull); | 274 expect(elementB, isNotNull); |
267 expect(elementA.name, 'a'); | 275 expect(elementA.name, 'a'); |
268 expect(elementB.name, 'b'); | 276 expect(elementB.name, 'b'); |
269 // unit.types | 277 // unit.types |
270 expect(unitElement.topLevelVariables, | 278 expect(unitElement.topLevelVariables, |
271 unorderedEquals([elementA.variable, elementB.variable])); | 279 unorderedEquals([elementA.variable, elementB.variable])); |
272 expect(unitElement.accessors, unorderedEquals([elementA, elementB])); | 280 expect(unitElement.accessors, unorderedEquals([elementA, elementB])); |
273 } | 281 } |
274 | 282 |
275 test_unitMembers_class_add() { | 283 test_unitMembers_class_add() { |
276 _buildOldUnit(r''' | 284 _buildOldUnit(r''' |
277 class A {} | 285 class A {} |
278 '''); | 286 '''); |
| 287 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
279 _buildNewUnit(r''' | 288 _buildNewUnit(r''' |
280 class A {} | 289 class A {} |
281 class B {} | 290 class B {} |
282 '''); | 291 '''); |
283 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
284 List<CompilationUnitMember> newNodes = newUnit.declarations; | 292 List<CompilationUnitMember> newNodes = newUnit.declarations; |
285 // nodes | 293 // nodes |
286 CompilationUnitMember nodeA = newNodes[0]; | 294 CompilationUnitMember nodeA = newNodes[0]; |
287 CompilationUnitMember nodeB = newNodes[1]; | 295 CompilationUnitMember nodeB = newNodes[1]; |
288 expect(nodeA, same(oldNodes[0])); | 296 expect(nodeA, same(oldNodes[0])); |
289 // elements | 297 // elements |
290 ClassElement elementA = nodeA.element; | 298 ClassElement elementA = nodeA.element; |
291 ClassElement elementB = nodeB.element; | 299 ClassElement elementB = nodeB.element; |
292 expect(elementA, isNotNull); | 300 expect(elementA, isNotNull); |
293 expect(elementB, isNotNull); | 301 expect(elementB, isNotNull); |
294 expect(elementA.name, 'A'); | 302 expect(elementA.name, 'A'); |
295 expect(elementB.name, 'B'); | 303 expect(elementB.name, 'B'); |
296 // unit.types | 304 // unit.types |
297 expect(unitElement.types, unorderedEquals([elementA, elementB])); | 305 expect(unitElement.types, unorderedEquals([elementA, elementB])); |
| 306 // verify delta |
| 307 expect(unitDelta.addedDeclarations, unorderedEquals([elementB])); |
| 308 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
298 } | 309 } |
299 | 310 |
300 test_unitMembers_class_comments() { | 311 test_unitMembers_class_comments() { |
301 _buildOldUnit(r''' | 312 _buildOldUnit(r''' |
302 /// reference [bool] type. | 313 /// reference [bool] type. |
303 class A {} | 314 class A {} |
304 /// reference [int] type. | 315 /// reference [int] type. |
305 class B {} | 316 class B {} |
306 /// reference [double] and [B] types. | 317 /// reference [double] and [B] types. |
307 class C {} | 318 class C {} |
308 '''); | 319 '''); |
| 320 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
309 _buildNewUnit(r''' | 321 _buildNewUnit(r''' |
310 /// reference [double] and [B] types. | 322 /// reference [double] and [B] types. |
311 class C {} | 323 class C {} |
312 /// reference [bool] type. | 324 /// reference [bool] type. |
313 class A {} | 325 class A {} |
314 /// reference [int] type. | 326 /// reference [int] type. |
315 class B {} | 327 class B {} |
316 '''); | 328 '''); |
317 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
318 List<CompilationUnitMember> newNodes = newUnit.declarations; | 329 List<CompilationUnitMember> newNodes = newUnit.declarations; |
319 { | 330 { |
320 CompilationUnitMember newNode = newNodes[0]; | 331 CompilationUnitMember newNode = newNodes[0]; |
321 expect(newNode, same(oldNodes[2])); | 332 expect(newNode, same(oldNodes[2])); |
322 expect(getNodeText(newNode), r''' | 333 expect(getNodeText(newNode), r''' |
323 /// reference [double] and [B] types. | 334 /// reference [double] and [B] types. |
324 class C {}'''); | 335 class C {}'''); |
325 ClassElement element = newNode.element; | 336 ClassElement element = newNode.element; |
326 expect(element, isNotNull); | 337 expect(element, isNotNull); |
327 expect(element.name, 'C'); | 338 expect(element.name, 'C'); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 expect(element, isNotNull); | 373 expect(element, isNotNull); |
363 expect(element.name, 'B'); | 374 expect(element.name, 'B'); |
364 expect(element.nameOffset, newCode.indexOf('B {}')); | 375 expect(element.nameOffset, newCode.indexOf('B {}')); |
365 // [int] is still resolved | 376 // [int] is still resolved |
366 { | 377 { |
367 var docReferences = newNode.documentationComment.references; | 378 var docReferences = newNode.documentationComment.references; |
368 expect(docReferences, hasLength(1)); | 379 expect(docReferences, hasLength(1)); |
369 expect(docReferences[0].identifier.staticElement.name, 'int'); | 380 expect(docReferences[0].identifier.staticElement.name, 'int'); |
370 } | 381 } |
371 } | 382 } |
| 383 // verify delta |
| 384 expect(unitDelta.addedDeclarations, unorderedEquals([])); |
| 385 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
| 386 } |
| 387 |
| 388 test_unitMembers_class_remove() { |
| 389 _buildOldUnit(r''' |
| 390 class A {} |
| 391 class B {} |
| 392 '''); |
| 393 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
| 394 _buildNewUnit(r''' |
| 395 class A {} |
| 396 '''); |
| 397 List<CompilationUnitMember> newNodes = newUnit.declarations; |
| 398 // nodes |
| 399 CompilationUnitMember nodeA = newNodes[0]; |
| 400 CompilationUnitMember nodeB = oldNodes[1]; |
| 401 expect(nodeA, same(oldNodes[0])); |
| 402 // elements |
| 403 ClassElement elementA = nodeA.element; |
| 404 ClassElement elementB = nodeB.element; |
| 405 expect(elementA, isNotNull); |
| 406 expect(elementB, isNotNull); |
| 407 expect(elementA.name, 'A'); |
| 408 expect(elementB.name, 'B'); |
| 409 // unit.types |
| 410 expect(unitElement.types, unorderedEquals([elementA])); |
| 411 // verify delta |
| 412 expect(unitDelta.addedDeclarations, unorderedEquals([])); |
| 413 expect(unitDelta.removedDeclarations, unorderedEquals([elementB])); |
372 } | 414 } |
373 | 415 |
374 test_unitMembers_class_reorder() { | 416 test_unitMembers_class_reorder() { |
375 _buildOldUnit(r''' | 417 _buildOldUnit(r''' |
376 class A {} | 418 class A {} |
377 class B {} | 419 class B {} |
378 class C {} | 420 class C {} |
379 '''); | 421 '''); |
| 422 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
380 _buildNewUnit(r''' | 423 _buildNewUnit(r''' |
381 class C {} | 424 class C {} |
382 class A {} | 425 class A {} |
383 class B {} | 426 class B {} |
384 '''); | 427 '''); |
385 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
386 List<CompilationUnitMember> newNodes = newUnit.declarations; | 428 List<CompilationUnitMember> newNodes = newUnit.declarations; |
387 { | 429 { |
388 CompilationUnitMember newNode = newNodes[0]; | 430 CompilationUnitMember newNode = newNodes[0]; |
389 expect(newNode, same(oldNodes[2])); | 431 expect(newNode, same(oldNodes[2])); |
390 expect(getNodeText(newNode), 'class C {}'); | 432 expect(getNodeText(newNode), 'class C {}'); |
391 ClassElement element = newNode.element; | 433 ClassElement element = newNode.element; |
392 expect(element, isNotNull); | 434 expect(element, isNotNull); |
393 expect(element.name, 'C'); | 435 expect(element.name, 'C'); |
394 expect(element.nameOffset, newCode.indexOf('C {}')); | 436 expect(element.nameOffset, newCode.indexOf('C {}')); |
395 } | 437 } |
396 { | 438 { |
397 CompilationUnitMember newNode = newNodes[1]; | 439 CompilationUnitMember newNode = newNodes[1]; |
398 expect(newNode, same(oldNodes[0])); | 440 expect(newNode, same(oldNodes[0])); |
399 expect(getNodeText(newNode), 'class A {}'); | 441 expect(getNodeText(newNode), 'class A {}'); |
400 ClassElement element = newNode.element; | 442 ClassElement element = newNode.element; |
401 expect(element, isNotNull); | 443 expect(element, isNotNull); |
402 expect(element.name, 'A'); | 444 expect(element.name, 'A'); |
403 expect(element.nameOffset, newCode.indexOf('A {}')); | 445 expect(element.nameOffset, newCode.indexOf('A {}')); |
404 } | 446 } |
405 { | 447 { |
406 CompilationUnitMember newNode = newNodes[2]; | 448 CompilationUnitMember newNode = newNodes[2]; |
407 expect(newNode, same(oldNodes[1])); | 449 expect(newNode, same(oldNodes[1])); |
408 expect(getNodeText(newNode), 'class B {}'); | 450 expect(getNodeText(newNode), 'class B {}'); |
409 ClassElement element = newNode.element; | 451 ClassElement element = newNode.element; |
410 expect(element, isNotNull); | 452 expect(element, isNotNull); |
411 expect(element.name, 'B'); | 453 expect(element.name, 'B'); |
412 expect(element.nameOffset, newCode.indexOf('B {}')); | 454 expect(element.nameOffset, newCode.indexOf('B {}')); |
413 } | 455 } |
| 456 // verify delta |
| 457 expect(unitDelta.addedDeclarations, unorderedEquals([])); |
| 458 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
414 } | 459 } |
415 | 460 |
416 test_unitMembers_enum_add() { | 461 test_unitMembers_enum_add() { |
417 _buildOldUnit(r''' | 462 _buildOldUnit(r''' |
418 enum A {A1, A2} | 463 enum A {A1, A2} |
419 '''); | 464 '''); |
| 465 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
420 _buildNewUnit(r''' | 466 _buildNewUnit(r''' |
421 enum A {A1, A2} | 467 enum A {A1, A2} |
422 enum B {B1, B2} | 468 enum B {B1, B2} |
423 '''); | 469 '''); |
424 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
425 List<CompilationUnitMember> newNodes = newUnit.declarations; | 470 List<CompilationUnitMember> newNodes = newUnit.declarations; |
426 // nodes | 471 // nodes |
427 CompilationUnitMember nodeA = newNodes[0]; | 472 CompilationUnitMember nodeA = newNodes[0]; |
428 CompilationUnitMember nodeB = newNodes[1]; | 473 CompilationUnitMember nodeB = newNodes[1]; |
429 expect(nodeA, same(oldNodes[0])); | 474 expect(nodeA, same(oldNodes[0])); |
430 // elements | 475 // elements |
431 ClassElement elementA = nodeA.element; | 476 ClassElement elementA = nodeA.element; |
432 ClassElement elementB = nodeB.element; | 477 ClassElement elementB = nodeB.element; |
433 expect(elementA, isNotNull); | 478 expect(elementA, isNotNull); |
434 expect(elementB, isNotNull); | 479 expect(elementB, isNotNull); |
435 expect(elementA.name, 'A'); | 480 expect(elementA.name, 'A'); |
436 expect(elementB.name, 'B'); | 481 expect(elementB.name, 'B'); |
437 // unit.types | 482 // unit.types |
438 expect(unitElement.enums, unorderedEquals([elementA, elementB])); | 483 expect(unitElement.enums, unorderedEquals([elementA, elementB])); |
| 484 // verify delta |
| 485 expect(unitDelta.addedDeclarations, unorderedEquals([elementB])); |
| 486 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
439 } | 487 } |
440 | 488 |
441 test_unitMembers_function_add() { | 489 test_unitMembers_function_add() { |
442 _buildOldUnit(r''' | 490 _buildOldUnit(r''' |
443 a() {} | 491 a() {} |
444 '''); | 492 '''); |
| 493 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
445 _buildNewUnit(r''' | 494 _buildNewUnit(r''' |
446 a() {} | 495 a() {} |
447 b() {} | 496 b() {} |
448 '''); | 497 '''); |
449 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
450 List<CompilationUnitMember> newNodes = newUnit.declarations; | 498 List<CompilationUnitMember> newNodes = newUnit.declarations; |
451 // nodes | 499 // nodes |
452 CompilationUnitMember nodeA = newNodes[0]; | 500 CompilationUnitMember nodeA = newNodes[0]; |
453 CompilationUnitMember nodeB = newNodes[1]; | 501 CompilationUnitMember nodeB = newNodes[1]; |
454 expect(nodeA, same(oldNodes[0])); | 502 expect(nodeA, same(oldNodes[0])); |
455 // elements | 503 // elements |
456 FunctionElement elementA = nodeA.element; | 504 FunctionElement elementA = nodeA.element; |
457 FunctionElement elementB = nodeB.element; | 505 FunctionElement elementB = nodeB.element; |
458 expect(elementA, isNotNull); | 506 expect(elementA, isNotNull); |
459 expect(elementB, isNotNull); | 507 expect(elementB, isNotNull); |
460 expect(elementA.name, 'a'); | 508 expect(elementA.name, 'a'); |
461 expect(elementB.name, 'b'); | 509 expect(elementB.name, 'b'); |
462 // unit.types | 510 // unit.types |
463 expect(unitElement.functions, unorderedEquals([elementA, elementB])); | 511 expect(unitElement.functions, unorderedEquals([elementA, elementB])); |
| 512 // verify delta |
| 513 expect(unitDelta.addedDeclarations, unorderedEquals([elementB])); |
| 514 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
464 } | 515 } |
465 | 516 |
466 test_unitMembers_functionTypeAlias_add() { | 517 test_unitMembers_functionTypeAlias_add() { |
467 _buildOldUnit(r''' | 518 _buildOldUnit(r''' |
468 typedef A(); | 519 typedef A(); |
469 '''); | 520 '''); |
| 521 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
470 _buildNewUnit(r''' | 522 _buildNewUnit(r''' |
471 typedef A(); | 523 typedef A(); |
472 typedef B(); | 524 typedef B(); |
473 '''); | 525 '''); |
474 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
475 List<CompilationUnitMember> newNodes = newUnit.declarations; | 526 List<CompilationUnitMember> newNodes = newUnit.declarations; |
476 // nodes | 527 // nodes |
477 CompilationUnitMember nodeA = newNodes[0]; | 528 CompilationUnitMember nodeA = newNodes[0]; |
478 CompilationUnitMember nodeB = newNodes[1]; | 529 CompilationUnitMember nodeB = newNodes[1]; |
479 expect(nodeA, same(oldNodes[0])); | 530 expect(nodeA, same(oldNodes[0])); |
480 // elements | 531 // elements |
481 FunctionTypeAliasElement elementA = nodeA.element; | 532 FunctionTypeAliasElement elementA = nodeA.element; |
482 FunctionTypeAliasElement elementB = nodeB.element; | 533 FunctionTypeAliasElement elementB = nodeB.element; |
483 expect(elementA, isNotNull); | 534 expect(elementA, isNotNull); |
484 expect(elementB, isNotNull); | 535 expect(elementB, isNotNull); |
485 expect(elementA.name, 'A'); | 536 expect(elementA.name, 'A'); |
486 expect(elementB.name, 'B'); | 537 expect(elementB.name, 'B'); |
487 // unit.types | 538 // unit.types |
488 expect( | 539 expect( |
489 unitElement.functionTypeAliases, unorderedEquals([elementA, elementB])); | 540 unitElement.functionTypeAliases, unorderedEquals([elementA, elementB])); |
| 541 // verify delta |
| 542 expect(unitDelta.addedDeclarations, unorderedEquals([elementB])); |
| 543 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
490 } | 544 } |
491 | 545 |
492 test_unitMembers_topLevelVariable_add() { | 546 test_unitMembers_topLevelVariable_add() { |
493 _buildOldUnit(r''' | 547 _buildOldUnit(r''' |
494 int a, b; | 548 int a, b; |
495 '''); | 549 '''); |
| 550 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
496 _buildNewUnit(r''' | 551 _buildNewUnit(r''' |
497 int a, b; | 552 int a, b; |
498 int c, d; | 553 int c, d; |
499 '''); | 554 '''); |
500 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
501 List<CompilationUnitMember> newNodes = newUnit.declarations; | 555 List<CompilationUnitMember> newNodes = newUnit.declarations; |
502 // nodes | 556 // nodes |
503 TopLevelVariableDeclaration node1 = newNodes[0]; | 557 TopLevelVariableDeclaration node1 = newNodes[0]; |
504 TopLevelVariableDeclaration node2 = newNodes[1]; | 558 TopLevelVariableDeclaration node2 = newNodes[1]; |
505 expect(node1, same(oldNodes[0])); | 559 expect(node1, same(oldNodes[0])); |
506 // elements | 560 // elements |
507 TopLevelVariableElement elementA = node1.variables.variables[0].element; | 561 TopLevelVariableElement elementA = node1.variables.variables[0].element; |
508 TopLevelVariableElement elementB = node1.variables.variables[1].element; | 562 TopLevelVariableElement elementB = node1.variables.variables[1].element; |
509 TopLevelVariableElement elementC = node2.variables.variables[0].element; | 563 TopLevelVariableElement elementC = node2.variables.variables[0].element; |
510 TopLevelVariableElement elementD = node2.variables.variables[1].element; | 564 TopLevelVariableElement elementD = node2.variables.variables[1].element; |
(...skipping 18 matching lines...) Expand all Loading... |
529 elementD.getter, | 583 elementD.getter, |
530 elementD.setter | 584 elementD.setter |
531 ])); | 585 ])); |
532 } | 586 } |
533 | 587 |
534 test_unitMembers_topLevelVariableDeclaration() { | 588 test_unitMembers_topLevelVariableDeclaration() { |
535 _buildOldUnit(r''' | 589 _buildOldUnit(r''' |
536 bool a = 1, b = 2; | 590 bool a = 1, b = 2; |
537 int c = 3; | 591 int c = 3; |
538 '''); | 592 '''); |
| 593 List<CompilationUnitMember> oldNodes = oldUnit.declarations.toList(); |
539 _buildNewUnit(r''' | 594 _buildNewUnit(r''' |
540 int c = 3; | 595 int c = 3; |
541 | 596 |
542 bool a =1, b = 2; | 597 bool a =1, b = 2; |
543 '''); | 598 '''); |
544 List<CompilationUnitMember> oldNodes = oldUnit.declarations; | |
545 List<CompilationUnitMember> newNodes = newUnit.declarations; | 599 List<CompilationUnitMember> newNodes = newUnit.declarations; |
546 { | 600 { |
547 TopLevelVariableDeclaration newNode = newNodes[0]; | 601 TopLevelVariableDeclaration newNode = newNodes[0]; |
548 expect(newNode, same(oldNodes[1])); | 602 expect(newNode, same(oldNodes[1])); |
549 expect(getNodeText(newNode), 'int c = 3;'); | 603 expect(getNodeText(newNode), 'int c = 3;'); |
550 { | 604 { |
551 TopLevelVariableElement element = | 605 TopLevelVariableElement element = |
552 newNode.variables.variables[0].element; | 606 newNode.variables.variables[0].element; |
553 expect(element, isNotNull); | 607 expect(element, isNotNull); |
554 expect(element.name, 'c'); | 608 expect(element.name, 'c'); |
(...skipping 12 matching lines...) Expand all Loading... |
567 expect(element.nameOffset, newCode.indexOf('a =1')); | 621 expect(element.nameOffset, newCode.indexOf('a =1')); |
568 } | 622 } |
569 { | 623 { |
570 TopLevelVariableElement element = | 624 TopLevelVariableElement element = |
571 newNode.variables.variables[1].element; | 625 newNode.variables.variables[1].element; |
572 expect(element, isNotNull); | 626 expect(element, isNotNull); |
573 expect(element.name, 'b'); | 627 expect(element.name, 'b'); |
574 expect(element.nameOffset, newCode.indexOf('b = 2')); | 628 expect(element.nameOffset, newCode.indexOf('b = 2')); |
575 } | 629 } |
576 } | 630 } |
| 631 // verify delta |
| 632 expect(unitDelta.addedDeclarations, unorderedEquals([])); |
| 633 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
577 } | 634 } |
578 | 635 |
579 void _buildNewUnit(String newCode) { | 636 void _buildNewUnit(String newCode) { |
580 this.newCode = newCode; | 637 this.newCode = newCode; |
581 context.setContents(source, newCode); | 638 context.setContents(source, newCode); |
582 newUnit = context.parseCompilationUnit(source); | 639 newUnit = context.parseCompilationUnit(source); |
583 new IncrementalCompilationUnitElementBuilder(oldUnit, newUnit).build(); | 640 IncrementalCompilationUnitElementBuilder builder = |
| 641 new IncrementalCompilationUnitElementBuilder(oldUnit, newUnit); |
| 642 builder.build(); |
| 643 unitDelta = builder.unitDelta; |
584 expect(newUnit.element, unitElement); | 644 expect(newUnit.element, unitElement); |
585 } | 645 } |
586 | 646 |
587 void _buildOldUnit(String oldCode, [Source libSource]) { | 647 void _buildOldUnit(String oldCode, [Source libSource]) { |
588 this.oldCode = oldCode; | 648 this.oldCode = oldCode; |
589 source = newSource('/test.dart', oldCode); | 649 source = newSource('/test.dart', oldCode); |
590 if (libSource == null) { | 650 if (libSource == null) { |
591 libSource = source; | 651 libSource = source; |
592 } | 652 } |
593 oldUnit = context.resolveCompilationUnit2(source, libSource); | 653 oldUnit = context.resolveCompilationUnit2(source, libSource); |
594 unitElement = oldUnit.element; | 654 unitElement = oldUnit.element; |
595 expect(unitElement, isNotNull); | 655 expect(unitElement, isNotNull); |
596 } | 656 } |
597 } | 657 } |
OLD | NEW |