| 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.dart.ast.ast_test; |
| 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/src/generated/java_core.dart'; |
| 9 import 'package:analyzer/src/generated/scanner.dart'; |
| 10 import 'package:analyzer/src/generated/testing/ast_factory.dart'; |
| 11 import 'package:analyzer/src/generated/testing/token_factory.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 |
| 14 import '../../generated/parser_test.dart' show ParserTestCase; |
| 15 import '../../generated/test_support.dart'; |
| 16 import '../../reflective_tests.dart'; |
| 17 import '../../utils.dart'; |
| 18 |
| 19 main() { |
| 20 initializeTestEnvironment(); |
| 21 runReflectiveTests(ClassDeclarationTest); |
| 22 runReflectiveTests(ClassTypeAliasTest); |
| 23 runReflectiveTests(ConstructorDeclarationTest); |
| 24 runReflectiveTests(FieldFormalParameterTest); |
| 25 runReflectiveTests(IndexExpressionTest); |
| 26 runReflectiveTests(NodeListTest); |
| 27 runReflectiveTests(SimpleIdentifierTest); |
| 28 runReflectiveTests(SimpleStringLiteralTest); |
| 29 runReflectiveTests(StringInterpolationTest); |
| 30 runReflectiveTests(VariableDeclarationTest); |
| 31 } |
| 32 |
| 33 @reflectiveTest |
| 34 class ClassDeclarationTest extends ParserTestCase { |
| 35 void test_getConstructor() { |
| 36 List<ConstructorInitializer> initializers = |
| 37 new List<ConstructorInitializer>(); |
| 38 ConstructorDeclaration defaultConstructor = |
| 39 AstFactory.constructorDeclaration(AstFactory.identifier3("Test"), null, |
| 40 AstFactory.formalParameterList(), initializers); |
| 41 ConstructorDeclaration aConstructor = AstFactory.constructorDeclaration( |
| 42 AstFactory.identifier3("Test"), |
| 43 "a", |
| 44 AstFactory.formalParameterList(), |
| 45 initializers); |
| 46 ConstructorDeclaration bConstructor = AstFactory.constructorDeclaration( |
| 47 AstFactory.identifier3("Test"), |
| 48 "b", |
| 49 AstFactory.formalParameterList(), |
| 50 initializers); |
| 51 ClassDeclaration clazz = AstFactory.classDeclaration(null, "Test", null, |
| 52 null, null, null, [defaultConstructor, aConstructor, bConstructor]); |
| 53 expect(clazz.getConstructor(null), same(defaultConstructor)); |
| 54 expect(clazz.getConstructor("a"), same(aConstructor)); |
| 55 expect(clazz.getConstructor("b"), same(bConstructor)); |
| 56 expect(clazz.getConstructor("noSuchConstructor"), same(null)); |
| 57 } |
| 58 |
| 59 void test_getField() { |
| 60 VariableDeclaration aVar = AstFactory.variableDeclaration("a"); |
| 61 VariableDeclaration bVar = AstFactory.variableDeclaration("b"); |
| 62 VariableDeclaration cVar = AstFactory.variableDeclaration("c"); |
| 63 ClassDeclaration clazz = |
| 64 AstFactory.classDeclaration(null, "Test", null, null, null, null, [ |
| 65 AstFactory.fieldDeclaration2(false, null, [aVar]), |
| 66 AstFactory.fieldDeclaration2(false, null, [bVar, cVar]) |
| 67 ]); |
| 68 expect(clazz.getField("a"), same(aVar)); |
| 69 expect(clazz.getField("b"), same(bVar)); |
| 70 expect(clazz.getField("c"), same(cVar)); |
| 71 expect(clazz.getField("noSuchField"), same(null)); |
| 72 } |
| 73 |
| 74 void test_getMethod() { |
| 75 MethodDeclaration aMethod = AstFactory.methodDeclaration(null, null, null, |
| 76 null, AstFactory.identifier3("a"), AstFactory.formalParameterList()); |
| 77 MethodDeclaration bMethod = AstFactory.methodDeclaration(null, null, null, |
| 78 null, AstFactory.identifier3("b"), AstFactory.formalParameterList()); |
| 79 ClassDeclaration clazz = AstFactory.classDeclaration( |
| 80 null, "Test", null, null, null, null, [aMethod, bMethod]); |
| 81 expect(clazz.getMethod("a"), same(aMethod)); |
| 82 expect(clazz.getMethod("b"), same(bMethod)); |
| 83 expect(clazz.getMethod("noSuchMethod"), same(null)); |
| 84 } |
| 85 |
| 86 void test_isAbstract() { |
| 87 expect( |
| 88 AstFactory |
| 89 .classDeclaration(null, "A", null, null, null, null) |
| 90 .isAbstract, |
| 91 isFalse); |
| 92 expect( |
| 93 AstFactory |
| 94 .classDeclaration(Keyword.ABSTRACT, "B", null, null, null, null) |
| 95 .isAbstract, |
| 96 isTrue); |
| 97 } |
| 98 } |
| 99 |
| 100 @reflectiveTest |
| 101 class ClassTypeAliasTest extends ParserTestCase { |
| 102 void test_isAbstract() { |
| 103 expect( |
| 104 AstFactory.classTypeAlias("A", null, null, null, null, null).isAbstract, |
| 105 isFalse); |
| 106 expect( |
| 107 AstFactory |
| 108 .classTypeAlias("B", null, Keyword.ABSTRACT, null, null, null) |
| 109 .isAbstract, |
| 110 isTrue); |
| 111 } |
| 112 } |
| 113 |
| 114 @reflectiveTest |
| 115 class ConstructorDeclarationTest extends EngineTestCase { |
| 116 void test_firstTokenAfterCommentAndMetadata_all_inverted() { |
| 117 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); |
| 118 externalKeyword.offset = 14; |
| 119 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( |
| 120 Keyword.CONST, |
| 121 Keyword.FACTORY, |
| 122 AstFactory.identifier3('int'), |
| 123 null, |
| 124 null, |
| 125 null, |
| 126 null); |
| 127 declaration.externalKeyword = externalKeyword; |
| 128 declaration.constKeyword.offset = 8; |
| 129 Token factoryKeyword = declaration.factoryKeyword; |
| 130 factoryKeyword.offset = 0; |
| 131 expect(declaration.firstTokenAfterCommentAndMetadata, factoryKeyword); |
| 132 } |
| 133 |
| 134 void test_firstTokenAfterCommentAndMetadata_all_normal() { |
| 135 Token token = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); |
| 136 token.offset = 0; |
| 137 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( |
| 138 Keyword.CONST, |
| 139 Keyword.FACTORY, |
| 140 AstFactory.identifier3('int'), |
| 141 null, |
| 142 null, |
| 143 null, |
| 144 null); |
| 145 declaration.externalKeyword = token; |
| 146 declaration.constKeyword.offset = 9; |
| 147 declaration.factoryKeyword.offset = 15; |
| 148 expect(declaration.firstTokenAfterCommentAndMetadata, token); |
| 149 } |
| 150 |
| 151 void test_firstTokenAfterCommentAndMetadata_constOnly() { |
| 152 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( |
| 153 Keyword.CONST, |
| 154 null, |
| 155 AstFactory.identifier3('int'), |
| 156 null, |
| 157 null, |
| 158 null, |
| 159 null); |
| 160 expect(declaration.firstTokenAfterCommentAndMetadata, |
| 161 declaration.constKeyword); |
| 162 } |
| 163 |
| 164 void test_firstTokenAfterCommentAndMetadata_externalOnly() { |
| 165 Token externalKeyword = TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); |
| 166 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( |
| 167 null, null, AstFactory.identifier3('int'), null, null, null, null); |
| 168 declaration.externalKeyword = externalKeyword; |
| 169 expect(declaration.firstTokenAfterCommentAndMetadata, externalKeyword); |
| 170 } |
| 171 |
| 172 void test_firstTokenAfterCommentAndMetadata_factoryOnly() { |
| 173 ConstructorDeclaration declaration = AstFactory.constructorDeclaration2( |
| 174 null, |
| 175 Keyword.FACTORY, |
| 176 AstFactory.identifier3('int'), |
| 177 null, |
| 178 null, |
| 179 null, |
| 180 null); |
| 181 expect(declaration.firstTokenAfterCommentAndMetadata, |
| 182 declaration.factoryKeyword); |
| 183 } |
| 184 } |
| 185 |
| 186 @reflectiveTest |
| 187 class FieldFormalParameterTest extends EngineTestCase { |
| 188 void test_endToken_noParameters() { |
| 189 FieldFormalParameter parameter = AstFactory.fieldFormalParameter2('field'); |
| 190 expect(parameter.endToken, parameter.identifier.endToken); |
| 191 } |
| 192 |
| 193 void test_endToken_parameters() { |
| 194 FieldFormalParameter parameter = AstFactory.fieldFormalParameter( |
| 195 null, null, 'field', AstFactory.formalParameterList([])); |
| 196 expect(parameter.endToken, parameter.parameters.endToken); |
| 197 } |
| 198 } |
| 199 |
| 200 @reflectiveTest |
| 201 class IndexExpressionTest extends EngineTestCase { |
| 202 void test_inGetterContext_assignment_compound_left() { |
| 203 IndexExpression expression = AstFactory.indexExpression( |
| 204 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 205 // a[b] += c |
| 206 AstFactory.assignmentExpression( |
| 207 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c")); |
| 208 expect(expression.inGetterContext(), isTrue); |
| 209 } |
| 210 |
| 211 void test_inGetterContext_assignment_simple_left() { |
| 212 IndexExpression expression = AstFactory.indexExpression( |
| 213 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 214 // a[b] = c |
| 215 AstFactory.assignmentExpression( |
| 216 expression, TokenType.EQ, AstFactory.identifier3("c")); |
| 217 expect(expression.inGetterContext(), isFalse); |
| 218 } |
| 219 |
| 220 void test_inGetterContext_nonAssignment() { |
| 221 IndexExpression expression = AstFactory.indexExpression( |
| 222 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 223 // a[b] + c |
| 224 AstFactory.binaryExpression( |
| 225 expression, TokenType.PLUS, AstFactory.identifier3("c")); |
| 226 expect(expression.inGetterContext(), isTrue); |
| 227 } |
| 228 |
| 229 void test_inSetterContext_assignment_compound_left() { |
| 230 IndexExpression expression = AstFactory.indexExpression( |
| 231 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 232 // a[b] += c |
| 233 AstFactory.assignmentExpression( |
| 234 expression, TokenType.PLUS_EQ, AstFactory.identifier3("c")); |
| 235 expect(expression.inSetterContext(), isTrue); |
| 236 } |
| 237 |
| 238 void test_inSetterContext_assignment_compound_right() { |
| 239 IndexExpression expression = AstFactory.indexExpression( |
| 240 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 241 // c += a[b] |
| 242 AstFactory.assignmentExpression( |
| 243 AstFactory.identifier3("c"), TokenType.PLUS_EQ, expression); |
| 244 expect(expression.inSetterContext(), isFalse); |
| 245 } |
| 246 |
| 247 void test_inSetterContext_assignment_simple_left() { |
| 248 IndexExpression expression = AstFactory.indexExpression( |
| 249 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 250 // a[b] = c |
| 251 AstFactory.assignmentExpression( |
| 252 expression, TokenType.EQ, AstFactory.identifier3("c")); |
| 253 expect(expression.inSetterContext(), isTrue); |
| 254 } |
| 255 |
| 256 void test_inSetterContext_assignment_simple_right() { |
| 257 IndexExpression expression = AstFactory.indexExpression( |
| 258 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 259 // c = a[b] |
| 260 AstFactory.assignmentExpression( |
| 261 AstFactory.identifier3("c"), TokenType.EQ, expression); |
| 262 expect(expression.inSetterContext(), isFalse); |
| 263 } |
| 264 |
| 265 void test_inSetterContext_nonAssignment() { |
| 266 IndexExpression expression = AstFactory.indexExpression( |
| 267 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 268 AstFactory.binaryExpression( |
| 269 expression, TokenType.PLUS, AstFactory.identifier3("c")); |
| 270 // a[b] + cc |
| 271 expect(expression.inSetterContext(), isFalse); |
| 272 } |
| 273 |
| 274 void test_inSetterContext_postfix() { |
| 275 IndexExpression expression = AstFactory.indexExpression( |
| 276 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 277 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS); |
| 278 // a[b]++ |
| 279 expect(expression.inSetterContext(), isTrue); |
| 280 } |
| 281 |
| 282 void test_inSetterContext_prefix_bang() { |
| 283 IndexExpression expression = AstFactory.indexExpression( |
| 284 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 285 // !a[b] |
| 286 AstFactory.prefixExpression(TokenType.BANG, expression); |
| 287 expect(expression.inSetterContext(), isFalse); |
| 288 } |
| 289 |
| 290 void test_inSetterContext_prefix_minusMinus() { |
| 291 IndexExpression expression = AstFactory.indexExpression( |
| 292 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 293 // --a[b] |
| 294 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression); |
| 295 expect(expression.inSetterContext(), isTrue); |
| 296 } |
| 297 |
| 298 void test_inSetterContext_prefix_plusPlus() { |
| 299 IndexExpression expression = AstFactory.indexExpression( |
| 300 AstFactory.identifier3("a"), AstFactory.identifier3("b")); |
| 301 // ++a[b] |
| 302 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression); |
| 303 expect(expression.inSetterContext(), isTrue); |
| 304 } |
| 305 } |
| 306 |
| 307 @reflectiveTest |
| 308 class NodeListTest extends EngineTestCase { |
| 309 void test_add() { |
| 310 AstNode parent = AstFactory.argumentList(); |
| 311 AstNode firstNode = AstFactory.booleanLiteral(true); |
| 312 AstNode secondNode = AstFactory.booleanLiteral(false); |
| 313 NodeList<AstNode> list = new NodeList<AstNode>(parent); |
| 314 list.insert(0, secondNode); |
| 315 list.insert(0, firstNode); |
| 316 expect(list, hasLength(2)); |
| 317 expect(list[0], same(firstNode)); |
| 318 expect(list[1], same(secondNode)); |
| 319 expect(firstNode.parent, same(parent)); |
| 320 expect(secondNode.parent, same(parent)); |
| 321 AstNode thirdNode = AstFactory.booleanLiteral(false); |
| 322 list.insert(1, thirdNode); |
| 323 expect(list, hasLength(3)); |
| 324 expect(list[0], same(firstNode)); |
| 325 expect(list[1], same(thirdNode)); |
| 326 expect(list[2], same(secondNode)); |
| 327 expect(firstNode.parent, same(parent)); |
| 328 expect(secondNode.parent, same(parent)); |
| 329 expect(thirdNode.parent, same(parent)); |
| 330 } |
| 331 |
| 332 void test_add_negative() { |
| 333 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 334 try { |
| 335 list.insert(-1, AstFactory.booleanLiteral(true)); |
| 336 fail("Expected IndexOutOfBoundsException"); |
| 337 } on RangeError { |
| 338 // Expected |
| 339 } |
| 340 } |
| 341 |
| 342 void test_add_tooBig() { |
| 343 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 344 try { |
| 345 list.insert(1, AstFactory.booleanLiteral(true)); |
| 346 fail("Expected IndexOutOfBoundsException"); |
| 347 } on RangeError { |
| 348 // Expected |
| 349 } |
| 350 } |
| 351 |
| 352 void test_addAll() { |
| 353 AstNode parent = AstFactory.argumentList(); |
| 354 List<AstNode> firstNodes = new List<AstNode>(); |
| 355 AstNode firstNode = AstFactory.booleanLiteral(true); |
| 356 AstNode secondNode = AstFactory.booleanLiteral(false); |
| 357 firstNodes.add(firstNode); |
| 358 firstNodes.add(secondNode); |
| 359 NodeList<AstNode> list = new NodeList<AstNode>(parent); |
| 360 list.addAll(firstNodes); |
| 361 expect(list, hasLength(2)); |
| 362 expect(list[0], same(firstNode)); |
| 363 expect(list[1], same(secondNode)); |
| 364 expect(firstNode.parent, same(parent)); |
| 365 expect(secondNode.parent, same(parent)); |
| 366 List<AstNode> secondNodes = new List<AstNode>(); |
| 367 AstNode thirdNode = AstFactory.booleanLiteral(true); |
| 368 AstNode fourthNode = AstFactory.booleanLiteral(false); |
| 369 secondNodes.add(thirdNode); |
| 370 secondNodes.add(fourthNode); |
| 371 list.addAll(secondNodes); |
| 372 expect(list, hasLength(4)); |
| 373 expect(list[0], same(firstNode)); |
| 374 expect(list[1], same(secondNode)); |
| 375 expect(list[2], same(thirdNode)); |
| 376 expect(list[3], same(fourthNode)); |
| 377 expect(firstNode.parent, same(parent)); |
| 378 expect(secondNode.parent, same(parent)); |
| 379 expect(thirdNode.parent, same(parent)); |
| 380 expect(fourthNode.parent, same(parent)); |
| 381 } |
| 382 |
| 383 void test_creation() { |
| 384 AstNode owner = AstFactory.argumentList(); |
| 385 NodeList<AstNode> list = new NodeList<AstNode>(owner); |
| 386 expect(list, isNotNull); |
| 387 expect(list, hasLength(0)); |
| 388 expect(list.owner, same(owner)); |
| 389 } |
| 390 |
| 391 void test_get_negative() { |
| 392 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 393 try { |
| 394 list[-1]; |
| 395 fail("Expected IndexOutOfBoundsException"); |
| 396 } on RangeError { |
| 397 // Expected |
| 398 } |
| 399 } |
| 400 |
| 401 void test_get_tooBig() { |
| 402 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 403 try { |
| 404 list[1]; |
| 405 fail("Expected IndexOutOfBoundsException"); |
| 406 } on RangeError { |
| 407 // Expected |
| 408 } |
| 409 } |
| 410 |
| 411 void test_getBeginToken_empty() { |
| 412 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 413 expect(list.beginToken, isNull); |
| 414 } |
| 415 |
| 416 void test_getBeginToken_nonEmpty() { |
| 417 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 418 AstNode node = |
| 419 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true)); |
| 420 list.add(node); |
| 421 expect(list.beginToken, same(node.beginToken)); |
| 422 } |
| 423 |
| 424 void test_getEndToken_empty() { |
| 425 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 426 expect(list.endToken, isNull); |
| 427 } |
| 428 |
| 429 void test_getEndToken_nonEmpty() { |
| 430 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 431 AstNode node = |
| 432 AstFactory.parenthesizedExpression(AstFactory.booleanLiteral(true)); |
| 433 list.add(node); |
| 434 expect(list.endToken, same(node.endToken)); |
| 435 } |
| 436 |
| 437 void test_indexOf() { |
| 438 List<AstNode> nodes = new List<AstNode>(); |
| 439 AstNode firstNode = AstFactory.booleanLiteral(true); |
| 440 AstNode secondNode = AstFactory.booleanLiteral(false); |
| 441 AstNode thirdNode = AstFactory.booleanLiteral(true); |
| 442 AstNode fourthNode = AstFactory.booleanLiteral(false); |
| 443 nodes.add(firstNode); |
| 444 nodes.add(secondNode); |
| 445 nodes.add(thirdNode); |
| 446 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 447 list.addAll(nodes); |
| 448 expect(list, hasLength(3)); |
| 449 expect(list.indexOf(firstNode), 0); |
| 450 expect(list.indexOf(secondNode), 1); |
| 451 expect(list.indexOf(thirdNode), 2); |
| 452 expect(list.indexOf(fourthNode), -1); |
| 453 expect(list.indexOf(null), -1); |
| 454 } |
| 455 |
| 456 void test_remove() { |
| 457 List<AstNode> nodes = new List<AstNode>(); |
| 458 AstNode firstNode = AstFactory.booleanLiteral(true); |
| 459 AstNode secondNode = AstFactory.booleanLiteral(false); |
| 460 AstNode thirdNode = AstFactory.booleanLiteral(true); |
| 461 nodes.add(firstNode); |
| 462 nodes.add(secondNode); |
| 463 nodes.add(thirdNode); |
| 464 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 465 list.addAll(nodes); |
| 466 expect(list, hasLength(3)); |
| 467 expect(list.removeAt(1), same(secondNode)); |
| 468 expect(list, hasLength(2)); |
| 469 expect(list[0], same(firstNode)); |
| 470 expect(list[1], same(thirdNode)); |
| 471 } |
| 472 |
| 473 void test_remove_negative() { |
| 474 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 475 try { |
| 476 list.removeAt(-1); |
| 477 fail("Expected IndexOutOfBoundsException"); |
| 478 } on RangeError { |
| 479 // Expected |
| 480 } |
| 481 } |
| 482 |
| 483 void test_remove_tooBig() { |
| 484 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 485 try { |
| 486 list.removeAt(1); |
| 487 fail("Expected IndexOutOfBoundsException"); |
| 488 } on RangeError { |
| 489 // Expected |
| 490 } |
| 491 } |
| 492 |
| 493 void test_set() { |
| 494 List<AstNode> nodes = new List<AstNode>(); |
| 495 AstNode firstNode = AstFactory.booleanLiteral(true); |
| 496 AstNode secondNode = AstFactory.booleanLiteral(false); |
| 497 AstNode thirdNode = AstFactory.booleanLiteral(true); |
| 498 nodes.add(firstNode); |
| 499 nodes.add(secondNode); |
| 500 nodes.add(thirdNode); |
| 501 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 502 list.addAll(nodes); |
| 503 expect(list, hasLength(3)); |
| 504 AstNode fourthNode = AstFactory.integer(0); |
| 505 expect(javaListSet(list, 1, fourthNode), same(secondNode)); |
| 506 expect(list, hasLength(3)); |
| 507 expect(list[0], same(firstNode)); |
| 508 expect(list[1], same(fourthNode)); |
| 509 expect(list[2], same(thirdNode)); |
| 510 } |
| 511 |
| 512 void test_set_negative() { |
| 513 AstNode node = AstFactory.booleanLiteral(true); |
| 514 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 515 try { |
| 516 javaListSet(list, -1, node); |
| 517 fail("Expected IndexOutOfBoundsException"); |
| 518 } on RangeError { |
| 519 // Expected |
| 520 } |
| 521 } |
| 522 |
| 523 void test_set_tooBig() { |
| 524 AstNode node = AstFactory.booleanLiteral(true); |
| 525 NodeList<AstNode> list = new NodeList<AstNode>(AstFactory.argumentList()); |
| 526 try { |
| 527 javaListSet(list, 1, node); |
| 528 fail("Expected IndexOutOfBoundsException"); |
| 529 } on RangeError { |
| 530 // Expected |
| 531 } |
| 532 } |
| 533 } |
| 534 |
| 535 @reflectiveTest |
| 536 class SimpleIdentifierTest extends ParserTestCase { |
| 537 void test_inDeclarationContext_catch_exception() { |
| 538 SimpleIdentifier identifier = |
| 539 AstFactory.catchClause("e").exceptionParameter; |
| 540 expect(identifier.inDeclarationContext(), isTrue); |
| 541 } |
| 542 |
| 543 void test_inDeclarationContext_catch_stack() { |
| 544 SimpleIdentifier identifier = |
| 545 AstFactory.catchClause2("e", "s").stackTraceParameter; |
| 546 expect(identifier.inDeclarationContext(), isTrue); |
| 547 } |
| 548 |
| 549 void test_inDeclarationContext_classDeclaration() { |
| 550 SimpleIdentifier identifier = |
| 551 AstFactory.classDeclaration(null, "C", null, null, null, null).name; |
| 552 expect(identifier.inDeclarationContext(), isTrue); |
| 553 } |
| 554 |
| 555 void test_inDeclarationContext_classTypeAlias() { |
| 556 SimpleIdentifier identifier = |
| 557 AstFactory.classTypeAlias("C", null, null, null, null, null).name; |
| 558 expect(identifier.inDeclarationContext(), isTrue); |
| 559 } |
| 560 |
| 561 void test_inDeclarationContext_constructorDeclaration() { |
| 562 SimpleIdentifier identifier = AstFactory |
| 563 .constructorDeclaration(AstFactory.identifier3("C"), "c", null, null) |
| 564 .name; |
| 565 expect(identifier.inDeclarationContext(), isTrue); |
| 566 } |
| 567 |
| 568 void test_inDeclarationContext_declaredIdentifier() { |
| 569 DeclaredIdentifier declaredIdentifier = AstFactory.declaredIdentifier3("v"); |
| 570 SimpleIdentifier identifier = declaredIdentifier.identifier; |
| 571 expect(identifier.inDeclarationContext(), isTrue); |
| 572 } |
| 573 |
| 574 void test_inDeclarationContext_enumConstantDeclaration() { |
| 575 EnumDeclaration enumDeclaration = |
| 576 AstFactory.enumDeclaration2('MyEnum', ['CONST']); |
| 577 SimpleIdentifier identifier = enumDeclaration.constants[0].name; |
| 578 expect(identifier.inDeclarationContext(), isTrue); |
| 579 } |
| 580 |
| 581 void test_inDeclarationContext_enumDeclaration() { |
| 582 EnumDeclaration enumDeclaration = |
| 583 AstFactory.enumDeclaration2('MyEnum', ['A', 'B', 'C']); |
| 584 SimpleIdentifier identifier = enumDeclaration.name; |
| 585 expect(identifier.inDeclarationContext(), isTrue); |
| 586 } |
| 587 |
| 588 void test_inDeclarationContext_fieldFormalParameter() { |
| 589 SimpleIdentifier identifier = |
| 590 AstFactory.fieldFormalParameter2("p").identifier; |
| 591 expect(identifier.inDeclarationContext(), isFalse); |
| 592 } |
| 593 |
| 594 void test_inDeclarationContext_functionDeclaration() { |
| 595 SimpleIdentifier identifier = |
| 596 AstFactory.functionDeclaration(null, null, "f", null).name; |
| 597 expect(identifier.inDeclarationContext(), isTrue); |
| 598 } |
| 599 |
| 600 void test_inDeclarationContext_functionTypeAlias() { |
| 601 SimpleIdentifier identifier = |
| 602 AstFactory.typeAlias(null, "F", null, null).name; |
| 603 expect(identifier.inDeclarationContext(), isTrue); |
| 604 } |
| 605 |
| 606 void test_inDeclarationContext_label_false() { |
| 607 SimpleIdentifier identifier = |
| 608 AstFactory.namedExpression2("l", AstFactory.integer(0)).name.label; |
| 609 expect(identifier.inDeclarationContext(), isFalse); |
| 610 } |
| 611 |
| 612 void test_inDeclarationContext_label_true() { |
| 613 Label label = AstFactory.label2("l"); |
| 614 SimpleIdentifier identifier = label.label; |
| 615 AstFactory.labeledStatement([label], AstFactory.emptyStatement()); |
| 616 expect(identifier.inDeclarationContext(), isTrue); |
| 617 } |
| 618 |
| 619 void test_inDeclarationContext_methodDeclaration() { |
| 620 SimpleIdentifier identifier = AstFactory.identifier3("m"); |
| 621 AstFactory.methodDeclaration2( |
| 622 null, null, null, null, identifier, null, null); |
| 623 expect(identifier.inDeclarationContext(), isTrue); |
| 624 } |
| 625 |
| 626 void test_inDeclarationContext_prefix() { |
| 627 SimpleIdentifier identifier = |
| 628 AstFactory.importDirective3("uri", "pref").prefix; |
| 629 expect(identifier.inDeclarationContext(), isTrue); |
| 630 } |
| 631 |
| 632 void test_inDeclarationContext_simpleFormalParameter() { |
| 633 SimpleIdentifier identifier = |
| 634 AstFactory.simpleFormalParameter3("p").identifier; |
| 635 expect(identifier.inDeclarationContext(), isTrue); |
| 636 } |
| 637 |
| 638 void test_inDeclarationContext_typeParameter_bound() { |
| 639 TypeName bound = AstFactory.typeName4("A"); |
| 640 SimpleIdentifier identifier = bound.name as SimpleIdentifier; |
| 641 AstFactory.typeParameter2("E", bound); |
| 642 expect(identifier.inDeclarationContext(), isFalse); |
| 643 } |
| 644 |
| 645 void test_inDeclarationContext_typeParameter_name() { |
| 646 SimpleIdentifier identifier = AstFactory.typeParameter("E").name; |
| 647 expect(identifier.inDeclarationContext(), isTrue); |
| 648 } |
| 649 |
| 650 void test_inDeclarationContext_variableDeclaration() { |
| 651 SimpleIdentifier identifier = AstFactory.variableDeclaration("v").name; |
| 652 expect(identifier.inDeclarationContext(), isTrue); |
| 653 } |
| 654 |
| 655 void test_inGetterContext() { |
| 656 for (_WrapperKind wrapper in _WrapperKind.values) { |
| 657 for (_AssignmentKind assignment in _AssignmentKind.values) { |
| 658 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment); |
| 659 if (assignment == _AssignmentKind.SIMPLE_LEFT && |
| 660 wrapper != _WrapperKind.PREFIXED_LEFT && |
| 661 wrapper != _WrapperKind.PROPERTY_LEFT) { |
| 662 if (identifier.inGetterContext()) { |
| 663 fail("Expected ${_topMostNode(identifier).toSource()} to be false"); |
| 664 } |
| 665 } else { |
| 666 if (!identifier.inGetterContext()) { |
| 667 fail("Expected ${_topMostNode(identifier).toSource()} to be true"); |
| 668 } |
| 669 } |
| 670 } |
| 671 } |
| 672 } |
| 673 |
| 674 void test_inGetterContext_forEachLoop() { |
| 675 SimpleIdentifier identifier = AstFactory.identifier3("a"); |
| 676 Expression iterator = AstFactory.listLiteral(); |
| 677 Statement body = AstFactory.block(); |
| 678 AstFactory.forEachStatement2(identifier, iterator, body); |
| 679 expect(identifier.inGetterContext(), isFalse); |
| 680 } |
| 681 |
| 682 void test_inReferenceContext() { |
| 683 SimpleIdentifier identifier = AstFactory.identifier3("id"); |
| 684 AstFactory.namedExpression( |
| 685 AstFactory.label(identifier), AstFactory.identifier3("_")); |
| 686 expect(identifier.inGetterContext(), isFalse); |
| 687 expect(identifier.inSetterContext(), isFalse); |
| 688 } |
| 689 |
| 690 void test_inSetterContext() { |
| 691 for (_WrapperKind wrapper in _WrapperKind.values) { |
| 692 for (_AssignmentKind assignment in _AssignmentKind.values) { |
| 693 SimpleIdentifier identifier = _createIdentifier(wrapper, assignment); |
| 694 if (wrapper == _WrapperKind.PREFIXED_LEFT || |
| 695 wrapper == _WrapperKind.PROPERTY_LEFT || |
| 696 assignment == _AssignmentKind.BINARY || |
| 697 assignment == _AssignmentKind.COMPOUND_RIGHT || |
| 698 assignment == _AssignmentKind.PREFIX_NOT || |
| 699 assignment == _AssignmentKind.SIMPLE_RIGHT || |
| 700 assignment == _AssignmentKind.NONE) { |
| 701 if (identifier.inSetterContext()) { |
| 702 fail("Expected ${_topMostNode(identifier).toSource()} to be false"); |
| 703 } |
| 704 } else { |
| 705 if (!identifier.inSetterContext()) { |
| 706 fail("Expected ${_topMostNode(identifier).toSource()} to be true"); |
| 707 } |
| 708 } |
| 709 } |
| 710 } |
| 711 } |
| 712 |
| 713 void test_inSetterContext_forEachLoop() { |
| 714 SimpleIdentifier identifier = AstFactory.identifier3("a"); |
| 715 Expression iterator = AstFactory.listLiteral(); |
| 716 Statement body = AstFactory.block(); |
| 717 AstFactory.forEachStatement2(identifier, iterator, body); |
| 718 expect(identifier.inSetterContext(), isTrue); |
| 719 } |
| 720 |
| 721 void test_isQualified_inMethodInvocation_noTarget() { |
| 722 MethodInvocation invocation = |
| 723 AstFactory.methodInvocation2("test", [AstFactory.identifier3("arg0")]); |
| 724 SimpleIdentifier identifier = invocation.methodName; |
| 725 expect(identifier.isQualified, isFalse); |
| 726 } |
| 727 |
| 728 void test_isQualified_inMethodInvocation_withTarget() { |
| 729 MethodInvocation invocation = AstFactory.methodInvocation( |
| 730 AstFactory.identifier3("target"), |
| 731 "test", |
| 732 [AstFactory.identifier3("arg0")]); |
| 733 SimpleIdentifier identifier = invocation.methodName; |
| 734 expect(identifier.isQualified, isTrue); |
| 735 } |
| 736 |
| 737 void test_isQualified_inPrefixedIdentifier_name() { |
| 738 SimpleIdentifier identifier = AstFactory.identifier3("test"); |
| 739 AstFactory.identifier4("prefix", identifier); |
| 740 expect(identifier.isQualified, isTrue); |
| 741 } |
| 742 |
| 743 void test_isQualified_inPrefixedIdentifier_prefix() { |
| 744 SimpleIdentifier identifier = AstFactory.identifier3("test"); |
| 745 AstFactory.identifier(identifier, AstFactory.identifier3("name")); |
| 746 expect(identifier.isQualified, isFalse); |
| 747 } |
| 748 |
| 749 void test_isQualified_inPropertyAccess_name() { |
| 750 SimpleIdentifier identifier = AstFactory.identifier3("test"); |
| 751 AstFactory.propertyAccess(AstFactory.identifier3("target"), identifier); |
| 752 expect(identifier.isQualified, isTrue); |
| 753 } |
| 754 |
| 755 void test_isQualified_inPropertyAccess_target() { |
| 756 SimpleIdentifier identifier = AstFactory.identifier3("test"); |
| 757 AstFactory.propertyAccess(identifier, AstFactory.identifier3("name")); |
| 758 expect(identifier.isQualified, isFalse); |
| 759 } |
| 760 |
| 761 void test_isQualified_inReturnStatement() { |
| 762 SimpleIdentifier identifier = AstFactory.identifier3("test"); |
| 763 AstFactory.returnStatement2(identifier); |
| 764 expect(identifier.isQualified, isFalse); |
| 765 } |
| 766 |
| 767 SimpleIdentifier _createIdentifier( |
| 768 _WrapperKind wrapper, _AssignmentKind assignment) { |
| 769 SimpleIdentifier identifier = AstFactory.identifier3("a"); |
| 770 Expression expression = identifier; |
| 771 while (true) { |
| 772 if (wrapper == _WrapperKind.PREFIXED_LEFT) { |
| 773 expression = |
| 774 AstFactory.identifier(identifier, AstFactory.identifier3("_")); |
| 775 } else if (wrapper == _WrapperKind.PREFIXED_RIGHT) { |
| 776 expression = |
| 777 AstFactory.identifier(AstFactory.identifier3("_"), identifier); |
| 778 } else if (wrapper == _WrapperKind.PROPERTY_LEFT) { |
| 779 expression = AstFactory.propertyAccess2(expression, "_"); |
| 780 } else if (wrapper == _WrapperKind.PROPERTY_RIGHT) { |
| 781 expression = |
| 782 AstFactory.propertyAccess(AstFactory.identifier3("_"), identifier); |
| 783 } else if (wrapper == _WrapperKind.NONE) {} |
| 784 break; |
| 785 } |
| 786 while (true) { |
| 787 if (assignment == _AssignmentKind.BINARY) { |
| 788 AstFactory.binaryExpression( |
| 789 expression, TokenType.PLUS, AstFactory.identifier3("_")); |
| 790 } else if (assignment == _AssignmentKind.COMPOUND_LEFT) { |
| 791 AstFactory.assignmentExpression( |
| 792 expression, TokenType.PLUS_EQ, AstFactory.identifier3("_")); |
| 793 } else if (assignment == _AssignmentKind.COMPOUND_RIGHT) { |
| 794 AstFactory.assignmentExpression( |
| 795 AstFactory.identifier3("_"), TokenType.PLUS_EQ, expression); |
| 796 } else if (assignment == _AssignmentKind.POSTFIX_INC) { |
| 797 AstFactory.postfixExpression(expression, TokenType.PLUS_PLUS); |
| 798 } else if (assignment == _AssignmentKind.PREFIX_DEC) { |
| 799 AstFactory.prefixExpression(TokenType.MINUS_MINUS, expression); |
| 800 } else if (assignment == _AssignmentKind.PREFIX_INC) { |
| 801 AstFactory.prefixExpression(TokenType.PLUS_PLUS, expression); |
| 802 } else if (assignment == _AssignmentKind.PREFIX_NOT) { |
| 803 AstFactory.prefixExpression(TokenType.BANG, expression); |
| 804 } else if (assignment == _AssignmentKind.SIMPLE_LEFT) { |
| 805 AstFactory.assignmentExpression( |
| 806 expression, TokenType.EQ, AstFactory.identifier3("_")); |
| 807 } else if (assignment == _AssignmentKind.SIMPLE_RIGHT) { |
| 808 AstFactory.assignmentExpression( |
| 809 AstFactory.identifier3("_"), TokenType.EQ, expression); |
| 810 } else if (assignment == _AssignmentKind.NONE) {} |
| 811 break; |
| 812 } |
| 813 return identifier; |
| 814 } |
| 815 |
| 816 /** |
| 817 * Return the top-most node in the AST structure containing the given identifi
er. |
| 818 * |
| 819 * @param identifier the identifier in the AST structure being traversed |
| 820 * @return the root of the AST structure containing the identifier |
| 821 */ |
| 822 AstNode _topMostNode(SimpleIdentifier identifier) { |
| 823 AstNode child = identifier; |
| 824 AstNode parent = identifier.parent; |
| 825 while (parent != null) { |
| 826 child = parent; |
| 827 parent = parent.parent; |
| 828 } |
| 829 return child; |
| 830 } |
| 831 } |
| 832 |
| 833 @reflectiveTest |
| 834 class SimpleStringLiteralTest extends ParserTestCase { |
| 835 void test_contentsEnd() { |
| 836 expect( |
| 837 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X") |
| 838 .contentsEnd, |
| 839 2); |
| 840 expect( |
| 841 new SimpleStringLiteral(TokenFactory.tokenFromString('"X"'), "X") |
| 842 .contentsEnd, |
| 843 2); |
| 844 |
| 845 expect( |
| 846 new SimpleStringLiteral(TokenFactory.tokenFromString('"""X"""'), "X") |
| 847 .contentsEnd, |
| 848 4); |
| 849 expect( |
| 850 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") |
| 851 .contentsEnd, |
| 852 4); |
| 853 expect( |
| 854 new SimpleStringLiteral( |
| 855 TokenFactory.tokenFromString("''' \nX'''"), "X") |
| 856 .contentsEnd, |
| 857 7); |
| 858 |
| 859 expect( |
| 860 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") |
| 861 .contentsEnd, |
| 862 3); |
| 863 expect( |
| 864 new SimpleStringLiteral(TokenFactory.tokenFromString('r"X"'), "X") |
| 865 .contentsEnd, |
| 866 3); |
| 867 |
| 868 expect( |
| 869 new SimpleStringLiteral(TokenFactory.tokenFromString('r"""X"""'), "X") |
| 870 .contentsEnd, |
| 871 5); |
| 872 expect( |
| 873 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") |
| 874 .contentsEnd, |
| 875 5); |
| 876 expect( |
| 877 new SimpleStringLiteral( |
| 878 TokenFactory.tokenFromString("r''' \nX'''"), "X") |
| 879 .contentsEnd, |
| 880 8); |
| 881 } |
| 882 |
| 883 void test_contentsOffset() { |
| 884 expect( |
| 885 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X") |
| 886 .contentsOffset, |
| 887 1); |
| 888 expect( |
| 889 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X") |
| 890 .contentsOffset, |
| 891 1); |
| 892 expect( |
| 893 new SimpleStringLiteral( |
| 894 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X") |
| 895 .contentsOffset, |
| 896 3); |
| 897 expect( |
| 898 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") |
| 899 .contentsOffset, |
| 900 3); |
| 901 expect( |
| 902 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") |
| 903 .contentsOffset, |
| 904 2); |
| 905 expect( |
| 906 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X") |
| 907 .contentsOffset, |
| 908 2); |
| 909 expect( |
| 910 new SimpleStringLiteral( |
| 911 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X") |
| 912 .contentsOffset, |
| 913 4); |
| 914 expect( |
| 915 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") |
| 916 .contentsOffset, |
| 917 4); |
| 918 // leading whitespace |
| 919 expect( |
| 920 new SimpleStringLiteral( |
| 921 TokenFactory.tokenFromString("''' \ \nX''"), "X") |
| 922 .contentsOffset, |
| 923 6); |
| 924 expect( |
| 925 new SimpleStringLiteral( |
| 926 TokenFactory.tokenFromString('r""" \ \nX"""'), "X") |
| 927 .contentsOffset, |
| 928 7); |
| 929 } |
| 930 |
| 931 void test_isMultiline() { |
| 932 expect( |
| 933 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X") |
| 934 .isMultiline, |
| 935 isFalse); |
| 936 expect( |
| 937 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") |
| 938 .isMultiline, |
| 939 isFalse); |
| 940 expect( |
| 941 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X") |
| 942 .isMultiline, |
| 943 isFalse); |
| 944 expect( |
| 945 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X") |
| 946 .isMultiline, |
| 947 isFalse); |
| 948 expect( |
| 949 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") |
| 950 .isMultiline, |
| 951 isTrue); |
| 952 expect( |
| 953 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") |
| 954 .isMultiline, |
| 955 isTrue); |
| 956 expect( |
| 957 new SimpleStringLiteral( |
| 958 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X") |
| 959 .isMultiline, |
| 960 isTrue); |
| 961 expect( |
| 962 new SimpleStringLiteral( |
| 963 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X") |
| 964 .isMultiline, |
| 965 isTrue); |
| 966 } |
| 967 |
| 968 void test_isRaw() { |
| 969 expect( |
| 970 new SimpleStringLiteral(TokenFactory.tokenFromString("'X'"), "X").isRaw, |
| 971 isFalse); |
| 972 expect( |
| 973 new SimpleStringLiteral(TokenFactory.tokenFromString("\"X\""), "X") |
| 974 .isRaw, |
| 975 isFalse); |
| 976 expect( |
| 977 new SimpleStringLiteral( |
| 978 TokenFactory.tokenFromString("\"\"\"X\"\"\""), "X") |
| 979 .isRaw, |
| 980 isFalse); |
| 981 expect( |
| 982 new SimpleStringLiteral(TokenFactory.tokenFromString("'''X'''"), "X") |
| 983 .isRaw, |
| 984 isFalse); |
| 985 expect( |
| 986 new SimpleStringLiteral(TokenFactory.tokenFromString("r'X'"), "X") |
| 987 .isRaw, |
| 988 isTrue); |
| 989 expect( |
| 990 new SimpleStringLiteral(TokenFactory.tokenFromString("r\"X\""), "X") |
| 991 .isRaw, |
| 992 isTrue); |
| 993 expect( |
| 994 new SimpleStringLiteral( |
| 995 TokenFactory.tokenFromString("r\"\"\"X\"\"\""), "X") |
| 996 .isRaw, |
| 997 isTrue); |
| 998 expect( |
| 999 new SimpleStringLiteral(TokenFactory.tokenFromString("r'''X'''"), "X") |
| 1000 .isRaw, |
| 1001 isTrue); |
| 1002 } |
| 1003 |
| 1004 void test_isSingleQuoted() { |
| 1005 // ' |
| 1006 { |
| 1007 var token = TokenFactory.tokenFromString("'X'"); |
| 1008 var node = new SimpleStringLiteral(token, null); |
| 1009 expect(node.isSingleQuoted, isTrue); |
| 1010 } |
| 1011 // ''' |
| 1012 { |
| 1013 var token = TokenFactory.tokenFromString("'''X'''"); |
| 1014 var node = new SimpleStringLiteral(token, null); |
| 1015 expect(node.isSingleQuoted, isTrue); |
| 1016 } |
| 1017 // " |
| 1018 { |
| 1019 var token = TokenFactory.tokenFromString('"X"'); |
| 1020 var node = new SimpleStringLiteral(token, null); |
| 1021 expect(node.isSingleQuoted, isFalse); |
| 1022 } |
| 1023 // """ |
| 1024 { |
| 1025 var token = TokenFactory.tokenFromString('"""X"""'); |
| 1026 var node = new SimpleStringLiteral(token, null); |
| 1027 expect(node.isSingleQuoted, isFalse); |
| 1028 } |
| 1029 } |
| 1030 |
| 1031 void test_isSingleQuoted_raw() { |
| 1032 // r' |
| 1033 { |
| 1034 var token = TokenFactory.tokenFromString("r'X'"); |
| 1035 var node = new SimpleStringLiteral(token, null); |
| 1036 expect(node.isSingleQuoted, isTrue); |
| 1037 } |
| 1038 // r''' |
| 1039 { |
| 1040 var token = TokenFactory.tokenFromString("r'''X'''"); |
| 1041 var node = new SimpleStringLiteral(token, null); |
| 1042 expect(node.isSingleQuoted, isTrue); |
| 1043 } |
| 1044 // r" |
| 1045 { |
| 1046 var token = TokenFactory.tokenFromString('r"X"'); |
| 1047 var node = new SimpleStringLiteral(token, null); |
| 1048 expect(node.isSingleQuoted, isFalse); |
| 1049 } |
| 1050 // r""" |
| 1051 { |
| 1052 var token = TokenFactory.tokenFromString('r"""X"""'); |
| 1053 var node = new SimpleStringLiteral(token, null); |
| 1054 expect(node.isSingleQuoted, isFalse); |
| 1055 } |
| 1056 } |
| 1057 |
| 1058 void test_simple() { |
| 1059 Token token = TokenFactory.tokenFromString("'value'"); |
| 1060 SimpleStringLiteral stringLiteral = new SimpleStringLiteral(token, "value"); |
| 1061 expect(stringLiteral.literal, same(token)); |
| 1062 expect(stringLiteral.beginToken, same(token)); |
| 1063 expect(stringLiteral.endToken, same(token)); |
| 1064 expect(stringLiteral.value, "value"); |
| 1065 } |
| 1066 } |
| 1067 |
| 1068 @reflectiveTest |
| 1069 class StringInterpolationTest extends ParserTestCase { |
| 1070 void test_contentsOffsetEnd() { |
| 1071 AstFactory.interpolationExpression(AstFactory.identifier3('bb')); |
| 1072 // 'a${bb}ccc' |
| 1073 { |
| 1074 var ae = AstFactory.interpolationString("'a", "a"); |
| 1075 var cToken = new StringToken(TokenType.STRING, "ccc'", 10); |
| 1076 var cElement = new InterpolationString(cToken, 'ccc'); |
| 1077 StringInterpolation node = AstFactory.string([ae, ae, cElement]); |
| 1078 expect(node.contentsOffset, 1); |
| 1079 expect(node.contentsEnd, 10 + 4 - 1); |
| 1080 } |
| 1081 // '''a${bb}ccc''' |
| 1082 { |
| 1083 var ae = AstFactory.interpolationString("'''a", "a"); |
| 1084 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10); |
| 1085 var cElement = new InterpolationString(cToken, 'ccc'); |
| 1086 StringInterpolation node = AstFactory.string([ae, ae, cElement]); |
| 1087 expect(node.contentsOffset, 3); |
| 1088 expect(node.contentsEnd, 10 + 4 - 1); |
| 1089 } |
| 1090 // """a${bb}ccc""" |
| 1091 { |
| 1092 var ae = AstFactory.interpolationString('"""a', "a"); |
| 1093 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10); |
| 1094 var cElement = new InterpolationString(cToken, 'ccc'); |
| 1095 StringInterpolation node = AstFactory.string([ae, ae, cElement]); |
| 1096 expect(node.contentsOffset, 3); |
| 1097 expect(node.contentsEnd, 10 + 4 - 1); |
| 1098 } |
| 1099 // r'a${bb}ccc' |
| 1100 { |
| 1101 var ae = AstFactory.interpolationString("r'a", "a"); |
| 1102 var cToken = new StringToken(TokenType.STRING, "ccc'", 10); |
| 1103 var cElement = new InterpolationString(cToken, 'ccc'); |
| 1104 StringInterpolation node = AstFactory.string([ae, ae, cElement]); |
| 1105 expect(node.contentsOffset, 2); |
| 1106 expect(node.contentsEnd, 10 + 4 - 1); |
| 1107 } |
| 1108 // r'''a${bb}ccc''' |
| 1109 { |
| 1110 var ae = AstFactory.interpolationString("r'''a", "a"); |
| 1111 var cToken = new StringToken(TokenType.STRING, "ccc'''", 10); |
| 1112 var cElement = new InterpolationString(cToken, 'ccc'); |
| 1113 StringInterpolation node = AstFactory.string([ae, ae, cElement]); |
| 1114 expect(node.contentsOffset, 4); |
| 1115 expect(node.contentsEnd, 10 + 4 - 1); |
| 1116 } |
| 1117 // r"""a${bb}ccc""" |
| 1118 { |
| 1119 var ae = AstFactory.interpolationString('r"""a', "a"); |
| 1120 var cToken = new StringToken(TokenType.STRING, 'ccc"""', 10); |
| 1121 var cElement = new InterpolationString(cToken, 'ccc'); |
| 1122 StringInterpolation node = AstFactory.string([ae, ae, cElement]); |
| 1123 expect(node.contentsOffset, 4); |
| 1124 expect(node.contentsEnd, 10 + 4 - 1); |
| 1125 } |
| 1126 } |
| 1127 |
| 1128 void test_isMultiline() { |
| 1129 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb')); |
| 1130 // ' |
| 1131 { |
| 1132 var a = AstFactory.interpolationString("'a", "a"); |
| 1133 var c = AstFactory.interpolationString("ccc'", "ccc"); |
| 1134 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1135 expect(node.isMultiline, isFalse); |
| 1136 } |
| 1137 // ''' |
| 1138 { |
| 1139 var a = AstFactory.interpolationString("'''a", "a"); |
| 1140 var c = AstFactory.interpolationString("ccc'''", "ccc"); |
| 1141 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1142 expect(node.isMultiline, isTrue); |
| 1143 } |
| 1144 // " |
| 1145 { |
| 1146 var a = AstFactory.interpolationString('"a', "a"); |
| 1147 var c = AstFactory.interpolationString('ccc"', "ccc"); |
| 1148 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1149 expect(node.isMultiline, isFalse); |
| 1150 } |
| 1151 // """ |
| 1152 { |
| 1153 var a = AstFactory.interpolationString('"""a', "a"); |
| 1154 var c = AstFactory.interpolationString('ccc"""', "ccc"); |
| 1155 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1156 expect(node.isMultiline, isTrue); |
| 1157 } |
| 1158 } |
| 1159 |
| 1160 void test_isRaw() { |
| 1161 StringInterpolation node = AstFactory.string(); |
| 1162 expect(node.isRaw, isFalse); |
| 1163 } |
| 1164 |
| 1165 void test_isSingleQuoted() { |
| 1166 var b = AstFactory.interpolationExpression(AstFactory.identifier3('bb')); |
| 1167 // " |
| 1168 { |
| 1169 var a = AstFactory.interpolationString('"a', "a"); |
| 1170 var c = AstFactory.interpolationString('ccc"', "ccc"); |
| 1171 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1172 expect(node.isSingleQuoted, isFalse); |
| 1173 } |
| 1174 // """ |
| 1175 { |
| 1176 var a = AstFactory.interpolationString('"""a', "a"); |
| 1177 var c = AstFactory.interpolationString('ccc"""', "ccc"); |
| 1178 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1179 expect(node.isSingleQuoted, isFalse); |
| 1180 } |
| 1181 // ' |
| 1182 { |
| 1183 var a = AstFactory.interpolationString("'a", "a"); |
| 1184 var c = AstFactory.interpolationString("ccc'", "ccc"); |
| 1185 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1186 expect(node.isSingleQuoted, isTrue); |
| 1187 } |
| 1188 // ''' |
| 1189 { |
| 1190 var a = AstFactory.interpolationString("'''a", "a"); |
| 1191 var c = AstFactory.interpolationString("ccc'''", "ccc"); |
| 1192 StringInterpolation node = AstFactory.string([a, b, c]); |
| 1193 expect(node.isSingleQuoted, isTrue); |
| 1194 } |
| 1195 } |
| 1196 } |
| 1197 |
| 1198 @reflectiveTest |
| 1199 class VariableDeclarationTest extends ParserTestCase { |
| 1200 void test_getDocumentationComment_onGrandParent() { |
| 1201 VariableDeclaration varDecl = AstFactory.variableDeclaration("a"); |
| 1202 TopLevelVariableDeclaration decl = |
| 1203 AstFactory.topLevelVariableDeclaration2(Keyword.VAR, [varDecl]); |
| 1204 Comment comment = Comment.createDocumentationComment(new List<Token>(0)); |
| 1205 expect(varDecl.documentationComment, isNull); |
| 1206 decl.documentationComment = comment; |
| 1207 expect(varDecl.documentationComment, isNotNull); |
| 1208 expect(decl.documentationComment, isNotNull); |
| 1209 } |
| 1210 |
| 1211 void test_getDocumentationComment_onNode() { |
| 1212 VariableDeclaration decl = AstFactory.variableDeclaration("a"); |
| 1213 Comment comment = Comment.createDocumentationComment(new List<Token>(0)); |
| 1214 decl.documentationComment = comment; |
| 1215 expect(decl.documentationComment, isNotNull); |
| 1216 } |
| 1217 } |
| 1218 |
| 1219 class _AssignmentKind { |
| 1220 static const _AssignmentKind BINARY = const _AssignmentKind('BINARY', 0); |
| 1221 |
| 1222 static const _AssignmentKind COMPOUND_LEFT = |
| 1223 const _AssignmentKind('COMPOUND_LEFT', 1); |
| 1224 |
| 1225 static const _AssignmentKind COMPOUND_RIGHT = |
| 1226 const _AssignmentKind('COMPOUND_RIGHT', 2); |
| 1227 |
| 1228 static const _AssignmentKind POSTFIX_INC = |
| 1229 const _AssignmentKind('POSTFIX_INC', 3); |
| 1230 |
| 1231 static const _AssignmentKind PREFIX_DEC = |
| 1232 const _AssignmentKind('PREFIX_DEC', 4); |
| 1233 |
| 1234 static const _AssignmentKind PREFIX_INC = |
| 1235 const _AssignmentKind('PREFIX_INC', 5); |
| 1236 |
| 1237 static const _AssignmentKind PREFIX_NOT = |
| 1238 const _AssignmentKind('PREFIX_NOT', 6); |
| 1239 |
| 1240 static const _AssignmentKind SIMPLE_LEFT = |
| 1241 const _AssignmentKind('SIMPLE_LEFT', 7); |
| 1242 |
| 1243 static const _AssignmentKind SIMPLE_RIGHT = |
| 1244 const _AssignmentKind('SIMPLE_RIGHT', 8); |
| 1245 |
| 1246 static const _AssignmentKind NONE = const _AssignmentKind('NONE', 9); |
| 1247 |
| 1248 static const List<_AssignmentKind> values = const [ |
| 1249 BINARY, |
| 1250 COMPOUND_LEFT, |
| 1251 COMPOUND_RIGHT, |
| 1252 POSTFIX_INC, |
| 1253 PREFIX_DEC, |
| 1254 PREFIX_INC, |
| 1255 PREFIX_NOT, |
| 1256 SIMPLE_LEFT, |
| 1257 SIMPLE_RIGHT, |
| 1258 NONE |
| 1259 ]; |
| 1260 |
| 1261 final String name; |
| 1262 |
| 1263 final int ordinal; |
| 1264 |
| 1265 const _AssignmentKind(this.name, this.ordinal); |
| 1266 |
| 1267 int get hashCode => ordinal; |
| 1268 |
| 1269 int compareTo(_AssignmentKind other) => ordinal - other.ordinal; |
| 1270 |
| 1271 String toString() => name; |
| 1272 } |
| 1273 |
| 1274 class _WrapperKind { |
| 1275 static const _WrapperKind PREFIXED_LEFT = |
| 1276 const _WrapperKind('PREFIXED_LEFT', 0); |
| 1277 |
| 1278 static const _WrapperKind PREFIXED_RIGHT = |
| 1279 const _WrapperKind('PREFIXED_RIGHT', 1); |
| 1280 |
| 1281 static const _WrapperKind PROPERTY_LEFT = |
| 1282 const _WrapperKind('PROPERTY_LEFT', 2); |
| 1283 |
| 1284 static const _WrapperKind PROPERTY_RIGHT = |
| 1285 const _WrapperKind('PROPERTY_RIGHT', 3); |
| 1286 |
| 1287 static const _WrapperKind NONE = const _WrapperKind('NONE', 4); |
| 1288 |
| 1289 static const List<_WrapperKind> values = const [ |
| 1290 PREFIXED_LEFT, |
| 1291 PREFIXED_RIGHT, |
| 1292 PROPERTY_LEFT, |
| 1293 PROPERTY_RIGHT, |
| 1294 NONE |
| 1295 ]; |
| 1296 |
| 1297 final String name; |
| 1298 |
| 1299 final int ordinal; |
| 1300 |
| 1301 const _WrapperKind(this.name, this.ordinal); |
| 1302 |
| 1303 int get hashCode => ordinal; |
| 1304 |
| 1305 int compareTo(_WrapperKind other) => ordinal - other.ordinal; |
| 1306 |
| 1307 String toString() => name; |
| 1308 } |
| OLD | NEW |