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

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

Issue 1406253004: Lexical support for configurable imports (DEP 40) (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 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';
11 import 'package:analyzer/src/generated/incremental_scanner.dart'; 11 import 'package:analyzer/src/generated/incremental_scanner.dart';
12 import 'package:analyzer/src/generated/parser.dart'; 12 import 'package:analyzer/src/generated/parser.dart';
13 import 'package:analyzer/src/generated/scanner.dart'; 13 import 'package:analyzer/src/generated/scanner.dart';
14 import 'package:analyzer/src/generated/source.dart' show Source; 14 import 'package:analyzer/src/generated/source.dart' show Source;
15 import 'package:analyzer/src/generated/testing/ast_factory.dart'; 15 import 'package:analyzer/src/generated/testing/ast_factory.dart';
16 import 'package:analyzer/src/generated/testing/element_factory.dart'; 16 import 'package:analyzer/src/generated/testing/element_factory.dart';
17 import 'package:analyzer/src/generated/testing/token_factory.dart'; 17 import 'package:analyzer/src/generated/testing/token_factory.dart';
18 import 'package:analyzer/src/generated/utilities_dart.dart'; 18 import 'package:analyzer/src/generated/utilities_dart.dart';
19 import 'package:unittest/unittest.dart'; 19 import 'package:unittest/unittest.dart' hide Configuration;
20 20
21 import '../reflective_tests.dart'; 21 import '../reflective_tests.dart';
22 import '../utils.dart'; 22 import '../utils.dart';
23 import 'test_support.dart'; 23 import 'test_support.dart';
24 24
25 main() { 25 main() {
26 initializeTestEnvironment(); 26 initializeTestEnvironment();
27 runReflectiveTests(ComplexParserTest); 27 runReflectiveTests(ComplexParserTest);
28 runReflectiveTests(ErrorParserTest); 28 runReflectiveTests(ErrorParserTest);
29 runReflectiveTests(IncrementalParserTest); 29 runReflectiveTests(IncrementalParserTest);
(...skipping 2553 matching lines...) Expand 10 before | Expand all | Expand 10 after
2583 * An empty list of objects used as arguments to zero-argument methods. 2583 * An empty list of objects used as arguments to zero-argument methods.
2584 */ 2584 */
2585 static const List<Object> _EMPTY_ARGUMENTS = const <Object>[]; 2585 static const List<Object> _EMPTY_ARGUMENTS = const <Object>[];
2586 2586
2587 /** 2587 /**
2588 * A flag indicating whether parser is to parse function bodies. 2588 * A flag indicating whether parser is to parse function bodies.
2589 */ 2589 */
2590 static bool parseFunctionBodies = true; 2590 static bool parseFunctionBodies = true;
2591 2591
2592 /** 2592 /**
2593 * A flag indicating whether conditional directives support should be enabled
2594 * for a specific test.
2595 */
2596 bool enableConditionalDirectives = false;
2597
2598 /**
2593 * A flag indicating whether generic method support should be enabled for a 2599 * A flag indicating whether generic method support should be enabled for a
2594 * specific test. 2600 * specific test.
2595 */ 2601 */
2596 bool enableGenericMethods = false; 2602 bool enableGenericMethods = false;
2597 2603
2598 /** 2604 /**
2599 * Return a CommentAndMetadata object with the given values that can be used f or testing. 2605 * Return a CommentAndMetadata object with the given values that can be used f or testing.
2600 * 2606 *
2601 * @param comment the comment to be wrapped in the object 2607 * @param comment the comment to be wrapped in the object
2602 * @param annotations the annotations to be wrapped in the object 2608 * @param annotations the annotations to be wrapped in the object
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
2637 // Scan the source. 2643 // Scan the source.
2638 // 2644 //
2639 Scanner scanner = 2645 Scanner scanner =
2640 new Scanner(null, new CharSequenceReader(source), listener); 2646 new Scanner(null, new CharSequenceReader(source), listener);
2641 Token tokenStream = scanner.tokenize(); 2647 Token tokenStream = scanner.tokenize();
2642 listener.setLineInfo(new TestSource(), scanner.lineStarts); 2648 listener.setLineInfo(new TestSource(), scanner.lineStarts);
2643 // 2649 //
2644 // Parse the source. 2650 // Parse the source.
2645 // 2651 //
2646 Parser parser = createParser(listener); 2652 Parser parser = createParser(listener);
2653 parser.parseConditionalDirectives = enableConditionalDirectives;
2647 parser.parseGenericMethods = enableGenericMethods; 2654 parser.parseGenericMethods = enableGenericMethods;
2648 parser.parseFunctionBodies = parseFunctionBodies; 2655 parser.parseFunctionBodies = parseFunctionBodies;
2649 Object result = 2656 Object result =
2650 invokeParserMethodImpl(parser, methodName, objects, tokenStream); 2657 invokeParserMethodImpl(parser, methodName, objects, tokenStream);
2651 // 2658 //
2652 // Partially test the results. 2659 // Partially test the results.
2653 // 2660 //
2654 if (!listener.hasErrors) { 2661 if (!listener.hasErrors) {
2655 expect(result, isNotNull); 2662 expect(result, isNotNull);
2656 } 2663 }
(...skipping 4223 matching lines...) Expand 10 before | Expand all | Expand 10 after
6880 void test_parseConditionalExpression() { 6887 void test_parseConditionalExpression() {
6881 ConditionalExpression expression = 6888 ConditionalExpression expression =
6882 parse4("parseConditionalExpression", "x ? y : z"); 6889 parse4("parseConditionalExpression", "x ? y : z");
6883 expect(expression.condition, isNotNull); 6890 expect(expression.condition, isNotNull);
6884 expect(expression.question, isNotNull); 6891 expect(expression.question, isNotNull);
6885 expect(expression.thenExpression, isNotNull); 6892 expect(expression.thenExpression, isNotNull);
6886 expect(expression.colon, isNotNull); 6893 expect(expression.colon, isNotNull);
6887 expect(expression.elseExpression, isNotNull); 6894 expect(expression.elseExpression, isNotNull);
6888 } 6895 }
6889 6896
6897 void test_parseConfiguration_noOperator_dottedIdentifier() {
6898 Configuration configuration =
6899 parse4('parseConfiguration', "if (a.b) 'c.dart'");
6900 expect(configuration.ifKeyword, isNotNull);
6901 expect(configuration.leftParenthesis, isNotNull);
6902 _expectDottedName(configuration.name, ["a", "b"]);
6903 expect(configuration.equalToken, isNull);
6904 expect(configuration.value, isNull);
6905 expect(configuration.rightParenthesis, isNotNull);
6906 expect(configuration.libraryUri, isNotNull);
6907 }
6908
6909 void test_parseConfiguration_noOperator_simpleIdentifier() {
6910 Configuration configuration =
6911 parse4('parseConfiguration', "if (a) 'b.dart'");
6912 expect(configuration.ifKeyword, isNotNull);
6913 expect(configuration.leftParenthesis, isNotNull);
6914 _expectDottedName(configuration.name, ["a"]);
6915 expect(configuration.equalToken, isNull);
6916 expect(configuration.value, isNull);
6917 expect(configuration.rightParenthesis, isNotNull);
6918 expect(configuration.libraryUri, isNotNull);
6919 }
6920
6921 void test_parseConfiguration_operator_dottedIdentifier() {
6922 Configuration configuration =
6923 parse4('parseConfiguration', "if (a.b == 'c') 'd.dart'");
6924 expect(configuration.ifKeyword, isNotNull);
6925 expect(configuration.leftParenthesis, isNotNull);
6926 _expectDottedName(configuration.name, ["a", "b"]);
6927 expect(configuration.equalToken, isNotNull);
6928 expect(configuration.value, isNotNull);
6929 expect(configuration.rightParenthesis, isNotNull);
6930 expect(configuration.libraryUri, isNotNull);
6931 }
6932
6933 void test_parseConfiguration_operator_simpleIdentifier() {
6934 Configuration configuration =
6935 parse4('parseConfiguration', "if (a == 'b') 'c.dart'");
6936 expect(configuration.ifKeyword, isNotNull);
6937 expect(configuration.leftParenthesis, isNotNull);
6938 _expectDottedName(configuration.name, ["a"]);
6939 expect(configuration.equalToken, isNotNull);
6940 expect(configuration.value, isNotNull);
6941 expect(configuration.rightParenthesis, isNotNull);
6942 expect(configuration.libraryUri, isNotNull);
6943 }
6944
6890 void test_parseConstExpression_instanceCreation() { 6945 void test_parseConstExpression_instanceCreation() {
6891 InstanceCreationExpression expression = 6946 InstanceCreationExpression expression =
6892 parse4("parseConstExpression", "const A()"); 6947 parse4("parseConstExpression", "const A()");
6893 expect(expression.keyword, isNotNull); 6948 expect(expression.keyword, isNotNull);
6894 ConstructorName name = expression.constructorName; 6949 ConstructorName name = expression.constructorName;
6895 expect(name, isNotNull); 6950 expect(name, isNotNull);
6896 expect(name.type, isNotNull); 6951 expect(name.type, isNotNull);
6897 expect(name.period, isNull); 6952 expect(name.period, isNull);
6898 expect(name.name, isNull); 6953 expect(name.name, isNull);
6899 expect(expression.argumentList, isNotNull); 6954 expect(expression.argumentList, isNotNull);
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
7145 DoStatement statement = parse4("parseDoStatement", "do {} while (x);"); 7200 DoStatement statement = parse4("parseDoStatement", "do {} while (x);");
7146 expect(statement.doKeyword, isNotNull); 7201 expect(statement.doKeyword, isNotNull);
7147 expect(statement.body, isNotNull); 7202 expect(statement.body, isNotNull);
7148 expect(statement.whileKeyword, isNotNull); 7203 expect(statement.whileKeyword, isNotNull);
7149 expect(statement.leftParenthesis, isNotNull); 7204 expect(statement.leftParenthesis, isNotNull);
7150 expect(statement.condition, isNotNull); 7205 expect(statement.condition, isNotNull);
7151 expect(statement.rightParenthesis, isNotNull); 7206 expect(statement.rightParenthesis, isNotNull);
7152 expect(statement.semicolon, isNotNull); 7207 expect(statement.semicolon, isNotNull);
7153 } 7208 }
7154 7209
7210 void test_parseDottedName_multiple() {
7211 DottedName name = parse4("parseDottedName", "a.b.c");
7212 _expectDottedName(name, ["a", "b", "c"]);
7213 }
7214
7215 void test_parseDottedName_single() {
7216 DottedName name = parse4("parseDottedName", "a");
7217 _expectDottedName(name, ["a"]);
7218 }
7219
7155 void test_parseEmptyStatement() { 7220 void test_parseEmptyStatement() {
7156 EmptyStatement statement = parse4("parseEmptyStatement", ";"); 7221 EmptyStatement statement = parse4("parseEmptyStatement", ";");
7157 expect(statement.semicolon, isNotNull); 7222 expect(statement.semicolon, isNotNull);
7158 } 7223 }
7159 7224
7160 void test_parseEnumDeclaration_one() { 7225 void test_parseEnumDeclaration_one() {
7161 EnumDeclaration declaration = parse("parseEnumDeclaration", 7226 EnumDeclaration declaration = parse("parseEnumDeclaration",
7162 <Object>[emptyCommentAndMetadata()], "enum E {ONE}"); 7227 <Object>[emptyCommentAndMetadata()], "enum E {ONE}");
7163 expect(declaration.documentationComment, isNull); 7228 expect(declaration.documentationComment, isNull);
7164 expect(declaration.enumKeyword, isNotNull); 7229 expect(declaration.enumKeyword, isNotNull);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
7201 void test_parseEqualityExpression_super() { 7266 void test_parseEqualityExpression_super() {
7202 BinaryExpression expression = 7267 BinaryExpression expression =
7203 parse4("parseEqualityExpression", "super == y"); 7268 parse4("parseEqualityExpression", "super == y");
7204 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression, 7269 EngineTestCase.assertInstanceOf((obj) => obj is SuperExpression,
7205 SuperExpression, expression.leftOperand); 7270 SuperExpression, expression.leftOperand);
7206 expect(expression.operator, isNotNull); 7271 expect(expression.operator, isNotNull);
7207 expect(expression.operator.type, TokenType.EQ_EQ); 7272 expect(expression.operator.type, TokenType.EQ_EQ);
7208 expect(expression.rightOperand, isNotNull); 7273 expect(expression.rightOperand, isNotNull);
7209 } 7274 }
7210 7275
7276 void test_parseExportDirective_configuration_multiple() {
7277 enableConditionalDirectives = true;
7278 ExportDirective directive = parse(
7279 "parseExportDirective",
7280 <Object>[emptyCommentAndMetadata()],
7281 "export 'lib/lib.dart' if (a) 'b.dart' if (c) 'd.dart';");
7282 expect(directive.keyword, isNotNull);
7283 expect(directive.uri, isNotNull);
7284 // TODO(brianwilkerson) Test the configurations.
Paul Berry 2015/10/21 17:10:47 I'd recommend resolving these testing TODOs before
Brian Wilkerson 2015/10/21 20:05:23 Done
7285 expect(directive.combinators, hasLength(0));
7286 expect(directive.semicolon, isNotNull);
7287 }
7288
7289 void test_parseExportDirective_configuration_single() {
7290 enableConditionalDirectives = true;
7291 ExportDirective directive = parse(
7292 "parseExportDirective",
7293 <Object>[emptyCommentAndMetadata()],
7294 "export 'lib/lib.dart' if (a.b == 'c.dart') '';");
7295 expect(directive.keyword, isNotNull);
7296 expect(directive.uri, isNotNull);
7297 // TODO(brianwilkerson) Test the configurations.
7298 expect(directive.combinators, hasLength(0));
7299 expect(directive.semicolon, isNotNull);
7300 }
7301
7211 void test_parseExportDirective_hide() { 7302 void test_parseExportDirective_hide() {
7212 ExportDirective directive = parse( 7303 ExportDirective directive = parse(
7213 "parseExportDirective", 7304 "parseExportDirective",
7214 <Object>[emptyCommentAndMetadata()], 7305 <Object>[emptyCommentAndMetadata()],
7215 "export 'lib/lib.dart' hide A, B;"); 7306 "export 'lib/lib.dart' hide A, B;");
7216 expect(directive.keyword, isNotNull); 7307 expect(directive.keyword, isNotNull);
7217 expect(directive.uri, isNotNull); 7308 expect(directive.uri, isNotNull);
7218 expect(directive.combinators, hasLength(1)); 7309 expect(directive.combinators, hasLength(1));
7219 expect(directive.semicolon, isNotNull); 7310 expect(directive.semicolon, isNotNull);
7220 } 7311 }
(...skipping 1090 matching lines...) Expand 10 before | Expand all | Expand 10 after
8311 expect(clause.interfaces, hasLength(3)); 8402 expect(clause.interfaces, hasLength(3));
8312 expect(clause.implementsKeyword, isNotNull); 8403 expect(clause.implementsKeyword, isNotNull);
8313 } 8404 }
8314 8405
8315 void test_parseImplementsClause_single() { 8406 void test_parseImplementsClause_single() {
8316 ImplementsClause clause = parse4("parseImplementsClause", "implements A"); 8407 ImplementsClause clause = parse4("parseImplementsClause", "implements A");
8317 expect(clause.interfaces, hasLength(1)); 8408 expect(clause.interfaces, hasLength(1));
8318 expect(clause.implementsKeyword, isNotNull); 8409 expect(clause.implementsKeyword, isNotNull);
8319 } 8410 }
8320 8411
8412 void test_parseImportDirective_configuration_multiple() {
8413 enableConditionalDirectives = true;
8414 ImportDirective directive = parse(
8415 "parseImportDirective",
8416 <Object>[emptyCommentAndMetadata()],
8417 "import 'lib/lib.dart' if (a) 'b.dart' if (c) 'd.dart';");
8418 expect(directive.keyword, isNotNull);
8419 expect(directive.uri, isNotNull);
8420 // TODO(brianwilkerson) Test the configurations.
8421 expect(directive.deferredKeyword, isNull);
8422 expect(directive.asKeyword, isNull);
8423 expect(directive.prefix, isNull);
8424 expect(directive.combinators, hasLength(0));
8425 expect(directive.semicolon, isNotNull);
8426 }
8427
8428 void test_parseImportDirective_configuration_single() {
8429 enableConditionalDirectives = true;
8430 ImportDirective directive = parse(
8431 "parseImportDirective",
8432 <Object>[emptyCommentAndMetadata()],
8433 "import 'lib/lib.dart' if (a.b == 'c.dart') '';");
8434 expect(directive.keyword, isNotNull);
8435 expect(directive.uri, isNotNull);
8436 // TODO(brianwilkerson) Test the configurations.
8437 expect(directive.deferredKeyword, isNull);
8438 expect(directive.asKeyword, isNull);
8439 expect(directive.prefix, isNull);
8440 expect(directive.combinators, hasLength(0));
8441 expect(directive.semicolon, isNotNull);
8442 }
8443
8321 void test_parseImportDirective_deferred() { 8444 void test_parseImportDirective_deferred() {
8322 ImportDirective directive = parse( 8445 ImportDirective directive = parse(
8323 "parseImportDirective", 8446 "parseImportDirective",
8324 <Object>[emptyCommentAndMetadata()], 8447 <Object>[emptyCommentAndMetadata()],
8325 "import 'lib/lib.dart' deferred as a;"); 8448 "import 'lib/lib.dart' deferred as a;");
8326 expect(directive.keyword, isNotNull); 8449 expect(directive.keyword, isNotNull);
8327 expect(directive.uri, isNotNull); 8450 expect(directive.uri, isNotNull);
8328 expect(directive.deferredKeyword, isNotNull); 8451 expect(directive.deferredKeyword, isNotNull);
8329 expect(directive.asKeyword, isNotNull); 8452 expect(directive.asKeyword, isNotNull);
8330 expect(directive.prefix, isNotNull); 8453 expect(directive.prefix, isNotNull);
(...skipping 2285 matching lines...) Expand 10 before | Expand all | Expand 10 after
10616 * 10739 *
10617 * @param source the source to be scanned to produce the token stream being te sted 10740 * @param source the source to be scanned to produce the token stream being te sted
10618 * @return the result of invoking the method 10741 * @return the result of invoking the method
10619 * @throws Exception if the method could not be invoked or throws an exception 10742 * @throws Exception if the method could not be invoked or throws an exception
10620 */ 10743 */
10621 SimpleStringLiteral _createSyntheticStringLiteral() { 10744 SimpleStringLiteral _createSyntheticStringLiteral() {
10622 GatheringErrorListener listener = new GatheringErrorListener(); 10745 GatheringErrorListener listener = new GatheringErrorListener();
10623 return invokeParserMethod2("createSyntheticStringLiteral", "", listener); 10746 return invokeParserMethod2("createSyntheticStringLiteral", "", listener);
10624 } 10747 }
10625 10748
10749 void _expectDottedName(DottedName name, List<String> expectedComponents) {
10750 int count = expectedComponents.length;
10751 NodeList<SimpleIdentifier> components = name.components;
10752 expect(components, hasLength(count));
10753 for (int i = 0; i < count; i++) {
10754 SimpleIdentifier component = components[i];
10755 expect(component, isNotNull);
10756 expect(component.name, expectedComponents[i]);
10757 }
10758 }
10759
10626 /** 10760 /**
10627 * Invoke the method [Parser.isFunctionDeclaration] with the parser set to the token 10761 * Invoke the method [Parser.isFunctionDeclaration] with the parser set to the token
10628 * stream produced by scanning the given source. 10762 * stream produced by scanning the given source.
10629 * 10763 *
10630 * @param source the source to be scanned to produce the token stream being te sted 10764 * @param source the source to be scanned to produce the token stream being te sted
10631 * @return the result of invoking the method 10765 * @return the result of invoking the method
10632 * @throws Exception if the method could not be invoked or throws an exception 10766 * @throws Exception if the method could not be invoked or throws an exception
10633 */ 10767 */
10634 bool _isFunctionDeclaration(String source) { 10768 bool _isFunctionDeclaration(String source) {
10635 GatheringErrorListener listener = new GatheringErrorListener(); 10769 GatheringErrorListener listener = new GatheringErrorListener();
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
10732 new Scanner(null, new CharSequenceReader(source), listener); 10866 new Scanner(null, new CharSequenceReader(source), listener);
10733 Token tokenStream = scanner.tokenize(); 10867 Token tokenStream = scanner.tokenize();
10734 // 10868 //
10735 // Parse the source. 10869 // Parse the source.
10736 // 10870 //
10737 Parser parser = new Parser(null, listener); 10871 Parser parser = new Parser(null, listener);
10738 return invokeParserMethodImpl( 10872 return invokeParserMethodImpl(
10739 parser, methodName, <Object>[tokenStream], tokenStream) as Token; 10873 parser, methodName, <Object>[tokenStream], tokenStream) as Token;
10740 } 10874 }
10741 } 10875 }
OLDNEW
« pkg/analyzer/lib/src/generated/parser.dart ('K') | « pkg/analyzer/lib/src/generated/visitors.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698