1 33 34 package edu.rice.cs.drjava.model.definitions.reducedmodel; 35 36 39 public final class ReducedModelTest extends BraceReductionTestCase implements ReducedModelStates { 40 41 public void testInsertGap() { 42 insertGap(model1, 4); 43 model1.move(-4); 44 assertTrue("#0.0", model1.currentToken().isGap()); 46 assertEquals("#0.2", 4, model1.currentToken().getSize()); 47 model1.move(4); 48 insertGap(model2, 5); 50 model2.move(-5); 51 assertTrue("#1.0", model2.currentToken().isGap()); 53 assertEquals("#1.2", 5, model2.currentToken().getSize()); 54 } 55 56 57 public void testInsertGapBeforeGap() { 58 insertGap(model1, 3); 59 assertTrue("#0.0.0", model1.atEnd()); 60 model1.move(-3); 61 insertGap(model1, 3); 62 assertTrue("#0.0", model1.currentToken().isGap()); 64 assertEquals("#0.1", 3, model1.absOffset()); 65 assertEquals("#0.2", 6, model1.currentToken().getSize()); 66 model1.move(-3); 67 insertGap(model1, 2); 68 assertTrue("#1.0", model1.currentToken().isGap()); 69 assertEquals("#1.1", 2, model1.absOffset()); 70 assertEquals("#1.2", 8, model1.currentToken().getSize()); 71 } 72 73 74 public void testInsertGapAfterGap() { 75 insertGap(model1, 3); 76 assertTrue("#0.0", model1.atEnd()); 77 model1.move(-3); 78 assertTrue("#0.1", model1.currentToken().isGap()); 79 assertEquals("#0.2", 3, model1.currentToken().getSize()); 80 insertGap(model1, 4); 81 assertTrue("#1.1", model1.currentToken().isGap()); 82 assertEquals("#1.2", 7, model1.currentToken().getSize()); 83 } 84 85 86 public void testInsertGapInsideGap() { 87 insertGap(model1, 3); 88 assertTrue("#0.0", model1.atEnd()); 89 model1.move(-3); 90 assertTrue("#0.1", model1.currentToken().isGap()); 91 assertEquals("#0.2", 3, model1.currentToken().getSize()); 92 insertGap(model1, 3); 93 assertTrue("#1.1", model1.currentToken().isGap()); 94 assertEquals("#1.2", 6, model1.currentToken().getSize()); 95 assertEquals("#1.3", 3, model1.absOffset()); 96 insertGap(model1, 4); 97 assertTrue("#1.1", model1.currentToken().isGap()); 98 assertEquals("#1.2", 10, model1.currentToken().getSize()); 99 assertEquals("#1.3", 7, model1._offset); 100 } 101 102 103 public void testInsertBraceAtStartAndEnd() { 104 model1.insertChar('('); 105 assertTrue("#0.0", model1.atEnd()); 106 model1.move(-1); 107 assertEquals("#0.1", "(", model1.currentToken().getType()); 108 assertEquals("#0.2", 1, model1.currentToken().getSize()); 109 model2.insertChar(')'); 110 assertTrue("#1.0", model2.atEnd()); 111 model2.move(-1); 112 assertEquals("#1.1", ")", model2.currentToken().getType()); 113 assertEquals("#1.2", 1, model2.currentToken().getSize()); 114 } 115 116 public void testInsertBraceInsideGap() { 118 insertGap(model1, 4); 119 model1.move(-4); 120 insertGap(model1, 3); 121 assertEquals("#0.0", 3, model1.absOffset()); 122 assertEquals("#0.1", 7, model1.currentToken().getSize()); 123 model1.insertChar('{'); 124 assertEquals("#1.0", 4, model1.absOffset()); 125 assertEquals("#1.1", 4, model1.currentToken().getSize()); 126 assertTrue("#1.2", model1.currentToken().isGap()); 127 model1.move(-1); 128 assertEquals("#2.0", 1, model1.currentToken().getSize()); 129 assertEquals("#2.1", "{", model1.currentToken().getType()); 130 model1.move(-3); 131 assertEquals("#3.0", 0, model1.absOffset()); 132 assertEquals("#3.1", 3, model1.currentToken().getSize()); 133 assertTrue("#3.2", model1.currentToken().isGap()); 134 } 135 136 139 public void testInsertBrace() { 140 model1.insertChar('{'); 141 assertTrue("#0.0", model1.atEnd()); 142 model1.move(-1); 143 assertEquals("#1.0", 1, model1.currentToken().getSize()); 144 assertEquals("#1.1", "{", model1.currentToken().getType()); 145 model1.insertChar('('); 146 model1.insertChar('['); 147 assertEquals("#2.0", 1, model1.currentToken().getSize()); 148 assertEquals("#2.1", "{", model1.currentToken().getType()); 149 model1.move(-1); 150 assertEquals("#3.0", 1, model1.currentToken().getSize()); 151 assertEquals("#3.1", "[", model1.currentToken().getType()); 152 model1.move(-1); 153 assertEquals("#3.0", 1, model1.currentToken().getSize()); 154 assertEquals("#3.1", "(", model1.currentToken().getType()); 155 } 156 157 160 public void testInsertBraceAndBreakLineComment() { 161 model1.insertChar('/'); 162 model1.insertChar('/'); 163 model1.move(-1); 164 assertEquals("#0.0", 2, model1.currentToken().getSize()); 165 model1.insertChar('{'); 167 assertEquals("#1.0", "/", model1.currentToken().getType()); 168 assertEquals("#1.1", 1, model1.currentToken().getSize()); 169 model1.move(-1); 170 assertEquals("#2.0", "{", model1.currentToken().getType()); 171 assertEquals("#2.1", 1, model1.currentToken().getSize()); 172 model1.move(-1); 173 assertEquals("#3.0", "/", model1.currentToken().getType()); 174 assertEquals("#3.1", 1, model1.currentToken().getSize()); 175 } 176 177 180 public void testInsertBraceAndBreakBlockCommentStart() { 181 model1.insertChar('/'); 182 model1.insertChar('*'); 183 model1.move(-2); 184 assertEquals("#0.0", 2, model1.currentToken().getSize()); 185 model1.move(1); 186 model1.insertChar('{'); 187 assertEquals("#1.0", "*", model1.currentToken().getType()); 188 assertEquals("#1.1", 1, model1.currentToken().getSize()); 189 model1.move(-1); 190 assertEquals("#2.0", "{", model1.currentToken().getType()); 191 assertEquals("#2.1", 1, model1.currentToken().getSize()); 192 model1.move(-1); 193 assertEquals("#3.0", "/", model1.currentToken().getType()); 194 assertEquals("#3.1", 1, model1.currentToken().getSize()); 195 } 196 197 public void testInsertMultipleBraces() { 199 model1.insertChar('/'); 200 model1.insertChar('*'); 201 model1.insertChar('{'); 202 model1.move(-1); 203 assertEquals("#0.0", INSIDE_BLOCK_COMMENT, model1.getStateAtCurrent()); 205 model1.move(-2); 206 assertEquals("#0.1", FREE, model1.currentToken().getState()); 207 model1.move(3); 208 model1.insertChar('*'); 209 model1.insertChar('/'); 210 model1.move(-2); 212 assertEquals("#1.0", FREE, model1.currentToken().getState()); 213 model1.move(1); 214 model1.insertChar('{'); 215 model1.move(0); 216 model1.move(-1); 218 assertEquals("#2.0", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 219 assertEquals("#2.1", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 220 model1.move(1); 221 assertEquals("#2.2", "/", model1.currentToken().getType()); 222 } 223 224 231 public void testComplexBraceInsertion() { 232 model1.insertChar('\n'); 233 model1.insertChar('\n'); 234 model1.move(-1); 235 assertEquals("#0.0", false, model1.atEnd()); 237 model1.insertChar('{'); 238 model1.insertChar('\n'); 239 model1.insertChar('\n'); 240 model1.insertChar('}'); 241 model1.move(-2); 242 assertEquals("#0.1", FREE, model1.currentToken().getState()); 244 model1.insertChar('{'); 245 model1.insertChar('{'); 246 model1.insertChar('}'); 247 model1.insertChar('}'); 248 assertEquals("#1.0", 4, model1.balanceBackward()); 250 model1.move(-1); 251 assertEquals("#1.1", 2, model1.balanceBackward()); 252 } 253 254 257 public void testCrazyCase1() { 258 model1.insertChar('/'); 259 insertGap(model1, 4); 260 model1.insertChar('*'); 261 model1.insertChar('/'); 262 model1.move(-1); 264 assertEquals("#0.0", "/", model1.currentToken().getType()); 265 model1.move(-1); 266 assertEquals("#0.1", "*", model1.currentToken().getType()); 267 model1.move(1); 269 model1.insertChar('/'); 270 assertEquals("#1.0", 2, model1.currentToken().getSize()); 272 model1.move(-2); 273 assertEquals("#1.0", "*", model1.currentToken().getType()); 275 model1.move(-4); 276 model1.insertChar('*'); 277 model1.move(-2); 279 assertEquals("#2.0", "/*", model1.currentToken().getType()); 280 assertEquals("#2.1", FREE, model1.currentToken().getState()); 281 model1.move(6); 282 assertEquals("#2.2", "*/", model1.currentToken().getType()); 284 assertEquals("#2.3", FREE, model1.currentToken().getState()); 285 assertEquals("#2.4", INSIDE_BLOCK_COMMENT, model1.getStateAtCurrent()); 286 } 288 289 290 public void testCrazyCase2() { 291 model1.insertChar('/'); 292 insertGap(model1, 4); 293 model1.move(-2); 294 model1.insertChar('/'); 295 model1.move(0); 296 model1.move(-3); 297 assertEquals("#0.0", 2, model1.currentToken().getSize()); 299 assertEquals("#0.3", FREE, model1.getStateAtCurrent()); 300 model1.move(2); 301 assertEquals("#0.2", 1, model1.currentToken().getSize()); 302 assertEquals("#0.1", "/", model1.currentToken().getType()); 303 model1.move(-2); 304 model1.insertChar('/'); 305 model1.move(-2); 306 assertEquals("#1.1", "//", model1.currentToken().getType()); 307 assertEquals("#1.3", FREE, model1.currentToken().getState()); 308 model1.move(2); 309 assertEquals("#1.2", INSIDE_LINE_COMMENT, model1.getStateAtCurrent()); 311 assertEquals("1.4", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 312 model1.move(2); 313 assertEquals("1.5", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 314 model1.move(-2); 315 } 316 317 320 public void testLineCommentBreakCrazy() { 321 model1.insertChar('/'); 322 model1.insertChar('/'); 323 insertGap(model1, 4); 324 model1.move(-2); 325 model1.insertChar('/'); 326 model1.move(-4); 329 model1.insertChar('/'); 330 model1.move(0); 331 model1.move(-2); 333 assertEquals("#2.0", "//", model1.currentToken().getType()); 334 assertEquals("#2.3", FREE, model1.currentToken().getState()); 335 model1.move(2); 336 assertEquals("#2.1", "/", model1.currentToken().getType()); 337 assertEquals("#2.2", INSIDE_LINE_COMMENT, model1.getStateAtCurrent()); 338 assertEquals("2.4", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 339 model1.move(1); 340 assertEquals("2.5", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 341 model1.move(-2); 343 model1.insertChar('*'); model1.move(0); 345 model1.move(-2); 347 assertEquals("#3.0", "/*", model1.currentToken().getType()); 348 assertEquals("#3.3", FREE, model1.currentToken().getState()); 349 model1.move(2); 350 assertEquals("#3.1", "/", model1.currentToken().getType()); 351 assertEquals("#3.3", INSIDE_BLOCK_COMMENT, model1.getStateAtCurrent()); 352 assertEquals("3.4", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 353 model1.move(1); 354 assertEquals("#3.2", "/", model1.currentToken().getType()); 355 assertEquals("3.5", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 356 } 357 358 361 public void testBreakBlockCommentWithStar() { 362 model1.insertChar('/'); 364 model1.insertChar('*'); 365 model1.insertChar('/'); 366 model1.insertChar('/'); 367 insertGap(model1, 2); 368 model1.insertChar('/'); 369 insertGap(model1, 2); 370 model1.move(-8); 372 model1.insertChar('*'); 373 model1.move(-2); 375 assertEquals("#4.0", "/*", model1.currentToken().getType()); 376 assertEquals("#4.3", FREE, model1.currentToken().getState()); 377 model1.move(2); 378 assertEquals("#4.1", "*/", model1.currentToken().getType()); 379 assertEquals("#4.3", INSIDE_BLOCK_COMMENT, model1.getStateAtCurrent()); 380 assertEquals("4.4", FREE, model1.currentToken().getState()); 381 model1.move(2); 382 assertEquals("#4.2", "/", model1.currentToken().getType()); 383 assertEquals("4.5", FREE, model1.currentToken().getState()); 384 } 385 386 389 public void testBreakCloseBlockCommentWithStar() { 390 model1.insertChar('/'); 391 model1.insertChar('*'); 392 model1.insertChar('*'); 393 model1.insertChar('/'); 394 model1.insertChar('/'); 395 insertGap(model1, 2); 396 model1.insertChar('/'); 397 insertGap(model1, 2); 398 model1.move(-7); 399 insertGap(model1, 3); 400 model1.move(-3); 402 assertEquals("#5.0", true, model1.currentToken().isGap()); 403 assertEquals("#5.4", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 404 model1.move(3); 405 assertEquals("#5.1", INSIDE_BLOCK_COMMENT, model1.getStateAtCurrent()); 406 assertEquals("#5.2", "/", model1.currentToken().getType()); 407 assertEquals("5.5", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 408 model1.move(1); 409 assertEquals("#5.3", "/", model1.currentToken().getType()); 410 assertEquals("5.6", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 411 } 412 413 416 public void testBasicBlockComment() { 417 model1.insertChar('/'); 418 model1.insertChar('*'); 419 model1.insertChar('*'); 420 model1.insertChar('/'); 421 model1.move(-4); 422 assertEquals("0.1", FREE, model1.currentToken().getState()); 423 assertEquals("0.2", "/*", model1.currentToken().getType()); 424 model1.move(2); 425 assertEquals("0.3", FREE, model1.currentToken().getState()); 426 assertEquals("0.4", "*/", model1.currentToken().getType()); 427 model1.insertChar('/'); 428 model1.move(-1); 429 assertEquals("1.1", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 430 assertEquals("1.3", "/", model1.currentToken().getType()); 431 model1.move(1); 432 assertEquals("1.0", FREE, model1.currentToken().getState()); 433 assertEquals("1.2", "*/", model1.currentToken().getType()); 434 } 435 436 439 public void testInsertBlockInsideBlockComment() { 440 model1.insertChar('/'); 441 model1.insertChar('*'); 442 model1.insertChar('/'); 443 model1.insertChar('*'); 444 model1.insertChar('/'); 445 model1.move(-2); 447 model1.insertChar('*'); 448 model1.move(-1); 450 assertEquals("1.1", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 451 assertEquals("1.3", "*", model1.currentToken().getType()); 452 model1.move(1); 453 assertEquals("1.0", FREE, model1.currentToken().getState()); 454 assertEquals("1.2", "*/", model1.currentToken().getType()); 455 } 456 457 460 public void testInsertBlockCommentEnd() { model1.insertChar('*'); 462 model1.insertChar('/'); 463 model1.move(-1); 464 assertEquals("#3.0", "/", model1.currentToken().getType()); 465 assertEquals("#3.1", 1, model1.currentToken().getSize()); 466 } 467 468 471 public void testGetStateAtCurrent() { 472 assertEquals("#0.0", FREE, model1.getStateAtCurrent()); 473 assertEquals("#0.1", FREE, model1.getStateAtCurrent()); 474 model1.insertChar('('); 475 model1.move(-1); 476 assertEquals("#1.0", FREE, model1.currentToken().getState()); 477 model1.move(1); 478 model1.insertChar('/'); 479 model1.insertChar('/'); 480 model1.move(-2); 481 assertEquals("#2.0", FREE, model1.currentToken().getState()); 482 model1.move(2); 483 assertEquals("#2.1", INSIDE_LINE_COMMENT, model1.getStateAtCurrent()); 484 model1.move(-3); 486 model1.insertChar('/'); 487 model1.insertChar('/'); 488 model1.move(-2); 490 assertEquals("#3.0", FREE, model1.currentToken().getState()); 491 model1.move(2); 492 assertEquals("#3.1", INSIDE_LINE_COMMENT, model1.getStateAtCurrent()); 493 assertEquals("#3.2", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 494 model1.move(1); 495 assertEquals("#3.3", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 496 assertEquals("#3.4", "/", model1.currentToken().getType()); 497 model1.move(1); 498 assertEquals("#4.1", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 499 assertEquals("#4.2", "/", model1.currentToken().getType()); 500 } 501 502 505 public void testQuotesSimple() { 506 model1.insertChar('\"'); 507 model1.insertChar('\"'); 508 model1.move(-2); 509 assertEquals("#0.0", "\"", model1.currentToken().getType()); 510 assertEquals("#0.3", FREE, model1.currentToken().getState()); 511 model1.move(1); 512 assertEquals("#0.1", "\"", model1.currentToken().getType()); 513 assertEquals("#0.2", FREE, model1.currentToken().getState()); 514 assertEquals("#0.4", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 515 } 516 517 520 public void testQuotesWithGap() { 521 model1.insertChar('\"'); 522 model1.insertChar('\"'); 523 model1.move(-2); 524 assertEquals("#0.1", "\"", model1.currentToken().getType()); 525 assertEquals("#0.3", FREE, model1.currentToken().getState()); 526 model1.move(1); 527 assertEquals("#0.0", "\"", model1.currentToken().getType()); 528 assertEquals("#0.2", FREE, model1.currentToken().getState()); 529 assertEquals("#0.4", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 530 insertGap(model1, 4); 531 model1.move(-4); 533 assertEquals("#1.1", true, model1.currentToken().isGap()); 534 assertEquals("#1.3", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 535 model1.move(4); 536 assertEquals("#1.0", "\"", model1.currentToken().getType()); 537 assertEquals("#1.2", FREE, model1.currentToken().getState()); 538 assertEquals("#1.4", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 539 model1.move(-2); 540 model1.insertChar('/'); 541 model1.move(-1); 543 assertEquals("#2.1", "/", model1.currentToken().getType()); 544 model1.move(1); 545 assertEquals("#2.0", true, model1.currentToken().isGap()); 546 assertEquals("#2.4", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 547 assertEquals("#2.6", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 548 model1.move(2); 549 assertEquals("#2.2", "\"", model1.currentToken().getType()); 550 assertEquals("#2.3", FREE, model1.currentToken().getState()); 551 } 552 553 556 public void testInsertQuoteToQuoteBlock() { 557 model1.insertChar('\"'); 558 insertGap(model1, 2); 559 model1.insertChar('/'); 560 insertGap(model1, 2); 561 model1.insertChar('\"'); 562 model1.move(-3); 563 model1.insertChar('\"'); 564 model1.move(-1); 566 assertEquals("#3.1", "\"", model1.currentToken().getType()); 567 assertEquals("#3.5", FREE, model1.currentToken().getState()); 568 model1.move(1); 569 assertEquals("#3.0", true, model1.currentToken().isGap()); 570 assertEquals("#3.4", FREE, model1.currentToken().getState()); 571 assertEquals("#3.6", FREE, model1.getStateAtCurrent()); 572 model1.move(2); 573 assertEquals("#3.2", "\"", model1.currentToken().getType()); 574 assertEquals("#3.3", FREE, model1.currentToken().getState()); 575 model1.move(-6); 577 assertEquals("#4.1", true, model1.currentToken().isGap()); 578 assertEquals("#4.5", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 579 model1.move(2); 580 assertEquals("#4.0", "/", model1.currentToken().getType()); 581 assertEquals("#4.4", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 582 assertEquals("#4.6", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 583 model1.move(1); 584 assertEquals("#4.2", "\"", model1.currentToken().getType()); 585 assertEquals("#4.3", FREE, model1.currentToken().getState()); 586 model1.move(-1); 587 model1.insertChar('\n'); 590 model1.move(-1); 592 assertEquals("#5.5", FREE, model1.currentToken().getState()); 593 model1.move(1); 594 assertEquals("#5.4", FREE, model1.currentToken().getState()); 595 assertEquals("#5.6", FREE, model1.getStateAtCurrent()); 596 model1.move(1); 597 assertEquals("#5.3", FREE, model1.currentToken().getState()); 598 model1.move(1); 599 assertEquals("#5.7", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 600 assertEquals("#5.8", true, model1.currentToken().isGap()); 601 } 602 603 606 public void testQuoteBreaksComment() { 607 model1.insertChar('/'); 608 model1.insertChar('*'); 609 model1.insertChar('*'); 610 model1.insertChar('/'); 611 model1.move(-2); 612 model1.insertChar('\"'); 613 model1.move(-1); 614 model1.move(-2); 616 assertEquals("#1.1", FREE, model1.currentToken().getState()); 617 model1.move(2); 618 assertEquals("#1.1", INSIDE_BLOCK_COMMENT, model1.getStateAtCurrent()); 619 assertEquals("#1.2", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 620 model1.move(1); 621 assertEquals("#1.2", FREE, model1.currentToken().getState()); 622 model1.move(-3); 623 model1.insertChar('\"'); 625 model1.move(-1); 626 assertEquals("#2.2", FREE, model1.currentToken().getState()); 627 model1.move(1); 628 assertEquals("#2.0", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 629 assertEquals("#2.1", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 630 assertEquals("#2.3", "/", model1.currentToken().getType()); 631 model1.move(1); 632 assertEquals("#2.4", "*", model1.currentToken().getType()); 633 model1.move(2); 635 assertEquals("#5.0", FREE, model1.getStateAtCurrent()); 637 assertEquals("#5.1", FREE, model1.currentToken().getState()); 638 assertEquals("#5.3", "*", model1.currentToken().getType()); 639 model1.move(1); 640 assertEquals("#5.4", "/", model1.currentToken().getType()); 641 assertEquals("#5.5", FREE, model1.currentToken().getState()); 642 } 643 644 647 public void testQuoteBreakComment2() { 648 model1.insertChar('/'); 649 model1.insertChar('*'); 650 model1.insertChar('*'); 651 model1.insertChar('/'); 652 model1.move(-4); 653 assertEquals("#0.0", "/*", model1.currentToken().getType()); 654 model1.move(2); 655 assertEquals("#0.1", "*/", model1.currentToken().getType()); 656 model1.move(-2); 657 model1.insertChar('\"'); 659 model1.move(-1); 660 assertEquals("#1.0", FREE, model1.currentToken().getState()); 661 assertEquals("#1.4", "\"", model1.currentToken().getType()); 662 model1.move(1); 663 assertEquals("#1.1", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 664 assertEquals("#1.4", "/", model1.currentToken().getType()); 665 assertEquals("#1.2", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 666 model1.move(1); 667 assertEquals("#1.3", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 668 assertEquals("#1.4", "*", model1.currentToken().getType()); 669 } 670 671 674 public void testInsertNewlineEndLineComment() { 675 model1.insertChar('/'); 676 model1.insertChar('/'); 677 insertGap(model1, 5); 678 model1.move(-2); 679 model1.insertChar('\n'); 680 model1.move(-1); 682 assertEquals("#0.2", "\n", model1.currentToken().getType()); 683 assertEquals("#0.4", FREE, model1.currentToken().getState()); 684 model1.move(1); 685 assertEquals("#0.0", FREE, model1.getStateAtCurrent()); 686 assertTrue("#0.1", model1.currentToken().isGap()); 687 assertEquals("#0.3", 2, model1.currentToken().getSize()); 688 assertEquals("#0.5", FREE, model1.currentToken().getState()); 689 } 690 691 694 public void testInsertNewlineEndQuote() { 695 model1.insertChar('\"'); 696 insertGap(model1, 5); 697 model1.move(-2); 698 model1.insertChar('\n'); 699 model1.move(-1); 701 assertEquals("#0.4", FREE, model1.currentToken().getState()); 702 assertEquals("#0.2", "\n", model1.currentToken().getType()); 703 model1.move(1); 704 assertEquals("#0.0", FREE, model1.getStateAtCurrent()); 705 assertTrue("#0.1", model1.currentToken().isGap()); 706 assertEquals("#0.3", 2, model1.currentToken().getSize()); 707 assertEquals("#0.5", FREE, model1.currentToken().getState()); 708 } 709 710 713 public void testInsertNewlineChainReaction() { 714 model1.insertChar('/'); 715 model1.insertChar('/'); 716 model1.insertChar('/'); 717 model1.insertChar('*'); 718 model1.move(-1); 720 model1.move(-1); 722 assertEquals("#0.2", "/", model1.currentToken().getType()); 723 assertEquals("#0.3", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 724 model1.move(1); 725 assertEquals("#0.0", INSIDE_LINE_COMMENT, model1.getStateAtCurrent()); 726 assertEquals("#0.1", "*", model1.currentToken().getType()); 727 assertEquals("#0.4", INSIDE_LINE_COMMENT, model1.currentToken().getState()); 728 model1.move(1); 729 model1.insertChar('\n'); 730 model1.insertChar('\"'); 731 model1.insertChar('*'); 732 model1.insertChar('/'); 733 model1.move(-1); 734 assertEquals("#1.0", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 737 assertEquals("#1.1", "/", model1.currentToken().getType()); 738 assertEquals("#1.4", INSIDE_DOUBLE_QUOTE, model1.currentToken().getState()); 739 model1.move(-5); 740 assertEquals("#2.1", "/", model1.currentToken().getType()); 741 model1.insertChar('\n'); 742 assertEquals("#3.0", FREE, model1.getStateAtCurrent()); 746 assertEquals("#3.4", FREE, model1.currentToken().getState()); 747 model1.move(1); 748 assertEquals("#3.1", "/*", model1.currentToken().getType()); 749 model1.move(2); 753 assertEquals("#4.0", INSIDE_BLOCK_COMMENT, model1.getStateAtCurrent()); 754 assertEquals("#4.1", "\"", model1.currentToken().getType()); 755 assertEquals("#4.4", INSIDE_BLOCK_COMMENT, model1.currentToken().getState()); 756 model1.move(1); 757 assertEquals("#4.6", "*/", model1.currentToken().getType()); 758 } 759 760 763 public void testMoveWithinToken() { 764 insertGap(model1, 10); 765 assertTrue("#0.0", model1.atEnd()); 766 assertEquals("#0.1", 10, model1.absOffset()); 767 model1.move(-5); 768 assertTrue("#1.0", model1.currentToken().isGap()); 769 assertEquals("#1.1", 5, model1.absOffset()); 770 model1.move(2); 771 assertTrue("#2.0", model1.currentToken().isGap()); 772 assertEquals("#2.1", 7, model1.absOffset()); 773 model1.move(-4); 774 assertTrue("#3.0", model1.currentToken().isGap()); 775 assertEquals("#3.1", 3, model1.absOffset()); 776 model1.move(-3); 777 assertTrue("#4.0", model1.currentToken().isGap()); 778 assertEquals("#4.1", 0, model1.absOffset()); 779 model1.move(10); 780 assertTrue("#5.0", model1.atEnd()); 781 assertEquals("#5.1", 10, model1.absOffset()); 782 } 783 784 787 public void testMoveOnEmpty() { 788 model1.move(0); 789 assertTrue("#0.0", model1.atStart()); 790 try { 791 model1.move(-1); 792 assertTrue("#0.1", false); 793 } catch (Exception e) { } 794 try { 795 model1.move(1); 796 assertTrue("#0.2", false); 797 } catch (Exception e) { } 798 } 799 800 803 public void testMove0StaysPut() { 804 model0.insertChar('/'); 805 assertEquals("#1", 1, model0.absOffset()); 806 model0.move(0); 807 assertEquals("#2", 1, model0.absOffset()); 808 model0.insertChar('/'); 809 assertEquals("#3", 2, model0.absOffset()); 810 model0.move(0); 811 assertEquals("#4", 2, model0.absOffset()); 812 model0.move(-1); 813 assertEquals("#5", 1, model0.absOffset()); 814 model0.move(0); 815 assertEquals("#6", 1, model0.absOffset()); 816 } 817 818 819 public void testInsideComment() { 820 assertEquals("#0.0", FREE, model0.getStateAtCurrent()); 821 model0.insertChar('/'); 822 model0.insertChar('*'); 823 assertEquals("#0.1", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 824 model1.insertChar('/'); 825 model1.insertChar('/'); 826 assertEquals("#0.2", INSIDE_LINE_COMMENT, model1.getStateAtCurrent()); 827 model1.insertChar('('); 828 assertEquals("#0.3", INSIDE_LINE_COMMENT, model1.getStateAtCurrent()); 829 model1.insertChar('\n'); 830 assertEquals("#0.4", FREE, model1.getStateAtCurrent()); 831 model0.insertChar('*'); 832 model0.insertChar('/'); 833 assertEquals("#0.4", FREE, model0.getStateAtCurrent()); 834 } 835 836 837 public void testInsideString() { 838 assertEquals("#0.0", FREE, model0.getStateAtCurrent()); 839 model0.insertChar('\"'); 840 assertEquals("#0.1", INSIDE_DOUBLE_QUOTE, model0.getStateAtCurrent()); 841 model1.insertChar('\"'); 842 assertEquals("#0.2", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 843 model1.insertChar('('); 844 assertEquals("#0.3", INSIDE_DOUBLE_QUOTE, model1.getStateAtCurrent()); 845 model1.insertChar('\"'); 846 assertEquals("#0.4", FREE, model1.getStateAtCurrent()); 847 } 848 849 850 public void testInsertBraces() { 851 assertEquals("#0.0", 0, model0.absOffset()); 852 model0.insertChar('/'); 853 assertEquals("#1.0", FREE, model0.getStateAtCurrent()); 855 model0.insertChar('*'); 856 assertEquals("#2.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 858 assertEquals("#2.1", 2, model0.absOffset()); 859 model0.move(-1); 860 assertEquals("#3.0", 1, model0.absOffset()); 862 model0.insertChar('('); 863 assertEquals("#4.0", FREE, model0.getStateAtCurrent()); 865 model0.move(-1); 866 model0.delete(1); 868 model0.move(1); 870 assertEquals("#5.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 872 model0.insertChar('*'); 873 assertEquals("#6.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 875 model0.insertChar('/'); 876 assertEquals("#7.0", 4, model0.absOffset()); 878 assertEquals("#7.1", FREE, model0.getStateAtCurrent()); 879 model0.move(-2); 880 assertEquals("#8.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 882 assertEquals("#8.1", 2, model0.absOffset()); 883 model0.insertChar('('); 884 assertEquals("#9.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 885 model0.move(1); 887 assertEquals("#10.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 889 model0.move(-2); 890 assertEquals("#11.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 892 model0.move(1); 893 assertEquals("#12.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 896 assertEquals("#12.1", 3, model0.absOffset()); 897 insertGap(model0, 4); 898 model0.move(-2); 900 assertEquals("#13.0", 5, model0.absOffset()); 902 model0.insertChar(')'); 903 assertEquals("#14.0", 6, model0.absOffset()); 905 model0.move(-1); 907 assertEquals("#12.0", INSIDE_BLOCK_COMMENT, model0.getStateAtCurrent()); 909 } 910 911 912 public void testInsertGap2() { 913 model0.insertChar('/'); 914 model0.insertChar('*'); 915 insertGap(model0, 5); 916 assertEquals("#0.0", 7, model0.absOffset()); 917 model0.insertChar('('); 918 model0.move(-1); 919 insertGap(model0, 3); 920 assertEquals("#1.0", 10, model0.absOffset()); 921 } 922 923 924 public void testMove() { 925 model0.insertChar('('); 926 insertGap(model0, 5); 927 model0.insertChar(')'); 928 model0.insertChar('\n'); 929 insertGap(model0, 2); 930 model0.insertChar('{'); 931 model0.insertChar('}'); 932 try { 933 model0.move(-30); 934 assertTrue("#0.0", false); 935 } catch (Exception e) { } 936 try { 937 model0.move(1); 938 assertTrue("#0.1", false); 939 } catch (Exception e) { } 940 assertEquals("#0.2", 12, model0.absOffset()); 941 model0.move(-2); 942 assertEquals("#0.3", 10, model0.absOffset()); 943 model0.move(-8); 944 assertEquals("#0.4", 2, model0.absOffset()); 945 model0.move(3); 946 assertEquals("#0.5", 5, model0.absOffset()); 947 model0.move(4); 948 assertEquals("#0.6", 9, model0.absOffset()); 949 } 950 951 952 protected ReducedModelControl setUpExample() { 953 ReducedModelControl model = new ReducedModelControl(); 954 model.insertChar('{'); 955 model.insertChar('\n'); 956 insertGap(model, 3); 957 model.insertChar('\n'); 958 model.insertChar('('); 959 insertGap(model, 2); 960 model.insertChar(')'); 961 model.insertChar('\n'); 962 insertGap(model, 3); 963 model.insertChar('/'); 964 model.insertChar('/'); 965 insertGap(model, 3); 966 model.insertChar('\n'); 967 model.insertChar('\"'); 968 insertGap(model, 1); 969 model.insertChar('{'); 970 insertGap(model, 1); 971 model.insertChar('\"'); 972 model.insertChar('/'); 973 model.insertChar('*'); 974 insertGap(model, 1); 975 model.insertChar('('); 976 insertGap(model, 1); 977 model.insertChar(')'); 978 insertGap(model, 1); 979 model.insertChar('*'); 980 model.insertChar('/'); 981 model.insertChar('\n'); 982 model.insertChar('}'); 983 return model; 990 } 991 992 993 public void testBalanceForward() { 994 assertEquals("#0.0", -1, model0.balanceForward()); 995 model0 = setUpExample(); 996 assertEquals("#1.0", -1, model0.balanceForward()); 997 model0.move(-1); 998 assertEquals("#2.0", -1, model0.balanceForward()); 1000 model0.move(-34); 1001 assertEquals("#3.0", 35, model0.balanceForward()); 1003 model0.move(1); 1004 assertEquals("#4.0", -1, model0.balanceForward()); 1006 model0.move(5); 1007 assertEquals("#5.0", 3, model0.balanceForward()); 1009 model0.move(27); 1010 assertEquals("#6.0", -1, model0.balanceForward()); 1012 model0.move(-20); 1013 assertEquals("#7.0", -1, model0.balanceForward()); 1015 model1.insertChar('('); 1016 model1.move(-1); 1017 assertEquals("#8.0", -1, model1.balanceForward()); 1019 model1.move(1); 1020 model1.insertChar('}'); 1021 model1.move(-1); 1022 assertEquals("#9.0", -1, model1.balanceForward()); 1024 } 1025 1026 1027 public void testBalanceBackward() { 1028 assertEquals("#0.0", -1, model0.balanceBackward()); 1029 model0 = setUpExample(); 1030 assertEquals("#1.0", 36, model0.balanceBackward()); 1037 model0.move(-2); 1038 assertEquals("#2.0", -1, model0.balanceBackward()); 1039 model0.move(-14); 1040 assertEquals("#3.0", -1, model0.balanceBackward()); 1041 model0.move(-10); 1042 assertEquals("#4.0", 4, model0.balanceBackward()); 1043 model0.move(-10); 1044 assertEquals("#5.0", -1, model0.balanceBackward()); 1045 model1.insertChar(')'); 1046 assertEquals("#6.0", -1, model1.balanceBackward()); 1047 model1.move(-1); 1048 model1.insertChar('{'); 1049 model1.move(1); 1050 assertEquals("#7.0", -1, model1.balanceBackward()); 1051 } 1052} 1053 1054 1055 1056; 1057 1058 | Popular Tags |