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

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

Issue 1691693002: Serialize local functions and variables. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Remove unused class. 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 analyzer.test.src.summary.summary_common; 5 library analyzer.test.src.summary.summary_common;
6 6
7 import 'package:analyzer/analyzer.dart'; 7 import 'package:analyzer/analyzer.dart';
8 import 'package:analyzer/dart/ast/ast.dart'; 8 import 'package:analyzer/dart/ast/ast.dart';
9 import 'package:analyzer/dart/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/src/generated/engine.dart'; 10 import 'package:analyzer/src/generated/engine.dart';
(...skipping 2731 matching lines...) Expand 10 before | Expand all | Expand 10 after
2742 String text = 'class C { C(); }'; 2742 String text = 'class C { C(); }';
2743 UnlinkedExecutable executable = 2743 UnlinkedExecutable executable =
2744 findExecutable('', executables: serializeClassText(text).executables); 2744 findExecutable('', executables: serializeClassText(text).executables);
2745 expect(executable.kind, UnlinkedExecutableKind.constructor); 2745 expect(executable.kind, UnlinkedExecutableKind.constructor);
2746 expect(executable.returnType, isNull); 2746 expect(executable.returnType, isNull);
2747 expect(executable.isExternal, isFalse); 2747 expect(executable.isExternal, isFalse);
2748 expect(executable.nameOffset, text.indexOf('C();')); 2748 expect(executable.nameOffset, text.indexOf('C();'));
2749 expect(executable.isRedirectedConstructor, isFalse); 2749 expect(executable.isRedirectedConstructor, isFalse);
2750 expect(executable.redirectedConstructor, isNull); 2750 expect(executable.redirectedConstructor, isNull);
2751 expect(executable.redirectedConstructorName, isEmpty); 2751 expect(executable.redirectedConstructorName, isEmpty);
2752 expect(executable.visibleOffset, 0);
2753 expect(executable.visibleLength, 0);
2752 } 2754 }
2753 2755
2754 test_constructor_anonymous() { 2756 test_constructor_anonymous() {
2755 UnlinkedExecutable executable = findExecutable('', 2757 UnlinkedExecutable executable = findExecutable('',
2756 executables: serializeClassText('class C { C(); }').executables); 2758 executables: serializeClassText('class C { C(); }').executables);
2757 expect(executable.name, isEmpty); 2759 expect(executable.name, isEmpty);
2758 } 2760 }
2759 2761
2760 test_constructor_const() { 2762 test_constructor_const() {
2761 UnlinkedExecutable executable = findExecutable('', 2763 UnlinkedExecutable executable = findExecutable('',
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after
3504 expect(executable.isAbstract, isFalse); 3506 expect(executable.isAbstract, isFalse);
3505 } 3507 }
3506 3508
3507 test_executable_function() { 3509 test_executable_function() {
3508 String text = ' f() {}'; 3510 String text = ' f() {}';
3509 UnlinkedExecutable executable = serializeExecutableText(text); 3511 UnlinkedExecutable executable = serializeExecutableText(text);
3510 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod); 3512 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod);
3511 expect(executable.returnType, isNull); 3513 expect(executable.returnType, isNull);
3512 expect(executable.isExternal, isFalse); 3514 expect(executable.isExternal, isFalse);
3513 expect(executable.nameOffset, text.indexOf('f')); 3515 expect(executable.nameOffset, text.indexOf('f'));
3516 expect(executable.visibleOffset, 0);
3517 expect(executable.visibleLength, 0);
3514 expect(unlinkedUnits[0].publicNamespace.names, hasLength(1)); 3518 expect(unlinkedUnits[0].publicNamespace.names, hasLength(1));
3515 expect(unlinkedUnits[0].publicNamespace.names[0].kind, 3519 expect(unlinkedUnits[0].publicNamespace.names[0].kind,
3516 ReferenceKind.topLevelFunction); 3520 ReferenceKind.topLevelFunction);
3517 expect(unlinkedUnits[0].publicNamespace.names[0].name, 'f'); 3521 expect(unlinkedUnits[0].publicNamespace.names[0].name, 'f');
3518 expect(unlinkedUnits[0].publicNamespace.names[0].numTypeParameters, 0); 3522 expect(unlinkedUnits[0].publicNamespace.names[0].numTypeParameters, 0);
3519 } 3523 }
3520 3524
3521 test_executable_function_explicit_return() { 3525 test_executable_function_explicit_return() {
3522 UnlinkedExecutable executable = 3526 UnlinkedExecutable executable =
3523 serializeExecutableText('dynamic f() => null;'); 3527 serializeExecutableText('dynamic f() => null;');
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3565 checkTypeRef(executable.returnType, 'dart:core', 'dart:core', 'int'); 3569 checkTypeRef(executable.returnType, 'dart:core', 'dart:core', 'int');
3566 expect(executable.parameters, isEmpty); 3570 expect(executable.parameters, isEmpty);
3567 } 3571 }
3568 3572
3569 test_executable_getter_type_implicit() { 3573 test_executable_getter_type_implicit() {
3570 UnlinkedExecutable executable = serializeExecutableText('get f => 1;'); 3574 UnlinkedExecutable executable = serializeExecutableText('get f => 1;');
3571 expect(executable.returnType, isNull); 3575 expect(executable.returnType, isNull);
3572 expect(executable.parameters, isEmpty); 3576 expect(executable.parameters, isEmpty);
3573 } 3577 }
3574 3578
3579 test_executable_localFunctions() {
Paul Berry 2016/02/11 15:29:19 Also test local variables and local functions insi
scheglov 2016/02/11 15:50:08 Done.
3580 String code = r'''
3581 f() { // 1
3582 f1() {}
3583 { // 2
3584 f2() {}
3585 } // 3
3586 } // 4
3587 ''';
3588 UnlinkedExecutable executable = serializeExecutableText(code);
3589 List<UnlinkedExecutable> functions = executable.executables;
3590 expect(functions, hasLength(2));
3591 {
3592 UnlinkedExecutable f1 = functions.singleWhere((v) => v.name == 'f1');
3593 _assertExecutableVisible(code, f1, '{ // 1', '} // 4');
3594 }
3595 {
3596 UnlinkedExecutable f2 = functions.singleWhere((v) => v.name == 'f2');
3597 _assertExecutableVisible(code, f2, '{ // 2', '} // 3');
3598 }
3599 }
3600
3601 test_executable_localVariables_empty() {
3602 UnlinkedExecutable executable = serializeExecutableText(r'''
3603 f() {
3604 }
3605 ''');
3606 expect(executable.variables, isEmpty);
3607 }
3608
3609 test_executable_localVariables_inConstructor() {
3610 String code = r'''
3611 class C {
3612 C() { // 1
3613 int v;
3614 } // 2
3615 }
3616 ''';
3617 UnlinkedExecutable executable =
3618 findExecutable('', executables: serializeClassText(code).executables);
3619 List<UnlinkedVariable> variables = executable.variables;
3620 expect(variables, hasLength(1));
3621 {
3622 UnlinkedVariable v = variables.singleWhere((v) => v.name == 'v');
3623 _assertVariableVisible(code, v, '{ // 1', '} // 2');
3624 checkTypeRef(v.type, 'dart:core', 'dart:core', 'int');
3625 }
3626 }
3627
3628 test_executable_localVariables_inLocalFunctions() {
3629 String code = r'''
3630 f() {
3631 f1() { // 1
3632 int v1 = 1;
3633 } // 2
3634 f2() { // 3
3635 int v1 = 1;
3636 f3() { // 4
3637 int v2 = 1;
3638 } // 5
3639 } // 6
3640 } // 7
3641 ''';
3642 UnlinkedExecutable executable = serializeExecutableText(code);
3643 List<UnlinkedExecutable> functions = executable.executables;
3644 expect(functions, hasLength(2));
3645 // f - f1
3646 {
3647 UnlinkedExecutable f1 = functions.singleWhere((v) => v.name == 'f1');
3648 List<UnlinkedVariable> variables = f1.variables;
3649 expect(variables, hasLength(1));
3650 // f1 - v1
3651 UnlinkedVariable v1 = variables.singleWhere((v) => v.name == 'v1');
3652 _assertVariableVisible(code, v1, '{ // 1', '} // 2');
3653 checkTypeRef(v1.type, 'dart:core', 'dart:core', 'int');
3654 }
3655 // f - f2
3656 {
3657 UnlinkedExecutable f2 = functions.singleWhere((v) => v.name == 'f2');
3658 List<UnlinkedVariable> variables2 = f2.variables;
3659 List<UnlinkedExecutable> functions2 = f2.executables;
3660 expect(variables2, hasLength(1));
3661 expect(functions2, hasLength(1));
3662 // f - f2 - v1
3663 UnlinkedVariable v1 = variables2.singleWhere((v) => v.name == 'v1');
3664 _assertVariableVisible(code, v1, '{ // 3', '} // 6');
3665 checkTypeRef(v1.type, 'dart:core', 'dart:core', 'int');
3666 // f - f2 - f3
3667 UnlinkedExecutable f3 = functions2.singleWhere((v) => v.name == 'f3');
3668 _assertExecutableVisible(code, f3, '{ // 3', '} // 6');
3669 List<UnlinkedVariable> variables3 = f3.variables;
3670 List<UnlinkedExecutable> functions3 = f3.executables;
3671 expect(variables3, hasLength(1));
3672 expect(functions3, hasLength(0));
3673 // f - f3 - v2
3674 UnlinkedVariable v2 = variables3.singleWhere((v) => v.name == 'v2');
3675 _assertVariableVisible(code, v2, '{ // 4', '} // 5');
3676 checkTypeRef(v2.type, 'dart:core', 'dart:core', 'int');
3677 }
3678 }
3679
3680 test_executable_localVariables_inTopLevelFunction() {
3681 String code = r'''
3682 f() { // 1
3683 int v1 = 1;
3684 { // 2
3685 int v2 = 2;
3686 } // 3
3687 var v3 = 3;
3688 } // 4
3689 ''';
3690 UnlinkedExecutable executable = serializeExecutableText(code);
3691 List<UnlinkedVariable> variables = executable.variables;
3692 expect(variables, hasLength(3));
3693 {
3694 UnlinkedVariable v1 = variables.singleWhere((v) => v.name == 'v1');
3695 _assertVariableVisible(code, v1, '{ // 1', '} // 4');
3696 checkTypeRef(v1.type, 'dart:core', 'dart:core', 'int');
3697 }
3698 {
3699 UnlinkedVariable v2 = variables.singleWhere((v) => v.name == 'v2');
3700 _assertVariableVisible(code, v2, '{ // 2', '} // 3');
3701 checkTypeRef(v2.type, 'dart:core', 'dart:core', 'int');
3702 }
3703 {
3704 UnlinkedVariable v3 = variables.singleWhere((v) => v.name == 'v3');
3705 _assertVariableVisible(code, v3, '{ // 1', '} // 4');
3706 expect(v3.type, isNull);
3707 }
3708 }
3709
3575 test_executable_member_function() { 3710 test_executable_member_function() {
3576 UnlinkedExecutable executable = findExecutable('f', 3711 UnlinkedExecutable executable = findExecutable('f',
3577 executables: serializeClassText('class C { f() {} }').executables); 3712 executables: serializeClassText('class C { f() {} }').executables);
3578 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod); 3713 expect(executable.kind, UnlinkedExecutableKind.functionOrMethod);
3579 expect(executable.returnType, isNull); 3714 expect(executable.returnType, isNull);
3580 expect(executable.isExternal, isFalse); 3715 expect(executable.isExternal, isFalse);
3716 expect(executable.visibleOffset, 0);
3717 expect(executable.visibleLength, 0);
3581 } 3718 }
3582 3719
3583 test_executable_member_function_explicit_return() { 3720 test_executable_member_function_explicit_return() {
3584 UnlinkedExecutable executable = findExecutable('f', 3721 UnlinkedExecutable executable = findExecutable('f',
3585 executables: 3722 executables:
3586 serializeClassText('class C { dynamic f() => null; }').executables); 3723 serializeClassText('class C { dynamic f() => null; }').executables);
3587 expect(executable.returnType, isNotNull); 3724 expect(executable.returnType, isNotNull);
3588 } 3725 }
3589 3726
3590 test_executable_member_function_external() { 3727 test_executable_member_function_external() {
(...skipping 2496 matching lines...) Expand 10 before | Expand all | Expand 10 after
6087 default: 6224 default:
6088 // This reference should have a dependency of 0, since it refers to 6225 // This reference should have a dependency of 0, since it refers to
6089 // an element that is contained within some other element. 6226 // an element that is contained within some other element.
6090 expect(reference.dependency, 0, 6227 expect(reference.dependency, 0,
6091 reason: 'Nonzero dependency for ${reference.kind}'); 6228 reason: 'Nonzero dependency for ${reference.kind}');
6092 } 6229 }
6093 } 6230 }
6094 } 6231 }
6095 } 6232 }
6096 6233
6234 void _assertExecutableVisible(String code, UnlinkedExecutable f,
6235 String visibleBegin, String visibleEnd) {
6236 int expectedVisibleOffset = code.indexOf(visibleBegin);
6237 int expectedVisibleLength =
6238 code.indexOf(visibleEnd) - expectedVisibleOffset + 1;
6239 expect(f.visibleOffset, expectedVisibleOffset);
6240 expect(f.visibleLength, expectedVisibleLength);
6241 }
6242
6097 void _assertUnlinkedConst(UnlinkedConst constExpr, 6243 void _assertUnlinkedConst(UnlinkedConst constExpr,
6098 {bool isInvalid: false, 6244 {bool isInvalid: false,
6099 List<UnlinkedConstOperation> operators: const <UnlinkedConstOperation>[], 6245 List<UnlinkedConstOperation> operators: const <UnlinkedConstOperation>[],
6100 List<int> ints: const <int>[], 6246 List<int> ints: const <int>[],
6101 List<double> doubles: const <double>[], 6247 List<double> doubles: const <double>[],
6102 List<String> strings: const <String>[], 6248 List<String> strings: const <String>[],
6103 List<_EntityRefValidator> referenceValidators: 6249 List<_EntityRefValidator> referenceValidators:
6104 const <_EntityRefValidator>[]}) { 6250 const <_EntityRefValidator>[]}) {
6105 expect(constExpr, isNotNull); 6251 expect(constExpr, isNotNull);
6106 expect(constExpr.isInvalid, isInvalid); 6252 expect(constExpr.isInvalid, isInvalid);
6107 expect(constExpr.operations, operators); 6253 expect(constExpr.operations, operators);
6108 expect(constExpr.ints, ints); 6254 expect(constExpr.ints, ints);
6109 expect(constExpr.doubles, doubles); 6255 expect(constExpr.doubles, doubles);
6110 expect(constExpr.strings, strings); 6256 expect(constExpr.strings, strings);
6111 expect(constExpr.references, hasLength(referenceValidators.length)); 6257 expect(constExpr.references, hasLength(referenceValidators.length));
6112 for (int i = 0; i < referenceValidators.length; i++) { 6258 for (int i = 0; i < referenceValidators.length; i++) {
6113 referenceValidators[i](constExpr.references[i]); 6259 referenceValidators[i](constExpr.references[i]);
6114 } 6260 }
6115 } 6261 }
6262
6263 void _assertVariableVisible(
6264 String code, UnlinkedVariable v, String visibleBegin, String visibleEnd) {
6265 int expectedVisibleOffset = code.indexOf(visibleBegin);
6266 int expectedVisibleLength =
6267 code.indexOf(visibleEnd) - expectedVisibleOffset + 1;
6268 expect(v.visibleOffset, expectedVisibleOffset);
6269 expect(v.visibleLength, expectedVisibleLength);
6270 }
6116 } 6271 }
6117 6272
6118 /** 6273 /**
6119 * Description of expectations for a prelinked prefix reference. 6274 * Description of expectations for a prelinked prefix reference.
6120 */ 6275 */
6121 class _PrefixExpectation { 6276 class _PrefixExpectation {
6122 final ReferenceKind kind; 6277 final ReferenceKind kind;
6123 final String name; 6278 final String name;
6124 final bool inLibraryDefiningUnit; 6279 final bool inLibraryDefiningUnit;
6125 final String absoluteUri; 6280 final String absoluteUri;
6126 final String relativeUri; 6281 final String relativeUri;
6127 final int numTypeParameters; 6282 final int numTypeParameters;
6128 6283
6129 _PrefixExpectation(this.kind, this.name, 6284 _PrefixExpectation(this.kind, this.name,
6130 {this.inLibraryDefiningUnit: false, 6285 {this.inLibraryDefiningUnit: false,
6131 this.absoluteUri, 6286 this.absoluteUri,
6132 this.relativeUri, 6287 this.relativeUri,
6133 this.numTypeParameters: 0}); 6288 this.numTypeParameters: 0});
6134 } 6289 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698