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