| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library analyzer.test.generated.ast_test; | |
| 6 | |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | |
| 8 import 'package:analyzer/dart/ast/visitor.dart'; | |
| 9 import 'package:analyzer/src/dart/ast/utilities.dart'; | |
| 10 import 'package:analyzer/src/generated/java_core.dart'; | |
| 11 import 'package:analyzer/src/generated/java_engine.dart' show Predicate; | |
| 12 import 'package:analyzer/src/generated/java_engine.dart'; | |
| 13 import 'package:analyzer/src/generated/scanner.dart'; | |
| 14 import 'package:analyzer/src/generated/testing/ast_factory.dart'; | |
| 15 import 'package:analyzer/src/generated/testing/token_factory.dart'; | |
| 16 import 'package:unittest/unittest.dart'; | |
| 17 | |
| 18 import '../reflective_tests.dart'; | |
| 19 import '../utils.dart'; | |
| 20 import 'parser_test.dart' show ParserTestCase; | |
| 21 import 'test_support.dart'; | |
| 22 | |
| 23 main() { | |
| 24 initializeTestEnvironment(); | |
| 25 runReflectiveTests(BreadthFirstVisitorTest); | |
| 26 runReflectiveTests(ClassDeclarationTest); | |
| 27 runReflectiveTests(ClassTypeAliasTest); | |
| 28 runReflectiveTests(ConstantEvaluatorTest); | |
| 29 runReflectiveTests(ConstructorDeclarationTest); | |
| 30 runReflectiveTests(FieldFormalParameterTest); | |
| 31 runReflectiveTests(IndexExpressionTest); | |
| 32 runReflectiveTests(NodeListTest); | |
| 33 runReflectiveTests(NodeLocatorTest); | |
| 34 runReflectiveTests(NodeLocator2Test); | |
| 35 runReflectiveTests(SimpleIdentifierTest); | |
| 36 runReflectiveTests(SimpleStringLiteralTest); | |
| 37 runReflectiveTests(StringInterpolationTest); | |
| 38 runReflectiveTests(ToSourceVisitorTest); | |
| 39 runReflectiveTests(VariableDeclarationTest); | |
| 40 } | |
| 41 | |
| 42 class AssignmentKind extends Enum<AssignmentKind> { | |
| 43 static const AssignmentKind BINARY = const AssignmentKind('BINARY', 0); | |
| 44 | |
| 45 static const AssignmentKind COMPOUND_LEFT = | |
| 46 const AssignmentKind('COMPOUND_LEFT', 1); | |
| 47 | |
| 48 static const AssignmentKind COMPOUND_RIGHT = | |
| 49 const AssignmentKind('COMPOUND_RIGHT', 2); | |
| 50 | |
| 51 static const AssignmentKind POSTFIX_INC = | |
| 52 const AssignmentKind('POSTFIX_INC', 3); | |
| 53 | |
| 54 static const AssignmentKind PREFIX_DEC = | |
| 55 const AssignmentKind('PREFIX_DEC', 4); | |
| 56 | |
| 57 static const AssignmentKind PREFIX_INC = | |
| 58 const AssignmentKind('PREFIX_INC', 5); | |
| 59 | |
| 60 static const AssignmentKind PREFIX_NOT = | |
| 61 const AssignmentKind('PREFIX_NOT', 6); | |
| 62 | |
| 63 static const AssignmentKind SIMPLE_LEFT = | |
| 64 const AssignmentKind('SIMPLE_LEFT', 7); | |
| 65 | |
| 66 static const AssignmentKind SIMPLE_RIGHT = | |
| 67 const AssignmentKind('SIMPLE_RIGHT', 8); | |
| 68 | |
| 69 static const AssignmentKind NONE = const AssignmentKind('NONE', 9); | |
| 70 | |
| 71 static const List<AssignmentKind> values = const [ | |
| 72 BINARY, | |
| 73 COMPOUND_LEFT, | |
| 74 COMPOUND_RIGHT, | |
| 75 POSTFIX_INC, | |
| 76 PREFIX_DEC, | |
| 77 PREFIX_INC, | |
| 78 PREFIX_NOT, | |
| 79 SIMPLE_LEFT, | |
| 80 SIMPLE_RIGHT, | |
| 81 NONE | |
| 82 ]; | |
| 83 | |
| 84 const AssignmentKind(String name, int ordinal) : super(name, ordinal); | |
| 85 } | |
| 86 | |
| 87 class BreadthFirstVisitor_BreadthFirstVisitorTest_testIt | |
| 88 extends BreadthFirstVisitor<Object> { | |
| 89 List<AstNode> nodes; | |
| 90 | |
| 91 BreadthFirstVisitor_BreadthFirstVisitorTest_testIt(this.nodes) : super(); | |
| 92 | |
| 93 @override | |
| 94 Object visitNode(AstNode node) { | |
| 95 nodes.add(node); | |
| 96 return super.visitNode(node); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 @reflectiveTest | |
| 101 class BreadthFirstVisitorTest extends ParserTestCase { | |
| 102 void test_it() { | |
| 103 String source = r''' | |
| 104 class A { | |
| 105 bool get g => true; | |
| 106 } | |
| 107 class B { | |
| 108 int f() { | |
| 109 num q() { | |
| 110 return 3; | |
| 111 } | |
| 112 return q() + 4; | |
| 113 } | |
| 114 } | |
| 115 A f(var p) { | |
| 116 if ((p as A).g) { | |
| 117 return p; | |
| 118 } else { | |
| 119 return null; | |
| 120 } | |
| 121 }'''; | |
| 122 CompilationUnit unit = ParserTestCase.parseCompilationUnit(source); | |
| 123 List<AstNode> nodes = new List<AstNode>(); | |
| 124 BreadthFirstVisitor<Object> visitor = | |
| 125 new BreadthFirstVisitor_BreadthFirstVisitorTest_testIt(nodes); | |
| 126 visitor.visitAllNodes(unit); | |
| 127 expect(nodes, hasLength(59)); | |
| 128 EngineTestCase.assertInstanceOf( | |
| 129 (obj) => obj is CompilationUnit, CompilationUnit, nodes[0]); | |
| 130 EngineTestCase.assertInstanceOf( | |
| 131 (obj) => obj is ClassDeclaration, ClassDeclaration, nodes[2]); | |
| 132 EngineTestCase.assertInstanceOf( | |
| 133 (obj) => obj is FunctionDeclaration, FunctionDeclaration, nodes[3]); | |
| 134 EngineTestCase.assertInstanceOf( | |
| 135 (obj) => obj is FunctionDeclarationStatement, | |
| 136 FunctionDeclarationStatement, | |
| 137 nodes[27]); | |
| 138 EngineTestCase.assertInstanceOf( | |
| 139 (obj) => obj is IntegerLiteral, IntegerLiteral, nodes[58]); | |
| 140 //3 | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 @reflectiveTest | |
| 145 class ClassDeclarationTest extends ParserTestCase { | |
| 146 void test_getConstructor() { | |
| 147 List<ConstructorInitializer> initializers = | |
| 148 new List<ConstructorInitializer>(); | |
| 149 ConstructorDeclaration defaultConstructor = | |
| 150 AstFactory.constructorDeclaration(AstFactory.identifier3("Test"), null, | |
| 151 AstFactory.formalParameterList(), initializers); | |
| 152 ConstructorDeclaration aConstructor = AstFactory.constructorDeclaration( | |
| 153 AstFactory.identifier3("Test"), | |
| 154 "a", | |
| 155 AstFactory.formalParameterList(), | |
| 156 initializers); | |
| 157 ConstructorDeclaration bConstructor = AstFactory.constructorDeclaration( | |
| 158 AstFactory.identifier3("Test"), | |
| 159 "b", | |
| 160 AstFactory.formalParameterList(), | |
| 161 initializers); | |
| 162 ClassDeclaration clazz = AstFactory.classDeclaration(null, "Test", null, | |
| 163 null, null, null, [defaultConstructor, aConstructor, bConstructor]); | |
| 164 expect(clazz.getConstructor(null), same(defaultConstructor)); | |
| 165 expect(clazz.getConstructor("a"), same(aConstructor)); | |
| 166 expect(clazz.getConstructor("b"), same(bConstructor)); | |
| 167 expect(clazz.getConstructor("noSuchConstructor"), same(null)); | |
| 168 } | |
| 169 | |
| 170 void test_getField() { | |
| 171 VariableDeclaration aVar = AstFactory.variableDeclaration("a"); | |
| 172 VariableDeclaration bVar = AstFactory.variableDeclaration("b"); | |
| 173 VariableDeclaration cVar = AstFactory.variableDeclaration("c"); | |
| 174 ClassDeclaration clazz = | |
| 175 AstFactory.classDeclaration(null, "Test", null, null, null, null, [ | |
| 176 AstFactory.fieldDeclaration2(false, null, [aVar]), | |
| 177 AstFactory.fieldDeclaration2(false, null, [bVar, cVar]) | |
| 178 ]); | |
| 179 expect(clazz.getField("a"), same(aVar)); | |
| 180 expect(clazz.getField("b"), same(bVar)); | |
| 181 expect(clazz.getField("c"), same(cVar)); | |
| 182 expect(clazz.getField("noSuchField"), same(null)); | |
| 183 } | |
| 184 | |
| 185 void test_getMethod() { | |
| 186 MethodDeclaration aMethod = AstFactory.methodDeclaration(null, null, null, | |
| 187 null, AstFactory.identifier3("a"), AstFactory.formalParameterList()); | |
| 188 MethodDeclaration bMethod = AstFactory.methodDeclaration(null, null, null, | |
| 189 null, AstFactory.identifier3("b"), AstFactory.formalParameterList()); | |
| 190 ClassDeclaration clazz = AstFactory.classDeclaration( | |
| 191 null, "Test", null, null, null, null, [aMethod, bMethod]); | |
| 192 expect(clazz.getMethod("a"), same(aMethod)); | |
| 193 expect(clazz.getMethod("b"), same(bMethod)); | |
| 194 expect(clazz.getMethod("noSuchMethod"), same(null)); | |
| 195 } | |
| 196 | |
| 197 void test_isAbstract() { | |
| 198 expect( | |
| 199 AstFactory | |
| 200 .classDeclaration(null, "A", null, null, null, null) | |
| 201 .isAbstract, | |
| 202 isFalse); | |
| 203 expect( | |
| 204 AstFactory | |
| 205 .classDeclaration(Keyword.ABSTRACT, "B", null, null, null, null) | |
| 206 .isAbstract, | |
| 207 isTrue); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 @reflectiveTest | |
| 212 class ClassTypeAliasTest extends ParserTestCase { | |
| 213 void test_isAbstract() { | |
| 214 expect( | |
| 215 AstFactory.classTypeAlias("A", null, null, null, null, null).isAbstract, | |
| 216 isFalse); | |
| 217 expect( | |
| 218 AstFactory | |
| 219 .classTypeAlias("B", null, Keyword.ABSTRACT, null, null, null) | |
| 220 .isAbstract, | |
| 221 isTrue); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 @reflectiveTest | |
| 226 class ConstantEvaluatorTest extends ParserTestCase { | |
| 227 void fail_constructor() { | |
| 228 Object value = _getConstantValue("?"); | |
| 229 expect(value, null); | |
| 230 } | |
| 231 | |
| 232 void fail_identifier_class() { | |
| 233 Object value = _getConstantValue("?"); | |
| 234 expect(value, null); | |
| 235 } | |
| 236 | |
| 237 void fail_identifier_function() { | |
| 238 Object value = _getConstantValue("?"); | |
| 239 expect(value, null); | |
| 240 } | |
| 241 | |
| 242 void fail_identifier_static() { | |
| 243 Object value = _getConstantValue("?"); | |
| 244 expect(value, null); | |
| 245 } | |
| 246 | |
| 247 void fail_identifier_staticMethod() { | |
| 248 Object value = _getConstantValue("?"); | |
| 249 expect(value, null); | |
| 250 } | |
| 251 | |
| 252 void fail_identifier_topLevel() { | |
| 253 Object value = _getConstantValue("?"); | |
| 254 expect(value, null); | |
| 255 } | |
| 256 | |
| 257 void fail_identifier_typeParameter() { | |
| 258 Object value = _getConstantValue("?"); | |
| 259 expect(value, null); | |
| 260 } | |
| 261 | |
| 262 void test_binary_bitAnd() { | |
| 263 Object value = _getConstantValue("74 & 42"); | |
| 264 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 265 expect(value as int, 74 & 42); | |
| 266 } | |
| 267 | |
| 268 void test_binary_bitOr() { | |
| 269 Object value = _getConstantValue("74 | 42"); | |
| 270 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 271 expect(value as int, 74 | 42); | |
| 272 } | |
| 273 | |
| 274 void test_binary_bitXor() { | |
| 275 Object value = _getConstantValue("74 ^ 42"); | |
| 276 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 277 expect(value as int, 74 ^ 42); | |
| 278 } | |
| 279 | |
| 280 void test_binary_divide_double() { | |
| 281 Object value = _getConstantValue("3.2 / 2.3"); | |
| 282 expect(value, 3.2 / 2.3); | |
| 283 } | |
| 284 | |
| 285 void test_binary_divide_integer() { | |
| 286 Object value = _getConstantValue("3 / 2"); | |
| 287 expect(value, 1.5); | |
| 288 } | |
| 289 | |
| 290 void test_binary_equal_boolean() { | |
| 291 Object value = _getConstantValue("true == false"); | |
| 292 expect(value, false); | |
| 293 } | |
| 294 | |
| 295 void test_binary_equal_integer() { | |
| 296 Object value = _getConstantValue("2 == 3"); | |
| 297 expect(value, false); | |
| 298 } | |
| 299 | |
| 300 void test_binary_equal_invalidLeft() { | |
| 301 Object value = _getConstantValue("a == 3"); | |
| 302 expect(value, ConstantEvaluator.NOT_A_CONSTANT); | |
| 303 } | |
| 304 | |
| 305 void test_binary_equal_invalidRight() { | |
| 306 Object value = _getConstantValue("2 == a"); | |
| 307 expect(value, ConstantEvaluator.NOT_A_CONSTANT); | |
| 308 } | |
| 309 | |
| 310 void test_binary_equal_string() { | |
| 311 Object value = _getConstantValue("'a' == 'b'"); | |
| 312 expect(value, false); | |
| 313 } | |
| 314 | |
| 315 void test_binary_greaterThan() { | |
| 316 Object value = _getConstantValue("2 > 3"); | |
| 317 expect(value, false); | |
| 318 } | |
| 319 | |
| 320 void test_binary_greaterThanOrEqual() { | |
| 321 Object value = _getConstantValue("2 >= 3"); | |
| 322 expect(value, false); | |
| 323 } | |
| 324 | |
| 325 void test_binary_leftShift() { | |
| 326 Object value = _getConstantValue("16 << 2"); | |
| 327 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 328 expect(value as int, 64); | |
| 329 } | |
| 330 | |
| 331 void test_binary_lessThan() { | |
| 332 Object value = _getConstantValue("2 < 3"); | |
| 333 expect(value, true); | |
| 334 } | |
| 335 | |
| 336 void test_binary_lessThanOrEqual() { | |
| 337 Object value = _getConstantValue("2 <= 3"); | |
| 338 expect(value, true); | |
| 339 } | |
| 340 | |
| 341 void test_binary_logicalAnd() { | |
| 342 Object value = _getConstantValue("true && false"); | |
| 343 expect(value, false); | |
| 344 } | |
| 345 | |
| 346 void test_binary_logicalOr() { | |
| 347 Object value = _getConstantValue("true || false"); | |
| 348 expect(value, true); | |
| 349 } | |
| 350 | |
| 351 void test_binary_minus_double() { | |
| 352 Object value = _getConstantValue("3.2 - 2.3"); | |
| 353 expect(value, 3.2 - 2.3); | |
| 354 } | |
| 355 | |
| 356 void test_binary_minus_integer() { | |
| 357 Object value = _getConstantValue("3 - 2"); | |
| 358 expect(value, 1); | |
| 359 } | |
| 360 | |
| 361 void test_binary_notEqual_boolean() { | |
| 362 Object value = _getConstantValue("true != false"); | |
| 363 expect(value, true); | |
| 364 } | |
| 365 | |
| 366 void test_binary_notEqual_integer() { | |
| 367 Object value = _getConstantValue("2 != 3"); | |
| 368 expect(value, true); | |
| 369 } | |
| 370 | |
| 371 void test_binary_notEqual_invalidLeft() { | |
| 372 Object value = _getConstantValue("a != 3"); | |
| 373 expect(value, ConstantEvaluator.NOT_A_CONSTANT); | |
| 374 } | |
| 375 | |
| 376 void test_binary_notEqual_invalidRight() { | |
| 377 Object value = _getConstantValue("2 != a"); | |
| 378 expect(value, ConstantEvaluator.NOT_A_CONSTANT); | |
| 379 } | |
| 380 | |
| 381 void test_binary_notEqual_string() { | |
| 382 Object value = _getConstantValue("'a' != 'b'"); | |
| 383 expect(value, true); | |
| 384 } | |
| 385 | |
| 386 void test_binary_plus_double() { | |
| 387 Object value = _getConstantValue("2.3 + 3.2"); | |
| 388 expect(value, 2.3 + 3.2); | |
| 389 } | |
| 390 | |
| 391 void test_binary_plus_integer() { | |
| 392 Object value = _getConstantValue("2 + 3"); | |
| 393 expect(value, 5); | |
| 394 } | |
| 395 | |
| 396 void test_binary_remainder_double() { | |
| 397 Object value = _getConstantValue("3.2 % 2.3"); | |
| 398 expect(value, 3.2 % 2.3); | |
| 399 } | |
| 400 | |
| 401 void test_binary_remainder_integer() { | |
| 402 Object value = _getConstantValue("8 % 3"); | |
| 403 expect(value, 2); | |
| 404 } | |
| 405 | |
| 406 void test_binary_rightShift() { | |
| 407 Object value = _getConstantValue("64 >> 2"); | |
| 408 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 409 expect(value as int, 16); | |
| 410 } | |
| 411 | |
| 412 void test_binary_times_double() { | |
| 413 Object value = _getConstantValue("2.3 * 3.2"); | |
| 414 expect(value, 2.3 * 3.2); | |
| 415 } | |
| 416 | |
| 417 void test_binary_times_integer() { | |
| 418 Object value = _getConstantValue("2 * 3"); | |
| 419 expect(value, 6); | |
| 420 } | |
| 421 | |
| 422 void test_binary_truncatingDivide_double() { | |
| 423 Object value = _getConstantValue("3.2 ~/ 2.3"); | |
| 424 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 425 expect(value as int, 1); | |
| 426 } | |
| 427 | |
| 428 void test_binary_truncatingDivide_integer() { | |
| 429 Object value = _getConstantValue("10 ~/ 3"); | |
| 430 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 431 expect(value as int, 3); | |
| 432 } | |
| 433 | |
| 434 void test_literal_boolean_false() { | |
| 435 Object value = _getConstantValue("false"); | |
| 436 expect(value, false); | |
| 437 } | |
| 438 | |
| 439 void test_literal_boolean_true() { | |
| 440 Object value = _getConstantValue("true"); | |
| 441 expect(value, true); | |
| 442 } | |
| 443 | |
| 444 void test_literal_list() { | |
| 445 Object value = _getConstantValue("['a', 'b', 'c']"); | |
| 446 EngineTestCase.assertInstanceOf((obj) => obj is List, List, value); | |
| 447 List list = value as List; | |
| 448 expect(list.length, 3); | |
| 449 expect(list[0], "a"); | |
| 450 expect(list[1], "b"); | |
| 451 expect(list[2], "c"); | |
| 452 } | |
| 453 | |
| 454 void test_literal_map() { | |
| 455 Object value = _getConstantValue("{'a' : 'm', 'b' : 'n', 'c' : 'o'}"); | |
| 456 EngineTestCase.assertInstanceOf((obj) => obj is Map, Map, value); | |
| 457 Map map = value as Map; | |
| 458 expect(map.length, 3); | |
| 459 expect(map["a"], "m"); | |
| 460 expect(map["b"], "n"); | |
| 461 expect(map["c"], "o"); | |
| 462 } | |
| 463 | |
| 464 void test_literal_null() { | |
| 465 Object value = _getConstantValue("null"); | |
| 466 expect(value, null); | |
| 467 } | |
| 468 | |
| 469 void test_literal_number_double() { | |
| 470 Object value = _getConstantValue("3.45"); | |
| 471 expect(value, 3.45); | |
| 472 } | |
| 473 | |
| 474 void test_literal_number_integer() { | |
| 475 Object value = _getConstantValue("42"); | |
| 476 expect(value, 42); | |
| 477 } | |
| 478 | |
| 479 void test_literal_string_adjacent() { | |
| 480 Object value = _getConstantValue("'abc' 'def'"); | |
| 481 expect(value, "abcdef"); | |
| 482 } | |
| 483 | |
| 484 void test_literal_string_interpolation_invalid() { | |
| 485 Object value = _getConstantValue("'a\${f()}c'"); | |
| 486 expect(value, ConstantEvaluator.NOT_A_CONSTANT); | |
| 487 } | |
| 488 | |
| 489 void test_literal_string_interpolation_valid() { | |
| 490 Object value = _getConstantValue("'a\${3}c'"); | |
| 491 expect(value, "a3c"); | |
| 492 } | |
| 493 | |
| 494 void test_literal_string_simple() { | |
| 495 Object value = _getConstantValue("'abc'"); | |
| 496 expect(value, "abc"); | |
| 497 } | |
| 498 | |
| 499 void test_parenthesizedExpression() { | |
| 500 Object value = _getConstantValue("('a')"); | |
| 501 expect(value, "a"); | |
| 502 } | |
| 503 | |
| 504 void test_unary_bitNot() { | |
| 505 Object value = _getConstantValue("~42"); | |
| 506 EngineTestCase.assertInstanceOf((obj) => obj is int, int, value); | |
| 507 expect(value as int, ~42); | |
| 508 } | |
| 509 | |
| 510 void test_unary_logicalNot() { | |
| 511 Object value = _getConstantValue("!true"); | |
| 512 expect(value, false); | |
| 513 } | |
| 514 | |
| 515 void test_unary_negated_double() { | |
| 516 Object value = _getConstantValue("-42.3"); | |
| 517 expect(value, -42.3); | |
| 518 } | |
| 519 | |
| 520 void test_unary_negated_integer() { | |
| 521 Object value = _getConstantValue("-42"); | |
| 522 expect(value, -42); | |
| 523 } | |
| 524 | |
| 525 Object _getConstantValue(String source) => | |
| 526 parseExpression(source).accept(new ConstantEvaluator()); | |
| 527 } | |
| 528 | |
| 529 @reflectiveTest | |
| 530 class ConstructorDeclarationTest extends EngineTestCase { | |
| 531 void test_firstTokenAfterCommentAndMetadata_all_inverted() { | |
| 532 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
| 533 externalKeyword.offset = 14; | |
| 534 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( | |
| 535 Keyword.CONST, | |
| 536 Keyword.FACTORY, | |
| 537 AstFactory.identifier3('int'), | |
| 538 null, | |
| 539 null, | |
| 540 null, | |
| 541 null); | |
| 542 declaration.externalKeyword = externalKeyword; | |
| 543 declaration.constKeyword.offset = 8; | |
| 544 Token factoryKeyword = declaration.factoryKeyword; | |
| 545 factoryKeyword.offset = 0; | |
| 546 expect(declaration.firstTokenAfterCommentAndMetadata, factoryKeyword); | |
| 547 } | |
| 548 | |
| 549 void test_firstTokenAfterCommentAndMetadata_all_normal() { | |
| 550 Token token = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
| 551 token.offset = 0; | |
| 552 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( | |
| 553 Keyword.CONST, | |
| 554 Keyword.FACTORY, | |
| 555 AstFactory.identifier3('int'), | |
| 556 null, | |
| 557 null, | |
| 558 null, | |
| 559 null); | |
| 560 declaration.externalKeyword = token; | |
| 561 declaration.constKeyword.offset = 9; | |
| 562 declaration.factoryKeyword.offset = 15; | |
| 563 expect(declaration.firstTokenAfterCommentAndMetadata, token); | |
| 564 } | |
| 565 | |
| 566 void test_firstTokenAfterCommentAndMetadata_constOnly() { | |
| 567 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( | |
| 568 Keyword.CONST, | |
| 569 null, | |
| 570 AstFactory.identifier3('int'), | |
| 571 null, | |
| 572 null, | |
| 573 null, | |
| 574 null); | |
| 575 expect(declaration.firstTokenAfterCommentAndMetadata, | |
| 576 declaration.constKeyword); | |
| 577 } | |
| 578 | |
| 579 void test_firstTokenAfterCommentAndMetadata_externalOnly() { | |
| 580 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
| 581 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( | |
| 582 null, null, AstFactory.identifier3('int'), null, null, null, null); | |
| 583 declaration.externalKeyword = externalKeyword; | |
| 584 expect(declaration.firstTokenAfterCommentAndMetadata, externalKeyword); | |
| 585 } | |
| 586 | |
| 587 void test_firstTokenAfterCommentAndMetadata_factoryOnly() { | |
| 588 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( | |
| 589 null, | |
| 590 Keyword.FACTORY, | |
| 591 AstFactory.identifier3('int'), | |
| 592 null, | |
| 593 null, | |
| 594 null, | |
| 595 null); | |
| 596 expect(declaration.firstTokenAfterCommentAndMetadata, | |
| 597 declaration.factoryKeyword); | |
| 598 } | |
| 599 } | |
| 600 | |
| 601 @reflectiveTest | |
| 602 class FieldFormalParameterTest extends EngineTestCase { | |
| 603 void test_endToken_noParameters() { | |
| 604 FieldFormalParameter parameter = AstFactory.fieldFormalParameter2('field'); | |
| 605 expect(parameter.endToken, parameter.identifier.endToken); | |
| 606 } | |
| 607 | |
| 608 void test_endToken_parameters() { | |
| 609 FieldFormalParameter parameter = AstFactory.fieldFormalParameter( | |
| 610 null, null, 'field', AstFactory.formalParameterList([])); | |
| 611 expect(parameter.endToken, parameter.parameters.endToken); | |
| 612 } | |
| 613 } | |
| 614 | |
| 615 @reflectiveTest | |
| 616 class IndexExpressionTest extends EngineTestCase { | |
| 617 void test_inGetterContext_assignment_compound_left() { | |
| 618 IndexExpression expression = AstFactory.indexExpression( | |
| 619 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 620 // a[b] += c | |
| 621 AstFactory.assignmentExpression( | |
| 622 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c")); | |
| 623 expect(expression.inGetterContext(), isTrue); | |
| 624 } | |
| 625 | |
| 626 void test_inGetterContext_assignment_simple_left() { | |
| 627 IndexExpression expression = AstFactory.indexExpression( | |
| 628 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 629 // a[b] = c | |
| 630 AstFactory.assignmentExpression( | |
| 631 expression, TokenType.EQ, AstFactory.identifier3("c")); | |
| 632 expect(expression.inGetterContext(), isFalse); | |
| 633 } | |
| 634 | |
| 635 void test_inGetterContext_nonAssignment() { | |
| 636 IndexExpression expression = AstFactory.indexExpression( | |
| 637 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 638 // a[b] + c | |
| 639 AstFactory.binaryExpression( | |
| 640 expression, TokenType.PLUS, AstFactory.identifier3("c")); | |
| 641 expect(expression.inGetterContext(), isTrue); | |
| 642 } | |
| 643 | |
| 644 void test_inSetterContext_assignment_compound_left() { | |
| 645 IndexExpression expression = AstFactory.indexExpression( | |
| 646 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 647 // a[b] += c | |
| 648 AstFactory.assignmentExpression( | |
| 649 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c")); | |
| 650 expect(expression.inSetterContext(), isTrue); | |
| 651 } | |
| 652 | |
| 653 void test_inSetterContext_assignment_compound_right() { | |
| 654 IndexExpression expression = AstFactory.indexExpression( | |
| 655 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 656 // c += a[b] | |
| 657 AstFactory.assignmentExpression( | |
| 658 AstFactory.identifier3("c"), TokenType.PLUS_EQ, expression); | |
| 659 expect(expression.inSetterContext(), isFalse); | |
| 660 } | |
| 661 | |
| 662 void test_inSetterContext_assignment_simple_left() { | |
| 663 IndexExpression expression = AstFactory.indexExpression( | |
| 664 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 665 // a[b] = c | |
| 666 AstFactory.assignmentExpression( | |
| 667 expression, TokenType.EQ, AstFactory.identifier3("c")); | |
| 668 expect(expression.inSetterContext(), isTrue); | |
| 669 } | |
| 670 | |
| 671 void test_inSetterContext_assignment_simple_right() { | |
| 672 IndexExpression expression = AstFactory.indexExpression( | |
| 673 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 674 // c = a[b] | |
| 675 AstFactory.assignmentExpression( | |
| 676 AstFactory.identifier3("c"), TokenType.EQ, expression); | |
| 677 expect(expression.inSetterContext(), isFalse); | |
| 678 } | |
| 679 | |
| 680 void test_inSetterContext_nonAssignment() { | |
| 681 IndexExpression expression = AstFactory.indexExpression( | |
| 682 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 683 AstFactory.binaryExpression( | |
| 684 expression, TokenType.PLUS, AstFactory.identifier3("c")); | |
| 685 // a[b] + cc | |
| 686 expect(expression.inSetterContext(), isFalse); | |
| 687 } | |
| 688 | |
| 689 void test_inSetterContext_postfix() { | |
| 690 IndexExpression expression = AstFactory.indexExpression( | |
| 691 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 692 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS); | |
| 693 // a[b]++ | |
| 694 expect(expression.inSetterContext(), isTrue); | |
| 695 } | |
| 696 | |
| 697 void test_inSetterContext_prefix_bang() { | |
| 698 IndexExpression expression = AstFactory.indexExpression( | |
| 699 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 700 // !a[b] | |
| 701 AstFactory.prefixExpression(TokenType.BANG, expression); | |
| 702 expect(expression.inSetterContext(), isFalse); | |
| 703 } | |
| 704 | |
| 705 void test_inSetterContext_prefix_minusMinus() { | |
| 706 IndexExpression expression = AstFactory.indexExpression( | |
| 707 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 708 // --a[b] | |
| 709 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression); | |
| 710 expect(expression.inSetterContext(), isTrue); | |
| 711 } | |
| 712 | |
| 713 void test_inSetterContext_prefix_plusPlus() { | |
| 714 IndexExpression expression = AstFactory.indexExpression( | |
| 715 AstFactory.identifier3("a"), AstFactory.identifier3("b")); | |
| 716 // ++a[b] | |
| 717 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression); | |
| 718 expect(expression.inSetterContext(), isTrue); | |
| 719 } | |
| 720 } | |
| 721 | |
| 722 @reflectiveTest | |
| 723 class NodeListTest extends EngineTestCase { | |
| 724 void test_add() { | |
| 725 AstNode parent = AstFactory.argumentList(); | |
| 726 AstNode firstNode = AstFactory.booleanLiteral(true); | |
| 727 AstNode secondNode = AstFactory.booleanLiteral(false); | |
| 728 NodeList<AstNode> list = new NodeList<AstNode>(parent); | |
| 729 list.insert(0, secondNode); | |
| 730 list.insert(0, firstNode); | |
| 731 expect(list, hasLength(2)); | |
| 732 expect(list[0], same(firstNode)); | |
| 733 expect(list[1], same(secondNode)); | |
| 734 expect(firstNode.parent, same(parent)); | |
| 735 expect(secondNode.parent, same(parent)); | |
| 736 AstNode thirdNode = AstFactory.booleanLiteral(false); | |
| 737 list.insert(1, thirdNode); | |
| 738 expect(list, hasLength(3)); | |
| 739 expect(list[0], same(firstNode)); | |
| 740 expect(list[1], same(thirdNode)); | |
| 741 expect(list[2], same(secondNode)); | |
| 742 expect(firstNode.parent, same(parent)); | |
| 743 expect(secondNode.parent, same(parent)); | |
| 744 expect(thirdNode.parent, same(parent)); | |
| 745 } | |
| 746 | |
| 747 void test_add_negative() { | |
| 748 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 749 try { | |
| 750 list.insert(-1, AstFactory.booleanLiteral(true)); | |
| 751 fail("Expected IndexOutOfBoundsException"); | |
| 752 } on RangeError { | |
| 753 // Expected | |
| 754 } | |
| 755 } | |
| 756 | |
| 757 void test_add_tooBig() { | |
| 758 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 759 try { | |
| 760 list.insert(1, AstFactory.booleanLiteral(true)); | |
| 761 fail("Expected IndexOutOfBoundsException"); | |
| 762 } on RangeError { | |
| 763 // Expected | |
| 764 } | |
| 765 } | |
| 766 | |
| 767 void test_addAll() { | |
| 768 AstNode parent = AstFactory.argumentList(); | |
| 769 List<AstNode> firstNodes = new List<AstNode>(); | |
| 770 AstNode firstNode = AstFactory.booleanLiteral(true); | |
| 771 AstNode secondNode = AstFactory.booleanLiteral(false); | |
| 772 firstNodes.add(firstNode); | |
| 773 firstNodes.add(secondNode); | |
| 774 NodeList<AstNode> list = new NodeList<AstNode>(parent); | |
| 775 list.addAll(firstNodes); | |
| 776 expect(list, hasLength(2)); | |
| 777 expect(list[0], same(firstNode)); | |
| 778 expect(list[1], same(secondNode)); | |
| 779 expect(firstNode.parent, same(parent)); | |
| 780 expect(secondNode.parent, same(parent)); | |
| 781 List<AstNode> secondNodes = new List<AstNode>(); | |
| 782 AstNode thirdNode = AstFactory.booleanLiteral(true); | |
| 783 AstNode fourthNode = AstFactory.booleanLiteral(false); | |
| 784 secondNodes.add(thirdNode); | |
| 785 secondNodes.add(fourthNode); | |
| 786 list.addAll(secondNodes); | |
| 787 expect(list, hasLength(4)); | |
| 788 expect(list[0], same(firstNode)); | |
| 789 expect(list[1], same(secondNode)); | |
| 790 expect(list[2], same(thirdNode)); | |
| 791 expect(list[3], same(fourthNode)); | |
| 792 expect(firstNode.parent, same(parent)); | |
| 793 expect(secondNode.parent, same(parent)); | |
| 794 expect(thirdNode.parent, same(parent)); | |
| 795 expect(fourthNode.parent, same(parent)); | |
| 796 } | |
| 797 | |
| 798 void test_creation() { | |
| 799 AstNode owner = AstFactory.argumentList(); | |
| 800 NodeList<AstNode> list = new NodeList<AstNode>(owner); | |
| 801 expect(list, isNotNull); | |
| 802 expect(list, hasLength(0)); | |
| 803 expect(list.owner, same(owner)); | |
| 804 } | |
| 805 | |
| 806 void test_get_negative() { | |
| 807 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 808 try { | |
| 809 list[-1]; | |
| 810 fail("Expected IndexOutOfBoundsException"); | |
| 811 } on RangeError { | |
| 812 // Expected | |
| 813 } | |
| 814 } | |
| 815 | |
| 816 void test_get_tooBig() { | |
| 817 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 818 try { | |
| 819 list[1]; | |
| 820 fail("Expected IndexOutOfBoundsException"); | |
| 821 } on RangeError { | |
| 822 // Expected | |
| 823 } | |
| 824 } | |
| 825 | |
| 826 void test_getBeginToken_empty() { | |
| 827 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 828 expect(list.beginToken, isNull); | |
| 829 } | |
| 830 | |
| 831 void test_getBeginToken_nonEmpty() { | |
| 832 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 833 AstNode node = | |
| 834 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true)); | |
| 835 list.add(node); | |
| 836 expect(list.beginToken, same(node.beginToken)); | |
| 837 } | |
| 838 | |
| 839 void test_getEndToken_empty() { | |
| 840 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 841 expect(list.endToken, isNull); | |
| 842 } | |
| 843 | |
| 844 void test_getEndToken_nonEmpty() { | |
| 845 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 846 AstNode node = | |
| 847 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true)); | |
| 848 list.add(node); | |
| 849 expect(list.endToken, same(node.endToken)); | |
| 850 } | |
| 851 | |
| 852 void test_indexOf() { | |
| 853 List<AstNode> nodes = new List<AstNode>(); | |
| 854 AstNode firstNode = AstFactory.booleanLiteral(true); | |
| 855 AstNode secondNode = AstFactory.booleanLiteral(false); | |
| 856 AstNode thirdNode = AstFactory.booleanLiteral(true); | |
| 857 AstNode fourthNode = AstFactory.booleanLiteral(false); | |
| 858 nodes.add(firstNode); | |
| 859 nodes.add(secondNode); | |
| 860 nodes.add(thirdNode); | |
| 861 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 862 list.addAll(nodes); | |
| 863 expect(list, hasLength(3)); | |
| 864 expect(list.indexOf(firstNode), 0); | |
| 865 expect(list.indexOf(secondNode), 1); | |
| 866 expect(list.indexOf(thirdNode), 2); | |
| 867 expect(list.indexOf(fourthNode), -1); | |
| 868 expect(list.indexOf(null), -1); | |
| 869 } | |
| 870 | |
| 871 void test_remove() { | |
| 872 List<AstNode> nodes = new List<AstNode>(); | |
| 873 AstNode firstNode = AstFactory.booleanLiteral(true); | |
| 874 AstNode secondNode = AstFactory.booleanLiteral(false); | |
| 875 AstNode thirdNode = AstFactory.booleanLiteral(true); | |
| 876 nodes.add(firstNode); | |
| 877 nodes.add(secondNode); | |
| 878 nodes.add(thirdNode); | |
| 879 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 880 list.addAll(nodes); | |
| 881 expect(list, hasLength(3)); | |
| 882 expect(list.removeAt(1), same(secondNode)); | |
| 883 expect(list, hasLength(2)); | |
| 884 expect(list[0], same(firstNode)); | |
| 885 expect(list[1], same(thirdNode)); | |
| 886 } | |
| 887 | |
| 888 void test_remove_negative() { | |
| 889 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 890 try { | |
| 891 list.removeAt(-1); | |
| 892 fail("Expected IndexOutOfBoundsException"); | |
| 893 } on RangeError { | |
| 894 // Expected | |
| 895 } | |
| 896 } | |
| 897 | |
| 898 void test_remove_tooBig() { | |
| 899 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 900 try { | |
| 901 list.removeAt(1); | |
| 902 fail("Expected IndexOutOfBoundsException"); | |
| 903 } on RangeError { | |
| 904 // Expected | |
| 905 } | |
| 906 } | |
| 907 | |
| 908 void test_set() { | |
| 909 List<AstNode> nodes = new List<AstNode>(); | |
| 910 AstNode firstNode = AstFactory.booleanLiteral(true); | |
| 911 AstNode secondNode = AstFactory.booleanLiteral(false); | |
| 912 AstNode thirdNode = AstFactory.booleanLiteral(true); | |
| 913 nodes.add(firstNode); | |
| 914 nodes.add(secondNode); | |
| 915 nodes.add(thirdNode); | |
| 916 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 917 list.addAll(nodes); | |
| 918 expect(list, hasLength(3)); | |
| 919 AstNode fourthNode = AstFactory.integer(0); | |
| 920 expect(javaListSet(list, 1, fourthNode), same(secondNode)); | |
| 921 expect(list, hasLength(3)); | |
| 922 expect(list[0], same(firstNode)); | |
| 923 expect(list[1], same(fourthNode)); | |
| 924 expect(list[2], same(thirdNode)); | |
| 925 } | |
| 926 | |
| 927 void test_set_negative() { | |
| 928 AstNode node = AstFactory.booleanLiteral(true); | |
| 929 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 930 try { | |
| 931 javaListSet(list, -1, node); | |
| 932 fail("Expected IndexOutOfBoundsException"); | |
| 933 } on RangeError { | |
| 934 // Expected | |
| 935 } | |
| 936 } | |
| 937 | |
| 938 void test_set_tooBig() { | |
| 939 AstNode node = AstFactory.booleanLiteral(true); | |
| 940 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); | |
| 941 try { | |
| 942 javaListSet(list, 1, node); | |
| 943 fail("Expected IndexOutOfBoundsException"); | |
| 944 } on RangeError { | |
| 945 // Expected | |
| 946 } | |
| 947 } | |
| 948 } | |
| 949 | |
| 950 @reflectiveTest | |
| 951 class NodeLocator2Test extends ParserTestCase { | |
| 952 void test_onlyStartOffset() { | |
| 953 String code = ' int vv; '; | |
| 954 // 012345678 | |
| 955 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code); | |
| 956 TopLevelVariableDeclaration declaration = unit.declarations[0]; | |
| 957 VariableDeclarationList variableList = declaration.variables; | |
| 958 Identifier typeName = variableList.type.name; | |
| 959 SimpleIdentifier varName = variableList.variables[0].name; | |
| 960 expect(new NodeLocator2(0).searchWithin(unit), same(unit)); | |
| 961 expect(new NodeLocator2(1).searchWithin(unit), same(typeName)); | |
| 962 expect(new NodeLocator2(2).searchWithin(unit), same(typeName)); | |
| 963 expect(new NodeLocator2(3).searchWithin(unit), same(typeName)); | |
| 964 expect(new NodeLocator2(4).searchWithin(unit), same(variableList)); | |
| 965 expect(new NodeLocator2(5).searchWithin(unit), same(varName)); | |
| 966 expect(new NodeLocator2(6).searchWithin(unit), same(varName)); | |
| 967 expect(new NodeLocator2(7).searchWithin(unit), same(declaration)); | |
| 968 expect(new NodeLocator2(8).searchWithin(unit), same(unit)); | |
| 969 expect(new NodeLocator2(9).searchWithin(unit), isNull); | |
| 970 expect(new NodeLocator2(100).searchWithin(unit), isNull); | |
| 971 } | |
| 972 | |
| 973 void test_startEndOffset() { | |
| 974 String code = ' int vv; '; | |
| 975 // 012345678 | |
| 976 CompilationUnit unit = ParserTestCase.parseCompilationUnit(code); | |
| 977 TopLevelVariableDeclaration declaration = unit.declarations[0]; | |
| 978 VariableDeclarationList variableList = declaration.variables; | |
| 979 Identifier typeName = variableList.type.name; | |
| 980 SimpleIdentifier varName = variableList.variables[0].name; | |
| 981 expect(new NodeLocator2(-1, 2).searchWithin(unit), isNull); | |
| 982 expect(new NodeLocator2(0, 2).searchWithin(unit), same(unit)); | |
| 983 expect(new NodeLocator2(1, 2).searchWithin(unit), same(typeName)); | |
| 984 expect(new NodeLocator2(1, 3).searchWithin(unit), same(typeName)); | |
| 985 expect(new NodeLocator2(1, 4).searchWithin(unit), same(variableList)); | |
| 986 expect(new NodeLocator2(5, 6).searchWithin(unit), same(varName)); | |
| 987 expect(new NodeLocator2(5, 7).searchWithin(unit), same(declaration)); | |
| 988 expect(new NodeLocator2(5, 8).searchWithin(unit), same(unit)); | |
| 989 expect(new NodeLocator2(5, 100).searchWithin(unit), isNull); | |
| 990 expect(new NodeLocator2(100, 200).searchWithin(unit), isNull); | |
| 991 } | |
| 992 } | |
| 993 | |
| 994 @reflectiveTest | |
| 995 class NodeLocatorTest extends ParserTestCase { | |
| 996 void test_range() { | |
| 997 CompilationUnit unit = | |
| 998 ParserTestCase.parseCompilationUnit("library myLib;"); | |
| 999 _assertLocate( | |
| 1000 unit, 4, 10, (node) => node is LibraryDirective, LibraryDirective); | |
| 1001 } | |
| 1002 | |
| 1003 void test_searchWithin_null() { | |
| 1004 NodeLocator locator = new NodeLocator(0, 0); | |
| 1005 expect(locator.searchWithin(null), isNull); | |
| 1006 } | |
| 1007 | |
| 1008 void test_searchWithin_offset() { | |
| 1009 CompilationUnit unit = | |
| 1010 ParserTestCase.parseCompilationUnit("library myLib;"); | |
| 1011 _assertLocate( | |
| 1012 unit, 10, 10, (node) => node is SimpleIdentifier, SimpleIdentifier); | |
| 1013 } | |
| 1014 | |
| 1015 void test_searchWithin_offsetAfterNode() { | |
| 1016 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r''' | |
| 1017 class A {} | |
| 1018 class B {}'''); | |
| 1019 NodeLocator locator = new NodeLocator(1024, 1024); | |
| 1020 AstNode node = locator.searchWithin(unit.declarations[0]); | |
| 1021 expect(node, isNull); | |
| 1022 } | |
| 1023 | |
| 1024 void test_searchWithin_offsetBeforeNode() { | |
| 1025 CompilationUnit unit = ParserTestCase.parseCompilationUnit(r''' | |
| 1026 class A {} | |
| 1027 class B {}'''); | |
| 1028 NodeLocator locator = new NodeLocator(0, 0); | |
| 1029 AstNode node = locator.searchWithin(unit.declarations[1]); | |
| 1030 expect(node, isNull); | |
| 1031 } | |
| 1032 | |
| 1033 void _assertLocate(CompilationUnit unit, int start, int end, | |
| 1034 Predicate<AstNode> predicate, Type expectedClass) { | |
| 1035 NodeLocator locator = new NodeLocator(start, end); | |
| 1036 AstNode node = locator.searchWithin(unit); | |
| 1037 expect(node, isNotNull); | |
| 1038 expect(locator.foundNode, same(node)); | |
| 1039 expect(node.offset <= start, isTrue, reason: "Node starts after range"); | |
| 1040 expect(node.offset + node.length > end, isTrue, | |
| 1041 reason: "Node ends before range"); | |
| 1042 EngineTestCase.assertInstanceOf(predicate, expectedClass, node); | |
| 1043 } | |
| 1044 } | |
| 1045 | |
| 1046 @reflectiveTest | |
| 1047 class SimpleIdentifierTest extends ParserTestCase { | |
| 1048 void test_inDeclarationContext_catch_exception() { | |
| 1049 SimpleIdentifier identifier = | |
| 1050 AstFactory.catchClause("e").exceptionParameter; | |
| 1051 expect(identifier.inDeclarationContext(), isTrue); | |
| 1052 } | |
| 1053 | |
| 1054 void test_inDeclarationContext_catch_stack() { | |
| 1055 SimpleIdentifier identifier = | |
| 1056 AstFactory.catchClause2("e", "s").stackTraceParameter; | |
| 1057 expect(identifier.inDeclarationContext(), isTrue); | |
| 1058 } | |
| 1059 | |
| 1060 void test_inDeclarationContext_classDeclaration() { | |
| 1061 SimpleIdentifier identifier = | |
| 1062 AstFactory.classDeclaration(null, "C", null, null, null, null).name; | |
| 1063 expect(identifier.inDeclarationContext(), isTrue); | |
| 1064 } | |
| 1065 | |
| 1066 void test_inDeclarationContext_classTypeAlias() { | |
| 1067 SimpleIdentifier identifier = | |
| 1068 AstFactory.classTypeAlias("C", null, null, null, null, null).name; | |
| 1069 expect(identifier.inDeclarationContext(), isTrue); | |
| 1070 } | |
| 1071 | |
| 1072 void test_inDeclarationContext_constructorDeclaration() { | |
| 1073 SimpleIdentifier identifier = AstFactory | |
| 1074 .constructorDeclaration(AstFactory.identifier3("C"), "c", null, null) | |
| 1075 .name; | |
| 1076 expect(identifier.inDeclarationContext(), isTrue); | |
| 1077 } | |
| 1078 | |
| 1079 void test_inDeclarationContext_declaredIdentifier() { | |
| 1080 DeclaredIdentifier declaredIdentifier = AstFactory.declaredIdentifier3("v"); | |
| 1081 SimpleIdentifier identifier = declaredIdentifier.identifier; | |
| 1082 expect(identifier.inDeclarationContext(), isTrue); | |
| 1083 } | |
| 1084 | |
| 1085 void test_inDeclarationContext_enumConstantDeclaration() { | |
| 1086 EnumDeclaration enumDeclaration = | |
| 1087 AstFactory.enumDeclaration2('MyEnum', ['CONST']); | |
| 1088 SimpleIdentifier identifier = enumDeclaration.constants[0].name; | |
| 1089 expect(identifier.inDeclarationContext(), isTrue); | |
| 1090 } | |
| 1091 | |
| 1092 void test_inDeclarationContext_enumDeclaration() { | |
| 1093 EnumDeclaration enumDeclaration = | |
| 1094 AstFactory.enumDeclaration2('MyEnum', ['A', 'B', 'C']); | |
| 1095 SimpleIdentifier identifier = enumDeclaration.name; | |
| 1096 expect(identifier.inDeclarationContext(), isTrue); | |
| 1097 } | |
| 1098 | |
| 1099 void test_inDeclarationContext_fieldFormalParameter() { | |
| 1100 SimpleIdentifier identifier = | |
| 1101 AstFactory.fieldFormalParameter2("p").identifier; | |
| 1102 expect(identifier.inDeclarationContext(), isFalse); | |
| 1103 } | |
| 1104 | |
| 1105 void test_inDeclarationContext_functionDeclaration() { | |
| 1106 SimpleIdentifier identifier = | |
| 1107 AstFactory.functionDeclaration(null, null, "f", null).name; | |
| 1108 expect(identifier.inDeclarationContext(), isTrue); | |
| 1109 } | |
| 1110 | |
| 1111 void test_inDeclarationContext_functionTypeAlias() { | |
| 1112 SimpleIdentifier identifier = | |
| 1113 AstFactory.typeAlias(null, "F", null, null).name; | |
| 1114 expect(identifier.inDeclarationContext(), isTrue); | |
| 1115 } | |
| 1116 | |
| 1117 void test_inDeclarationContext_label_false() { | |
| 1118 SimpleIdentifier identifier = | |
| 1119 AstFactory.namedExpression2("l", AstFactory.integer(0)).name.label; | |
| 1120 expect(identifier.inDeclarationContext(), isFalse); | |
| 1121 } | |
| 1122 | |
| 1123 void test_inDeclarationContext_label_true() { | |
| 1124 Label label = AstFactory.label2("l"); | |
| 1125 SimpleIdentifier identifier = label.label; | |
| 1126 AstFactory.labeledStatement([label], AstFactory.emptyStatement()); | |
| 1127 expect(identifier.inDeclarationContext(), isTrue); | |
| 1128 } | |
| 1129 | |
| 1130 void test_inDeclarationContext_methodDeclaration() { | |
| 1131 SimpleIdentifier identifier = AstFactory.identifier3("m"); | |
| 1132 AstFactory.methodDeclaration2( | |
| 1133 null, null, null, null, identifier, null, null); | |
| 1134 expect(identifier.inDeclarationContext(), isTrue); | |
| 1135 } | |
| 1136 | |
| 1137 void test_inDeclarationContext_prefix() { | |
| 1138 SimpleIdentifier identifier = | |
| 1139 AstFactory.importDirective3("uri", "pref").prefix; | |
| 1140 expect(identifier.inDeclarationContext(), isTrue); | |
| 1141 } | |
| 1142 | |
| 1143 void test_inDeclarationContext_simpleFormalParameter() { | |
| 1144 SimpleIdentifier identifier = | |
| 1145 AstFactory.simpleFormalParameter3("p").identifier; | |
| 1146 expect(identifier.inDeclarationContext(), isTrue); | |
| 1147 } | |
| 1148 | |
| 1149 void test_inDeclarationContext_typeParameter_bound() { | |
| 1150 TypeName bound = AstFactory.typeName4("A"); | |
| 1151 SimpleIdentifier identifier = bound.name as SimpleIdentifier; | |
| 1152 AstFactory.typeParameter2("E", bound); | |
| 1153 expect(identifier.inDeclarationContext(), isFalse); | |
| 1154 } | |
| 1155 | |
| 1156 void test_inDeclarationContext_typeParameter_name() { | |
| 1157 SimpleIdentifier identifier = AstFactory.typeParameter("E").name; | |
| 1158 expect(identifier.inDeclarationContext(), isTrue); | |
| 1159 } | |
| 1160 | |
| 1161 void test_inDeclarationContext_variableDeclaration() { | |
| 1162 SimpleIdentifier identifier = AstFactory.variableDeclaration("v").name; | |
| 1163 expect(identifier.inDeclarationContext(), isTrue); | |
| 1164 } | |
| 1165 | |
| 1166 void test_inGetterContext() { | |
| 1167 for (WrapperKind wrapper in WrapperKind.values) { | |
| 1168 for (AssignmentKind assignment in AssignmentKind.values) { | |
| 1169 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment); | |
| 1170 if (assignment == AssignmentKind.SIMPLE_LEFT && | |
| 1171 wrapper != WrapperKind.PREFIXED_LEFT && | |
| 1172 wrapper != WrapperKind.PROPERTY_LEFT) { | |
| 1173 if (identifier.inGetterContext()) { | |
| 1174 fail("Expected ${_topMostNode(identifier).toSource()} to be false"); | |
| 1175 } | |
| 1176 } else { | |
| 1177 if (!identifier.inGetterContext()) { | |
| 1178 fail("Expected ${_topMostNode(identifier).toSource()} to be true"); | |
| 1179 } | |
| 1180 } | |
| 1181 } | |
| 1182 } | |
| 1183 } | |
| 1184 | |
| 1185 void test_inGetterContext_forEachLoop() { | |
| 1186 SimpleIdentifier identifier = AstFactory.identifier3("a"); | |
| 1187 Expression iterator = AstFactory.listLiteral(); | |
| 1188 Statement body = AstFactory.block(); | |
| 1189 AstFactory.forEachStatement2(identifier, iterator, body); | |
| 1190 expect(identifier.inGetterContext(), isFalse); | |
| 1191 } | |
| 1192 | |
| 1193 void test_inReferenceContext() { | |
| 1194 SimpleIdentifier identifier = AstFactory.identifier3("id"); | |
| 1195 AstFactory.namedExpression( | |
| 1196 AstFactory.label(identifier), AstFactory.identifier3("_")); | |
| 1197 expect(identifier.inGetterContext(), isFalse); | |
| 1198 expect(identifier.inSetterContext(), isFalse); | |
| 1199 } | |
| 1200 | |
| 1201 void test_inSetterContext() { | |
| 1202 for (WrapperKind wrapper in WrapperKind.values) { | |
| 1203 for (AssignmentKind assignment in AssignmentKind.values) { | |
| 1204 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment); | |
| 1205 if (wrapper == WrapperKind.PREFIXED_LEFT || | |
| 1206 wrapper == WrapperKind.PROPERTY_LEFT || | |
| 1207 assignment == AssignmentKind.BINARY || | |
| 1208 assignment == AssignmentKind.COMPOUND_RIGHT || | |
| 1209 assignment == AssignmentKind.PREFIX_NOT || | |
| 1210 assignment == AssignmentKind.SIMPLE_RIGHT || | |
| 1211 assignment == AssignmentKind.NONE) { | |
| 1212 if (identifier.inSetterContext()) { | |
| 1213 fail("Expected ${_topMostNode(identifier).toSource()} to be false"); | |
| 1214 } | |
| 1215 } else { | |
| 1216 if (!identifier.inSetterContext()) { | |
| 1217 fail("Expected ${_topMostNode(identifier).toSource()} to be true"); | |
| 1218 } | |
| 1219 } | |
| 1220 } | |
| 1221 } | |
| 1222 } | |
| 1223 | |
| 1224 void test_inSetterContext_forEachLoop() { | |
| 1225 SimpleIdentifier identifier = AstFactory.identifier3("a"); | |
| 1226 Expression iterator = AstFactory.listLiteral(); | |
| 1227 Statement body = AstFactory.block(); | |
| 1228 AstFactory.forEachStatement2(identifier, iterator, body); | |
| 1229 expect(identifier.inSetterContext(), isTrue); | |
| 1230 } | |
| 1231 | |
| 1232 void test_isQualified_inMethodInvocation_noTarget() { | |
| 1233 MethodInvocation invocation = | |
| 1234 AstFactory.methodInvocation2("test", [AstFactory.identifier3("arg0")]); | |
| 1235 SimpleIdentifier identifier = invocation.methodName; | |
| 1236 expect(identifier.isQualified, isFalse); | |
| 1237 } | |
| 1238 | |
| 1239 void test_isQualified_inMethodInvocation_withTarget() { | |
| 1240 MethodInvocation invocation = AstFactory.methodInvocation( | |
| 1241 AstFactory.identifier3("target"), | |
| 1242 "test", | |
| 1243 [AstFactory.identifier3("arg0")]); | |
| 1244 SimpleIdentifier identifier = invocation.methodName; | |
| 1245 expect(identifier.isQualified, isTrue); | |
| 1246 } | |
| 1247 | |
| 1248 void test_isQualified_inPrefixedIdentifier_name() { | |
| 1249 SimpleIdentifier identifier = AstFactory.identifier3("test"); | |
| 1250 AstFactory.identifier4("prefix", identifier); | |
| 1251 expect(identifier.isQualified, isTrue); | |
| 1252 } | |
| 1253 | |
| 1254 void test_isQualified_inPrefixedIdentifier_prefix() { | |
| 1255 SimpleIdentifier identifier = AstFactory.identifier3("test"); | |
| 1256 AstFactory.identifier(identifier, AstFactory.identifier3("name")); | |
| 1257 expect(identifier.isQualified, isFalse); | |
| 1258 } | |
| 1259 | |
| 1260 void test_isQualified_inPropertyAccess_name() { | |
| 1261 SimpleIdentifier identifier = AstFactory.identifier3("test"); | |
| 1262 AstFactory.propertyAccess(AstFactory.identifier3("target"), identifier); | |
| 1263 expect(identifier.isQualified, isTrue); | |
| 1264 } | |
| 1265 | |
| 1266 void test_isQualified_inPropertyAccess_target() { | |
| 1267 SimpleIdentifier identifier = AstFactory.identifier3("test"); | |
| 1268 AstFactory.propertyAccess(identifier, AstFactory.identifier3("name")); | |
| 1269 expect(identifier.isQualified, isFalse); | |
| 1270 } | |
| 1271 | |
| 1272 void test_isQualified_inReturnStatement() { | |
| 1273 SimpleIdentifier identifier = AstFactory.identifier3("test"); | |
| 1274 AstFactory.returnStatement2(identifier); | |
| 1275 expect(identifier.isQualified, isFalse); | |
| 1276 } | |
| 1277 | |
| 1278 SimpleIdentifier _createIdentifier( | |
| 1279 WrapperKind wrapper, AssignmentKind assignment) { | |
| 1280 SimpleIdentifier identifier = AstFactory.identifier3("a"); | |
| 1281 Expression expression = identifier; | |
| 1282 while (true) { | |
| 1283 if (wrapper == WrapperKind.PREFIXED_LEFT) { | |
| 1284 expression = | |
| 1285 AstFactory.identifier(identifier, AstFactory.identifier3("_")); | |
| 1286 } else if (wrapper == WrapperKind.PREFIXED_RIGHT) { | |
| 1287 expression = | |
| 1288 AstFactory.identifier(AstFactory.identifier3("_"), identifier); | |
| 1289 } else if (wrapper == WrapperKind.PROPERTY_LEFT) { | |
| 1290 expression = AstFactory.propertyAccess2(expression, "_"); | |
| 1291 } else if (wrapper == WrapperKind.PROPERTY_RIGHT) { | |
| 1292 expression = | |
| 1293 AstFactory.propertyAccess(AstFactory.identifier3("_"), identifier); | |
| 1294 } else if (wrapper == WrapperKind.NONE) {} | |
| 1295 break; | |
| 1296 } | |
| 1297 while (true) { | |
| 1298 if (assignment == AssignmentKind.BINARY) { | |
| 1299 AstFactory.binaryExpression( | |
| 1300 expression, TokenType.PLUS, AstFactory.identifier3("_")); | |
| 1301 } else if (assignment == AssignmentKind.COMPOUND_LEFT) { | |
| 1302 AstFactory.assignmentExpression( | |
| 1303 expression, TokenType.PLUS_EQ, AstFactory.identifier3("_")); | |
| 1304 } else if (assignment == AssignmentKind.COMPOUND_RIGHT) { | |
| 1305 AstFactory.assignmentExpression( | |
| 1306 AstFactory.identifier3("_"), TokenType.PLUS_EQ, expression); | |
| 1307 } else if (assignment == AssignmentKind.POSTFIX_INC) { | |
| 1308 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS); | |
| 1309 } else if (assignment == AssignmentKind.PREFIX_DEC) { | |
| 1310 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression); | |
| 1311 } else if (assignment == AssignmentKind.PREFIX_INC) { | |
| 1312 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression); | |
| 1313 } else if (assignment == AssignmentKind.PREFIX_NOT) { | |
| 1314 AstFactory.prefixExpression(TokenType.BANG, expression); | |
| 1315 } else if (assignment == AssignmentKind.SIMPLE_LEFT) { | |
| 1316 AstFactory.assignmentExpression( | |
| 1317 expression, TokenType.EQ, AstFactory.identifier3("_")); | |
| 1318 } else if (assignment == AssignmentKind.SIMPLE_RIGHT) { | |
| 1319 AstFactory.assignmentExpression( | |
| 1320 AstFactory.identifier3("_"), TokenType.EQ, expression); | |
| 1321 } else if (assignment == AssignmentKind.NONE) {} | |
| 1322 break; | |
| 1323 } | |
| 1324 return identifier; | |
| 1325 } | |
| 1326 | |
| 1327 /** | |
| 1328 * Return the top-most node in the AST structure containing the given identifi
er. | |
| 1329 * | |
| 1330 * @param identifier the identifier in the AST structure being traversed | |
| 1331 * @return the root of the AST structure containing the identifier | |
| 1332 */ | |
| 1333 AstNode _topMostNode(SimpleIdentifier identifier) { | |
| 1334 AstNode child = identifier; | |
| 1335 AstNode parent = identifier.parent; | |
| 1336 while (parent != null) { | |
| 1337 child = parent; | |
| 1338 parent = parent.parent; | |
| 1339 } | |
| 1340 return child; | |
| 1341 } | |
| 1342 } | |
| 1343 | |
| 1344 @reflectiveTest | |
| 1345 class SimpleStringLiteralTest extends ParserTestCase { | |
| 1346 void test_contentsEnd() { | |
| 1347 expect( | |
| 1348 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X") | |
| 1349 .contentsEnd, | |
| 1350 2); | |
| 1351 expect( | |
| 1352 new SimpleStringLiteral(TokenFactory.tokenFromString('"X"'), "X") | |
| 1353 .contentsEnd, | |
| 1354 2); | |
| 1355 | |
| 1356 expect( | |
| 1357 new SimpleStringLiteral(TokenFactory.tokenFromString('"""X"""'), "X") | |
| 1358 .contentsEnd, | |
| 1359 4); | |
| 1360 expect( | |
| 1361 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") | |
| 1362 .contentsEnd, | |
| 1363 4); | |
| 1364 expect( | |
| 1365 new SimpleStringLiteral( | |
| 1366 TokenFactory.tokenFromString("''' \nX'''"), "X") | |
| 1367 .contentsEnd, | |
| 1368 7); | |
| 1369 | |
| 1370 expect( | |
| 1371 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") | |
| 1372 .contentsEnd, | |
| 1373 3); | |
| 1374 expect( | |
| 1375 new SimpleStringLiteral(TokenFactory.tokenFromString('r"X"'), "X") | |
| 1376 .contentsEnd, | |
| 1377 3); | |
| 1378 | |
| 1379 expect( | |
| 1380 new SimpleStringLiteral(TokenFactory.tokenFromString('r"""X"""'), "X") | |
| 1381 .contentsEnd, | |
| 1382 5); | |
| 1383 expect( | |
| 1384 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") | |
| 1385 .contentsEnd, | |
| 1386 5); | |
| 1387 expect( | |
| 1388 new SimpleStringLiteral( | |
| 1389 TokenFactory.tokenFromString("r''' \nX'''"), "X") | |
| 1390 .contentsEnd, | |
| 1391 8); | |
| 1392 } | |
| 1393 | |
| 1394 void test_contentsOffset() { | |
| 1395 expect( | |
| 1396 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X") | |
| 1397 .contentsOffset, | |
| 1398 1); | |
| 1399 expect( | |
| 1400 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X") | |
| 1401 .contentsOffset, | |
| 1402 1); | |
| 1403 expect( | |
| 1404 new SimpleStringLiteral( | |
| 1405 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X") | |
| 1406 .contentsOffset, | |
| 1407 3); | |
| 1408 expect( | |
| 1409 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") | |
| 1410 .contentsOffset, | |
| 1411 3); | |
| 1412 expect( | |
| 1413 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") | |
| 1414 .contentsOffset, | |
| 1415 2); | |
| 1416 expect( | |
| 1417 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X") | |
| 1418 .contentsOffset, | |
| 1419 2); | |
| 1420 expect( | |
| 1421 new SimpleStringLiteral( | |
| 1422 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X") | |
| 1423 .contentsOffset, | |
| 1424 4); | |
| 1425 expect( | |
| 1426 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") | |
| 1427 .contentsOffset, | |
| 1428 4); | |
| 1429 // leading whitespace | |
| 1430 expect( | |
| 1431 new SimpleStringLiteral( | |
| 1432 TokenFactory.tokenFromString("''' \ \nX''"), "X") | |
| 1433 .contentsOffset, | |
| 1434 6); | |
| 1435 expect( | |
| 1436 new SimpleStringLiteral( | |
| 1437 TokenFactory.tokenFromString('r""" \ \nX"""'), "X") | |
| 1438 .contentsOffset, | |
| 1439 7); | |
| 1440 } | |
| 1441 | |
| 1442 void test_isMultiline() { | |
| 1443 expect( | |
| 1444 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X") | |
| 1445 .isMultiline, | |
| 1446 isFalse); | |
| 1447 expect( | |
| 1448 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") | |
| 1449 .isMultiline, | |
| 1450 isFalse); | |
| 1451 expect( | |
| 1452 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X") | |
| 1453 .isMultiline, | |
| 1454 isFalse); | |
| 1455 expect( | |
| 1456 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X") | |
| 1457 .isMultiline, | |
| 1458 isFalse); | |
| 1459 expect( | |
| 1460 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") | |
| 1461 .isMultiline, | |
| 1462 isTrue); | |
| 1463 expect( | |
| 1464 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") | |
| 1465 .isMultiline, | |
| 1466 isTrue); | |
| 1467 expect( | |
| 1468 new SimpleStringLiteral( | |
| 1469 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X") | |
| 1470 .isMultiline, | |
| 1471 isTrue); | |
| 1472 expect( | |
| 1473 new SimpleStringLiteral( | |
| 1474 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X") | |
| 1475 .isMultiline, | |
| 1476 isTrue); | |
| 1477 } | |
| 1478 | |
| 1479 void test_isRaw() { | |
| 1480 expect( | |
| 1481 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X").isRaw, | |
| 1482 isFalse); | |
| 1483 expect( | |
| 1484 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X") | |
| 1485 .isRaw, | |
| 1486 isFalse); | |
| 1487 expect( | |
| 1488 new SimpleStringLiteral( | |
| 1489 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X") | |
| 1490 .isRaw, | |
| 1491 isFalse); | |
| 1492 expect( | |
| 1493 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") | |
| 1494 .isRaw, | |
| 1495 isFalse); | |
| 1496 expect( | |
| 1497 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") | |
| 1498 .isRaw, | |
| 1499 isTrue); | |
| 1500 expect( | |
| 1501 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X") | |
| 1502 .isRaw, | |
| 1503 isTrue); | |
| 1504 expect( | |
| 1505 new SimpleStringLiteral( | |
| 1506 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X") | |
| 1507 .isRaw, | |
| 1508 isTrue); | |
| 1509 expect( | |
| 1510 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") | |
| 1511 .isRaw, | |
| 1512 isTrue); | |
| 1513 } | |
| 1514 | |
| 1515 void test_isSingleQuoted() { | |
| 1516 // ' | |
| 1517 { | |
| 1518 var token = TokenFactory.tokenFromString("'X'"); | |
| 1519 var node = new SimpleStringLiteral(token, null); | |
| 1520 expect(node.isSingleQuoted, isTrue); | |
| 1521 } | |
| 1522 // ''' | |
| 1523 { | |
| 1524 var token = TokenFactory.tokenFromString("'''X'''"); | |
| 1525 var node = new SimpleStringLiteral(token, null); | |
| 1526 expect(node.isSingleQuoted, isTrue); | |
| 1527 } | |
| 1528 // " | |
| 1529 { | |
| 1530 var token = TokenFactory.tokenFromString('"X"'); | |
| 1531 var node = new SimpleStringLiteral(token, null); | |
| 1532 expect(node.isSingleQuoted, isFalse); | |
| 1533 } | |
| 1534 // """ | |
| 1535 { | |
| 1536 var token = TokenFactory.tokenFromString('"""X"""'); | |
| 1537 var node = new SimpleStringLiteral(token, null); | |
| 1538 expect(node.isSingleQuoted, isFalse); | |
| 1539 } | |
| 1540 } | |
| 1541 | |
| 1542 void test_isSingleQuoted_raw() { | |
| 1543 // r' | |
| 1544 { | |
| 1545 var token = TokenFactory.tokenFromString("r'X'"); | |
| 1546 var node = new SimpleStringLiteral(token, null); | |
| 1547 expect(node.isSingleQuoted, isTrue); | |
| 1548 } | |
| 1549 // r''' | |
| 1550 { | |
| 1551 var token = TokenFactory.tokenFromString("r'''X'''"); | |
| 1552 var node = new SimpleStringLiteral(token, null); | |
| 1553 expect(node.isSingleQuoted, isTrue); | |
| 1554 } | |
| 1555 // r" | |
| 1556 { | |
| 1557 var token = TokenFactory.tokenFromString('r"X"'); | |
| 1558 var node = new SimpleStringLiteral(token, null); | |
| 1559 expect(node.isSingleQuoted, isFalse); | |
| 1560 } | |
| 1561 // r""" | |
| 1562 { | |
| 1563 var token = TokenFactory.tokenFromString('r"""X"""'); | |
| 1564 var node = new SimpleStringLiteral(token, null); | |
| 1565 expect(node.isSingleQuoted, isFalse); | |
| 1566 } | |
| 1567 } | |
| 1568 | |
| 1569 void test_simple() { | |
| 1570 Token token = TokenFactory.tokenFromString("'value'"); | |
| 1571 SimpleStringLiteral stringLiteral = new SimpleStringLiteral(token, "value"); | |
| 1572 expect(stringLiteral.literal, same(token)); | |
| 1573 expect(stringLiteral.beginToken, same(token)); | |
| 1574 expect(stringLiteral.endToken, same(token)); | |
| 1575 expect(stringLiteral.value, "value"); | |
| 1576 } | |
| 1577 } | |
| 1578 | |
| 1579 @reflectiveTest | |
| 1580 class StringInterpolationTest extends ParserTestCase { | |
| 1581 void test_contentsOffsetEnd() { | |
| 1582 AstFactory.interpolationExpression(AstFactory.identifier3('bb')); | |
| 1583 // 'a${bb}ccc' | |
| 1584 { | |
| 1585 var ae = AstFactory.interpolationString("'a", "a"); | |
| 1586 var cToken = new StringToken(TokenType.STRING, "ccc'", 10); | |
| 1587 var cElement = new InterpolationString(cToken, 'ccc'); | |
| 1588 StringInterpolation node = AstFactory.string([ae, ae, cElement]); | |
| 1589 expect(node.contentsOffset, 1); | |
| 1590 expect(node.contentsEnd, 10 + 4 - 1); | |
| 1591 } | |
| 1592 // '''a${bb}ccc''' | |
| 1593 { | |
| 1594 var ae = AstFactory.interpolationString("'''a", "a"); | |
| 1595 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10); | |
| 1596 var cElement = new InterpolationString(cToken, 'ccc'); | |
| 1597 StringInterpolation node = AstFactory.string([ae, ae, cElement]); | |
| 1598 expect(node.contentsOffset, 3); | |
| 1599 expect(node.contentsEnd, 10 + 4 - 1); | |
| 1600 } | |
| 1601 // """a${bb}ccc""" | |
| 1602 { | |
| 1603 var ae = AstFactory.interpolationString('"""a', "a"); | |
| 1604 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10); | |
| 1605 var cElement = new InterpolationString(cToken, 'ccc'); | |
| 1606 StringInterpolation node = AstFactory.string([ae, ae, cElement]); | |
| 1607 expect(node.contentsOffset, 3); | |
| 1608 expect(node.contentsEnd, 10 + 4 - 1); | |
| 1609 } | |
| 1610 // r'a${bb}ccc' | |
| 1611 { | |
| 1612 var ae = AstFactory.interpolationString("r'a", "a"); | |
| 1613 var cToken = new StringToken(TokenType.STRING, "ccc'", 10); | |
| 1614 var cElement = new InterpolationString(cToken, 'ccc'); | |
| 1615 StringInterpolation node = AstFactory.string([ae, ae, cElement]); | |
| 1616 expect(node.contentsOffset, 2); | |
| 1617 expect(node.contentsEnd, 10 + 4 - 1); | |
| 1618 } | |
| 1619 // r'''a${bb}ccc''' | |
| 1620 { | |
| 1621 var ae = AstFactory.interpolationString("r'''a", "a"); | |
| 1622 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10); | |
| 1623 var cElement = new InterpolationString(cToken, 'ccc'); | |
| 1624 StringInterpolation node = AstFactory.string([ae, ae, cElement]); | |
| 1625 expect(node.contentsOffset, 4); | |
| 1626 expect(node.contentsEnd, 10 + 4 - 1); | |
| 1627 } | |
| 1628 // r"""a${bb}ccc""" | |
| 1629 { | |
| 1630 var ae = AstFactory.interpolationString('r"""a', "a"); | |
| 1631 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10); | |
| 1632 var cElement = new InterpolationString(cToken, 'ccc'); | |
| 1633 StringInterpolation node = AstFactory.string([ae, ae, cElement]); | |
| 1634 expect(node.contentsOffset, 4); | |
| 1635 expect(node.contentsEnd, 10 + 4 - 1); | |
| 1636 } | |
| 1637 } | |
| 1638 | |
| 1639 void test_isMultiline() { | |
| 1640 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb')); | |
| 1641 // ' | |
| 1642 { | |
| 1643 var a = AstFactory.interpolationString("'a", "a"); | |
| 1644 var c = AstFactory.interpolationString("ccc'", "ccc"); | |
| 1645 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1646 expect(node.isMultiline, isFalse); | |
| 1647 } | |
| 1648 // ''' | |
| 1649 { | |
| 1650 var a = AstFactory.interpolationString("'''a", "a"); | |
| 1651 var c = AstFactory.interpolationString("ccc'''", "ccc"); | |
| 1652 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1653 expect(node.isMultiline, isTrue); | |
| 1654 } | |
| 1655 // " | |
| 1656 { | |
| 1657 var a = AstFactory.interpolationString('"a', "a"); | |
| 1658 var c = AstFactory.interpolationString('ccc"', "ccc"); | |
| 1659 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1660 expect(node.isMultiline, isFalse); | |
| 1661 } | |
| 1662 // """ | |
| 1663 { | |
| 1664 var a = AstFactory.interpolationString('"""a', "a"); | |
| 1665 var c = AstFactory.interpolationString('ccc"""', "ccc"); | |
| 1666 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1667 expect(node.isMultiline, isTrue); | |
| 1668 } | |
| 1669 } | |
| 1670 | |
| 1671 void test_isRaw() { | |
| 1672 StringInterpolation node = AstFactory.string(); | |
| 1673 expect(node.isRaw, isFalse); | |
| 1674 } | |
| 1675 | |
| 1676 void test_isSingleQuoted() { | |
| 1677 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb')); | |
| 1678 // " | |
| 1679 { | |
| 1680 var a = AstFactory.interpolationString('"a', "a"); | |
| 1681 var c = AstFactory.interpolationString('ccc"', "ccc"); | |
| 1682 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1683 expect(node.isSingleQuoted, isFalse); | |
| 1684 } | |
| 1685 // """ | |
| 1686 { | |
| 1687 var a = AstFactory.interpolationString('"""a', "a"); | |
| 1688 var c = AstFactory.interpolationString('ccc"""', "ccc"); | |
| 1689 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1690 expect(node.isSingleQuoted, isFalse); | |
| 1691 } | |
| 1692 // ' | |
| 1693 { | |
| 1694 var a = AstFactory.interpolationString("'a", "a"); | |
| 1695 var c = AstFactory.interpolationString("ccc'", "ccc"); | |
| 1696 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1697 expect(node.isSingleQuoted, isTrue); | |
| 1698 } | |
| 1699 // ''' | |
| 1700 { | |
| 1701 var a = AstFactory.interpolationString("'''a", "a"); | |
| 1702 var c = AstFactory.interpolationString("ccc'''", "ccc"); | |
| 1703 StringInterpolation node = AstFactory.string([a, b, c]); | |
| 1704 expect(node.isSingleQuoted, isTrue); | |
| 1705 } | |
| 1706 } | |
| 1707 } | |
| 1708 | |
| 1709 @reflectiveTest | |
| 1710 class ToSourceVisitorTest extends EngineTestCase { | |
| 1711 void test_visitAdjacentStrings() { | |
| 1712 _assertSource( | |
| 1713 "'a' 'b'", | |
| 1714 AstFactory.adjacentStrings( | |
| 1715 [AstFactory.string2("a"), AstFactory.string2("b")])); | |
| 1716 } | |
| 1717 | |
| 1718 void test_visitAnnotation_constant() { | |
| 1719 _assertSource("@A", AstFactory.annotation(AstFactory.identifier3("A"))); | |
| 1720 } | |
| 1721 | |
| 1722 void test_visitAnnotation_constructor() { | |
| 1723 _assertSource( | |
| 1724 "@A.c()", | |
| 1725 AstFactory.annotation2(AstFactory.identifier3("A"), | |
| 1726 AstFactory.identifier3("c"), AstFactory.argumentList())); | |
| 1727 } | |
| 1728 | |
| 1729 void test_visitArgumentList() { | |
| 1730 _assertSource( | |
| 1731 "(a, b)", | |
| 1732 AstFactory.argumentList( | |
| 1733 [AstFactory.identifier3("a"), AstFactory.identifier3("b")])); | |
| 1734 } | |
| 1735 | |
| 1736 void test_visitAsExpression() { | |
| 1737 _assertSource( | |
| 1738 "e as T", | |
| 1739 AstFactory.asExpression( | |
| 1740 AstFactory.identifier3("e"), AstFactory.typeName4("T"))); | |
| 1741 } | |
| 1742 | |
| 1743 void test_visitAssertStatement() { | |
| 1744 _assertSource( | |
| 1745 "assert (a);", AstFactory.assertStatement(AstFactory.identifier3("a"))); | |
| 1746 } | |
| 1747 | |
| 1748 void test_visitAssertStatement_withMessage() { | |
| 1749 _assertSource( | |
| 1750 "assert (a, b);", | |
| 1751 AstFactory.assertStatement( | |
| 1752 AstFactory.identifier3("a"), AstFactory.identifier3('b'))); | |
| 1753 } | |
| 1754 | |
| 1755 void test_visitAssignmentExpression() { | |
| 1756 _assertSource( | |
| 1757 "a = b", | |
| 1758 AstFactory.assignmentExpression(AstFactory.identifier3("a"), | |
| 1759 TokenType.EQ, AstFactory.identifier3("b"))); | |
| 1760 } | |
| 1761 | |
| 1762 void test_visitAwaitExpression() { | |
| 1763 _assertSource( | |
| 1764 "await e", AstFactory.awaitExpression(AstFactory.identifier3("e"))); | |
| 1765 } | |
| 1766 | |
| 1767 void test_visitBinaryExpression() { | |
| 1768 _assertSource( | |
| 1769 "a + b", | |
| 1770 AstFactory.binaryExpression(AstFactory.identifier3("a"), TokenType.PLUS, | |
| 1771 AstFactory.identifier3("b"))); | |
| 1772 } | |
| 1773 | |
| 1774 void test_visitBlock_empty() { | |
| 1775 _assertSource("{}", AstFactory.block()); | |
| 1776 } | |
| 1777 | |
| 1778 void test_visitBlock_nonEmpty() { | |
| 1779 _assertSource( | |
| 1780 "{break; break;}", | |
| 1781 AstFactory | |
| 1782 .block([AstFactory.breakStatement(), AstFactory.breakStatement()])); | |
| 1783 } | |
| 1784 | |
| 1785 void test_visitBlockFunctionBody_async() { | |
| 1786 _assertSource("async {}", AstFactory.asyncBlockFunctionBody()); | |
| 1787 } | |
| 1788 | |
| 1789 void test_visitBlockFunctionBody_async_star() { | |
| 1790 _assertSource("async* {}", AstFactory.asyncGeneratorBlockFunctionBody()); | |
| 1791 } | |
| 1792 | |
| 1793 void test_visitBlockFunctionBody_simple() { | |
| 1794 _assertSource("{}", AstFactory.blockFunctionBody2()); | |
| 1795 } | |
| 1796 | |
| 1797 void test_visitBlockFunctionBody_sync() { | |
| 1798 _assertSource("sync {}", AstFactory.syncBlockFunctionBody()); | |
| 1799 } | |
| 1800 | |
| 1801 void test_visitBlockFunctionBody_sync_star() { | |
| 1802 _assertSource("sync* {}", AstFactory.syncGeneratorBlockFunctionBody()); | |
| 1803 } | |
| 1804 | |
| 1805 void test_visitBooleanLiteral_false() { | |
| 1806 _assertSource("false", AstFactory.booleanLiteral(false)); | |
| 1807 } | |
| 1808 | |
| 1809 void test_visitBooleanLiteral_true() { | |
| 1810 _assertSource("true", AstFactory.booleanLiteral(true)); | |
| 1811 } | |
| 1812 | |
| 1813 void test_visitBreakStatement_label() { | |
| 1814 _assertSource("break l;", AstFactory.breakStatement2("l")); | |
| 1815 } | |
| 1816 | |
| 1817 void test_visitBreakStatement_noLabel() { | |
| 1818 _assertSource("break;", AstFactory.breakStatement()); | |
| 1819 } | |
| 1820 | |
| 1821 void test_visitCascadeExpression_field() { | |
| 1822 _assertSource( | |
| 1823 "a..b..c", | |
| 1824 AstFactory.cascadeExpression(AstFactory.identifier3("a"), [ | |
| 1825 AstFactory.cascadedPropertyAccess("b"), | |
| 1826 AstFactory.cascadedPropertyAccess("c") | |
| 1827 ])); | |
| 1828 } | |
| 1829 | |
| 1830 void test_visitCascadeExpression_index() { | |
| 1831 _assertSource( | |
| 1832 "a..[0]..[1]", | |
| 1833 AstFactory.cascadeExpression(AstFactory.identifier3("a"), [ | |
| 1834 AstFactory.cascadedIndexExpression(AstFactory.integer(0)), | |
| 1835 AstFactory.cascadedIndexExpression(AstFactory.integer(1)) | |
| 1836 ])); | |
| 1837 } | |
| 1838 | |
| 1839 void test_visitCascadeExpression_method() { | |
| 1840 _assertSource( | |
| 1841 "a..b()..c()", | |
| 1842 AstFactory.cascadeExpression(AstFactory.identifier3("a"), [ | |
| 1843 AstFactory.cascadedMethodInvocation("b"), | |
| 1844 AstFactory.cascadedMethodInvocation("c") | |
| 1845 ])); | |
| 1846 } | |
| 1847 | |
| 1848 void test_visitCatchClause_catch_noStack() { | |
| 1849 _assertSource("catch (e) {}", AstFactory.catchClause("e")); | |
| 1850 } | |
| 1851 | |
| 1852 void test_visitCatchClause_catch_stack() { | |
| 1853 _assertSource("catch (e, s) {}", AstFactory.catchClause2("e", "s")); | |
| 1854 } | |
| 1855 | |
| 1856 void test_visitCatchClause_on() { | |
| 1857 _assertSource( | |
| 1858 "on E {}", AstFactory.catchClause3(AstFactory.typeName4("E"))); | |
| 1859 } | |
| 1860 | |
| 1861 void test_visitCatchClause_on_catch() { | |
| 1862 _assertSource("on E catch (e) {}", | |
| 1863 AstFactory.catchClause4(AstFactory.typeName4("E"), "e")); | |
| 1864 } | |
| 1865 | |
| 1866 void test_visitClassDeclaration_abstract() { | |
| 1867 _assertSource( | |
| 1868 "abstract class C {}", | |
| 1869 AstFactory.classDeclaration( | |
| 1870 Keyword.ABSTRACT, "C", null, null, null, null)); | |
| 1871 } | |
| 1872 | |
| 1873 void test_visitClassDeclaration_empty() { | |
| 1874 _assertSource("class C {}", | |
| 1875 AstFactory.classDeclaration(null, "C", null, null, null, null)); | |
| 1876 } | |
| 1877 | |
| 1878 void test_visitClassDeclaration_extends() { | |
| 1879 _assertSource( | |
| 1880 "class C extends A {}", | |
| 1881 AstFactory.classDeclaration(null, "C", null, | |
| 1882 AstFactory.extendsClause(AstFactory.typeName4("A")), null, null)); | |
| 1883 } | |
| 1884 | |
| 1885 void test_visitClassDeclaration_extends_implements() { | |
| 1886 _assertSource( | |
| 1887 "class C extends A implements B {}", | |
| 1888 AstFactory.classDeclaration( | |
| 1889 null, | |
| 1890 "C", | |
| 1891 null, | |
| 1892 AstFactory.extendsClause(AstFactory.typeName4("A")), | |
| 1893 null, | |
| 1894 AstFactory.implementsClause([AstFactory.typeName4("B")]))); | |
| 1895 } | |
| 1896 | |
| 1897 void test_visitClassDeclaration_extends_with() { | |
| 1898 _assertSource( | |
| 1899 "class C extends A with M {}", | |
| 1900 AstFactory.classDeclaration( | |
| 1901 null, | |
| 1902 "C", | |
| 1903 null, | |
| 1904 AstFactory.extendsClause(AstFactory.typeName4("A")), | |
| 1905 AstFactory.withClause([AstFactory.typeName4("M")]), | |
| 1906 null)); | |
| 1907 } | |
| 1908 | |
| 1909 void test_visitClassDeclaration_extends_with_implements() { | |
| 1910 _assertSource( | |
| 1911 "class C extends A with M implements B {}", | |
| 1912 AstFactory.classDeclaration( | |
| 1913 null, | |
| 1914 "C", | |
| 1915 null, | |
| 1916 AstFactory.extendsClause(AstFactory.typeName4("A")), | |
| 1917 AstFactory.withClause([AstFactory.typeName4("M")]), | |
| 1918 AstFactory.implementsClause([AstFactory.typeName4("B")]))); | |
| 1919 } | |
| 1920 | |
| 1921 void test_visitClassDeclaration_implements() { | |
| 1922 _assertSource( | |
| 1923 "class C implements B {}", | |
| 1924 AstFactory.classDeclaration(null, "C", null, null, null, | |
| 1925 AstFactory.implementsClause([AstFactory.typeName4("B")]))); | |
| 1926 } | |
| 1927 | |
| 1928 void test_visitClassDeclaration_multipleMember() { | |
| 1929 _assertSource( | |
| 1930 "class C {var a; var b;}", | |
| 1931 AstFactory.classDeclaration(null, "C", null, null, null, null, [ | |
| 1932 AstFactory.fieldDeclaration2( | |
| 1933 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]), | |
| 1934 AstFactory.fieldDeclaration2( | |
| 1935 false, Keyword.VAR, [AstFactory.variableDeclaration("b")]) | |
| 1936 ])); | |
| 1937 } | |
| 1938 | |
| 1939 void test_visitClassDeclaration_parameters() { | |
| 1940 _assertSource( | |
| 1941 "class C<E> {}", | |
| 1942 AstFactory.classDeclaration( | |
| 1943 null, "C", AstFactory.typeParameterList(["E"]), null, null, null)); | |
| 1944 } | |
| 1945 | |
| 1946 void test_visitClassDeclaration_parameters_extends() { | |
| 1947 _assertSource( | |
| 1948 "class C<E> extends A {}", | |
| 1949 AstFactory.classDeclaration( | |
| 1950 null, | |
| 1951 "C", | |
| 1952 AstFactory.typeParameterList(["E"]), | |
| 1953 AstFactory.extendsClause(AstFactory.typeName4("A")), | |
| 1954 null, | |
| 1955 null)); | |
| 1956 } | |
| 1957 | |
| 1958 void test_visitClassDeclaration_parameters_extends_implements() { | |
| 1959 _assertSource( | |
| 1960 "class C<E> extends A implements B {}", | |
| 1961 AstFactory.classDeclaration( | |
| 1962 null, | |
| 1963 "C", | |
| 1964 AstFactory.typeParameterList(["E"]), | |
| 1965 AstFactory.extendsClause(AstFactory.typeName4("A")), | |
| 1966 null, | |
| 1967 AstFactory.implementsClause([AstFactory.typeName4("B")]))); | |
| 1968 } | |
| 1969 | |
| 1970 void test_visitClassDeclaration_parameters_extends_with() { | |
| 1971 _assertSource( | |
| 1972 "class C<E> extends A with M {}", | |
| 1973 AstFactory.classDeclaration( | |
| 1974 null, | |
| 1975 "C", | |
| 1976 AstFactory.typeParameterList(["E"]), | |
| 1977 AstFactory.extendsClause(AstFactory.typeName4("A")), | |
| 1978 AstFactory.withClause([AstFactory.typeName4("M")]), | |
| 1979 null)); | |
| 1980 } | |
| 1981 | |
| 1982 void test_visitClassDeclaration_parameters_extends_with_implements() { | |
| 1983 _assertSource( | |
| 1984 "class C<E> extends A with M implements B {}", | |
| 1985 AstFactory.classDeclaration( | |
| 1986 null, | |
| 1987 "C", | |
| 1988 AstFactory.typeParameterList(["E"]), | |
| 1989 AstFactory.extendsClause(AstFactory.typeName4("A")), | |
| 1990 AstFactory.withClause([AstFactory.typeName4("M")]), | |
| 1991 AstFactory.implementsClause([AstFactory.typeName4("B")]))); | |
| 1992 } | |
| 1993 | |
| 1994 void test_visitClassDeclaration_parameters_implements() { | |
| 1995 _assertSource( | |
| 1996 "class C<E> implements B {}", | |
| 1997 AstFactory.classDeclaration( | |
| 1998 null, | |
| 1999 "C", | |
| 2000 AstFactory.typeParameterList(["E"]), | |
| 2001 null, | |
| 2002 null, | |
| 2003 AstFactory.implementsClause([AstFactory.typeName4("B")]))); | |
| 2004 } | |
| 2005 | |
| 2006 void test_visitClassDeclaration_singleMember() { | |
| 2007 _assertSource( | |
| 2008 "class C {var a;}", | |
| 2009 AstFactory.classDeclaration(null, "C", null, null, null, null, [ | |
| 2010 AstFactory.fieldDeclaration2( | |
| 2011 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]) | |
| 2012 ])); | |
| 2013 } | |
| 2014 | |
| 2015 void test_visitClassDeclaration_withMetadata() { | |
| 2016 ClassDeclaration declaration = | |
| 2017 AstFactory.classDeclaration(null, "C", null, null, null, null); | |
| 2018 declaration.metadata | |
| 2019 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 2020 _assertSource("@deprecated class C {}", declaration); | |
| 2021 } | |
| 2022 | |
| 2023 void test_visitClassTypeAlias_abstract() { | |
| 2024 _assertSource( | |
| 2025 "abstract class C = S with M1;", | |
| 2026 AstFactory.classTypeAlias( | |
| 2027 "C", | |
| 2028 null, | |
| 2029 Keyword.ABSTRACT, | |
| 2030 AstFactory.typeName4("S"), | |
| 2031 AstFactory.withClause([AstFactory.typeName4("M1")]), | |
| 2032 null)); | |
| 2033 } | |
| 2034 | |
| 2035 void test_visitClassTypeAlias_abstract_implements() { | |
| 2036 _assertSource( | |
| 2037 "abstract class C = S with M1 implements I;", | |
| 2038 AstFactory.classTypeAlias( | |
| 2039 "C", | |
| 2040 null, | |
| 2041 Keyword.ABSTRACT, | |
| 2042 AstFactory.typeName4("S"), | |
| 2043 AstFactory.withClause([AstFactory.typeName4("M1")]), | |
| 2044 AstFactory.implementsClause([AstFactory.typeName4("I")]))); | |
| 2045 } | |
| 2046 | |
| 2047 void test_visitClassTypeAlias_generic() { | |
| 2048 _assertSource( | |
| 2049 "class C<E> = S<E> with M1<E>;", | |
| 2050 AstFactory.classTypeAlias( | |
| 2051 "C", | |
| 2052 AstFactory.typeParameterList(["E"]), | |
| 2053 null, | |
| 2054 AstFactory.typeName4("S", [AstFactory.typeName4("E")]), | |
| 2055 AstFactory.withClause([ | |
| 2056 AstFactory.typeName4("M1", [AstFactory.typeName4("E")]) | |
| 2057 ]), | |
| 2058 null)); | |
| 2059 } | |
| 2060 | |
| 2061 void test_visitClassTypeAlias_implements() { | |
| 2062 _assertSource( | |
| 2063 "class C = S with M1 implements I;", | |
| 2064 AstFactory.classTypeAlias( | |
| 2065 "C", | |
| 2066 null, | |
| 2067 null, | |
| 2068 AstFactory.typeName4("S"), | |
| 2069 AstFactory.withClause([AstFactory.typeName4("M1")]), | |
| 2070 AstFactory.implementsClause([AstFactory.typeName4("I")]))); | |
| 2071 } | |
| 2072 | |
| 2073 void test_visitClassTypeAlias_minimal() { | |
| 2074 _assertSource( | |
| 2075 "class C = S with M1;", | |
| 2076 AstFactory.classTypeAlias("C", null, null, AstFactory.typeName4("S"), | |
| 2077 AstFactory.withClause([AstFactory.typeName4("M1")]), null)); | |
| 2078 } | |
| 2079 | |
| 2080 void test_visitClassTypeAlias_parameters_abstract() { | |
| 2081 _assertSource( | |
| 2082 "abstract class C<E> = S with M1;", | |
| 2083 AstFactory.classTypeAlias( | |
| 2084 "C", | |
| 2085 AstFactory.typeParameterList(["E"]), | |
| 2086 Keyword.ABSTRACT, | |
| 2087 AstFactory.typeName4("S"), | |
| 2088 AstFactory.withClause([AstFactory.typeName4("M1")]), | |
| 2089 null)); | |
| 2090 } | |
| 2091 | |
| 2092 void test_visitClassTypeAlias_parameters_abstract_implements() { | |
| 2093 _assertSource( | |
| 2094 "abstract class C<E> = S with M1 implements I;", | |
| 2095 AstFactory.classTypeAlias( | |
| 2096 "C", | |
| 2097 AstFactory.typeParameterList(["E"]), | |
| 2098 Keyword.ABSTRACT, | |
| 2099 AstFactory.typeName4("S"), | |
| 2100 AstFactory.withClause([AstFactory.typeName4("M1")]), | |
| 2101 AstFactory.implementsClause([AstFactory.typeName4("I")]))); | |
| 2102 } | |
| 2103 | |
| 2104 void test_visitClassTypeAlias_parameters_implements() { | |
| 2105 _assertSource( | |
| 2106 "class C<E> = S with M1 implements I;", | |
| 2107 AstFactory.classTypeAlias( | |
| 2108 "C", | |
| 2109 AstFactory.typeParameterList(["E"]), | |
| 2110 null, | |
| 2111 AstFactory.typeName4("S"), | |
| 2112 AstFactory.withClause([AstFactory.typeName4("M1")]), | |
| 2113 AstFactory.implementsClause([AstFactory.typeName4("I")]))); | |
| 2114 } | |
| 2115 | |
| 2116 void test_visitClassTypeAlias_withMetadata() { | |
| 2117 ClassTypeAlias declaration = AstFactory.classTypeAlias( | |
| 2118 "C", | |
| 2119 null, | |
| 2120 null, | |
| 2121 AstFactory.typeName4("S"), | |
| 2122 AstFactory.withClause([AstFactory.typeName4("M1")]), | |
| 2123 null); | |
| 2124 declaration.metadata | |
| 2125 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 2126 _assertSource("@deprecated class C = S with M1;", declaration); | |
| 2127 } | |
| 2128 | |
| 2129 void test_visitComment() { | |
| 2130 _assertSource( | |
| 2131 "", | |
| 2132 Comment.createBlockComment( | |
| 2133 <Token>[TokenFactory.tokenFromString("/* comment */")])); | |
| 2134 } | |
| 2135 | |
| 2136 void test_visitCommentReference() { | |
| 2137 _assertSource("", new CommentReference(null, AstFactory.identifier3("a"))); | |
| 2138 } | |
| 2139 | |
| 2140 void test_visitCompilationUnit_declaration() { | |
| 2141 _assertSource( | |
| 2142 "var a;", | |
| 2143 AstFactory.compilationUnit2([ | |
| 2144 AstFactory.topLevelVariableDeclaration2( | |
| 2145 Keyword.VAR, [AstFactory.variableDeclaration("a")]) | |
| 2146 ])); | |
| 2147 } | |
| 2148 | |
| 2149 void test_visitCompilationUnit_directive() { | |
| 2150 _assertSource("library l;", | |
| 2151 AstFactory.compilationUnit3([AstFactory.libraryDirective2("l")])); | |
| 2152 } | |
| 2153 | |
| 2154 void test_visitCompilationUnit_directive_declaration() { | |
| 2155 _assertSource( | |
| 2156 "library l; var a;", | |
| 2157 AstFactory.compilationUnit4([ | |
| 2158 AstFactory.libraryDirective2("l") | |
| 2159 ], [ | |
| 2160 AstFactory.topLevelVariableDeclaration2( | |
| 2161 Keyword.VAR, [AstFactory.variableDeclaration("a")]) | |
| 2162 ])); | |
| 2163 } | |
| 2164 | |
| 2165 void test_visitCompilationUnit_empty() { | |
| 2166 _assertSource("", AstFactory.compilationUnit()); | |
| 2167 } | |
| 2168 | |
| 2169 void test_visitCompilationUnit_script() { | |
| 2170 _assertSource( | |
| 2171 "!#/bin/dartvm", AstFactory.compilationUnit5("!#/bin/dartvm")); | |
| 2172 } | |
| 2173 | |
| 2174 void test_visitCompilationUnit_script_declaration() { | |
| 2175 _assertSource( | |
| 2176 "!#/bin/dartvm var a;", | |
| 2177 AstFactory.compilationUnit6("!#/bin/dartvm", [ | |
| 2178 AstFactory.topLevelVariableDeclaration2( | |
| 2179 Keyword.VAR, [AstFactory.variableDeclaration("a")]) | |
| 2180 ])); | |
| 2181 } | |
| 2182 | |
| 2183 void test_visitCompilationUnit_script_directive() { | |
| 2184 _assertSource( | |
| 2185 "!#/bin/dartvm library l;", | |
| 2186 AstFactory.compilationUnit7( | |
| 2187 "!#/bin/dartvm", [AstFactory.libraryDirective2("l")])); | |
| 2188 } | |
| 2189 | |
| 2190 void test_visitCompilationUnit_script_directives_declarations() { | |
| 2191 _assertSource( | |
| 2192 "!#/bin/dartvm library l; var a;", | |
| 2193 AstFactory.compilationUnit8("!#/bin/dartvm", [ | |
| 2194 AstFactory.libraryDirective2("l") | |
| 2195 ], [ | |
| 2196 AstFactory.topLevelVariableDeclaration2( | |
| 2197 Keyword.VAR, [AstFactory.variableDeclaration("a")]) | |
| 2198 ])); | |
| 2199 } | |
| 2200 | |
| 2201 void test_visitConditionalExpression() { | |
| 2202 _assertSource( | |
| 2203 "a ? b : c", | |
| 2204 AstFactory.conditionalExpression(AstFactory.identifier3("a"), | |
| 2205 AstFactory.identifier3("b"), AstFactory.identifier3("c"))); | |
| 2206 } | |
| 2207 | |
| 2208 void test_visitConstructorDeclaration_const() { | |
| 2209 _assertSource( | |
| 2210 "const C() {}", | |
| 2211 AstFactory.constructorDeclaration2( | |
| 2212 Keyword.CONST, | |
| 2213 null, | |
| 2214 AstFactory.identifier3("C"), | |
| 2215 null, | |
| 2216 AstFactory.formalParameterList(), | |
| 2217 null, | |
| 2218 AstFactory.blockFunctionBody2())); | |
| 2219 } | |
| 2220 | |
| 2221 void test_visitConstructorDeclaration_external() { | |
| 2222 _assertSource( | |
| 2223 "external C();", | |
| 2224 AstFactory.constructorDeclaration(AstFactory.identifier3("C"), null, | |
| 2225 AstFactory.formalParameterList(), null)); | |
| 2226 } | |
| 2227 | |
| 2228 void test_visitConstructorDeclaration_minimal() { | |
| 2229 _assertSource( | |
| 2230 "C() {}", | |
| 2231 AstFactory.constructorDeclaration2( | |
| 2232 null, | |
| 2233 null, | |
| 2234 AstFactory.identifier3("C"), | |
| 2235 null, | |
| 2236 AstFactory.formalParameterList(), | |
| 2237 null, | |
| 2238 AstFactory.blockFunctionBody2())); | |
| 2239 } | |
| 2240 | |
| 2241 void test_visitConstructorDeclaration_multipleInitializers() { | |
| 2242 _assertSource( | |
| 2243 "C() : a = b, c = d {}", | |
| 2244 AstFactory.constructorDeclaration2( | |
| 2245 null, | |
| 2246 null, | |
| 2247 AstFactory.identifier3("C"), | |
| 2248 null, | |
| 2249 AstFactory.formalParameterList(), | |
| 2250 [ | |
| 2251 AstFactory.constructorFieldInitializer( | |
| 2252 false, "a", AstFactory.identifier3("b")), | |
| 2253 AstFactory.constructorFieldInitializer( | |
| 2254 false, "c", AstFactory.identifier3("d")) | |
| 2255 ], | |
| 2256 AstFactory.blockFunctionBody2())); | |
| 2257 } | |
| 2258 | |
| 2259 void test_visitConstructorDeclaration_multipleParameters() { | |
| 2260 _assertSource( | |
| 2261 "C(var a, var b) {}", | |
| 2262 AstFactory.constructorDeclaration2( | |
| 2263 null, | |
| 2264 null, | |
| 2265 AstFactory.identifier3("C"), | |
| 2266 null, | |
| 2267 AstFactory.formalParameterList([ | |
| 2268 AstFactory.simpleFormalParameter(Keyword.VAR, "a"), | |
| 2269 AstFactory.simpleFormalParameter(Keyword.VAR, "b") | |
| 2270 ]), | |
| 2271 null, | |
| 2272 AstFactory.blockFunctionBody2())); | |
| 2273 } | |
| 2274 | |
| 2275 void test_visitConstructorDeclaration_named() { | |
| 2276 _assertSource( | |
| 2277 "C.m() {}", | |
| 2278 AstFactory.constructorDeclaration2( | |
| 2279 null, | |
| 2280 null, | |
| 2281 AstFactory.identifier3("C"), | |
| 2282 "m", | |
| 2283 AstFactory.formalParameterList(), | |
| 2284 null, | |
| 2285 AstFactory.blockFunctionBody2())); | |
| 2286 } | |
| 2287 | |
| 2288 void test_visitConstructorDeclaration_singleInitializer() { | |
| 2289 _assertSource( | |
| 2290 "C() : a = b {}", | |
| 2291 AstFactory.constructorDeclaration2( | |
| 2292 null, | |
| 2293 null, | |
| 2294 AstFactory.identifier3("C"), | |
| 2295 null, | |
| 2296 AstFactory.formalParameterList(), | |
| 2297 [ | |
| 2298 AstFactory.constructorFieldInitializer( | |
| 2299 false, "a", AstFactory.identifier3("b")) | |
| 2300 ], | |
| 2301 AstFactory.blockFunctionBody2())); | |
| 2302 } | |
| 2303 | |
| 2304 void test_visitConstructorDeclaration_withMetadata() { | |
| 2305 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( | |
| 2306 null, | |
| 2307 null, | |
| 2308 AstFactory.identifier3("C"), | |
| 2309 null, | |
| 2310 AstFactory.formalParameterList(), | |
| 2311 null, | |
| 2312 AstFactory.blockFunctionBody2()); | |
| 2313 declaration.metadata | |
| 2314 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 2315 _assertSource("@deprecated C() {}", declaration); | |
| 2316 } | |
| 2317 | |
| 2318 void test_visitConstructorFieldInitializer_withoutThis() { | |
| 2319 _assertSource( | |
| 2320 "a = b", | |
| 2321 AstFactory.constructorFieldInitializer( | |
| 2322 false, "a", AstFactory.identifier3("b"))); | |
| 2323 } | |
| 2324 | |
| 2325 void test_visitConstructorFieldInitializer_withThis() { | |
| 2326 _assertSource( | |
| 2327 "this.a = b", | |
| 2328 AstFactory.constructorFieldInitializer( | |
| 2329 true, "a", AstFactory.identifier3("b"))); | |
| 2330 } | |
| 2331 | |
| 2332 void test_visitConstructorName_named_prefix() { | |
| 2333 _assertSource("p.C.n", | |
| 2334 AstFactory.constructorName(AstFactory.typeName4("p.C.n"), null)); | |
| 2335 } | |
| 2336 | |
| 2337 void test_visitConstructorName_unnamed_noPrefix() { | |
| 2338 _assertSource( | |
| 2339 "C", AstFactory.constructorName(AstFactory.typeName4("C"), null)); | |
| 2340 } | |
| 2341 | |
| 2342 void test_visitConstructorName_unnamed_prefix() { | |
| 2343 _assertSource( | |
| 2344 "p.C", | |
| 2345 AstFactory.constructorName( | |
| 2346 AstFactory.typeName3(AstFactory.identifier5("p", "C")), null)); | |
| 2347 } | |
| 2348 | |
| 2349 void test_visitContinueStatement_label() { | |
| 2350 _assertSource("continue l;", AstFactory.continueStatement("l")); | |
| 2351 } | |
| 2352 | |
| 2353 void test_visitContinueStatement_noLabel() { | |
| 2354 _assertSource("continue;", AstFactory.continueStatement()); | |
| 2355 } | |
| 2356 | |
| 2357 void test_visitDefaultFormalParameter_annotation() { | |
| 2358 DefaultFormalParameter parameter = AstFactory.positionalFormalParameter( | |
| 2359 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0)); | |
| 2360 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A"))); | |
| 2361 _assertSource('@A p = 0', parameter); | |
| 2362 } | |
| 2363 | |
| 2364 void test_visitDefaultFormalParameter_named_noValue() { | |
| 2365 _assertSource( | |
| 2366 "p", | |
| 2367 AstFactory.namedFormalParameter( | |
| 2368 AstFactory.simpleFormalParameter3("p"), null)); | |
| 2369 } | |
| 2370 | |
| 2371 void test_visitDefaultFormalParameter_named_value() { | |
| 2372 _assertSource( | |
| 2373 "p : 0", | |
| 2374 AstFactory.namedFormalParameter( | |
| 2375 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0))); | |
| 2376 } | |
| 2377 | |
| 2378 void test_visitDefaultFormalParameter_positional_noValue() { | |
| 2379 _assertSource( | |
| 2380 "p", | |
| 2381 AstFactory.positionalFormalParameter( | |
| 2382 AstFactory.simpleFormalParameter3("p"), null)); | |
| 2383 } | |
| 2384 | |
| 2385 void test_visitDefaultFormalParameter_positional_value() { | |
| 2386 _assertSource( | |
| 2387 "p = 0", | |
| 2388 AstFactory.positionalFormalParameter( | |
| 2389 AstFactory.simpleFormalParameter3("p"), AstFactory.integer(0))); | |
| 2390 } | |
| 2391 | |
| 2392 void test_visitDoStatement() { | |
| 2393 _assertSource( | |
| 2394 "do {} while (c);", | |
| 2395 AstFactory.doStatement( | |
| 2396 AstFactory.block(), AstFactory.identifier3("c"))); | |
| 2397 } | |
| 2398 | |
| 2399 void test_visitDoubleLiteral() { | |
| 2400 _assertSource("4.2", AstFactory.doubleLiteral(4.2)); | |
| 2401 } | |
| 2402 | |
| 2403 void test_visitEmptyFunctionBody() { | |
| 2404 _assertSource(";", AstFactory.emptyFunctionBody()); | |
| 2405 } | |
| 2406 | |
| 2407 void test_visitEmptyStatement() { | |
| 2408 _assertSource(";", AstFactory.emptyStatement()); | |
| 2409 } | |
| 2410 | |
| 2411 void test_visitEnumDeclaration_multiple() { | |
| 2412 _assertSource( | |
| 2413 "enum E {ONE, TWO}", AstFactory.enumDeclaration2("E", ["ONE", "TWO"])); | |
| 2414 } | |
| 2415 | |
| 2416 void test_visitEnumDeclaration_single() { | |
| 2417 _assertSource("enum E {ONE}", AstFactory.enumDeclaration2("E", ["ONE"])); | |
| 2418 } | |
| 2419 | |
| 2420 void test_visitExportDirective_combinator() { | |
| 2421 _assertSource( | |
| 2422 "export 'a.dart' show A;", | |
| 2423 AstFactory.exportDirective2("a.dart", [ | |
| 2424 AstFactory.showCombinator([AstFactory.identifier3("A")]) | |
| 2425 ])); | |
| 2426 } | |
| 2427 | |
| 2428 void test_visitExportDirective_combinators() { | |
| 2429 _assertSource( | |
| 2430 "export 'a.dart' show A hide B;", | |
| 2431 AstFactory.exportDirective2("a.dart", [ | |
| 2432 AstFactory.showCombinator([AstFactory.identifier3("A")]), | |
| 2433 AstFactory.hideCombinator([AstFactory.identifier3("B")]) | |
| 2434 ])); | |
| 2435 } | |
| 2436 | |
| 2437 void test_visitExportDirective_minimal() { | |
| 2438 _assertSource("export 'a.dart';", AstFactory.exportDirective2("a.dart")); | |
| 2439 } | |
| 2440 | |
| 2441 void test_visitExportDirective_withMetadata() { | |
| 2442 ExportDirective directive = AstFactory.exportDirective2("a.dart"); | |
| 2443 directive.metadata | |
| 2444 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 2445 _assertSource("@deprecated export 'a.dart';", directive); | |
| 2446 } | |
| 2447 | |
| 2448 void test_visitExpressionFunctionBody_async() { | |
| 2449 _assertSource("async => a;", | |
| 2450 AstFactory.asyncExpressionFunctionBody(AstFactory.identifier3("a"))); | |
| 2451 } | |
| 2452 | |
| 2453 void test_visitExpressionFunctionBody_simple() { | |
| 2454 _assertSource("=> a;", | |
| 2455 AstFactory.expressionFunctionBody(AstFactory.identifier3("a"))); | |
| 2456 } | |
| 2457 | |
| 2458 void test_visitExpressionStatement() { | |
| 2459 _assertSource( | |
| 2460 "a;", AstFactory.expressionStatement(AstFactory.identifier3("a"))); | |
| 2461 } | |
| 2462 | |
| 2463 void test_visitExtendsClause() { | |
| 2464 _assertSource( | |
| 2465 "extends C", AstFactory.extendsClause(AstFactory.typeName4("C"))); | |
| 2466 } | |
| 2467 | |
| 2468 void test_visitFieldDeclaration_instance() { | |
| 2469 _assertSource( | |
| 2470 "var a;", | |
| 2471 AstFactory.fieldDeclaration2( | |
| 2472 false, Keyword.VAR, [AstFactory.variableDeclaration("a")])); | |
| 2473 } | |
| 2474 | |
| 2475 void test_visitFieldDeclaration_static() { | |
| 2476 _assertSource( | |
| 2477 "static var a;", | |
| 2478 AstFactory.fieldDeclaration2( | |
| 2479 true, Keyword.VAR, [AstFactory.variableDeclaration("a")])); | |
| 2480 } | |
| 2481 | |
| 2482 void test_visitFieldDeclaration_withMetadata() { | |
| 2483 FieldDeclaration declaration = AstFactory.fieldDeclaration2( | |
| 2484 false, Keyword.VAR, [AstFactory.variableDeclaration("a")]); | |
| 2485 declaration.metadata | |
| 2486 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 2487 _assertSource("@deprecated var a;", declaration); | |
| 2488 } | |
| 2489 | |
| 2490 void test_visitFieldFormalParameter_annotation() { | |
| 2491 FieldFormalParameter parameter = AstFactory.fieldFormalParameter2('f'); | |
| 2492 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A"))); | |
| 2493 _assertSource('@A this.f', parameter); | |
| 2494 } | |
| 2495 | |
| 2496 void test_visitFieldFormalParameter_functionTyped() { | |
| 2497 _assertSource( | |
| 2498 "A this.a(b)", | |
| 2499 AstFactory.fieldFormalParameter( | |
| 2500 null, | |
| 2501 AstFactory.typeName4("A"), | |
| 2502 "a", | |
| 2503 AstFactory.formalParameterList( | |
| 2504 [AstFactory.simpleFormalParameter3("b")]))); | |
| 2505 } | |
| 2506 | |
| 2507 void test_visitFieldFormalParameter_functionTyped_typeParameters() { | |
| 2508 _assertSource( | |
| 2509 "A this.a<E, F>(b)", | |
| 2510 new FieldFormalParameter( | |
| 2511 null, | |
| 2512 null, | |
| 2513 null, | |
| 2514 AstFactory.typeName4('A'), | |
| 2515 TokenFactory.tokenFromKeyword(Keyword.THIS), | |
| 2516 TokenFactory.tokenFromType(TokenType.PERIOD), | |
| 2517 AstFactory.identifier3('a'), | |
| 2518 AstFactory.typeParameterList(['E', 'F']), | |
| 2519 AstFactory.formalParameterList( | |
| 2520 [AstFactory.simpleFormalParameter3("b")]))); | |
| 2521 } | |
| 2522 | |
| 2523 void test_visitFieldFormalParameter_keyword() { | |
| 2524 _assertSource( | |
| 2525 "var this.a", AstFactory.fieldFormalParameter(Keyword.VAR, null, "a")); | |
| 2526 } | |
| 2527 | |
| 2528 void test_visitFieldFormalParameter_keywordAndType() { | |
| 2529 _assertSource( | |
| 2530 "final A this.a", | |
| 2531 AstFactory.fieldFormalParameter( | |
| 2532 Keyword.FINAL, AstFactory.typeName4("A"), "a")); | |
| 2533 } | |
| 2534 | |
| 2535 void test_visitFieldFormalParameter_type() { | |
| 2536 _assertSource("A this.a", | |
| 2537 AstFactory.fieldFormalParameter(null, AstFactory.typeName4("A"), "a")); | |
| 2538 } | |
| 2539 | |
| 2540 void test_visitForEachStatement_declared() { | |
| 2541 _assertSource( | |
| 2542 "for (var a in b) {}", | |
| 2543 AstFactory.forEachStatement(AstFactory.declaredIdentifier3("a"), | |
| 2544 AstFactory.identifier3("b"), AstFactory.block())); | |
| 2545 } | |
| 2546 | |
| 2547 void test_visitForEachStatement_variable() { | |
| 2548 _assertSource( | |
| 2549 "for (a in b) {}", | |
| 2550 new ForEachStatement.withReference( | |
| 2551 null, | |
| 2552 TokenFactory.tokenFromKeyword(Keyword.FOR), | |
| 2553 TokenFactory.tokenFromType(TokenType.OPEN_PAREN), | |
| 2554 AstFactory.identifier3("a"), | |
| 2555 TokenFactory.tokenFromKeyword(Keyword.IN), | |
| 2556 AstFactory.identifier3("b"), | |
| 2557 TokenFactory.tokenFromType(TokenType.CLOSE_PAREN), | |
| 2558 AstFactory.block())); | |
| 2559 } | |
| 2560 | |
| 2561 void test_visitForEachStatement_variable_await() { | |
| 2562 _assertSource( | |
| 2563 "await for (a in b) {}", | |
| 2564 new ForEachStatement.withReference( | |
| 2565 TokenFactory.tokenFromString("await"), | |
| 2566 TokenFactory.tokenFromKeyword(Keyword.FOR), | |
| 2567 TokenFactory.tokenFromType(TokenType.OPEN_PAREN), | |
| 2568 AstFactory.identifier3("a"), | |
| 2569 TokenFactory.tokenFromKeyword(Keyword.IN), | |
| 2570 AstFactory.identifier3("b"), | |
| 2571 TokenFactory.tokenFromType(TokenType.CLOSE_PAREN), | |
| 2572 AstFactory.block())); | |
| 2573 } | |
| 2574 | |
| 2575 void test_visitFormalParameterList_empty() { | |
| 2576 _assertSource("()", AstFactory.formalParameterList()); | |
| 2577 } | |
| 2578 | |
| 2579 void test_visitFormalParameterList_n() { | |
| 2580 _assertSource( | |
| 2581 "({a : 0})", | |
| 2582 AstFactory.formalParameterList([ | |
| 2583 AstFactory.namedFormalParameter( | |
| 2584 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0)) | |
| 2585 ])); | |
| 2586 } | |
| 2587 | |
| 2588 void test_visitFormalParameterList_nn() { | |
| 2589 _assertSource( | |
| 2590 "({a : 0, b : 1})", | |
| 2591 AstFactory.formalParameterList([ | |
| 2592 AstFactory.namedFormalParameter( | |
| 2593 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0)), | |
| 2594 AstFactory.namedFormalParameter( | |
| 2595 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)) | |
| 2596 ])); | |
| 2597 } | |
| 2598 | |
| 2599 void test_visitFormalParameterList_p() { | |
| 2600 _assertSource( | |
| 2601 "([a = 0])", | |
| 2602 AstFactory.formalParameterList([ | |
| 2603 AstFactory.positionalFormalParameter( | |
| 2604 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0)) | |
| 2605 ])); | |
| 2606 } | |
| 2607 | |
| 2608 void test_visitFormalParameterList_pp() { | |
| 2609 _assertSource( | |
| 2610 "([a = 0, b = 1])", | |
| 2611 AstFactory.formalParameterList([ | |
| 2612 AstFactory.positionalFormalParameter( | |
| 2613 AstFactory.simpleFormalParameter3("a"), AstFactory.integer(0)), | |
| 2614 AstFactory.positionalFormalParameter( | |
| 2615 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)) | |
| 2616 ])); | |
| 2617 } | |
| 2618 | |
| 2619 void test_visitFormalParameterList_r() { | |
| 2620 _assertSource( | |
| 2621 "(a)", | |
| 2622 AstFactory | |
| 2623 .formalParameterList([AstFactory.simpleFormalParameter3("a")])); | |
| 2624 } | |
| 2625 | |
| 2626 void test_visitFormalParameterList_rn() { | |
| 2627 _assertSource( | |
| 2628 "(a, {b : 1})", | |
| 2629 AstFactory.formalParameterList([ | |
| 2630 AstFactory.simpleFormalParameter3("a"), | |
| 2631 AstFactory.namedFormalParameter( | |
| 2632 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)) | |
| 2633 ])); | |
| 2634 } | |
| 2635 | |
| 2636 void test_visitFormalParameterList_rnn() { | |
| 2637 _assertSource( | |
| 2638 "(a, {b : 1, c : 2})", | |
| 2639 AstFactory.formalParameterList([ | |
| 2640 AstFactory.simpleFormalParameter3("a"), | |
| 2641 AstFactory.namedFormalParameter( | |
| 2642 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)), | |
| 2643 AstFactory.namedFormalParameter( | |
| 2644 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(2)) | |
| 2645 ])); | |
| 2646 } | |
| 2647 | |
| 2648 void test_visitFormalParameterList_rp() { | |
| 2649 _assertSource( | |
| 2650 "(a, [b = 1])", | |
| 2651 AstFactory.formalParameterList([ | |
| 2652 AstFactory.simpleFormalParameter3("a"), | |
| 2653 AstFactory.positionalFormalParameter( | |
| 2654 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)) | |
| 2655 ])); | |
| 2656 } | |
| 2657 | |
| 2658 void test_visitFormalParameterList_rpp() { | |
| 2659 _assertSource( | |
| 2660 "(a, [b = 1, c = 2])", | |
| 2661 AstFactory.formalParameterList([ | |
| 2662 AstFactory.simpleFormalParameter3("a"), | |
| 2663 AstFactory.positionalFormalParameter( | |
| 2664 AstFactory.simpleFormalParameter3("b"), AstFactory.integer(1)), | |
| 2665 AstFactory.positionalFormalParameter( | |
| 2666 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(2)) | |
| 2667 ])); | |
| 2668 } | |
| 2669 | |
| 2670 void test_visitFormalParameterList_rr() { | |
| 2671 _assertSource( | |
| 2672 "(a, b)", | |
| 2673 AstFactory.formalParameterList([ | |
| 2674 AstFactory.simpleFormalParameter3("a"), | |
| 2675 AstFactory.simpleFormalParameter3("b") | |
| 2676 ])); | |
| 2677 } | |
| 2678 | |
| 2679 void test_visitFormalParameterList_rrn() { | |
| 2680 _assertSource( | |
| 2681 "(a, b, {c : 3})", | |
| 2682 AstFactory.formalParameterList([ | |
| 2683 AstFactory.simpleFormalParameter3("a"), | |
| 2684 AstFactory.simpleFormalParameter3("b"), | |
| 2685 AstFactory.namedFormalParameter( | |
| 2686 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3)) | |
| 2687 ])); | |
| 2688 } | |
| 2689 | |
| 2690 void test_visitFormalParameterList_rrnn() { | |
| 2691 _assertSource( | |
| 2692 "(a, b, {c : 3, d : 4})", | |
| 2693 AstFactory.formalParameterList([ | |
| 2694 AstFactory.simpleFormalParameter3("a"), | |
| 2695 AstFactory.simpleFormalParameter3("b"), | |
| 2696 AstFactory.namedFormalParameter( | |
| 2697 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3)), | |
| 2698 AstFactory.namedFormalParameter( | |
| 2699 AstFactory.simpleFormalParameter3("d"), AstFactory.integer(4)) | |
| 2700 ])); | |
| 2701 } | |
| 2702 | |
| 2703 void test_visitFormalParameterList_rrp() { | |
| 2704 _assertSource( | |
| 2705 "(a, b, [c = 3])", | |
| 2706 AstFactory.formalParameterList([ | |
| 2707 AstFactory.simpleFormalParameter3("a"), | |
| 2708 AstFactory.simpleFormalParameter3("b"), | |
| 2709 AstFactory.positionalFormalParameter( | |
| 2710 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3)) | |
| 2711 ])); | |
| 2712 } | |
| 2713 | |
| 2714 void test_visitFormalParameterList_rrpp() { | |
| 2715 _assertSource( | |
| 2716 "(a, b, [c = 3, d = 4])", | |
| 2717 AstFactory.formalParameterList([ | |
| 2718 AstFactory.simpleFormalParameter3("a"), | |
| 2719 AstFactory.simpleFormalParameter3("b"), | |
| 2720 AstFactory.positionalFormalParameter( | |
| 2721 AstFactory.simpleFormalParameter3("c"), AstFactory.integer(3)), | |
| 2722 AstFactory.positionalFormalParameter( | |
| 2723 AstFactory.simpleFormalParameter3("d"), AstFactory.integer(4)) | |
| 2724 ])); | |
| 2725 } | |
| 2726 | |
| 2727 void test_visitForStatement_c() { | |
| 2728 _assertSource( | |
| 2729 "for (; c;) {}", | |
| 2730 AstFactory.forStatement( | |
| 2731 null, AstFactory.identifier3("c"), null, AstFactory.block())); | |
| 2732 } | |
| 2733 | |
| 2734 void test_visitForStatement_cu() { | |
| 2735 _assertSource( | |
| 2736 "for (; c; u) {}", | |
| 2737 AstFactory.forStatement(null, AstFactory.identifier3("c"), | |
| 2738 [AstFactory.identifier3("u")], AstFactory.block())); | |
| 2739 } | |
| 2740 | |
| 2741 void test_visitForStatement_e() { | |
| 2742 _assertSource( | |
| 2743 "for (e;;) {}", | |
| 2744 AstFactory.forStatement( | |
| 2745 AstFactory.identifier3("e"), null, null, AstFactory.block())); | |
| 2746 } | |
| 2747 | |
| 2748 void test_visitForStatement_ec() { | |
| 2749 _assertSource( | |
| 2750 "for (e; c;) {}", | |
| 2751 AstFactory.forStatement(AstFactory.identifier3("e"), | |
| 2752 AstFactory.identifier3("c"), null, AstFactory.block())); | |
| 2753 } | |
| 2754 | |
| 2755 void test_visitForStatement_ecu() { | |
| 2756 _assertSource( | |
| 2757 "for (e; c; u) {}", | |
| 2758 AstFactory.forStatement( | |
| 2759 AstFactory.identifier3("e"), | |
| 2760 AstFactory.identifier3("c"), | |
| 2761 [AstFactory.identifier3("u")], | |
| 2762 AstFactory.block())); | |
| 2763 } | |
| 2764 | |
| 2765 void test_visitForStatement_eu() { | |
| 2766 _assertSource( | |
| 2767 "for (e;; u) {}", | |
| 2768 AstFactory.forStatement(AstFactory.identifier3("e"), null, | |
| 2769 [AstFactory.identifier3("u")], AstFactory.block())); | |
| 2770 } | |
| 2771 | |
| 2772 void test_visitForStatement_i() { | |
| 2773 _assertSource( | |
| 2774 "for (var i;;) {}", | |
| 2775 AstFactory.forStatement2( | |
| 2776 AstFactory.variableDeclarationList2( | |
| 2777 Keyword.VAR, [AstFactory.variableDeclaration("i")]), | |
| 2778 null, | |
| 2779 null, | |
| 2780 AstFactory.block())); | |
| 2781 } | |
| 2782 | |
| 2783 void test_visitForStatement_ic() { | |
| 2784 _assertSource( | |
| 2785 "for (var i; c;) {}", | |
| 2786 AstFactory.forStatement2( | |
| 2787 AstFactory.variableDeclarationList2( | |
| 2788 Keyword.VAR, [AstFactory.variableDeclaration("i")]), | |
| 2789 AstFactory.identifier3("c"), | |
| 2790 null, | |
| 2791 AstFactory.block())); | |
| 2792 } | |
| 2793 | |
| 2794 void test_visitForStatement_icu() { | |
| 2795 _assertSource( | |
| 2796 "for (var i; c; u) {}", | |
| 2797 AstFactory.forStatement2( | |
| 2798 AstFactory.variableDeclarationList2( | |
| 2799 Keyword.VAR, [AstFactory.variableDeclaration("i")]), | |
| 2800 AstFactory.identifier3("c"), | |
| 2801 [AstFactory.identifier3("u")], | |
| 2802 AstFactory.block())); | |
| 2803 } | |
| 2804 | |
| 2805 void test_visitForStatement_iu() { | |
| 2806 _assertSource( | |
| 2807 "for (var i;; u) {}", | |
| 2808 AstFactory.forStatement2( | |
| 2809 AstFactory.variableDeclarationList2( | |
| 2810 Keyword.VAR, [AstFactory.variableDeclaration("i")]), | |
| 2811 null, | |
| 2812 [AstFactory.identifier3("u")], | |
| 2813 AstFactory.block())); | |
| 2814 } | |
| 2815 | |
| 2816 void test_visitForStatement_u() { | |
| 2817 _assertSource( | |
| 2818 "for (;; u) {}", | |
| 2819 AstFactory.forStatement( | |
| 2820 null, null, [AstFactory.identifier3("u")], AstFactory.block())); | |
| 2821 } | |
| 2822 | |
| 2823 void test_visitFunctionDeclaration_external() { | |
| 2824 FunctionDeclaration functionDeclaration = AstFactory.functionDeclaration( | |
| 2825 null, | |
| 2826 null, | |
| 2827 "f", | |
| 2828 AstFactory.functionExpression2( | |
| 2829 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody())); | |
| 2830 functionDeclaration.externalKeyword = | |
| 2831 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
| 2832 _assertSource("external f();", functionDeclaration); | |
| 2833 } | |
| 2834 | |
| 2835 void test_visitFunctionDeclaration_getter() { | |
| 2836 _assertSource( | |
| 2837 "get f() {}", | |
| 2838 AstFactory.functionDeclaration( | |
| 2839 null, Keyword.GET, "f", AstFactory.functionExpression())); | |
| 2840 } | |
| 2841 | |
| 2842 void test_visitFunctionDeclaration_local_blockBody() { | |
| 2843 FunctionDeclaration f = AstFactory.functionDeclaration( | |
| 2844 null, null, "f", AstFactory.functionExpression()); | |
| 2845 FunctionDeclarationStatement fStatement = | |
| 2846 new FunctionDeclarationStatement(f); | |
| 2847 _assertSource( | |
| 2848 "main() {f() {} 42;}", | |
| 2849 AstFactory.functionDeclaration( | |
| 2850 null, | |
| 2851 null, | |
| 2852 "main", | |
| 2853 AstFactory.functionExpression2( | |
| 2854 AstFactory.formalParameterList(), | |
| 2855 AstFactory.blockFunctionBody2([ | |
| 2856 fStatement, | |
| 2857 AstFactory.expressionStatement(AstFactory.integer(42)) | |
| 2858 ])))); | |
| 2859 } | |
| 2860 | |
| 2861 void test_visitFunctionDeclaration_local_expressionBody() { | |
| 2862 FunctionDeclaration f = AstFactory.functionDeclaration( | |
| 2863 null, | |
| 2864 null, | |
| 2865 "f", | |
| 2866 AstFactory.functionExpression2(AstFactory.formalParameterList(), | |
| 2867 AstFactory.expressionFunctionBody(AstFactory.integer(1)))); | |
| 2868 FunctionDeclarationStatement fStatement = | |
| 2869 new FunctionDeclarationStatement(f); | |
| 2870 _assertSource( | |
| 2871 "main() {f() => 1; 2;}", | |
| 2872 AstFactory.functionDeclaration( | |
| 2873 null, | |
| 2874 null, | |
| 2875 "main", | |
| 2876 AstFactory.functionExpression2( | |
| 2877 AstFactory.formalParameterList(), | |
| 2878 AstFactory.blockFunctionBody2([ | |
| 2879 fStatement, | |
| 2880 AstFactory.expressionStatement(AstFactory.integer(2)) | |
| 2881 ])))); | |
| 2882 } | |
| 2883 | |
| 2884 void test_visitFunctionDeclaration_normal() { | |
| 2885 _assertSource( | |
| 2886 "f() {}", | |
| 2887 AstFactory.functionDeclaration( | |
| 2888 null, null, "f", AstFactory.functionExpression())); | |
| 2889 } | |
| 2890 | |
| 2891 void test_visitFunctionDeclaration_setter() { | |
| 2892 _assertSource( | |
| 2893 "set f() {}", | |
| 2894 AstFactory.functionDeclaration( | |
| 2895 null, Keyword.SET, "f", AstFactory.functionExpression())); | |
| 2896 } | |
| 2897 | |
| 2898 void test_visitFunctionDeclaration_typeParameters() { | |
| 2899 _assertSource( | |
| 2900 "f<E>() {}", | |
| 2901 AstFactory.functionDeclaration( | |
| 2902 null, | |
| 2903 null, | |
| 2904 "f", | |
| 2905 AstFactory.functionExpression3( | |
| 2906 AstFactory.typeParameterList(['E']), | |
| 2907 AstFactory.formalParameterList(), | |
| 2908 AstFactory.blockFunctionBody2()))); | |
| 2909 } | |
| 2910 | |
| 2911 void test_visitFunctionDeclaration_withMetadata() { | |
| 2912 FunctionDeclaration declaration = AstFactory.functionDeclaration( | |
| 2913 null, null, "f", AstFactory.functionExpression()); | |
| 2914 declaration.metadata | |
| 2915 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 2916 _assertSource("@deprecated f() {}", declaration); | |
| 2917 } | |
| 2918 | |
| 2919 void test_visitFunctionDeclarationStatement() { | |
| 2920 _assertSource( | |
| 2921 "f() {}", | |
| 2922 AstFactory.functionDeclarationStatement( | |
| 2923 null, null, "f", AstFactory.functionExpression())); | |
| 2924 } | |
| 2925 | |
| 2926 void test_visitFunctionExpression() { | |
| 2927 _assertSource("() {}", AstFactory.functionExpression()); | |
| 2928 } | |
| 2929 | |
| 2930 void test_visitFunctionExpression_typeParameters() { | |
| 2931 _assertSource( | |
| 2932 "<E>() {}", | |
| 2933 AstFactory.functionExpression3(AstFactory.typeParameterList(['E']), | |
| 2934 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2())); | |
| 2935 } | |
| 2936 | |
| 2937 void test_visitFunctionExpressionInvocation_minimal() { | |
| 2938 _assertSource("f()", | |
| 2939 AstFactory.functionExpressionInvocation(AstFactory.identifier3("f"))); | |
| 2940 } | |
| 2941 | |
| 2942 void test_visitFunctionExpressionInvocation_typeArguments() { | |
| 2943 _assertSource( | |
| 2944 "f<A>()", | |
| 2945 AstFactory.functionExpressionInvocation2(AstFactory.identifier3("f"), | |
| 2946 AstFactory.typeArgumentList([AstFactory.typeName4('A')]))); | |
| 2947 } | |
| 2948 | |
| 2949 void test_visitFunctionTypeAlias_generic() { | |
| 2950 _assertSource( | |
| 2951 "typedef A F<B>();", | |
| 2952 AstFactory.typeAlias( | |
| 2953 AstFactory.typeName4("A"), | |
| 2954 "F", | |
| 2955 AstFactory.typeParameterList(["B"]), | |
| 2956 AstFactory.formalParameterList())); | |
| 2957 } | |
| 2958 | |
| 2959 void test_visitFunctionTypeAlias_nonGeneric() { | |
| 2960 _assertSource( | |
| 2961 "typedef A F();", | |
| 2962 AstFactory.typeAlias(AstFactory.typeName4("A"), "F", null, | |
| 2963 AstFactory.formalParameterList())); | |
| 2964 } | |
| 2965 | |
| 2966 void test_visitFunctionTypeAlias_withMetadata() { | |
| 2967 FunctionTypeAlias declaration = AstFactory.typeAlias( | |
| 2968 AstFactory.typeName4("A"), "F", null, AstFactory.formalParameterList()); | |
| 2969 declaration.metadata | |
| 2970 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 2971 _assertSource("@deprecated typedef A F();", declaration); | |
| 2972 } | |
| 2973 | |
| 2974 void test_visitFunctionTypedFormalParameter_annotation() { | |
| 2975 FunctionTypedFormalParameter parameter = | |
| 2976 AstFactory.functionTypedFormalParameter(null, "f"); | |
| 2977 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A"))); | |
| 2978 _assertSource('@A f()', parameter); | |
| 2979 } | |
| 2980 | |
| 2981 void test_visitFunctionTypedFormalParameter_noType() { | |
| 2982 _assertSource("f()", AstFactory.functionTypedFormalParameter(null, "f")); | |
| 2983 } | |
| 2984 | |
| 2985 void test_visitFunctionTypedFormalParameter_type() { | |
| 2986 _assertSource( | |
| 2987 "T f()", | |
| 2988 AstFactory.functionTypedFormalParameter( | |
| 2989 AstFactory.typeName4("T"), "f")); | |
| 2990 } | |
| 2991 | |
| 2992 void test_visitFunctionTypedFormalParameter_typeParameters() { | |
| 2993 _assertSource( | |
| 2994 "T f<E>()", | |
| 2995 new FunctionTypedFormalParameter( | |
| 2996 null, | |
| 2997 null, | |
| 2998 AstFactory.typeName4("T"), | |
| 2999 AstFactory.identifier3('f'), | |
| 3000 AstFactory.typeParameterList(['E']), | |
| 3001 AstFactory.formalParameterList([]))); | |
| 3002 } | |
| 3003 | |
| 3004 void test_visitIfStatement_withElse() { | |
| 3005 _assertSource( | |
| 3006 "if (c) {} else {}", | |
| 3007 AstFactory.ifStatement2(AstFactory.identifier3("c"), AstFactory.block(), | |
| 3008 AstFactory.block())); | |
| 3009 } | |
| 3010 | |
| 3011 void test_visitIfStatement_withoutElse() { | |
| 3012 _assertSource( | |
| 3013 "if (c) {}", | |
| 3014 AstFactory.ifStatement( | |
| 3015 AstFactory.identifier3("c"), AstFactory.block())); | |
| 3016 } | |
| 3017 | |
| 3018 void test_visitImplementsClause_multiple() { | |
| 3019 _assertSource( | |
| 3020 "implements A, B", | |
| 3021 AstFactory.implementsClause( | |
| 3022 [AstFactory.typeName4("A"), AstFactory.typeName4("B")])); | |
| 3023 } | |
| 3024 | |
| 3025 void test_visitImplementsClause_single() { | |
| 3026 _assertSource("implements A", | |
| 3027 AstFactory.implementsClause([AstFactory.typeName4("A")])); | |
| 3028 } | |
| 3029 | |
| 3030 void test_visitImportDirective_combinator() { | |
| 3031 _assertSource( | |
| 3032 "import 'a.dart' show A;", | |
| 3033 AstFactory.importDirective3("a.dart", null, [ | |
| 3034 AstFactory.showCombinator([AstFactory.identifier3("A")]) | |
| 3035 ])); | |
| 3036 } | |
| 3037 | |
| 3038 void test_visitImportDirective_combinators() { | |
| 3039 _assertSource( | |
| 3040 "import 'a.dart' show A hide B;", | |
| 3041 AstFactory.importDirective3("a.dart", null, [ | |
| 3042 AstFactory.showCombinator([AstFactory.identifier3("A")]), | |
| 3043 AstFactory.hideCombinator([AstFactory.identifier3("B")]) | |
| 3044 ])); | |
| 3045 } | |
| 3046 | |
| 3047 void test_visitImportDirective_deferred() { | |
| 3048 _assertSource("import 'a.dart' deferred as p;", | |
| 3049 AstFactory.importDirective2("a.dart", true, "p")); | |
| 3050 } | |
| 3051 | |
| 3052 void test_visitImportDirective_minimal() { | |
| 3053 _assertSource( | |
| 3054 "import 'a.dart';", AstFactory.importDirective3("a.dart", null)); | |
| 3055 } | |
| 3056 | |
| 3057 void test_visitImportDirective_prefix() { | |
| 3058 _assertSource( | |
| 3059 "import 'a.dart' as p;", AstFactory.importDirective3("a.dart", "p")); | |
| 3060 } | |
| 3061 | |
| 3062 void test_visitImportDirective_prefix_combinator() { | |
| 3063 _assertSource( | |
| 3064 "import 'a.dart' as p show A;", | |
| 3065 AstFactory.importDirective3("a.dart", "p", [ | |
| 3066 AstFactory.showCombinator([AstFactory.identifier3("A")]) | |
| 3067 ])); | |
| 3068 } | |
| 3069 | |
| 3070 void test_visitImportDirective_prefix_combinators() { | |
| 3071 _assertSource( | |
| 3072 "import 'a.dart' as p show A hide B;", | |
| 3073 AstFactory.importDirective3("a.dart", "p", [ | |
| 3074 AstFactory.showCombinator([AstFactory.identifier3("A")]), | |
| 3075 AstFactory.hideCombinator([AstFactory.identifier3("B")]) | |
| 3076 ])); | |
| 3077 } | |
| 3078 | |
| 3079 void test_visitImportDirective_withMetadata() { | |
| 3080 ImportDirective directive = AstFactory.importDirective3("a.dart", null); | |
| 3081 directive.metadata | |
| 3082 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3083 _assertSource("@deprecated import 'a.dart';", directive); | |
| 3084 } | |
| 3085 | |
| 3086 void test_visitImportHideCombinator_multiple() { | |
| 3087 _assertSource( | |
| 3088 "hide a, b", | |
| 3089 AstFactory.hideCombinator( | |
| 3090 [AstFactory.identifier3("a"), AstFactory.identifier3("b")])); | |
| 3091 } | |
| 3092 | |
| 3093 void test_visitImportHideCombinator_single() { | |
| 3094 _assertSource( | |
| 3095 "hide a", AstFactory.hideCombinator([AstFactory.identifier3("a")])); | |
| 3096 } | |
| 3097 | |
| 3098 void test_visitImportShowCombinator_multiple() { | |
| 3099 _assertSource( | |
| 3100 "show a, b", | |
| 3101 AstFactory.showCombinator( | |
| 3102 [AstFactory.identifier3("a"), AstFactory.identifier3("b")])); | |
| 3103 } | |
| 3104 | |
| 3105 void test_visitImportShowCombinator_single() { | |
| 3106 _assertSource( | |
| 3107 "show a", AstFactory.showCombinator([AstFactory.identifier3("a")])); | |
| 3108 } | |
| 3109 | |
| 3110 void test_visitIndexExpression() { | |
| 3111 _assertSource( | |
| 3112 "a[i]", | |
| 3113 AstFactory.indexExpression( | |
| 3114 AstFactory.identifier3("a"), AstFactory.identifier3("i"))); | |
| 3115 } | |
| 3116 | |
| 3117 void test_visitInstanceCreationExpression_const() { | |
| 3118 _assertSource( | |
| 3119 "const C()", | |
| 3120 AstFactory.instanceCreationExpression2( | |
| 3121 Keyword.CONST, AstFactory.typeName4("C"))); | |
| 3122 } | |
| 3123 | |
| 3124 void test_visitInstanceCreationExpression_named() { | |
| 3125 _assertSource( | |
| 3126 "new C.c()", | |
| 3127 AstFactory.instanceCreationExpression3( | |
| 3128 Keyword.NEW, AstFactory.typeName4("C"), "c")); | |
| 3129 } | |
| 3130 | |
| 3131 void test_visitInstanceCreationExpression_unnamed() { | |
| 3132 _assertSource( | |
| 3133 "new C()", | |
| 3134 AstFactory.instanceCreationExpression2( | |
| 3135 Keyword.NEW, AstFactory.typeName4("C"))); | |
| 3136 } | |
| 3137 | |
| 3138 void test_visitIntegerLiteral() { | |
| 3139 _assertSource("42", AstFactory.integer(42)); | |
| 3140 } | |
| 3141 | |
| 3142 void test_visitInterpolationExpression_expression() { | |
| 3143 _assertSource("\${a}", | |
| 3144 AstFactory.interpolationExpression(AstFactory.identifier3("a"))); | |
| 3145 } | |
| 3146 | |
| 3147 void test_visitInterpolationExpression_identifier() { | |
| 3148 _assertSource("\$a", AstFactory.interpolationExpression2("a")); | |
| 3149 } | |
| 3150 | |
| 3151 void test_visitInterpolationString() { | |
| 3152 _assertSource("'x", AstFactory.interpolationString("'x", "x")); | |
| 3153 } | |
| 3154 | |
| 3155 void test_visitIsExpression_negated() { | |
| 3156 _assertSource( | |
| 3157 "a is! C", | |
| 3158 AstFactory.isExpression( | |
| 3159 AstFactory.identifier3("a"), true, AstFactory.typeName4("C"))); | |
| 3160 } | |
| 3161 | |
| 3162 void test_visitIsExpression_normal() { | |
| 3163 _assertSource( | |
| 3164 "a is C", | |
| 3165 AstFactory.isExpression( | |
| 3166 AstFactory.identifier3("a"), false, AstFactory.typeName4("C"))); | |
| 3167 } | |
| 3168 | |
| 3169 void test_visitLabel() { | |
| 3170 _assertSource("a:", AstFactory.label2("a")); | |
| 3171 } | |
| 3172 | |
| 3173 void test_visitLabeledStatement_multiple() { | |
| 3174 _assertSource( | |
| 3175 "a: b: return;", | |
| 3176 AstFactory.labeledStatement( | |
| 3177 [AstFactory.label2("a"), AstFactory.label2("b")], | |
| 3178 AstFactory.returnStatement())); | |
| 3179 } | |
| 3180 | |
| 3181 void test_visitLabeledStatement_single() { | |
| 3182 _assertSource( | |
| 3183 "a: return;", | |
| 3184 AstFactory.labeledStatement( | |
| 3185 [AstFactory.label2("a")], AstFactory.returnStatement())); | |
| 3186 } | |
| 3187 | |
| 3188 void test_visitLibraryDirective() { | |
| 3189 _assertSource("library l;", AstFactory.libraryDirective2("l")); | |
| 3190 } | |
| 3191 | |
| 3192 void test_visitLibraryDirective_withMetadata() { | |
| 3193 LibraryDirective directive = AstFactory.libraryDirective2("l"); | |
| 3194 directive.metadata | |
| 3195 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3196 _assertSource("@deprecated library l;", directive); | |
| 3197 } | |
| 3198 | |
| 3199 void test_visitLibraryIdentifier_multiple() { | |
| 3200 _assertSource( | |
| 3201 "a.b.c", | |
| 3202 AstFactory.libraryIdentifier([ | |
| 3203 AstFactory.identifier3("a"), | |
| 3204 AstFactory.identifier3("b"), | |
| 3205 AstFactory.identifier3("c") | |
| 3206 ])); | |
| 3207 } | |
| 3208 | |
| 3209 void test_visitLibraryIdentifier_single() { | |
| 3210 _assertSource( | |
| 3211 "a", AstFactory.libraryIdentifier([AstFactory.identifier3("a")])); | |
| 3212 } | |
| 3213 | |
| 3214 void test_visitListLiteral_const() { | |
| 3215 _assertSource("const []", AstFactory.listLiteral2(Keyword.CONST, null)); | |
| 3216 } | |
| 3217 | |
| 3218 void test_visitListLiteral_empty() { | |
| 3219 _assertSource("[]", AstFactory.listLiteral()); | |
| 3220 } | |
| 3221 | |
| 3222 void test_visitListLiteral_nonEmpty() { | |
| 3223 _assertSource( | |
| 3224 "[a, b, c]", | |
| 3225 AstFactory.listLiteral([ | |
| 3226 AstFactory.identifier3("a"), | |
| 3227 AstFactory.identifier3("b"), | |
| 3228 AstFactory.identifier3("c") | |
| 3229 ])); | |
| 3230 } | |
| 3231 | |
| 3232 void test_visitMapLiteral_const() { | |
| 3233 _assertSource("const {}", AstFactory.mapLiteral(Keyword.CONST, null)); | |
| 3234 } | |
| 3235 | |
| 3236 void test_visitMapLiteral_empty() { | |
| 3237 _assertSource("{}", AstFactory.mapLiteral2()); | |
| 3238 } | |
| 3239 | |
| 3240 void test_visitMapLiteral_nonEmpty() { | |
| 3241 _assertSource( | |
| 3242 "{'a' : a, 'b' : b, 'c' : c}", | |
| 3243 AstFactory.mapLiteral2([ | |
| 3244 AstFactory.mapLiteralEntry("a", AstFactory.identifier3("a")), | |
| 3245 AstFactory.mapLiteralEntry("b", AstFactory.identifier3("b")), | |
| 3246 AstFactory.mapLiteralEntry("c", AstFactory.identifier3("c")) | |
| 3247 ])); | |
| 3248 } | |
| 3249 | |
| 3250 void test_visitMapLiteralEntry() { | |
| 3251 _assertSource("'a' : b", | |
| 3252 AstFactory.mapLiteralEntry("a", AstFactory.identifier3("b"))); | |
| 3253 } | |
| 3254 | |
| 3255 void test_visitMethodDeclaration_external() { | |
| 3256 _assertSource( | |
| 3257 "external m();", | |
| 3258 AstFactory.methodDeclaration(null, null, null, null, | |
| 3259 AstFactory.identifier3("m"), AstFactory.formalParameterList())); | |
| 3260 } | |
| 3261 | |
| 3262 void test_visitMethodDeclaration_external_returnType() { | |
| 3263 _assertSource( | |
| 3264 "external T m();", | |
| 3265 AstFactory.methodDeclaration( | |
| 3266 null, | |
| 3267 AstFactory.typeName4("T"), | |
| 3268 null, | |
| 3269 null, | |
| 3270 AstFactory.identifier3("m"), | |
| 3271 AstFactory.formalParameterList())); | |
| 3272 } | |
| 3273 | |
| 3274 void test_visitMethodDeclaration_getter() { | |
| 3275 _assertSource( | |
| 3276 "get m {}", | |
| 3277 AstFactory.methodDeclaration2( | |
| 3278 null, | |
| 3279 null, | |
| 3280 Keyword.GET, | |
| 3281 null, | |
| 3282 AstFactory.identifier3("m"), | |
| 3283 null, | |
| 3284 AstFactory.blockFunctionBody2())); | |
| 3285 } | |
| 3286 | |
| 3287 void test_visitMethodDeclaration_getter_returnType() { | |
| 3288 _assertSource( | |
| 3289 "T get m {}", | |
| 3290 AstFactory.methodDeclaration2( | |
| 3291 null, | |
| 3292 AstFactory.typeName4("T"), | |
| 3293 Keyword.GET, | |
| 3294 null, | |
| 3295 AstFactory.identifier3("m"), | |
| 3296 null, | |
| 3297 AstFactory.blockFunctionBody2())); | |
| 3298 } | |
| 3299 | |
| 3300 void test_visitMethodDeclaration_getter_seturnType() { | |
| 3301 _assertSource( | |
| 3302 "T set m(var v) {}", | |
| 3303 AstFactory.methodDeclaration2( | |
| 3304 null, | |
| 3305 AstFactory.typeName4("T"), | |
| 3306 Keyword.SET, | |
| 3307 null, | |
| 3308 AstFactory.identifier3("m"), | |
| 3309 AstFactory.formalParameterList( | |
| 3310 [AstFactory.simpleFormalParameter(Keyword.VAR, "v")]), | |
| 3311 AstFactory.blockFunctionBody2())); | |
| 3312 } | |
| 3313 | |
| 3314 void test_visitMethodDeclaration_minimal() { | |
| 3315 _assertSource( | |
| 3316 "m() {}", | |
| 3317 AstFactory.methodDeclaration2( | |
| 3318 null, | |
| 3319 null, | |
| 3320 null, | |
| 3321 null, | |
| 3322 AstFactory.identifier3("m"), | |
| 3323 AstFactory.formalParameterList(), | |
| 3324 AstFactory.blockFunctionBody2())); | |
| 3325 } | |
| 3326 | |
| 3327 void test_visitMethodDeclaration_multipleParameters() { | |
| 3328 _assertSource( | |
| 3329 "m(var a, var b) {}", | |
| 3330 AstFactory.methodDeclaration2( | |
| 3331 null, | |
| 3332 null, | |
| 3333 null, | |
| 3334 null, | |
| 3335 AstFactory.identifier3("m"), | |
| 3336 AstFactory.formalParameterList([ | |
| 3337 AstFactory.simpleFormalParameter(Keyword.VAR, "a"), | |
| 3338 AstFactory.simpleFormalParameter(Keyword.VAR, "b") | |
| 3339 ]), | |
| 3340 AstFactory.blockFunctionBody2())); | |
| 3341 } | |
| 3342 | |
| 3343 void test_visitMethodDeclaration_operator() { | |
| 3344 _assertSource( | |
| 3345 "operator +() {}", | |
| 3346 AstFactory.methodDeclaration2( | |
| 3347 null, | |
| 3348 null, | |
| 3349 null, | |
| 3350 Keyword.OPERATOR, | |
| 3351 AstFactory.identifier3("+"), | |
| 3352 AstFactory.formalParameterList(), | |
| 3353 AstFactory.blockFunctionBody2())); | |
| 3354 } | |
| 3355 | |
| 3356 void test_visitMethodDeclaration_operator_returnType() { | |
| 3357 _assertSource( | |
| 3358 "T operator +() {}", | |
| 3359 AstFactory.methodDeclaration2( | |
| 3360 null, | |
| 3361 AstFactory.typeName4("T"), | |
| 3362 null, | |
| 3363 Keyword.OPERATOR, | |
| 3364 AstFactory.identifier3("+"), | |
| 3365 AstFactory.formalParameterList(), | |
| 3366 AstFactory.blockFunctionBody2())); | |
| 3367 } | |
| 3368 | |
| 3369 void test_visitMethodDeclaration_returnType() { | |
| 3370 _assertSource( | |
| 3371 "T m() {}", | |
| 3372 AstFactory.methodDeclaration2( | |
| 3373 null, | |
| 3374 AstFactory.typeName4("T"), | |
| 3375 null, | |
| 3376 null, | |
| 3377 AstFactory.identifier3("m"), | |
| 3378 AstFactory.formalParameterList(), | |
| 3379 AstFactory.blockFunctionBody2())); | |
| 3380 } | |
| 3381 | |
| 3382 void test_visitMethodDeclaration_setter() { | |
| 3383 _assertSource( | |
| 3384 "set m(var v) {}", | |
| 3385 AstFactory.methodDeclaration2( | |
| 3386 null, | |
| 3387 null, | |
| 3388 Keyword.SET, | |
| 3389 null, | |
| 3390 AstFactory.identifier3("m"), | |
| 3391 AstFactory.formalParameterList( | |
| 3392 [AstFactory.simpleFormalParameter(Keyword.VAR, "v")]), | |
| 3393 AstFactory.blockFunctionBody2())); | |
| 3394 } | |
| 3395 | |
| 3396 void test_visitMethodDeclaration_static() { | |
| 3397 _assertSource( | |
| 3398 "static m() {}", | |
| 3399 AstFactory.methodDeclaration2( | |
| 3400 Keyword.STATIC, | |
| 3401 null, | |
| 3402 null, | |
| 3403 null, | |
| 3404 AstFactory.identifier3("m"), | |
| 3405 AstFactory.formalParameterList(), | |
| 3406 AstFactory.blockFunctionBody2())); | |
| 3407 } | |
| 3408 | |
| 3409 void test_visitMethodDeclaration_static_returnType() { | |
| 3410 _assertSource( | |
| 3411 "static T m() {}", | |
| 3412 AstFactory.methodDeclaration2( | |
| 3413 Keyword.STATIC, | |
| 3414 AstFactory.typeName4("T"), | |
| 3415 null, | |
| 3416 null, | |
| 3417 AstFactory.identifier3("m"), | |
| 3418 AstFactory.formalParameterList(), | |
| 3419 AstFactory.blockFunctionBody2())); | |
| 3420 } | |
| 3421 | |
| 3422 void test_visitMethodDeclaration_typeParameters() { | |
| 3423 _assertSource( | |
| 3424 "m<E>() {}", | |
| 3425 AstFactory.methodDeclaration3( | |
| 3426 null, | |
| 3427 null, | |
| 3428 null, | |
| 3429 null, | |
| 3430 AstFactory.identifier3("m"), | |
| 3431 AstFactory.typeParameterList(['E']), | |
| 3432 AstFactory.formalParameterList(), | |
| 3433 AstFactory.blockFunctionBody2())); | |
| 3434 } | |
| 3435 | |
| 3436 void test_visitMethodDeclaration_withMetadata() { | |
| 3437 MethodDeclaration declaration = AstFactory.methodDeclaration2( | |
| 3438 null, | |
| 3439 null, | |
| 3440 null, | |
| 3441 null, | |
| 3442 AstFactory.identifier3("m"), | |
| 3443 AstFactory.formalParameterList(), | |
| 3444 AstFactory.blockFunctionBody2()); | |
| 3445 declaration.metadata | |
| 3446 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3447 _assertSource("@deprecated m() {}", declaration); | |
| 3448 } | |
| 3449 | |
| 3450 void test_visitMethodInvocation_conditional() { | |
| 3451 _assertSource( | |
| 3452 "t?.m()", | |
| 3453 AstFactory.methodInvocation( | |
| 3454 AstFactory.identifier3("t"), "m", null, TokenType.QUESTION_PERIOD)); | |
| 3455 } | |
| 3456 | |
| 3457 void test_visitMethodInvocation_noTarget() { | |
| 3458 _assertSource("m()", AstFactory.methodInvocation2("m")); | |
| 3459 } | |
| 3460 | |
| 3461 void test_visitMethodInvocation_target() { | |
| 3462 _assertSource( | |
| 3463 "t.m()", AstFactory.methodInvocation(AstFactory.identifier3("t"), "m")); | |
| 3464 } | |
| 3465 | |
| 3466 void test_visitMethodInvocation_typeArguments() { | |
| 3467 _assertSource( | |
| 3468 "m<A>()", | |
| 3469 AstFactory.methodInvocation3(null, "m", | |
| 3470 AstFactory.typeArgumentList([AstFactory.typeName4('A')]))); | |
| 3471 } | |
| 3472 | |
| 3473 void test_visitNamedExpression() { | |
| 3474 _assertSource( | |
| 3475 "a: b", AstFactory.namedExpression2("a", AstFactory.identifier3("b"))); | |
| 3476 } | |
| 3477 | |
| 3478 void test_visitNamedFormalParameter() { | |
| 3479 _assertSource( | |
| 3480 "var a : 0", | |
| 3481 AstFactory.namedFormalParameter( | |
| 3482 AstFactory.simpleFormalParameter(Keyword.VAR, "a"), | |
| 3483 AstFactory.integer(0))); | |
| 3484 } | |
| 3485 | |
| 3486 void test_visitNativeClause() { | |
| 3487 _assertSource("native 'code'", AstFactory.nativeClause("code")); | |
| 3488 } | |
| 3489 | |
| 3490 void test_visitNativeFunctionBody() { | |
| 3491 _assertSource("native 'str';", AstFactory.nativeFunctionBody("str")); | |
| 3492 } | |
| 3493 | |
| 3494 void test_visitNullLiteral() { | |
| 3495 _assertSource("null", AstFactory.nullLiteral()); | |
| 3496 } | |
| 3497 | |
| 3498 void test_visitParenthesizedExpression() { | |
| 3499 _assertSource( | |
| 3500 "(a)", AstFactory.parenthesizedExpression(AstFactory.identifier3("a"))); | |
| 3501 } | |
| 3502 | |
| 3503 void test_visitPartDirective() { | |
| 3504 _assertSource("part 'a.dart';", AstFactory.partDirective2("a.dart")); | |
| 3505 } | |
| 3506 | |
| 3507 void test_visitPartDirective_withMetadata() { | |
| 3508 PartDirective directive = AstFactory.partDirective2("a.dart"); | |
| 3509 directive.metadata | |
| 3510 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3511 _assertSource("@deprecated part 'a.dart';", directive); | |
| 3512 } | |
| 3513 | |
| 3514 void test_visitPartOfDirective() { | |
| 3515 _assertSource("part of l;", | |
| 3516 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["l"]))); | |
| 3517 } | |
| 3518 | |
| 3519 void test_visitPartOfDirective_withMetadata() { | |
| 3520 PartOfDirective directive = | |
| 3521 AstFactory.partOfDirective(AstFactory.libraryIdentifier2(["l"])); | |
| 3522 directive.metadata | |
| 3523 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3524 _assertSource("@deprecated part of l;", directive); | |
| 3525 } | |
| 3526 | |
| 3527 void test_visitPositionalFormalParameter() { | |
| 3528 _assertSource( | |
| 3529 "var a = 0", | |
| 3530 AstFactory.positionalFormalParameter( | |
| 3531 AstFactory.simpleFormalParameter(Keyword.VAR, "a"), | |
| 3532 AstFactory.integer(0))); | |
| 3533 } | |
| 3534 | |
| 3535 void test_visitPostfixExpression() { | |
| 3536 _assertSource( | |
| 3537 "a++", | |
| 3538 AstFactory.postfixExpression( | |
| 3539 AstFactory.identifier3("a"), TokenType.PLUS_PLUS)); | |
| 3540 } | |
| 3541 | |
| 3542 void test_visitPrefixedIdentifier() { | |
| 3543 _assertSource("a.b", AstFactory.identifier5("a", "b")); | |
| 3544 } | |
| 3545 | |
| 3546 void test_visitPrefixExpression() { | |
| 3547 _assertSource( | |
| 3548 "-a", | |
| 3549 AstFactory.prefixExpression( | |
| 3550 TokenType.MINUS, AstFactory.identifier3("a"))); | |
| 3551 } | |
| 3552 | |
| 3553 void test_visitPropertyAccess() { | |
| 3554 _assertSource( | |
| 3555 "a.b", AstFactory.propertyAccess2(AstFactory.identifier3("a"), "b")); | |
| 3556 } | |
| 3557 | |
| 3558 void test_visitPropertyAccess_conditional() { | |
| 3559 _assertSource( | |
| 3560 "a?.b", | |
| 3561 AstFactory.propertyAccess2( | |
| 3562 AstFactory.identifier3("a"), "b", TokenType.QUESTION_PERIOD)); | |
| 3563 } | |
| 3564 | |
| 3565 void test_visitRedirectingConstructorInvocation_named() { | |
| 3566 _assertSource( | |
| 3567 "this.c()", AstFactory.redirectingConstructorInvocation2("c")); | |
| 3568 } | |
| 3569 | |
| 3570 void test_visitRedirectingConstructorInvocation_unnamed() { | |
| 3571 _assertSource("this()", AstFactory.redirectingConstructorInvocation()); | |
| 3572 } | |
| 3573 | |
| 3574 void test_visitRethrowExpression() { | |
| 3575 _assertSource("rethrow", AstFactory.rethrowExpression()); | |
| 3576 } | |
| 3577 | |
| 3578 void test_visitReturnStatement_expression() { | |
| 3579 _assertSource( | |
| 3580 "return a;", AstFactory.returnStatement2(AstFactory.identifier3("a"))); | |
| 3581 } | |
| 3582 | |
| 3583 void test_visitReturnStatement_noExpression() { | |
| 3584 _assertSource("return;", AstFactory.returnStatement()); | |
| 3585 } | |
| 3586 | |
| 3587 void test_visitScriptTag() { | |
| 3588 String scriptTag = "!#/bin/dart.exe"; | |
| 3589 _assertSource(scriptTag, AstFactory.scriptTag(scriptTag)); | |
| 3590 } | |
| 3591 | |
| 3592 void test_visitSimpleFormalParameter_annotation() { | |
| 3593 SimpleFormalParameter parameter = AstFactory.simpleFormalParameter3('x'); | |
| 3594 parameter.metadata.add(AstFactory.annotation(AstFactory.identifier3("A"))); | |
| 3595 _assertSource('@A x', parameter); | |
| 3596 } | |
| 3597 | |
| 3598 void test_visitSimpleFormalParameter_keyword() { | |
| 3599 _assertSource("var a", AstFactory.simpleFormalParameter(Keyword.VAR, "a")); | |
| 3600 } | |
| 3601 | |
| 3602 void test_visitSimpleFormalParameter_keyword_type() { | |
| 3603 _assertSource( | |
| 3604 "final A a", | |
| 3605 AstFactory.simpleFormalParameter2( | |
| 3606 Keyword.FINAL, AstFactory.typeName4("A"), "a")); | |
| 3607 } | |
| 3608 | |
| 3609 void test_visitSimpleFormalParameter_type() { | |
| 3610 _assertSource("A a", | |
| 3611 AstFactory.simpleFormalParameter4(AstFactory.typeName4("A"), "a")); | |
| 3612 } | |
| 3613 | |
| 3614 void test_visitSimpleIdentifier() { | |
| 3615 _assertSource("a", AstFactory.identifier3("a")); | |
| 3616 } | |
| 3617 | |
| 3618 void test_visitSimpleStringLiteral() { | |
| 3619 _assertSource("'a'", AstFactory.string2("a")); | |
| 3620 } | |
| 3621 | |
| 3622 void test_visitStringInterpolation() { | |
| 3623 _assertSource( | |
| 3624 "'a\${e}b'", | |
| 3625 AstFactory.string([ | |
| 3626 AstFactory.interpolationString("'a", "a"), | |
| 3627 AstFactory.interpolationExpression(AstFactory.identifier3("e")), | |
| 3628 AstFactory.interpolationString("b'", "b") | |
| 3629 ])); | |
| 3630 } | |
| 3631 | |
| 3632 void test_visitSuperConstructorInvocation() { | |
| 3633 _assertSource("super()", AstFactory.superConstructorInvocation()); | |
| 3634 } | |
| 3635 | |
| 3636 void test_visitSuperConstructorInvocation_named() { | |
| 3637 _assertSource("super.c()", AstFactory.superConstructorInvocation2("c")); | |
| 3638 } | |
| 3639 | |
| 3640 void test_visitSuperExpression() { | |
| 3641 _assertSource("super", AstFactory.superExpression()); | |
| 3642 } | |
| 3643 | |
| 3644 void test_visitSwitchCase_multipleLabels() { | |
| 3645 _assertSource( | |
| 3646 "l1: l2: case a: {}", | |
| 3647 AstFactory.switchCase2( | |
| 3648 [AstFactory.label2("l1"), AstFactory.label2("l2")], | |
| 3649 AstFactory.identifier3("a"), | |
| 3650 [AstFactory.block()])); | |
| 3651 } | |
| 3652 | |
| 3653 void test_visitSwitchCase_multipleStatements() { | |
| 3654 _assertSource( | |
| 3655 "case a: {} {}", | |
| 3656 AstFactory.switchCase(AstFactory.identifier3("a"), | |
| 3657 [AstFactory.block(), AstFactory.block()])); | |
| 3658 } | |
| 3659 | |
| 3660 void test_visitSwitchCase_noLabels() { | |
| 3661 _assertSource( | |
| 3662 "case a: {}", | |
| 3663 AstFactory | |
| 3664 .switchCase(AstFactory.identifier3("a"), [AstFactory.block()])); | |
| 3665 } | |
| 3666 | |
| 3667 void test_visitSwitchCase_singleLabel() { | |
| 3668 _assertSource( | |
| 3669 "l1: case a: {}", | |
| 3670 AstFactory.switchCase2([AstFactory.label2("l1")], | |
| 3671 AstFactory.identifier3("a"), [AstFactory.block()])); | |
| 3672 } | |
| 3673 | |
| 3674 void test_visitSwitchDefault_multipleLabels() { | |
| 3675 _assertSource( | |
| 3676 "l1: l2: default: {}", | |
| 3677 AstFactory.switchDefault( | |
| 3678 [AstFactory.label2("l1"), AstFactory.label2("l2")], | |
| 3679 [AstFactory.block()])); | |
| 3680 } | |
| 3681 | |
| 3682 void test_visitSwitchDefault_multipleStatements() { | |
| 3683 _assertSource("default: {} {}", | |
| 3684 AstFactory.switchDefault2([AstFactory.block(), AstFactory.block()])); | |
| 3685 } | |
| 3686 | |
| 3687 void test_visitSwitchDefault_noLabels() { | |
| 3688 _assertSource( | |
| 3689 "default: {}", AstFactory.switchDefault2([AstFactory.block()])); | |
| 3690 } | |
| 3691 | |
| 3692 void test_visitSwitchDefault_singleLabel() { | |
| 3693 _assertSource( | |
| 3694 "l1: default: {}", | |
| 3695 AstFactory | |
| 3696 .switchDefault([AstFactory.label2("l1")], [AstFactory.block()])); | |
| 3697 } | |
| 3698 | |
| 3699 void test_visitSwitchStatement() { | |
| 3700 _assertSource( | |
| 3701 "switch (a) {case 'b': {} default: {}}", | |
| 3702 AstFactory.switchStatement(AstFactory.identifier3("a"), [ | |
| 3703 AstFactory.switchCase(AstFactory.string2("b"), [AstFactory.block()]), | |
| 3704 AstFactory.switchDefault2([AstFactory.block()]) | |
| 3705 ])); | |
| 3706 } | |
| 3707 | |
| 3708 void test_visitSymbolLiteral_multiple() { | |
| 3709 _assertSource("#a.b.c", AstFactory.symbolLiteral(["a", "b", "c"])); | |
| 3710 } | |
| 3711 | |
| 3712 void test_visitSymbolLiteral_single() { | |
| 3713 _assertSource("#a", AstFactory.symbolLiteral(["a"])); | |
| 3714 } | |
| 3715 | |
| 3716 void test_visitThisExpression() { | |
| 3717 _assertSource("this", AstFactory.thisExpression()); | |
| 3718 } | |
| 3719 | |
| 3720 void test_visitThrowStatement() { | |
| 3721 _assertSource( | |
| 3722 "throw e", AstFactory.throwExpression2(AstFactory.identifier3("e"))); | |
| 3723 } | |
| 3724 | |
| 3725 void test_visitTopLevelVariableDeclaration_multiple() { | |
| 3726 _assertSource( | |
| 3727 "var a;", | |
| 3728 AstFactory.topLevelVariableDeclaration2( | |
| 3729 Keyword.VAR, [AstFactory.variableDeclaration("a")])); | |
| 3730 } | |
| 3731 | |
| 3732 void test_visitTopLevelVariableDeclaration_single() { | |
| 3733 _assertSource( | |
| 3734 "var a, b;", | |
| 3735 AstFactory.topLevelVariableDeclaration2(Keyword.VAR, [ | |
| 3736 AstFactory.variableDeclaration("a"), | |
| 3737 AstFactory.variableDeclaration("b") | |
| 3738 ])); | |
| 3739 } | |
| 3740 | |
| 3741 void test_visitTryStatement_catch() { | |
| 3742 _assertSource( | |
| 3743 "try {} on E {}", | |
| 3744 AstFactory.tryStatement2(AstFactory.block(), | |
| 3745 [AstFactory.catchClause3(AstFactory.typeName4("E"))])); | |
| 3746 } | |
| 3747 | |
| 3748 void test_visitTryStatement_catches() { | |
| 3749 _assertSource( | |
| 3750 "try {} on E {} on F {}", | |
| 3751 AstFactory.tryStatement2(AstFactory.block(), [ | |
| 3752 AstFactory.catchClause3(AstFactory.typeName4("E")), | |
| 3753 AstFactory.catchClause3(AstFactory.typeName4("F")) | |
| 3754 ])); | |
| 3755 } | |
| 3756 | |
| 3757 void test_visitTryStatement_catchFinally() { | |
| 3758 _assertSource( | |
| 3759 "try {} on E {} finally {}", | |
| 3760 AstFactory.tryStatement3( | |
| 3761 AstFactory.block(), | |
| 3762 [AstFactory.catchClause3(AstFactory.typeName4("E"))], | |
| 3763 AstFactory.block())); | |
| 3764 } | |
| 3765 | |
| 3766 void test_visitTryStatement_finally() { | |
| 3767 _assertSource("try {} finally {}", | |
| 3768 AstFactory.tryStatement(AstFactory.block(), AstFactory.block())); | |
| 3769 } | |
| 3770 | |
| 3771 void test_visitTypeArgumentList_multiple() { | |
| 3772 _assertSource( | |
| 3773 "<E, F>", | |
| 3774 AstFactory.typeArgumentList( | |
| 3775 [AstFactory.typeName4("E"), AstFactory.typeName4("F")])); | |
| 3776 } | |
| 3777 | |
| 3778 void test_visitTypeArgumentList_single() { | |
| 3779 _assertSource( | |
| 3780 "<E>", AstFactory.typeArgumentList([AstFactory.typeName4("E")])); | |
| 3781 } | |
| 3782 | |
| 3783 void test_visitTypeName_multipleArgs() { | |
| 3784 _assertSource( | |
| 3785 "C<D, E>", | |
| 3786 AstFactory.typeName4( | |
| 3787 "C", [AstFactory.typeName4("D"), AstFactory.typeName4("E")])); | |
| 3788 } | |
| 3789 | |
| 3790 void test_visitTypeName_nestedArg() { | |
| 3791 _assertSource( | |
| 3792 "C<D<E>>", | |
| 3793 AstFactory.typeName4("C", [ | |
| 3794 AstFactory.typeName4("D", [AstFactory.typeName4("E")]) | |
| 3795 ])); | |
| 3796 } | |
| 3797 | |
| 3798 void test_visitTypeName_noArgs() { | |
| 3799 _assertSource("C", AstFactory.typeName4("C")); | |
| 3800 } | |
| 3801 | |
| 3802 void test_visitTypeName_singleArg() { | |
| 3803 _assertSource( | |
| 3804 "C<D>", AstFactory.typeName4("C", [AstFactory.typeName4("D")])); | |
| 3805 } | |
| 3806 | |
| 3807 void test_visitTypeParameter_withExtends() { | |
| 3808 _assertSource("E extends C", | |
| 3809 AstFactory.typeParameter2("E", AstFactory.typeName4("C"))); | |
| 3810 } | |
| 3811 | |
| 3812 void test_visitTypeParameter_withMetadata() { | |
| 3813 TypeParameter parameter = AstFactory.typeParameter("E"); | |
| 3814 parameter.metadata | |
| 3815 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3816 _assertSource("@deprecated E", parameter); | |
| 3817 } | |
| 3818 | |
| 3819 void test_visitTypeParameter_withoutExtends() { | |
| 3820 _assertSource("E", AstFactory.typeParameter("E")); | |
| 3821 } | |
| 3822 | |
| 3823 void test_visitTypeParameterList_multiple() { | |
| 3824 _assertSource("<E, F>", AstFactory.typeParameterList(["E", "F"])); | |
| 3825 } | |
| 3826 | |
| 3827 void test_visitTypeParameterList_single() { | |
| 3828 _assertSource("<E>", AstFactory.typeParameterList(["E"])); | |
| 3829 } | |
| 3830 | |
| 3831 void test_visitVariableDeclaration_initialized() { | |
| 3832 _assertSource("a = b", | |
| 3833 AstFactory.variableDeclaration2("a", AstFactory.identifier3("b"))); | |
| 3834 } | |
| 3835 | |
| 3836 void test_visitVariableDeclaration_uninitialized() { | |
| 3837 _assertSource("a", AstFactory.variableDeclaration("a")); | |
| 3838 } | |
| 3839 | |
| 3840 void test_visitVariableDeclaration_withMetadata() { | |
| 3841 VariableDeclaration declaration = AstFactory.variableDeclaration("a"); | |
| 3842 declaration.metadata | |
| 3843 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3844 _assertSource("@deprecated a", declaration); | |
| 3845 } | |
| 3846 | |
| 3847 void test_visitVariableDeclarationList_const_type() { | |
| 3848 _assertSource( | |
| 3849 "const C a, b", | |
| 3850 AstFactory.variableDeclarationList( | |
| 3851 Keyword.CONST, AstFactory.typeName4("C"), [ | |
| 3852 AstFactory.variableDeclaration("a"), | |
| 3853 AstFactory.variableDeclaration("b") | |
| 3854 ])); | |
| 3855 } | |
| 3856 | |
| 3857 void test_visitVariableDeclarationList_final_noType() { | |
| 3858 _assertSource( | |
| 3859 "final a, b", | |
| 3860 AstFactory.variableDeclarationList2(Keyword.FINAL, [ | |
| 3861 AstFactory.variableDeclaration("a"), | |
| 3862 AstFactory.variableDeclaration("b") | |
| 3863 ])); | |
| 3864 } | |
| 3865 | |
| 3866 void test_visitVariableDeclarationList_final_withMetadata() { | |
| 3867 VariableDeclarationList declarationList = AstFactory | |
| 3868 .variableDeclarationList2(Keyword.FINAL, [ | |
| 3869 AstFactory.variableDeclaration("a"), | |
| 3870 AstFactory.variableDeclaration("b") | |
| 3871 ]); | |
| 3872 declarationList.metadata | |
| 3873 .add(AstFactory.annotation(AstFactory.identifier3("deprecated"))); | |
| 3874 _assertSource("@deprecated final a, b", declarationList); | |
| 3875 } | |
| 3876 | |
| 3877 void test_visitVariableDeclarationList_type() { | |
| 3878 _assertSource( | |
| 3879 "C a, b", | |
| 3880 AstFactory.variableDeclarationList(null, AstFactory.typeName4("C"), [ | |
| 3881 AstFactory.variableDeclaration("a"), | |
| 3882 AstFactory.variableDeclaration("b") | |
| 3883 ])); | |
| 3884 } | |
| 3885 | |
| 3886 void test_visitVariableDeclarationList_var() { | |
| 3887 _assertSource( | |
| 3888 "var a, b", | |
| 3889 AstFactory.variableDeclarationList2(Keyword.VAR, [ | |
| 3890 AstFactory.variableDeclaration("a"), | |
| 3891 AstFactory.variableDeclaration("b") | |
| 3892 ])); | |
| 3893 } | |
| 3894 | |
| 3895 void test_visitVariableDeclarationStatement() { | |
| 3896 _assertSource( | |
| 3897 "C c;", | |
| 3898 AstFactory.variableDeclarationStatement(null, AstFactory.typeName4("C"), | |
| 3899 [AstFactory.variableDeclaration("c")])); | |
| 3900 } | |
| 3901 | |
| 3902 void test_visitWhileStatement() { | |
| 3903 _assertSource( | |
| 3904 "while (c) {}", | |
| 3905 AstFactory.whileStatement( | |
| 3906 AstFactory.identifier3("c"), AstFactory.block())); | |
| 3907 } | |
| 3908 | |
| 3909 void test_visitWithClause_multiple() { | |
| 3910 _assertSource( | |
| 3911 "with A, B, C", | |
| 3912 AstFactory.withClause([ | |
| 3913 AstFactory.typeName4("A"), | |
| 3914 AstFactory.typeName4("B"), | |
| 3915 AstFactory.typeName4("C") | |
| 3916 ])); | |
| 3917 } | |
| 3918 | |
| 3919 void test_visitWithClause_single() { | |
| 3920 _assertSource("with A", AstFactory.withClause([AstFactory.typeName4("A")])); | |
| 3921 } | |
| 3922 | |
| 3923 void test_visitYieldStatement() { | |
| 3924 _assertSource( | |
| 3925 "yield e;", AstFactory.yieldStatement(AstFactory.identifier3("e"))); | |
| 3926 } | |
| 3927 | |
| 3928 void test_visitYieldStatement_each() { | |
| 3929 _assertSource("yield* e;", | |
| 3930 AstFactory.yieldEachStatement(AstFactory.identifier3("e"))); | |
| 3931 } | |
| 3932 | |
| 3933 /** | |
| 3934 * Assert that a `ToSourceVisitor` will produce the expected source when visit
ing the given | |
| 3935 * node. | |
| 3936 * | |
| 3937 * @param expectedSource the source string that the visitor is expected to pro
duce | |
| 3938 * @param node the AST node being visited to produce the actual source | |
| 3939 * @throws AFE if the visitor does not produce the expected source for the giv
en node | |
| 3940 */ | |
| 3941 void _assertSource(String expectedSource, AstNode node) { | |
| 3942 PrintStringWriter writer = new PrintStringWriter(); | |
| 3943 node.accept(new ToSourceVisitor(writer)); | |
| 3944 expect(writer.toString(), expectedSource); | |
| 3945 } | |
| 3946 } | |
| 3947 | |
| 3948 @reflectiveTest | |
| 3949 class VariableDeclarationTest extends ParserTestCase { | |
| 3950 void test_getDocumentationComment_onGrandParent() { | |
| 3951 VariableDeclaration varDecl = AstFactory.variableDeclaration("a"); | |
| 3952 TopLevelVariableDeclaration decl = | |
| 3953 AstFactory.topLevelVariableDeclaration2(Keyword.VAR, [varDecl]); | |
| 3954 Comment comment = Comment.createDocumentationComment(new List<Token>(0)); | |
| 3955 expect(varDecl.documentationComment, isNull); | |
| 3956 decl.documentationComment = comment; | |
| 3957 expect(varDecl.documentationComment, isNotNull); | |
| 3958 expect(decl.documentationComment, isNotNull); | |
| 3959 } | |
| 3960 | |
| 3961 void test_getDocumentationComment_onNode() { | |
| 3962 VariableDeclaration decl = AstFactory.variableDeclaration("a"); | |
| 3963 Comment comment = Comment.createDocumentationComment(new List<Token>(0)); | |
| 3964 decl.documentationComment = comment; | |
| 3965 expect(decl.documentationComment, isNotNull); | |
| 3966 } | |
| 3967 } | |
| 3968 | |
| 3969 class WrapperKind extends Enum<WrapperKind> { | |
| 3970 static const WrapperKind PREFIXED_LEFT = | |
| 3971 const WrapperKind('PREFIXED_LEFT', 0); | |
| 3972 | |
| 3973 static const WrapperKind PREFIXED_RIGHT = | |
| 3974 const WrapperKind('PREFIXED_RIGHT', 1); | |
| 3975 | |
| 3976 static const WrapperKind PROPERTY_LEFT = | |
| 3977 const WrapperKind('PROPERTY_LEFT', 2); | |
| 3978 | |
| 3979 static const WrapperKind PROPERTY_RIGHT = | |
| 3980 const WrapperKind('PROPERTY_RIGHT', 3); | |
| 3981 | |
| 3982 static const WrapperKind NONE = const WrapperKind('NONE', 4); | |
| 3983 | |
| 3984 static const List<WrapperKind> values = const [ | |
| 3985 PREFIXED_LEFT, | |
| 3986 PREFIXED_RIGHT, | |
| 3987 PROPERTY_LEFT, | |
| 3988 PROPERTY_RIGHT, | |
| 3989 NONE | |
| 3990 ]; | |
| 3991 | |
| 3992 const WrapperKind(String name, int ordinal) : super(name, ordinal); | |
| 3993 } | |
| OLD | NEW |