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

Side by Side Diff: pkg/analyzer2dart/test/tree_shaker_test.dart

Issue 609783002: Support static field access in analyzer2dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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 'mock_sdk.dart'; 5 import 'mock_sdk.dart';
6 import 'package:analyzer/file_system/memory_file_system.dart'; 6 import 'package:analyzer/file_system/memory_file_system.dart';
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/sdk.dart'; 9 import 'package:analyzer/src/generated/sdk.dart';
10 import 'package:analyzer/src/generated/source.dart'; 10 import 'package:analyzer/src/generated/source.dart';
11 import 'package:compiler/implementation/dart2jslib.dart' show NullSink; 11 import 'package:compiler/implementation/dart2jslib.dart' show NullSink;
12 import 'package:unittest/unittest.dart'; 12 import 'package:unittest/unittest.dart';
13 13
14 import '../lib/src/closed_world.dart'; 14 import '../lib/src/closed_world.dart';
15 import '../lib/src/driver.dart'; 15 import '../lib/src/driver.dart';
16 16
17 main() { 17 main() {
18 test('Toplevel function', () { 18 test('Toplevel function', () {
19 var helper = new TreeShakerTestHelper(''' 19 var helper = new TreeShakerTestHelper('''
20 main() { 20 main() {
21 foo(); 21 foo();
22 } 22 }
23 foo() { 23 foo() {
24 } 24 }
25 '''); 25 ''');
26 helper.assertHasFunction('main'); 26 helper.assertHasFunction('main');
27 helper.assertHasFunction('foo'); 27 helper.assertHasFunction('foo');
28 }); 28 });
29 29
30 test('Toplevel field access', () {
31 var helper = new TreeShakerTestHelper('''
32 main() {
33 return foo;
34 }
35 var foo;
36 var bar;
37 ''');
38 helper.assertHasFunction('main');
39 helper.assertHasVariable('foo');
40 helper.assertNoVariable('bar');
41 });
42
43 test('Toplevel field invocation', () {
44 var helper = new TreeShakerTestHelper('''
45 main() {
46 return foo();
47 }
48 var foo;
49 var bar;
50 ''');
51 helper.assertHasFunction('main');
52 helper.assertHasVariable('foo');
53 helper.assertNoVariable('bar');
54 });
Paul Berry 2014/09/26 16:42:39 We should probably also test that this drags a "ca
Johnni Winther 2014/09/29 13:07:25 Added with 'foo()' instead of 'return foo'.
55
30 test('Class instantiation', () { 56 test('Class instantiation', () {
31 var helper = new TreeShakerTestHelper(''' 57 var helper = new TreeShakerTestHelper('''
32 main() { 58 main() {
33 var x = new A(); 59 var x = new A();
34 } 60 }
35 class A {} 61 class A {}
36 class B {} 62 class B {}
37 '''); 63 ''');
38 helper.assertHasInstantiatedClass('A'); 64 helper.assertHasInstantiatedClass('A');
39 helper.assertNoInstantiatedClass('B'); 65 helper.assertNoInstantiatedClass('B');
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 * Getters contained in [world], indexed by className.propertyName. 185 * Getters contained in [world], indexed by className.propertyName.
160 */ 186 */
161 Map<String, MethodDeclaration> getters = <String, MethodDeclaration>{}; 187 Map<String, MethodDeclaration> getters = <String, MethodDeclaration>{};
162 188
163 /** 189 /**
164 * Fields contained in [world], indexed by className.fieldName. 190 * Fields contained in [world], indexed by className.fieldName.
165 */ 191 */
166 Map<String, VariableDeclaration> fields = <String, VariableDeclaration>{}; 192 Map<String, VariableDeclaration> fields = <String, VariableDeclaration>{};
167 193
168 /** 194 /**
195 * Top level variables contained in [world], indexed by name.
196 */
197 Map<String, VariableDeclaration> variables = <String, VariableDeclaration>{};
198
199 /**
169 * Classes instantiated in [world], indexed by name. 200 * Classes instantiated in [world], indexed by name.
170 */ 201 */
171 Map<String, ClassDeclaration> instantiatedClasses = <String, 202 Map<String, ClassDeclaration> instantiatedClasses = <String,
172 ClassDeclaration>{}; 203 ClassDeclaration>{};
173 204
174 /** 205 /**
175 * Create a TreeShakerTestHelper based on the given file contents. 206 * Create a TreeShakerTestHelper based on the given file contents.
176 */ 207 */
177 TreeShakerTestHelper(String contents) { 208 TreeShakerTestHelper(String contents) {
178 MemoryResourceProvider provider = new MemoryResourceProvider(); 209 MemoryResourceProvider provider = new MemoryResourceProvider();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 expect(declaration, isNotNull); 244 expect(declaration, isNotNull);
214 expect(declaration.element, equals(element)); 245 expect(declaration.element, equals(element));
215 instantiatedClasses[element.name] = declaration; 246 instantiatedClasses[element.name] = declaration;
216 }); 247 });
217 world.fields.forEach( 248 world.fields.forEach(
218 (FieldElement element, VariableDeclaration declaration) { 249 (FieldElement element, VariableDeclaration declaration) {
219 expect(declaration, isNotNull); 250 expect(declaration, isNotNull);
220 expect(declaration.element, equals(element)); 251 expect(declaration.element, equals(element));
221 fields['${element.enclosingElement.name}.${element.name}'] = declaration; 252 fields['${element.enclosingElement.name}.${element.name}'] = declaration;
222 }); 253 });
254 world.variables.forEach(
255 (TopLevelVariableElement element, VariableDeclaration declaration) {
256 expect(declaration, isNotNull);
257 expect(declaration.element, equals(element));
258 variables['${element.name}'] = declaration;
259 });
223 } 260 }
224 261
225 /** 262 /**
226 * Asserts that [world] contains a field with the given qualified name. 263 * Asserts that [world] contains a field with the given qualified name.
227 */ 264 */
228 void assertHasField(String qualifiedName) { 265 void assertHasField(String qualifiedName) {
229 expect(fields, contains(qualifiedName)); 266 expect(fields, contains(qualifiedName));
230 } 267 }
231 268
232 /** 269 /**
270 * Asserts that [world] contains a top level variable with the given name.
271 */
272 void assertHasVariable(String name) {
273 expect(variables, contains(name));
274 }
275
276 /**
233 * Asserts that [world] contains a top-level function with the given name. 277 * Asserts that [world] contains a top-level function with the given name.
234 */ 278 */
235 void assertHasFunction(String name) { 279 void assertHasFunction(String name) {
236 expect(functions, contains(name)); 280 expect(functions, contains(name));
237 } 281 }
238 282
239 /** 283 /**
240 * Asserts that [world] contains a getter with the given qualified name. 284 * Asserts that [world] contains a getter with the given qualified name.
241 */ 285 */
242 void assertHasGetter(String qualifiedName) { 286 void assertHasGetter(String qualifiedName) {
(...skipping 18 matching lines...) Expand all
261 305
262 /** 306 /**
263 * Asserts that [world] doesn't contain a field with the given qualified 307 * Asserts that [world] doesn't contain a field with the given qualified
264 * name. 308 * name.
265 */ 309 */
266 void assertNoField(String qualifiedName) { 310 void assertNoField(String qualifiedName) {
267 expect(fields, isNot(contains(qualifiedName))); 311 expect(fields, isNot(contains(qualifiedName)));
268 } 312 }
269 313
270 /** 314 /**
315 * Asserts that [world] doesn't contain a top level variable with the given
316 * name.
317 */
318 void assertNoVariable(String name) {
319 expect(variables, isNot(contains(name)));
320 }
321
322 /**
271 * Asserts that [world] doesn't contain a top-level function with the given 323 * Asserts that [world] doesn't contain a top-level function with the given
272 * name. 324 * name.
273 */ 325 */
274 void assertNoFunction(String name) { 326 void assertNoFunction(String name) {
275 expect(functions, isNot(contains(name))); 327 expect(functions, isNot(contains(name)));
276 } 328 }
277 329
278 /** 330 /**
279 * Asserts that [world] doesn't contain a getter with the given qualified 331 * Asserts that [world] doesn't contain a getter with the given qualified
280 * name. 332 * name.
(...skipping 12 matching lines...) Expand all
293 /** 345 /**
294 * Asserts that [world] doesn't contain a method with the given qualified 346 * Asserts that [world] doesn't contain a method with the given qualified
295 * name. 347 * name.
296 * 348 *
297 * [qualifiedName] - the qualified name in form 'className.methodName'. 349 * [qualifiedName] - the qualified name in form 'className.methodName'.
298 */ 350 */
299 void assertNoMethod(String qualifiedName) { 351 void assertNoMethod(String qualifiedName) {
300 expect(methods, isNot(contains(qualifiedName))); 352 expect(methods, isNot(contains(qualifiedName)));
301 } 353 }
302 } 354 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698