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

Side by Side Diff: pkg/analyzer/test/generated/parser_test.dart

Issue 1239513005: Switch on null-aware operators by default in analyzer. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 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) 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 engine.parser_test; 5 library engine.parser_test;
6 6
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/engine.dart'; 9 import 'package:analyzer/src/generated/engine.dart';
10 import 'package:analyzer/src/generated/error.dart'; 10 import 'package:analyzer/src/generated/error.dart';
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 Expression lhs = (section as AssignmentExpression).leftHandSide; 352 Expression lhs = (section as AssignmentExpression).leftHandSide;
353 EngineTestCase.assertInstanceOf( 353 EngineTestCase.assertInstanceOf(
354 (obj) => obj is IndexExpression, IndexExpression, lhs); 354 (obj) => obj is IndexExpression, IndexExpression, lhs);
355 IndexExpression index = lhs as IndexExpression; 355 IndexExpression index = lhs as IndexExpression;
356 expect(index.isCascaded, isTrue); 356 expect(index.isCascaded, isTrue);
357 expect(index.realTarget, same(target)); 357 expect(index.realTarget, same(target));
358 } 358 }
359 } 359 }
360 360
361 void test_conditionalExpression_precedence_ifNullExpression() { 361 void test_conditionalExpression_precedence_ifNullExpression() {
362 _enableNullAwareOperators = true;
363 ConditionalExpression expression = parseExpression('a ?? b ? y : z'); 362 ConditionalExpression expression = parseExpression('a ?? b ? y : z');
364 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression, 363 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
365 BinaryExpression, expression.condition); 364 BinaryExpression, expression.condition);
366 } 365 }
367 366
368 void test_conditionalExpression_precedence_logicalOrExpression() { 367 void test_conditionalExpression_precedence_logicalOrExpression() {
369 ConditionalExpression expression = parseExpression("a | b ? y : z"); 368 ConditionalExpression expression = parseExpression("a | b ? y : z");
370 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression, 369 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
371 BinaryExpression, expression.condition); 370 BinaryExpression, expression.condition);
372 } 371 }
(...skipping 29 matching lines...) Expand all
402 } 401 }
403 402
404 void test_equalityExpression_super() { 403 void test_equalityExpression_super() {
405 BinaryExpression expression = parseExpression("super == y != z", 404 BinaryExpression expression = parseExpression("super == y != z",
406 [ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND]); 405 [ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND]);
407 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression, 406 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
408 BinaryExpression, expression.leftOperand); 407 BinaryExpression, expression.leftOperand);
409 } 408 }
410 409
411 void test_ifNullExpression() { 410 void test_ifNullExpression() {
412 _enableNullAwareOperators = true;
413 BinaryExpression expression = parseExpression('x ?? y ?? z'); 411 BinaryExpression expression = parseExpression('x ?? y ?? z');
414 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression, 412 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
415 BinaryExpression, expression.leftOperand); 413 BinaryExpression, expression.leftOperand);
416 } 414 }
417 415
418 void test_ifNullExpression_precedence_logicalOr_left() { 416 void test_ifNullExpression_precedence_logicalOr_left() {
419 _enableNullAwareOperators = true;
420 BinaryExpression expression = parseExpression('x || y ?? z'); 417 BinaryExpression expression = parseExpression('x || y ?? z');
421 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression, 418 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
422 BinaryExpression, expression.leftOperand); 419 BinaryExpression, expression.leftOperand);
423 } 420 }
424 421
425 void test_ifNullExpression_precendce_logicalOr_right() { 422 void test_ifNullExpression_precendce_logicalOr_right() {
426 _enableNullAwareOperators = true;
427 BinaryExpression expression = parseExpression('x ?? y || z'); 423 BinaryExpression expression = parseExpression('x ?? y || z');
428 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression, 424 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
429 BinaryExpression, expression.rightOperand); 425 BinaryExpression, expression.rightOperand);
430 } 426 }
431 427
432 void test_logicalAndExpression() { 428 void test_logicalAndExpression() {
433 BinaryExpression expression = parseExpression("x && y && z"); 429 BinaryExpression expression = parseExpression("x && y && z");
434 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression, 430 EngineTestCase.assertInstanceOf((obj) => obj is BinaryExpression,
435 BinaryExpression, expression.leftOperand); 431 BinaryExpression, expression.leftOperand);
436 } 432 }
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after
1331 void test_invalidInterpolationIdentifier_startWithDigit() { 1327 void test_invalidInterpolationIdentifier_startWithDigit() {
1332 parse4("parseStringLiteral", "'\$1'", [ParserErrorCode.MISSING_IDENTIFIER]); 1328 parse4("parseStringLiteral", "'\$1'", [ParserErrorCode.MISSING_IDENTIFIER]);
1333 } 1329 }
1334 1330
1335 void test_invalidOperator() { 1331 void test_invalidOperator() {
1336 parse3("parseClassMember", <Object>["C"], "void operator ===(x) {}", 1332 parse3("parseClassMember", <Object>["C"], "void operator ===(x) {}",
1337 [ParserErrorCode.INVALID_OPERATOR]); 1333 [ParserErrorCode.INVALID_OPERATOR]);
1338 } 1334 }
1339 1335
1340 void test_invalidOperatorAfterSuper_assignableExpression() { 1336 void test_invalidOperatorAfterSuper_assignableExpression() {
1341 _enableNullAwareOperators = true;
1342 parse3('parseAssignableExpression', <Object>[false], 'super?.v', 1337 parse3('parseAssignableExpression', <Object>[false], 'super?.v',
1343 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]); 1338 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]);
1344 } 1339 }
1345 1340
1346 void test_invalidOperatorAfterSuper_primaryExpression() { 1341 void test_invalidOperatorAfterSuper_primaryExpression() {
1347 _enableNullAwareOperators = true;
1348 parse4('parsePrimaryExpression', 'super?.v', 1342 parse4('parsePrimaryExpression', 'super?.v',
1349 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]); 1343 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]);
1350 } 1344 }
1351 1345
1352 void test_invalidOperatorForSuper() { 1346 void test_invalidOperatorForSuper() {
1353 parse4("parseUnaryExpression", "++super", 1347 parse4("parseUnaryExpression", "++super",
1354 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]); 1348 [ParserErrorCode.INVALID_OPERATOR_FOR_SUPER]);
1355 } 1349 }
1356 1350
1357 void test_invalidStarAfterAsync() { 1351 void test_invalidStarAfterAsync() {
(...skipping 1221 matching lines...) Expand 10 before | Expand all | Expand 10 after
2579 */ 2573 */
2580 static bool parseFunctionBodies = true; 2574 static bool parseFunctionBodies = true;
2581 2575
2582 /** 2576 /**
2583 * A flag indicating whether generic method support should be enabled for a 2577 * A flag indicating whether generic method support should be enabled for a
2584 * specific test. 2578 * specific test.
2585 */ 2579 */
2586 bool enableGenericMethods = false; 2580 bool enableGenericMethods = false;
2587 2581
2588 /** 2582 /**
2589 * If non-null, this value is used to override the default value of
2590 * [Scanner.enableNullAwareOperators] before scanning.
2591 */
2592 bool _enableNullAwareOperators;
2593
2594 /**
2595 * Return a CommentAndMetadata object with the given values that can be used f or testing. 2583 * Return a CommentAndMetadata object with the given values that can be used f or testing.
2596 * 2584 *
2597 * @param comment the comment to be wrapped in the object 2585 * @param comment the comment to be wrapped in the object
2598 * @param annotations the annotations to be wrapped in the object 2586 * @param annotations the annotations to be wrapped in the object
2599 * @return a CommentAndMetadata object that can be used for testing 2587 * @return a CommentAndMetadata object that can be used for testing
2600 */ 2588 */
2601 CommentAndMetadata commentAndMetadata(Comment comment, 2589 CommentAndMetadata commentAndMetadata(Comment comment,
2602 [List<Annotation> annotations]) { 2590 [List<Annotation> annotations]) {
2603 return new CommentAndMetadata(comment, annotations); 2591 return new CommentAndMetadata(comment, annotations);
2604 } 2592 }
(...skipping 22 matching lines...) Expand all
2627 * @throws AssertionFailedError if the result is `null` or the errors produced while 2615 * @throws AssertionFailedError if the result is `null` or the errors produced while
2628 * scanning and parsing the source do not match the expected errors 2616 * scanning and parsing the source do not match the expected errors
2629 */ 2617 */
2630 Object invokeParserMethod(String methodName, List<Object> objects, 2618 Object invokeParserMethod(String methodName, List<Object> objects,
2631 String source, GatheringErrorListener listener) { 2619 String source, GatheringErrorListener listener) {
2632 // 2620 //
2633 // Scan the source. 2621 // Scan the source.
2634 // 2622 //
2635 Scanner scanner = 2623 Scanner scanner =
2636 new Scanner(null, new CharSequenceReader(source), listener); 2624 new Scanner(null, new CharSequenceReader(source), listener);
2637 if (_enableNullAwareOperators != null) {
2638 scanner.enableNullAwareOperators = _enableNullAwareOperators;
2639 }
2640 Token tokenStream = scanner.tokenize(); 2625 Token tokenStream = scanner.tokenize();
2641 listener.setLineInfo(new TestSource(), scanner.lineStarts); 2626 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2642 // 2627 //
2643 // Parse the source. 2628 // Parse the source.
2644 // 2629 //
2645 Parser parser = createParser(listener); 2630 Parser parser = createParser(listener);
2646 parser.parseGenericMethods = enableGenericMethods; 2631 parser.parseGenericMethods = enableGenericMethods;
2647 parser.parseFunctionBodies = parseFunctionBodies; 2632 parser.parseFunctionBodies = parseFunctionBodies;
2648 Object result = 2633 Object result =
2649 invokeParserMethodImpl(parser, methodName, objects, tokenStream); 2634 invokeParserMethodImpl(parser, methodName, objects, tokenStream);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2764 * @param errorCodes the error codes of the errors that are expected to be fou nd 2749 * @param errorCodes the error codes of the errors that are expected to be fou nd
2765 * @return the expression that was parsed 2750 * @return the expression that was parsed
2766 * @throws Exception if the source could not be parsed, if the compilation err ors in the source do 2751 * @throws Exception if the source could not be parsed, if the compilation err ors in the source do
2767 * not match those that are expected, or if the result would have be en `null` 2752 * not match those that are expected, or if the result would have be en `null`
2768 */ 2753 */
2769 Expression parseExpression(String source, 2754 Expression parseExpression(String source,
2770 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) { 2755 [List<ErrorCode> errorCodes = ErrorCode.EMPTY_LIST]) {
2771 GatheringErrorListener listener = new GatheringErrorListener(); 2756 GatheringErrorListener listener = new GatheringErrorListener();
2772 Scanner scanner = 2757 Scanner scanner =
2773 new Scanner(null, new CharSequenceReader(source), listener); 2758 new Scanner(null, new CharSequenceReader(source), listener);
2774 if (_enableNullAwareOperators != null) {
2775 scanner.enableNullAwareOperators = _enableNullAwareOperators;
2776 }
2777 listener.setLineInfo(new TestSource(), scanner.lineStarts); 2759 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2778 Token token = scanner.tokenize(); 2760 Token token = scanner.tokenize();
2779 Parser parser = createParser(listener); 2761 Parser parser = createParser(listener);
2780 parser.parseGenericMethods = enableGenericMethods; 2762 parser.parseGenericMethods = enableGenericMethods;
2781 Expression expression = parser.parseExpression(token); 2763 Expression expression = parser.parseExpression(token);
2782 expect(expression, isNotNull); 2764 expect(expression, isNotNull);
2783 listener.assertErrorsWithCodes(errorCodes); 2765 listener.assertErrorsWithCodes(errorCodes);
2784 return expression; 2766 return expression;
2785 } 2767 }
2786 2768
(...skipping 2282 matching lines...) Expand 10 before | Expand all | Expand 10 after
5069 void test_parseAssignableExpression_expression_index() { 5051 void test_parseAssignableExpression_expression_index() {
5070 IndexExpression expression = 5052 IndexExpression expression =
5071 parse("parseAssignableExpression", <Object>[false], "(x)[y]"); 5053 parse("parseAssignableExpression", <Object>[false], "(x)[y]");
5072 expect(expression.target, isNotNull); 5054 expect(expression.target, isNotNull);
5073 expect(expression.leftBracket, isNotNull); 5055 expect(expression.leftBracket, isNotNull);
5074 expect(expression.index, isNotNull); 5056 expect(expression.index, isNotNull);
5075 expect(expression.rightBracket, isNotNull); 5057 expect(expression.rightBracket, isNotNull);
5076 } 5058 }
5077 5059
5078 void test_parseAssignableExpression_expression_question_dot() { 5060 void test_parseAssignableExpression_expression_question_dot() {
5079 _enableNullAwareOperators = true;
5080 PropertyAccess propertyAccess = 5061 PropertyAccess propertyAccess =
5081 parse("parseAssignableExpression", <Object>[false], "(x)?.y"); 5062 parse("parseAssignableExpression", <Object>[false], "(x)?.y");
5082 expect(propertyAccess.target, isNotNull); 5063 expect(propertyAccess.target, isNotNull);
5083 expect(propertyAccess.operator.type, TokenType.QUESTION_PERIOD); 5064 expect(propertyAccess.operator.type, TokenType.QUESTION_PERIOD);
5084 expect(propertyAccess.propertyName, isNotNull); 5065 expect(propertyAccess.propertyName, isNotNull);
5085 } 5066 }
5086 5067
5087 void test_parseAssignableExpression_identifier() { 5068 void test_parseAssignableExpression_identifier() {
5088 SimpleIdentifier identifier = 5069 SimpleIdentifier identifier =
5089 parse("parseAssignableExpression", <Object>[false], "x"); 5070 parse("parseAssignableExpression", <Object>[false], "x");
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
5129 void test_parseAssignableExpression_identifier_index() { 5110 void test_parseAssignableExpression_identifier_index() {
5130 IndexExpression expression = 5111 IndexExpression expression =
5131 parse("parseAssignableExpression", <Object>[false], "x[y]"); 5112 parse("parseAssignableExpression", <Object>[false], "x[y]");
5132 expect(expression.target, isNotNull); 5113 expect(expression.target, isNotNull);
5133 expect(expression.leftBracket, isNotNull); 5114 expect(expression.leftBracket, isNotNull);
5134 expect(expression.index, isNotNull); 5115 expect(expression.index, isNotNull);
5135 expect(expression.rightBracket, isNotNull); 5116 expect(expression.rightBracket, isNotNull);
5136 } 5117 }
5137 5118
5138 void test_parseAssignableExpression_identifier_question_dot() { 5119 void test_parseAssignableExpression_identifier_question_dot() {
5139 _enableNullAwareOperators = true;
5140 PropertyAccess propertyAccess = 5120 PropertyAccess propertyAccess =
5141 parse("parseAssignableExpression", <Object>[false], "x?.y"); 5121 parse("parseAssignableExpression", <Object>[false], "x?.y");
5142 expect(propertyAccess.target, isNotNull); 5122 expect(propertyAccess.target, isNotNull);
5143 expect(propertyAccess.operator.type, TokenType.QUESTION_PERIOD); 5123 expect(propertyAccess.operator.type, TokenType.QUESTION_PERIOD);
5144 expect(propertyAccess.propertyName, isNotNull); 5124 expect(propertyAccess.propertyName, isNotNull);
5145 } 5125 }
5146 5126
5147 void test_parseAssignableExpression_super_dot() { 5127 void test_parseAssignableExpression_super_dot() {
5148 PropertyAccess propertyAccess = 5128 PropertyAccess propertyAccess =
5149 parse("parseAssignableExpression", <Object>[false], "super.y"); 5129 parse("parseAssignableExpression", <Object>[false], "super.y");
(...skipping 30 matching lines...) Expand all
5180 5160
5181 void test_parseAssignableSelector_none() { 5161 void test_parseAssignableSelector_none() {
5182 SimpleIdentifier selector = parse("parseAssignableSelector", <Object>[ 5162 SimpleIdentifier selector = parse("parseAssignableSelector", <Object>[
5183 new SimpleIdentifier(null), 5163 new SimpleIdentifier(null),
5184 true 5164 true
5185 ], ";"); 5165 ], ";");
5186 expect(selector, isNotNull); 5166 expect(selector, isNotNull);
5187 } 5167 }
5188 5168
5189 void test_parseAssignableSelector_question_dot() { 5169 void test_parseAssignableSelector_question_dot() {
5190 _enableNullAwareOperators = true;
5191 PropertyAccess selector = 5170 PropertyAccess selector =
5192 parse("parseAssignableSelector", <Object>[null, true], "?.x"); 5171 parse("parseAssignableSelector", <Object>[null, true], "?.x");
5193 expect(selector.operator.type, TokenType.QUESTION_PERIOD); 5172 expect(selector.operator.type, TokenType.QUESTION_PERIOD);
5194 expect(selector.propertyName, isNotNull); 5173 expect(selector.propertyName, isNotNull);
5195 } 5174 }
5196 5175
5197 void test_parseAwaitExpression() { 5176 void test_parseAwaitExpression() {
5198 AwaitExpression expression = parse4("parseAwaitExpression", "await x;"); 5177 AwaitExpression expression = parse4("parseAwaitExpression", "await x;");
5199 expect(expression.awaitKeyword, isNotNull); 5178 expect(expression.awaitKeyword, isNotNull);
5200 expect(expression.expression, isNotNull); 5179 expect(expression.expression, isNotNull);
(...skipping 3850 matching lines...) Expand 10 before | Expand all | Expand 10 after
9051 void test_parsePostfixExpression_none_methodInvocation() { 9030 void test_parsePostfixExpression_none_methodInvocation() {
9052 MethodInvocation expression = parse4("parsePostfixExpression", "a.m()"); 9031 MethodInvocation expression = parse4("parsePostfixExpression", "a.m()");
9053 expect(expression.target, isNotNull); 9032 expect(expression.target, isNotNull);
9054 expect(expression.operator.type, TokenType.PERIOD); 9033 expect(expression.operator.type, TokenType.PERIOD);
9055 expect(expression.methodName, isNotNull); 9034 expect(expression.methodName, isNotNull);
9056 expect(expression.typeArguments, isNull); 9035 expect(expression.typeArguments, isNull);
9057 expect(expression.argumentList, isNotNull); 9036 expect(expression.argumentList, isNotNull);
9058 } 9037 }
9059 9038
9060 void test_parsePostfixExpression_none_methodInvocation_question_dot() { 9039 void test_parsePostfixExpression_none_methodInvocation_question_dot() {
9061 _enableNullAwareOperators = true;
9062 MethodInvocation expression = parse4('parsePostfixExpression', 'a?.m()'); 9040 MethodInvocation expression = parse4('parsePostfixExpression', 'a?.m()');
9063 expect(expression.target, isNotNull); 9041 expect(expression.target, isNotNull);
9064 expect(expression.operator.type, TokenType.QUESTION_PERIOD); 9042 expect(expression.operator.type, TokenType.QUESTION_PERIOD);
9065 expect(expression.methodName, isNotNull); 9043 expect(expression.methodName, isNotNull);
9066 expect(expression.typeArguments, isNull); 9044 expect(expression.typeArguments, isNull);
9067 expect(expression.argumentList, isNotNull); 9045 expect(expression.argumentList, isNotNull);
9068 } 9046 }
9069 9047
9070 void test_parsePostfixExpression_none_methodInvocation_question_dot_typeArgume nts() { 9048 void test_parsePostfixExpression_none_methodInvocation_question_dot_typeArgume nts() {
9071 enableGenericMethods = true; 9049 enableGenericMethods = true;
9072 _enableNullAwareOperators = true;
9073 MethodInvocation expression = parse4('parsePostfixExpression', 'a?.m<E>()'); 9050 MethodInvocation expression = parse4('parsePostfixExpression', 'a?.m<E>()');
9074 expect(expression.target, isNotNull); 9051 expect(expression.target, isNotNull);
9075 expect(expression.operator.type, TokenType.QUESTION_PERIOD); 9052 expect(expression.operator.type, TokenType.QUESTION_PERIOD);
9076 expect(expression.methodName, isNotNull); 9053 expect(expression.methodName, isNotNull);
9077 expect(expression.typeArguments, isNotNull); 9054 expect(expression.typeArguments, isNotNull);
9078 expect(expression.argumentList, isNotNull); 9055 expect(expression.argumentList, isNotNull);
9079 } 9056 }
9080 9057
9081 void test_parsePostfixExpression_none_methodInvocation_typeArguments() { 9058 void test_parsePostfixExpression_none_methodInvocation_typeArguments() {
9082 enableGenericMethods = true; 9059 enableGenericMethods = true;
(...skipping 1591 matching lines...) Expand 10 before | Expand all | Expand 10 after
10674 new Scanner(null, new CharSequenceReader(source), listener); 10651 new Scanner(null, new CharSequenceReader(source), listener);
10675 Token tokenStream = scanner.tokenize(); 10652 Token tokenStream = scanner.tokenize();
10676 // 10653 //
10677 // Parse the source. 10654 // Parse the source.
10678 // 10655 //
10679 Parser parser = new Parser(null, listener); 10656 Parser parser = new Parser(null, listener);
10680 return invokeParserMethodImpl( 10657 return invokeParserMethodImpl(
10681 parser, methodName, <Object>[tokenStream], tokenStream) as Token; 10658 parser, methodName, <Object>[tokenStream], tokenStream) as Token;
10682 } 10659 }
10683 } 10660 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/generated/non_error_resolver_test.dart ('k') | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698