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

Side by Side Diff: pkg/analyzer/test/src/summary/resynthesize_test.dart

Issue 1949603003: Finer grain control over specifying which variables have invalid constant initializers. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 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
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_ast_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.serialization.elements_test; 5 library test.src.serialization.elements_test;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/constant/value.dart'; 10 import 'package:analyzer/dart/constant/value.dart';
(...skipping 25 matching lines...) Expand all
36 groupSep = ' | '; 36 groupSep = ' | ';
37 runReflectiveTests(ResynthesizeElementTest); 37 runReflectiveTests(ResynthesizeElementTest);
38 } 38 }
39 39
40 /** 40 /**
41 * Abstract base class for resynthesizing and comparing elements. 41 * Abstract base class for resynthesizing and comparing elements.
42 */ 42 */
43 abstract class AbstractResynthesizeTest extends AbstractSingleUnitTest { 43 abstract class AbstractResynthesizeTest extends AbstractSingleUnitTest {
44 Set<Source> otherLibrarySources = new Set<Source>(); 44 Set<Source> otherLibrarySources = new Set<Source>();
45 45
46 /**
47 * Names of variables which have initializers that are not valid constants,
48 * so they are not resynthesized.
49 */
50 Set<String> variablesWithNotConstInitializers = new Set<String>();
51
46 bool get checkPropagatedTypes => true; 52 bool get checkPropagatedTypes => true;
47 53
48 /** 54 /**
49 * Derived classes can override this getter to return `true` in order to 55 * Derived classes can override this getter to return `true` in order to
50 * cause certain checks to be skipped if they are known to fail with 56 * cause certain checks to be skipped if they are known to fail with
51 * AST-based type inference. 57 * AST-based type inference.
52 * 58 *
53 * TODO(paulberry): remove this flag once AST-based type inference is fully 59 * TODO(paulberry): remove this flag once AST-based type inference is fully
54 * working. 60 * working.
55 */ 61 */
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 expect(rItem.value, oItem.value); 333 expect(rItem.value, oItem.value);
328 } else if (rItem is InterpolationExpression && 334 } else if (rItem is InterpolationExpression &&
329 oItem is InterpolationExpression) { 335 oItem is InterpolationExpression) {
330 compareConstAsts(rItem.expression, oItem.expression, desc); 336 compareConstAsts(rItem.expression, oItem.expression, desc);
331 } else if (rItem is MapLiteralEntry && oItem is MapLiteralEntry) { 337 } else if (rItem is MapLiteralEntry && oItem is MapLiteralEntry) {
332 compareConstAsts(rItem.key, oItem.key, desc); 338 compareConstAsts(rItem.key, oItem.key, desc);
333 compareConstAsts(rItem.value, oItem.value, desc); 339 compareConstAsts(rItem.value, oItem.value, desc);
334 } else if (oItem is ConstructorFieldInitializer && 340 } else if (oItem is ConstructorFieldInitializer &&
335 rItem is ConstructorFieldInitializer) { 341 rItem is ConstructorFieldInitializer) {
336 compareConstAsts(rItem.fieldName, oItem.fieldName, desc); 342 compareConstAsts(rItem.fieldName, oItem.fieldName, desc);
337 compareConstAsts(rItem.expression, oItem.expression, desc); 343 if (variablesWithNotConstInitializers.contains(rItem.fieldName.name)) {
344 _assertUnresolvedIdentifier(rItem.expression, desc);
345 } else {
346 compareConstAsts(rItem.expression, oItem.expression, desc);
347 }
338 } else if (oItem is SuperConstructorInvocation && 348 } else if (oItem is SuperConstructorInvocation &&
339 rItem is SuperConstructorInvocation) { 349 rItem is SuperConstructorInvocation) {
340 compareElements(rItem.staticElement, oItem.staticElement, desc); 350 compareElements(rItem.staticElement, oItem.staticElement, desc);
341 compareConstAsts(rItem.constructorName, oItem.constructorName, desc); 351 compareConstAsts(rItem.constructorName, oItem.constructorName, desc);
342 compareConstAstLists( 352 compareConstAstLists(
343 rItem.argumentList.arguments, oItem.argumentList.arguments, desc); 353 rItem.argumentList.arguments, oItem.argumentList.arguments, desc);
344 } else if (oItem is RedirectingConstructorInvocation && 354 } else if (oItem is RedirectingConstructorInvocation &&
345 rItem is RedirectingConstructorInvocation) { 355 rItem is RedirectingConstructorInvocation) {
346 compareElements(rItem.staticElement, oItem.staticElement, desc); 356 compareElements(rItem.staticElement, oItem.staticElement, desc);
347 compareConstAsts(rItem.constructorName, oItem.constructorName, desc); 357 compareConstAsts(rItem.constructorName, oItem.constructorName, desc);
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 compareFunctionElements(resynthesizedActual.initializer, 1081 compareFunctionElements(resynthesizedActual.initializer,
1072 originalActual.initializer, '$desc initializer'); 1082 originalActual.initializer, '$desc initializer');
1073 } 1083 }
1074 if (originalActual is ConstVariableElement) { 1084 if (originalActual is ConstVariableElement) {
1075 Element oEnclosing = original.enclosingElement; 1085 Element oEnclosing = original.enclosingElement;
1076 if (oEnclosing is ClassElement && oEnclosing.isEnum) { 1086 if (oEnclosing is ClassElement && oEnclosing.isEnum) {
1077 compareConstValues( 1087 compareConstValues(
1078 resynthesized.constantValue, original.constantValue, desc); 1088 resynthesized.constantValue, original.constantValue, desc);
1079 } else { 1089 } else {
1080 Expression initializer = resynthesizedActual.constantInitializer; 1090 Expression initializer = resynthesizedActual.constantInitializer;
1081 compareConstAsts(initializer, originalActual.constantInitializer, 1091 if (variablesWithNotConstInitializers.contains(resynthesized.name)) {
1082 '$desc initializer'); 1092 _assertUnresolvedIdentifier(initializer, desc);
1093 } else {
1094 compareConstAsts(initializer, originalActual.constantInitializer,
1095 '$desc initializer');
1096 }
1083 } 1097 }
1084 } 1098 }
1085 checkPossibleMember(resynthesized, original, desc); 1099 checkPossibleMember(resynthesized, original, desc);
1086 checkPossibleLocalElements(resynthesized, original); 1100 checkPossibleLocalElements(resynthesized, original);
1087 } 1101 }
1088 1102
1089 DartSdk createDartSdk() => AbstractContextTest.SHARED_MOCK_SDK; 1103 DartSdk createDartSdk() => AbstractContextTest.SHARED_MOCK_SDK;
1090 1104
1091 /** 1105 /**
1092 * Determine the analysis options that should be used for this test. 1106 * Determine the analysis options that should be used for this test.
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
1188 expect(type.isDynamic || type.isVoid, isTrue); 1202 expect(type.isDynamic || type.isVoid, isTrue);
1189 return false; 1203 return false;
1190 } 1204 }
1191 } 1205 }
1192 1206
1193 @override 1207 @override
1194 void setUp() { 1208 void setUp() {
1195 super.setUp(); 1209 super.setUp();
1196 prepareAnalysisContext(createOptions()); 1210 prepareAnalysisContext(createOptions());
1197 } 1211 }
1212
1213 void _assertUnresolvedIdentifier(Expression initializer, String desc) {
1214 expect(initializer, new isInstanceOf<SimpleIdentifier>(), reason: desc);
1215 SimpleIdentifier identifier = initializer;
1216 expect(identifier.staticElement, isNull, reason: desc);
1217 }
1198 } 1218 }
1199 1219
1200 @reflectiveTest 1220 @reflectiveTest
1201 class ResynthesizeElementTest extends ResynthesizeTest { 1221 class ResynthesizeElementTest extends ResynthesizeTest {
1202 @override 1222 @override
1203 LibraryElementImpl checkLibrary(String text, 1223 LibraryElementImpl checkLibrary(String text,
1204 {bool allowErrors: false, bool dumpSummaries: false}) { 1224 {bool allowErrors: false, bool dumpSummaries: false}) {
1205 Source source = addTestSource(text); 1225 Source source = addTestSource(text);
1206 LibraryElementImpl original = context.computeLibraryElement(source); 1226 LibraryElementImpl original = context.computeLibraryElement(source);
1207 LibraryElementImpl resynthesized = resynthesizeLibraryElement( 1227 LibraryElementImpl resynthesized = resynthesizeLibraryElement(
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
1650 test_closure_executable_with_return_type_from_closure() { 1670 test_closure_executable_with_return_type_from_closure() {
1651 checkLibrary(''' 1671 checkLibrary('''
1652 f() { 1672 f() {
1653 print(() {}); 1673 print(() {});
1654 print(() => () => 0); 1674 print(() => () => 0);
1655 } 1675 }
1656 '''); 1676 ''');
1657 } 1677 }
1658 1678
1659 test_const_invalid_field_const() { 1679 test_const_invalid_field_const() {
1680 variablesWithNotConstInitializers.add('f');
1660 checkLibrary( 1681 checkLibrary(
1661 r''' 1682 r'''
1662 class C { 1683 class C {
1663 static const f = 1 + foo(); 1684 static const f = 1 + foo();
1664 } 1685 }
1665 int foo() => 42; 1686 int foo() => 42;
1666 ''', 1687 ''',
1667 allowErrors: true); 1688 allowErrors: true);
1668 } 1689 }
1669 1690
1670 test_const_invalid_field_final() { 1691 test_const_invalid_field_final() {
1692 variablesWithNotConstInitializers.add('f');
1671 checkLibrary( 1693 checkLibrary(
1672 r''' 1694 r'''
1673 class C { 1695 class C {
1674 final f = 1 + foo(); 1696 final f = 1 + foo();
1675 } 1697 }
1676 int foo() => 42; 1698 int foo() => 42;
1677 ''', 1699 ''',
1678 allowErrors: true); 1700 allowErrors: true);
1679 } 1701 }
1680 1702
1681 test_const_invalid_topLevel() { 1703 test_const_invalid_topLevel() {
1704 variablesWithNotConstInitializers.add('v');
1682 checkLibrary( 1705 checkLibrary(
1683 r''' 1706 r'''
1684 const v = 1 + foo(); 1707 const v = 1 + foo();
1685 int foo() => 42; 1708 int foo() => 42;
1686 ''', 1709 ''',
1687 allowErrors: true); 1710 allowErrors: true);
1688 } 1711 }
1689 1712
1690 test_const_invokeConstructor_generic_named() { 1713 test_const_invokeConstructor_generic_named() {
1691 checkLibrary(r''' 1714 checkLibrary(r'''
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after
2414 test_constructor_initializers_field() { 2437 test_constructor_initializers_field() {
2415 checkLibrary(''' 2438 checkLibrary('''
2416 class C { 2439 class C {
2417 final x; 2440 final x;
2418 const C() : x = 42; 2441 const C() : x = 42;
2419 } 2442 }
2420 '''); 2443 ''');
2421 } 2444 }
2422 2445
2423 test_constructor_initializers_field_notConst() { 2446 test_constructor_initializers_field_notConst() {
2447 variablesWithNotConstInitializers.add('x');
2424 checkLibrary( 2448 checkLibrary(
2425 ''' 2449 '''
2426 class C { 2450 class C {
2427 final x; 2451 final x;
2428 const A() : x = foo(); 2452 const A() : x = foo();
2429 } 2453 }
2430 int foo() => 42; 2454 int foo() => 42;
2431 ''', 2455 ''',
2432 allowErrors: true); 2456 allowErrors: true);
2433 } 2457 }
(...skipping 1917 matching lines...) Expand 10 before | Expand all | Expand 10 after
4351 fail('Unexpectedly tried to get unlinked summary for $uri'); 4375 fail('Unexpectedly tried to get unlinked summary for $uri');
4352 } 4376 }
4353 return serializedUnit; 4377 return serializedUnit;
4354 } 4378 }
4355 4379
4356 @override 4380 @override
4357 bool hasLibrarySummary(String uri) { 4381 bool hasLibrarySummary(String uri) {
4358 return true; 4382 return true;
4359 } 4383 }
4360 } 4384 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_ast_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698