| OLD | NEW |
| 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 library analyzer.test.generated.parser_test; | 5 library analyzer.test.generated.parser_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; | 8 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; |
| 9 import 'package:analyzer/dart/ast/token.dart'; | 9 import 'package:analyzer/dart/ast/token.dart'; |
| 10 import 'package:analyzer/dart/ast/visitor.dart'; | 10 import 'package:analyzer/dart/ast/visitor.dart'; |
| (...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1085 expect(constructor.parameters.parameters, isEmpty); | 1085 expect(constructor.parameters.parameters, isEmpty); |
| 1086 expect(constructor.separator.type, TokenType.EQ); | 1086 expect(constructor.separator.type, TokenType.EQ); |
| 1087 expect(constructor.initializers, isEmpty); | 1087 expect(constructor.initializers, isEmpty); |
| 1088 expect(constructor.redirectedConstructor, isNotNull); | 1088 expect(constructor.redirectedConstructor, isNotNull); |
| 1089 expect(constructor.redirectedConstructor.type.name.name, 'B'); | 1089 expect(constructor.redirectedConstructor.type.name.name, 'B'); |
| 1090 expect(constructor.redirectedConstructor.period, isNull); | 1090 expect(constructor.redirectedConstructor.period, isNull); |
| 1091 expect(constructor.redirectedConstructor.name, isNull); | 1091 expect(constructor.redirectedConstructor.name, isNull); |
| 1092 expect(constructor.body, new isInstanceOf<EmptyFunctionBody>()); | 1092 expect(constructor.body, new isInstanceOf<EmptyFunctionBody>()); |
| 1093 } | 1093 } |
| 1094 | 1094 |
| 1095 void test_parseConstructor_assert() { |
| 1096 enableAssertInitializer = true; |
| 1097 createParser('C(x, y) : _x = x, assert (x < y), _y = y;'); |
| 1098 ClassMember member = parser.parseClassMember('C'); |
| 1099 expect(member, isNotNull); |
| 1100 assertNoErrors(); |
| 1101 expect(member, new isInstanceOf<ConstructorDeclaration>()); |
| 1102 ConstructorDeclaration constructor = member as ConstructorDeclaration; |
| 1103 NodeList<ConstructorInitializer> initializers = constructor.initializers; |
| 1104 expect(initializers, hasLength(3)); |
| 1105 ConstructorInitializer initializer = initializers[1]; |
| 1106 expect(initializer, new isInstanceOf<AssertInitializer>()); |
| 1107 AssertInitializer assertInitializer = initializer; |
| 1108 expect(assertInitializer.condition, isNotNull); |
| 1109 expect(assertInitializer.message, isNull); |
| 1110 } |
| 1111 |
| 1095 void test_parseConstructor_initializers_field() { | 1112 void test_parseConstructor_initializers_field() { |
| 1096 createParser('C(x, y) : _x = x, this._y = y;'); | 1113 createParser('C(x, y) : _x = x, this._y = y;'); |
| 1097 ClassMember member = parser.parseClassMember('C'); | 1114 ClassMember member = parser.parseClassMember('C'); |
| 1098 expect(member, isNotNull); | 1115 expect(member, isNotNull); |
| 1099 assertNoErrors(); | 1116 assertNoErrors(); |
| 1100 expect(member, new isInstanceOf<ConstructorDeclaration>()); | 1117 expect(member, new isInstanceOf<ConstructorDeclaration>()); |
| 1101 ConstructorDeclaration constructor = member as ConstructorDeclaration; | 1118 ConstructorDeclaration constructor = member as ConstructorDeclaration; |
| 1102 NodeList<ConstructorInitializer> initializers = constructor.initializers; | 1119 NodeList<ConstructorInitializer> initializers = constructor.initializers; |
| 1103 expect(initializers, hasLength(2)); | 1120 expect(initializers, hasLength(2)); |
| 1104 | 1121 |
| 1105 { | 1122 { |
| 1106 var initializer = initializers[0] as ConstructorFieldInitializer; | 1123 var initializer = initializers[0] as ConstructorFieldInitializer; |
| 1107 expect(initializer.thisKeyword, isNull); | 1124 expect(initializer.thisKeyword, isNull); |
| 1108 expect(initializer.period, isNull); | 1125 expect(initializer.period, isNull); |
| 1109 expect(initializer.fieldName.name, '_x'); | 1126 expect(initializer.fieldName.name, '_x'); |
| 1110 expect(initializer.expression, isNotNull); | 1127 expect(initializer.expression, isNotNull); |
| 1111 } | 1128 } |
| 1112 | 1129 |
| 1113 { | 1130 { |
| 1114 var initializer = initializers[1] as ConstructorFieldInitializer; | 1131 var initializer = initializers[1] as ConstructorFieldInitializer; |
| 1115 expect(initializer.thisKeyword, isNotNull); | 1132 expect(initializer.thisKeyword, isNotNull); |
| 1116 expect(initializer.period, isNotNull); | 1133 expect(initializer.period, isNotNull); |
| 1117 expect(initializer.fieldName.name, '_y'); | 1134 expect(initializer.fieldName.name, '_y'); |
| 1118 expect(initializer.expression, isNotNull); | 1135 expect(initializer.expression, isNotNull); |
| 1119 } | 1136 } |
| 1120 } | 1137 } |
| 1121 | 1138 |
| 1122 void test_parseConstructor_assert() { | |
| 1123 enableAssertInitializer = true; | |
| 1124 createParser('C(x, y) : _x = x, assert (x < y), _y = y;'); | |
| 1125 ClassMember member = parser.parseClassMember('C'); | |
| 1126 expect(member, isNotNull); | |
| 1127 assertNoErrors(); | |
| 1128 expect(member, new isInstanceOf<ConstructorDeclaration>()); | |
| 1129 ConstructorDeclaration constructor = member as ConstructorDeclaration; | |
| 1130 NodeList<ConstructorInitializer> initializers = constructor.initializers; | |
| 1131 expect(initializers, hasLength(3)); | |
| 1132 ConstructorInitializer initializer = initializers[1]; | |
| 1133 expect(initializer, new isInstanceOf<AssertInitializer>()); | |
| 1134 AssertInitializer assertInitializer = initializer; | |
| 1135 expect(assertInitializer.condition, isNotNull); | |
| 1136 expect(assertInitializer.message, isNull); | |
| 1137 } | |
| 1138 | |
| 1139 void test_parseConstructor_named() { | 1139 void test_parseConstructor_named() { |
| 1140 createParser('C.foo();'); | 1140 createParser('C.foo();'); |
| 1141 var constructor = parser.parseClassMember('C') as ConstructorDeclaration; | 1141 var constructor = parser.parseClassMember('C') as ConstructorDeclaration; |
| 1142 assertNoErrors(); | 1142 assertNoErrors(); |
| 1143 expect(constructor, isNotNull); | 1143 expect(constructor, isNotNull); |
| 1144 expect(constructor.externalKeyword, isNull); | 1144 expect(constructor.externalKeyword, isNull); |
| 1145 expect(constructor.constKeyword, isNull); | 1145 expect(constructor.constKeyword, isNull); |
| 1146 expect(constructor.factoryKeyword, isNull); | 1146 expect(constructor.factoryKeyword, isNull); |
| 1147 expect(constructor.returnType.name, 'C'); | 1147 expect(constructor.returnType.name, 'C'); |
| 1148 _assertIsDeclarationName(constructor.returnType, false); | 1148 _assertIsDeclarationName(constructor.returnType, false); |
| (...skipping 3006 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4155 listener.assertErrorsWithCodes( | 4155 listener.assertErrorsWithCodes( |
| 4156 [ParserErrorCode.WRONG_TERMINATOR_FOR_PARAMETER_GROUP]); | 4156 [ParserErrorCode.WRONG_TERMINATOR_FOR_PARAMETER_GROUP]); |
| 4157 } | 4157 } |
| 4158 } | 4158 } |
| 4159 | 4159 |
| 4160 @reflectiveTest | 4160 @reflectiveTest |
| 4161 class ExpressionParserTest extends ParserTestCase | 4161 class ExpressionParserTest extends ParserTestCase |
| 4162 with ExpressionParserTestMixin {} | 4162 with ExpressionParserTestMixin {} |
| 4163 | 4163 |
| 4164 abstract class ExpressionParserTestMixin implements AbstractParserTestCase { | 4164 abstract class ExpressionParserTestMixin implements AbstractParserTestCase { |
| 4165 void test_namedArgument() { |
| 4166 var invocation = parseExpression('m(a: 1, b: 2)') as MethodInvocation; |
| 4167 List<Expression> arguments = invocation.argumentList.arguments; |
| 4168 |
| 4169 var a = arguments[0] as NamedExpression; |
| 4170 expect(a.name.label.name, 'a'); |
| 4171 expect(a.expression, isNotNull); |
| 4172 |
| 4173 var b = arguments[1] as NamedExpression; |
| 4174 expect(b.name.label.name, 'b'); |
| 4175 expect(b.expression, isNotNull); |
| 4176 } |
| 4177 |
| 4165 void test_parseAdditiveExpression_normal() { | 4178 void test_parseAdditiveExpression_normal() { |
| 4166 Expression expression = parseAdditiveExpression('x + y'); | 4179 Expression expression = parseAdditiveExpression('x + y'); |
| 4167 expect(expression, isNotNull); | 4180 expect(expression, isNotNull); |
| 4168 assertNoErrors(); | 4181 assertNoErrors(); |
| 4169 var binaryExpression = expression as BinaryExpression; | 4182 var binaryExpression = expression as BinaryExpression; |
| 4170 expect(binaryExpression.leftOperand, isNotNull); | 4183 expect(binaryExpression.leftOperand, isNotNull); |
| 4171 expect(binaryExpression.operator, isNotNull); | 4184 expect(binaryExpression.operator, isNotNull); |
| 4172 expect(binaryExpression.operator.type, TokenType.PLUS); | 4185 expect(binaryExpression.operator.type, TokenType.PLUS); |
| 4173 expect(binaryExpression.rightOperand, isNotNull); | 4186 expect(binaryExpression.rightOperand, isNotNull); |
| 4174 } | 4187 } |
| (...skipping 1437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5612 expect(expression, isNotNull); | 5625 expect(expression, isNotNull); |
| 5613 assertNoErrors(); | 5626 assertNoErrors(); |
| 5614 var methodInvocation = expression as MethodInvocation; | 5627 var methodInvocation = expression as MethodInvocation; |
| 5615 expect(methodInvocation.target, isNotNull); | 5628 expect(methodInvocation.target, isNotNull); |
| 5616 expect(methodInvocation.operator.type, TokenType.PERIOD); | 5629 expect(methodInvocation.operator.type, TokenType.PERIOD); |
| 5617 expect(methodInvocation.methodName, isNotNull); | 5630 expect(methodInvocation.methodName, isNotNull); |
| 5618 expect(methodInvocation.typeArguments, isNull); | 5631 expect(methodInvocation.typeArguments, isNull); |
| 5619 expect(methodInvocation.argumentList, isNotNull); | 5632 expect(methodInvocation.argumentList, isNotNull); |
| 5620 } | 5633 } |
| 5621 | 5634 |
| 5622 void test_namedArgument() { | |
| 5623 var invocation = parseExpression('m(a: 1, b: 2)') as MethodInvocation; | |
| 5624 List<Expression> arguments = invocation.argumentList.arguments; | |
| 5625 | |
| 5626 var a = arguments[0] as NamedExpression; | |
| 5627 expect(a.name.label.name, 'a'); | |
| 5628 expect(a.expression, isNotNull); | |
| 5629 | |
| 5630 var b = arguments[1] as NamedExpression; | |
| 5631 expect(b.name.label.name, 'b'); | |
| 5632 expect(b.expression, isNotNull); | |
| 5633 } | |
| 5634 | |
| 5635 void test_parsePostfixExpression_none_methodInvocation_question_dot() { | 5635 void test_parsePostfixExpression_none_methodInvocation_question_dot() { |
| 5636 Expression expression = parsePostfixExpression('a?.m()'); | 5636 Expression expression = parsePostfixExpression('a?.m()'); |
| 5637 expect(expression, isNotNull); | 5637 expect(expression, isNotNull); |
| 5638 assertNoErrors(); | 5638 assertNoErrors(); |
| 5639 var methodInvocation = expression as MethodInvocation; | 5639 var methodInvocation = expression as MethodInvocation; |
| 5640 expect(methodInvocation.target, isNotNull); | 5640 expect(methodInvocation.target, isNotNull); |
| 5641 expect(methodInvocation.operator.type, TokenType.QUESTION_PERIOD); | 5641 expect(methodInvocation.operator.type, TokenType.QUESTION_PERIOD); |
| 5642 expect(methodInvocation.methodName, isNotNull); | 5642 expect(methodInvocation.methodName, isNotNull); |
| 5643 expect(methodInvocation.typeArguments, isNull); | 5643 expect(methodInvocation.typeArguments, isNull); |
| 5644 expect(methodInvocation.argumentList, isNotNull); | 5644 expect(methodInvocation.argumentList, isNotNull); |
| (...skipping 5449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 11094 | 11094 |
| 11095 void test_parseFunctionBody_skip_expression() { | 11095 void test_parseFunctionBody_skip_expression() { |
| 11096 ParserTestCase.parseFunctionBodies = false; | 11096 ParserTestCase.parseFunctionBodies = false; |
| 11097 createParser('=> y;'); | 11097 createParser('=> y;'); |
| 11098 FunctionBody functionBody = parser.parseFunctionBody(false, null, false); | 11098 FunctionBody functionBody = parser.parseFunctionBody(false, null, false); |
| 11099 expectNotNullIfNoErrors(functionBody); | 11099 expectNotNullIfNoErrors(functionBody); |
| 11100 listener.assertNoErrors(); | 11100 listener.assertNoErrors(); |
| 11101 expect(functionBody, new isInstanceOf<EmptyFunctionBody>()); | 11101 expect(functionBody, new isInstanceOf<EmptyFunctionBody>()); |
| 11102 } | 11102 } |
| 11103 | 11103 |
| 11104 void test_parseFunctionDeclarationStatement() { | |
| 11105 createParser('void f(int p) => p * 2;'); | |
| 11106 FunctionDeclarationStatement statement = | |
| 11107 parser.parseFunctionDeclarationStatement(); | |
| 11108 expectNotNullIfNoErrors(statement); | |
| 11109 listener.assertNoErrors(); | |
| 11110 expect(statement.functionDeclaration, isNotNull); | |
| 11111 } | |
| 11112 | |
| 11113 void test_parseFunctionDeclarationStatement_typeParameterComments() { | |
| 11114 enableGenericMethodComments = true; | |
| 11115 createParser('/*=E*/ f/*<E>*/(/*=E*/ p) => p * 2;'); | |
| 11116 FunctionDeclarationStatement statement = | |
| 11117 parser.parseFunctionDeclarationStatement(); | |
| 11118 expectNotNullIfNoErrors(statement); | |
| 11119 listener.assertNoErrors(); | |
| 11120 FunctionDeclaration f = statement.functionDeclaration; | |
| 11121 expect(f, isNotNull); | |
| 11122 expect(f.functionExpression.typeParameters, isNotNull); | |
| 11123 expect(f.returnType, isNotNull); | |
| 11124 SimpleFormalParameter p = f.functionExpression.parameters.parameters[0]; | |
| 11125 expect(p.type, isNotNull); | |
| 11126 } | |
| 11127 | |
| 11128 void test_parseFunctionDeclarationStatement_typeParameters() { | |
| 11129 createParser('E f<E>(E p) => p * 2;'); | |
| 11130 FunctionDeclarationStatement statement = | |
| 11131 parser.parseFunctionDeclarationStatement(); | |
| 11132 expectNotNullIfNoErrors(statement); | |
| 11133 listener.assertNoErrors(); | |
| 11134 expect(statement.functionDeclaration, isNotNull); | |
| 11135 expect(statement.functionDeclaration.functionExpression.typeParameters, | |
| 11136 isNotNull); | |
| 11137 } | |
| 11138 | |
| 11139 void test_parseFunctionDeclarationStatement_typeParameters_noReturnType() { | |
| 11140 createParser('f<E>(E p) => p * 2;'); | |
| 11141 FunctionDeclarationStatement statement = | |
| 11142 parser.parseFunctionDeclarationStatement(); | |
| 11143 expectNotNullIfNoErrors(statement); | |
| 11144 listener.assertNoErrors(); | |
| 11145 expect(statement.functionDeclaration, isNotNull); | |
| 11146 expect(statement.functionDeclaration.functionExpression.typeParameters, | |
| 11147 isNotNull); | |
| 11148 } | |
| 11149 | |
| 11150 void test_parseIdentifierList_multiple() { | 11104 void test_parseIdentifierList_multiple() { |
| 11151 createParser('a, b, c'); | 11105 createParser('a, b, c'); |
| 11152 List<SimpleIdentifier> list = parser.parseIdentifierList(); | 11106 List<SimpleIdentifier> list = parser.parseIdentifierList(); |
| 11153 expectNotNullIfNoErrors(list); | 11107 expectNotNullIfNoErrors(list); |
| 11154 listener.assertNoErrors(); | 11108 listener.assertNoErrors(); |
| 11155 expect(list, hasLength(3)); | 11109 expect(list, hasLength(3)); |
| 11156 } | 11110 } |
| 11157 | 11111 |
| 11158 void test_parseIdentifierList_single() { | 11112 void test_parseIdentifierList_single() { |
| 11159 createParser('a'); | 11113 createParser('a'); |
| (...skipping 1349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12509 expect(forStatement.variables, isNull); | 12463 expect(forStatement.variables, isNull); |
| 12510 expect(forStatement.initialization, isNull); | 12464 expect(forStatement.initialization, isNull); |
| 12511 expect(forStatement.leftSeparator, isNotNull); | 12465 expect(forStatement.leftSeparator, isNotNull); |
| 12512 expect(forStatement.condition, isNull); | 12466 expect(forStatement.condition, isNull); |
| 12513 expect(forStatement.rightSeparator, isNotNull); | 12467 expect(forStatement.rightSeparator, isNotNull); |
| 12514 expect(forStatement.updaters, hasLength(1)); | 12468 expect(forStatement.updaters, hasLength(1)); |
| 12515 expect(forStatement.rightParenthesis, isNotNull); | 12469 expect(forStatement.rightParenthesis, isNotNull); |
| 12516 expect(forStatement.body, isNotNull); | 12470 expect(forStatement.body, isNotNull); |
| 12517 } | 12471 } |
| 12518 | 12472 |
| 12473 void test_parseFunctionDeclarationStatement() { |
| 12474 var statement = parseStatement('void f(int p) => p * 2;') |
| 12475 as FunctionDeclarationStatement; |
| 12476 assertNoErrors(); |
| 12477 expect(statement.functionDeclaration, isNotNull); |
| 12478 } |
| 12479 |
| 12480 void test_parseFunctionDeclarationStatement_typeParameterComments() { |
| 12481 enableGenericMethodComments = true; |
| 12482 var statement = parseStatement('/*=E*/ f/*<E>*/(/*=E*/ p) => p * 2;') |
| 12483 as FunctionDeclarationStatement; |
| 12484 assertNoErrors(); |
| 12485 FunctionDeclaration f = statement.functionDeclaration; |
| 12486 expect(f, isNotNull); |
| 12487 expect(f.functionExpression.typeParameters, isNotNull); |
| 12488 expect(f.returnType, isNotNull); |
| 12489 SimpleFormalParameter p = f.functionExpression.parameters.parameters[0]; |
| 12490 expect(p.type, isNotNull); |
| 12491 } |
| 12492 |
| 12493 void test_parseFunctionDeclarationStatement_typeParameters() { |
| 12494 var statement = |
| 12495 parseStatement('E f<E>(E p) => p * 2;') as FunctionDeclarationStatement; |
| 12496 assertNoErrors(); |
| 12497 expect(statement.functionDeclaration, isNotNull); |
| 12498 expect(statement.functionDeclaration.functionExpression.typeParameters, |
| 12499 isNotNull); |
| 12500 } |
| 12501 |
| 12502 void test_parseFunctionDeclarationStatement_typeParameters_noReturnType() { |
| 12503 var statement = |
| 12504 parseStatement('f<E>(E p) => p * 2;') as FunctionDeclarationStatement; |
| 12505 assertNoErrors(); |
| 12506 expect(statement.functionDeclaration, isNotNull); |
| 12507 expect(statement.functionDeclaration.functionExpression.typeParameters, |
| 12508 isNotNull); |
| 12509 } |
| 12510 |
| 12519 void test_parseIfStatement_else_block() { | 12511 void test_parseIfStatement_else_block() { |
| 12520 var statement = parseStatement('if (x) {} else {}') as IfStatement; | 12512 var statement = parseStatement('if (x) {} else {}') as IfStatement; |
| 12521 assertNoErrors(); | 12513 assertNoErrors(); |
| 12522 expect(statement.ifKeyword, isNotNull); | 12514 expect(statement.ifKeyword, isNotNull); |
| 12523 expect(statement.leftParenthesis, isNotNull); | 12515 expect(statement.leftParenthesis, isNotNull); |
| 12524 expect(statement.condition, isNotNull); | 12516 expect(statement.condition, isNotNull); |
| 12525 expect(statement.rightParenthesis, isNotNull); | 12517 expect(statement.rightParenthesis, isNotNull); |
| 12526 expect(statement.thenStatement, isNotNull); | 12518 expect(statement.thenStatement, isNotNull); |
| 12527 expect(statement.elseKeyword, isNotNull); | 12519 expect(statement.elseKeyword, isNotNull); |
| 12528 expect(statement.elseStatement, isNotNull); | 12520 expect(statement.elseStatement, isNotNull); |
| (...skipping 2251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 14780 expectCommentText(typeVariable.documentationComment, '/// Doc'); | 14772 expectCommentText(typeVariable.documentationComment, '/// Doc'); |
| 14781 } | 14773 } |
| 14782 | 14774 |
| 14783 /** | 14775 /** |
| 14784 * Assert that the given [name] is in declaration context. | 14776 * Assert that the given [name] is in declaration context. |
| 14785 */ | 14777 */ |
| 14786 void _assertIsDeclarationName(SimpleIdentifier name) { | 14778 void _assertIsDeclarationName(SimpleIdentifier name) { |
| 14787 expect(name.inDeclarationContext(), isTrue); | 14779 expect(name.inDeclarationContext(), isTrue); |
| 14788 } | 14780 } |
| 14789 } | 14781 } |
| OLD | NEW |