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

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

Issue 1651653002: Verify AST instead of constant values. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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
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 'package:analyzer/dart/ast/ast.dart';
7 import 'package:analyzer/dart/element/element.dart'; 8 import 'package:analyzer/dart/element/element.dart';
8 import 'package:analyzer/dart/element/type.dart'; 9 import 'package:analyzer/dart/element/type.dart';
9 import 'package:analyzer/src/dart/element/element.dart'; 10 import 'package:analyzer/src/dart/element/element.dart';
10 import 'package:analyzer/src/dart/element/type.dart'; 11 import 'package:analyzer/src/dart/element/type.dart';
11 import 'package:analyzer/src/generated/constant.dart';
12 import 'package:analyzer/src/generated/element_handle.dart'; 12 import 'package:analyzer/src/generated/element_handle.dart';
13 import 'package:analyzer/src/generated/engine.dart'; 13 import 'package:analyzer/src/generated/engine.dart';
14 import 'package:analyzer/src/generated/resolver.dart' 14 import 'package:analyzer/src/generated/resolver.dart'
15 show Namespace, TypeProvider; 15 show Namespace, TypeProvider;
16 import 'package:analyzer/src/generated/source.dart'; 16 import 'package:analyzer/src/generated/source.dart';
17 import 'package:analyzer/src/summary/format.dart'; 17 import 'package:analyzer/src/summary/format.dart';
18 import 'package:analyzer/src/summary/resynthesize.dart'; 18 import 'package:analyzer/src/summary/resynthesize.dart';
19 import 'package:analyzer/src/summary/summarize_elements.dart'; 19 import 'package:analyzer/src/summary/summarize_elements.dart';
20 import 'package:unittest/unittest.dart'; 20 import 'package:unittest/unittest.dart';
21 21
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 comparePropertyAccessorElements(resynthesized.accessors[i], 196 comparePropertyAccessorElements(resynthesized.accessors[i],
197 original.accessors[i], 'getter ${original.accessors[i].name}'); 197 original.accessors[i], 'getter ${original.accessors[i].name}');
198 } else { 198 } else {
199 comparePropertyAccessorElements(resynthesized.accessors[i], 199 comparePropertyAccessorElements(resynthesized.accessors[i],
200 original.accessors[i], 'setter ${original.accessors[i].name}'); 200 original.accessors[i], 'setter ${original.accessors[i].name}');
201 } 201 }
202 } 202 }
203 // TODO(paulberry): test metadata and offsetToElementMap. 203 // TODO(paulberry): test metadata and offsetToElementMap.
204 } 204 }
205 205
206 void compareConstantValues( 206 void compareConstantExpressions(Expression r, Expression o, String desc) {
207 DartObject resynthesized, DartObject original, String desc) { 207 void compareLists(List<Object> rItems, List<Object> oItems) {
208 if (original == null) { 208 if (rItems == null && oItems == null) {
209 expect(resynthesized, isNull, reason: desc); 209 return;
210 }
211 expect(rItems != null && oItems != null, isTrue);
212 expect(rItems, hasLength(oItems.length));
213 for (int i = 0; i < oItems.length; i++) {
214 Object rItem = rItems[i];
215 Object oItem = oItems[i];
216 if (rItem is Expression && oItem is Expression) {
217 compareConstantExpressions(rItem, oItem, desc);
218 } else if (rItem is TypeName && oItem is TypeName) {
219 compareConstantExpressions(rItem.name, oItem.name, desc);
220 } else if (rItem is InterpolationString &&
221 oItem is InterpolationString) {
222 expect(rItem.value, oItem.value);
223 } else if (rItem is InterpolationExpression &&
224 oItem is InterpolationExpression) {
225 compareConstantExpressions(rItem.expression, oItem.expression, desc);
226 } else if (rItem is MapLiteralEntry && oItem is MapLiteralEntry) {
227 compareConstantExpressions(rItem.key, oItem.key, desc);
228 compareConstantExpressions(rItem.value, oItem.value, desc);
229 } else {
230 fail('$desc Incompatible item types: '
231 '${rItem.runtimeType} vs. ${oItem.runtimeType}');
232 }
233 }
234 }
235 if (o == null) {
236 expect(r, isNull, reason: desc);
210 } else { 237 } else {
211 expect(resynthesized, isNotNull, reason: desc); 238 expect(r, isNotNull, reason: desc);
212 compareTypes(resynthesized.type, original.type, desc); 239 compareTypes(r.staticType, o.staticType, desc);
213 expect(resynthesized.hasKnownValue, original.hasKnownValue, reason: desc); 240 if (o is ParenthesizedExpression) {
214 if (original.isNull) { 241 // We don't resynthesize parenthesis, so just ignore it.
215 expect(resynthesized.isNull, isTrue, reason: desc); 242 compareConstantExpressions(r, o.expression, desc);
216 } else if (original.toBoolValue() != null) { 243 } else if (o is SimpleIdentifier && r is SimpleIdentifier) {
217 expect(resynthesized.toBoolValue(), original.toBoolValue(), 244 expect(r.name, o.name);
245 compareTypes(r.staticType, o.staticType, desc);
246 compareElements(r.staticElement, o.staticElement, desc);
247 } else if (o is PrefixedIdentifier) {
248 // We don't resynthesize prefixed identifiers.
249 // We use simple identifiers with correct elements and types.
250 compareConstantExpressions(r as SimpleIdentifier, o.identifier, desc);
251 } else if (o is NullLiteral) {
252 expect(r, new isInstanceOf<NullLiteral>(), reason: desc);
253 } else if (o is BooleanLiteral && r is BooleanLiteral) {
254 expect(r.value, o.value, reason: desc);
255 } else if (o is IntegerLiteral && r is IntegerLiteral) {
256 expect(r.value, o.value, reason: desc);
257 } else if (o is DoubleLiteral && r is DoubleLiteral) {
258 expect(r.value, o.value, reason: desc);
259 } else if (o is StringInterpolation && r is StringInterpolation) {
260 compareLists(r.elements, o.elements);
261 } else if (o is StringLiteral && r is StringLiteral) {
262 // We don't keep all the tokens of AdjacentStrings.
263 // So, we can compare only their values.
264 expect(r.stringValue, o.stringValue, reason: desc);
265 } else if (o is SymbolLiteral && r is SymbolLiteral) {
266 // We don't keep all the tokens of symbol literals.
267 // So, we can compare only their values.
268 expect(r.components.map((t) => t.lexeme).join('.'),
269 o.components.map((t) => t.lexeme).join('.'),
218 reason: desc); 270 reason: desc);
219 } else if (original.toIntValue() != null) { 271 } else if (o is BinaryExpression && r is BinaryExpression) {
220 expect(resynthesized.toIntValue(), original.toIntValue(), reason: desc); 272 expect(r.operator.lexeme, o.operator.lexeme, reason: desc);
221 } else if (original.toDoubleValue() != null) { 273 compareConstantExpressions(r.leftOperand, o.leftOperand, desc);
222 expect(resynthesized.toDoubleValue(), original.toDoubleValue(), 274 compareConstantExpressions(r.rightOperand, o.rightOperand, desc);
223 reason: desc); 275 } else if (o is PrefixExpression && r is PrefixExpression) {
224 } else if (original.toListValue() != null) { 276 expect(r.operator.lexeme, o.operator.lexeme, reason: desc);
225 List<DartObject> resynthesizedList = resynthesized.toListValue(); 277 compareConstantExpressions(r.operand, o.operand, desc);
226 List<DartObject> originalList = original.toListValue(); 278 } else if (o is ConditionalExpression && r is ConditionalExpression) {
227 expect(resynthesizedList, hasLength(originalList.length)); 279 compareConstantExpressions(r.condition, o.condition, desc);
228 for (int i = 0; i < originalList.length; i++) { 280 compareConstantExpressions(r.thenExpression, o.thenExpression, desc);
229 compareConstantValues(resynthesizedList[i], originalList[i], desc); 281 compareConstantExpressions(r.elseExpression, o.elseExpression, desc);
230 } 282 } else if (o is ListLiteral && r is ListLiteral) {
231 } else if (original.toMapValue() != null) { 283 compareLists(r.typeArguments?.arguments, o.typeArguments?.arguments);
232 Map<DartObject, DartObject> resynthesizedMap = 284 compareLists(r.elements, o.elements);
233 resynthesized.toMapValue(); 285 } else if (o is MapLiteral && r is MapLiteral) {
234 Map<DartObject, DartObject> originalMap = original.toMapValue(); 286 compareLists(r.typeArguments?.arguments, o.typeArguments?.arguments);
235 expect(resynthesizedMap, hasLength(originalMap.length)); 287 compareLists(r.entries, o.entries);
236 List<DartObject> resynthesizedKeys = resynthesizedMap.keys.toList(); 288 } else {
237 List<DartObject> originalKeys = originalMap.keys.toList(); 289 fail('Not implemented for ${r.runtimeType} vs. ${o.runtimeType}');
238 for (int i = 0; i < originalKeys.length; i++) {
239 DartObject resynthesizedKey = resynthesizedKeys[i];
240 DartObject originalKey = originalKeys[i];
241 compareConstantValues(resynthesizedKey, originalKey, desc);
242 DartObject resynthesizedValue = resynthesizedMap[resynthesizedKey];
243 DartObject originalValue = originalMap[originalKey];
244 compareConstantValues(resynthesizedValue, originalValue, desc);
245 }
246 } else if (original.toStringValue() != null) {
247 expect(resynthesized.toStringValue(), original.toStringValue(),
248 reason: desc);
249 } else if (original.toSymbolValue() != null) {
250 expect(resynthesized.toSymbolValue(), original.toSymbolValue(),
251 reason: desc);
252 } else if (original.toTypeValue() != null) {
253 fail('Not implemented');
254 } 290 }
255 // TODO(scheglov) implement
256 } 291 }
257 } 292 }
258 293
259 void compareConstructorElements(ConstructorElementImpl resynthesized, 294 void compareConstructorElements(ConstructorElementImpl resynthesized,
260 ConstructorElementImpl original, String desc) { 295 ConstructorElementImpl original, String desc) {
261 compareExecutableElements(resynthesized, original, desc); 296 compareExecutableElements(resynthesized, original, desc);
262 // TODO(paulberry): test redirectedConstructor and constantInitializers 297 // TODO(paulberry): test redirectedConstructor and constantInitializers
263 } 298 }
264 299
265 void compareElements(Element resynthesized, Element original, String desc) { 300 void compareElements(Element resynthesized, Element original, String desc) {
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 expect(resynthesized.uri, original.uri); 593 expect(resynthesized.uri, original.uri);
559 expect(resynthesized.uriOffset, original.uriOffset, reason: desc); 594 expect(resynthesized.uriOffset, original.uriOffset, reason: desc);
560 expect(resynthesized.uriEnd, original.uriEnd, reason: desc); 595 expect(resynthesized.uriEnd, original.uriEnd, reason: desc);
561 } 596 }
562 597
563 void compareVariableElements(VariableElementImpl resynthesized, 598 void compareVariableElements(VariableElementImpl resynthesized,
564 VariableElementImpl original, String desc) { 599 VariableElementImpl original, String desc) {
565 compareElements(resynthesized, original, desc); 600 compareElements(resynthesized, original, desc);
566 compareTypes(resynthesized.type, original.type, desc); 601 compareTypes(resynthesized.type, original.type, desc);
567 if (shouldCompareConstValues) { 602 if (shouldCompareConstValues) {
568 compareConstantValues( 603 compareConstantExpressions(resynthesized.constantInitializer,
569 resynthesized.constantValue, original.constantValue, desc); 604 original.constantInitializer, desc);
570 } 605 }
571 // TODO(paulberry): test initializer 606 // TODO(paulberry): test initializer
572 } 607 }
573 608
574 /** 609 /**
575 * Serialize the given [library] into a summary. Then create a 610 * Serialize the given [library] into a summary. Then create a
576 * [_TestSummaryResynthesizer] which can deserialize it, along with any 611 * [_TestSummaryResynthesizer] which can deserialize it, along with any
577 * references it makes to `dart:core`. 612 * references it makes to `dart:core`.
578 * 613 *
579 * Errors will lead to a test failure unless [allowErrors] is `true`. 614 * Errors will lead to a test failure unless [allowErrors] is `true`.
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 const vInt = 1; 1053 const vInt = 1;
1019 const vIntLong = 0x9876543210987654321; 1054 const vIntLong = 0x9876543210987654321;
1020 const vDouble = 2.3; 1055 const vDouble = 2.3;
1021 const vString = 'abc'; 1056 const vString = 'abc';
1022 const vStringConcat = 'aaa' 'bbb'; 1057 const vStringConcat = 'aaa' 'bbb';
1023 const vStringInterpolation = 'aaa ${true} ${42} bbb'; 1058 const vStringInterpolation = 'aaa ${true} ${42} bbb';
1024 const vSymbol = #aaa.bbb.ccc; 1059 const vSymbol = #aaa.bbb.ccc;
1025 '''); 1060 ''');
1026 } 1061 }
1027 1062
1063 test_const_topLevel_prefix() {
1064 shouldCompareConstValues = true;
1065 checkLibrary(r'''
1066 const vNotEqual = 1 != 2;
1067 const vNot = !true;
1068 const vNegate = -1;
1069 const vComplement = ~1;
1070 ''');
1071 }
1072
1028 test_const_topLevel_typedList() { 1073 test_const_topLevel_typedList() {
1029 shouldCompareConstValues = true; 1074 shouldCompareConstValues = true;
1030 checkLibrary(r''' 1075 checkLibrary(r'''
1031 const vNull = const <Null>[]; 1076 const vNull = const <Null>[];
1032 const vDynamic = const <dynamic>[1, 2, 3]; 1077 const vDynamic = const <dynamic>[1, 2, 3];
1033 const vInterfaceNoTypeParameters = const <int>[1, 2, 3]; 1078 const vInterfaceNoTypeParameters = const <int>[1, 2, 3];
1034 const vInterfaceNoTypeArguments = const <List>[]; 1079 const vInterfaceNoTypeArguments = const <List>[];
1035 const vInterfaceWithTypeArguments = const <List<String>>[]; 1080 const vInterfaceWithTypeArguments = const <List<String>>[];
1036 const vInterfaceWithTypeArguments2 = const <Map<int, List<String>>>[]; 1081 const vInterfaceWithTypeArguments2 = const <Map<int, List<String>>>[];
1037 '''); 1082 ''');
1038 } 1083 }
1039 1084
1040 test_const_topLevel_typedList_imported() { 1085 test_const_topLevel_typedList_imported() {
1041 shouldCompareConstValues = true; 1086 shouldCompareConstValues = true;
1042 addNamedSource('/a.dart', 'class C {}'); 1087 addLibrarySource('/a.dart', 'class C {}');
1043 checkLibrary(r''' 1088 checkLibrary(r'''
1044 import 'a.dart'; 1089 import 'a.dart';
1045 const v = const <C>[]; 1090 const v = const <C>[];
1046 '''); 1091 ''');
1047 } 1092 }
1048 1093
1049 test_const_topLevel_typedList_importedWithPrefix() { 1094 test_const_topLevel_typedList_importedWithPrefix() {
1050 shouldCompareConstValues = true; 1095 shouldCompareConstValues = true;
1051 addNamedSource('/a.dart', 'class C {}'); 1096 addLibrarySource('/a.dart', 'class C {}');
1052 checkLibrary(r''' 1097 checkLibrary(r'''
1053 import 'a.dart' as p; 1098 import 'a.dart' as p;
1054 const v = const <p.C>[]; 1099 const v = const <p.C>[];
1055 '''); 1100 ''');
1056 } 1101 }
1057 1102
1058 test_const_topLevel_typedMap() { 1103 test_const_topLevel_typedMap() {
1059 shouldCompareConstValues = true; 1104 shouldCompareConstValues = true;
1060 checkLibrary(r''' 1105 checkLibrary(r'''
1061 const vDynamic1 = const <dynamic, int>{}; 1106 const vDynamic1 = const <dynamic, int>{};
1062 const vDynamic2 = const <int, dynamic>{}; 1107 const vDynamic2 = const <int, dynamic>{};
1063 const vInterface = const <int, String>{}; 1108 const vInterface = const <int, String>{};
1064 const vInterfaceWithTypeArguments = const <int, List<String>>{}; 1109 const vInterfaceWithTypeArguments = const <int, List<String>>{};
1065 '''); 1110 ''');
1066 } 1111 }
1067 1112
1068 test_const_topLevel_unary() {
1069 shouldCompareConstValues = true;
1070 checkLibrary(r'''
1071 const vNotEqual = 1 != 2;
1072 const vNot = !true;
1073 const vNegate = -1;
1074 const vComplement = ~1;
1075 ''');
1076 }
1077
1078 test_const_topLevel_untypedList() { 1113 test_const_topLevel_untypedList() {
1079 shouldCompareConstValues = true; 1114 shouldCompareConstValues = true;
1080 checkLibrary(r''' 1115 checkLibrary(r'''
1081 const v = const [1, 2, 3]; 1116 const v = const [1, 2, 3];
1082 '''); 1117 ''');
1083 } 1118 }
1084 1119
1085 test_const_topLevel_untypedMap() { 1120 test_const_topLevel_untypedMap() {
1086 shouldCompareConstValues = true; 1121 shouldCompareConstValues = true;
1087 checkLibrary(r''' 1122 checkLibrary(r'''
(...skipping 941 matching lines...) Expand 10 before | Expand all | Expand 10 after
2029 fail('Unexpectedly tried to get unlinked summary for $uri'); 2064 fail('Unexpectedly tried to get unlinked summary for $uri');
2030 } 2065 }
2031 return serializedUnit; 2066 return serializedUnit;
2032 } 2067 }
2033 2068
2034 @override 2069 @override
2035 bool hasLibrarySummary(String uri) { 2070 bool hasLibrarySummary(String uri) {
2036 return true; 2071 return true;
2037 } 2072 }
2038 } 2073 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/summarize_const_expr.dart ('k') | pkg/analyzer/test/src/summary/summary_common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698