1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
|
#ifndef QT_NO_DEBUG
#include "qeditor_t.h"
#include "qeditor.h"
#include "qdocumentline.h"
#include "testutil.h"
#include "smallUsefulFunctions.h"
#include "qdocument_p.h"
#include <QtTest/QtTest>
Q_DECLARE_METATYPE(QList<int>);
QEditorTest::QEditorTest(QEditor* ed, bool executeAllTests):allTests(executeAllTests)
{
editor=ed;
if (editor->getFileCodec()!=0) defaultCodec = editor->getFileCodec();
else defaultCodec = QTextCodec::codecForName("latin1");
ed->setFlag(QEditor::HardLineWrap, false);
ed->setFlag(QEditor::AutoCloseChars, true);
}
QEditorTest::~QEditorTest(){
}
void QEditorTest::setText_data(){
//reason is to test if the last new line ending is preserved
QTest::addColumn<QString>("text");
QTest::newRow("empty") << "";
QTest::newRow("single word") << "hallo";
QTest::newRow("single line") << "hallo welt\n";
QTest::newRow("two lines") << "hallo welt\njipjipiu";
QTest::newRow("three lines") << "hallo welt\njipjipiu\n";
QTest::newRow("many lines a") << "hallo welt\njipjipiu\n\n\n\n";
QTest::newRow("many lines b") << "hallo welt\njipjipiu\n\n\n\nb";
}
void QEditorTest::setText(){
Q_ASSERT(editor);
QFETCH(QString, text);
editor->setText(text, false);
QString restext = editor->text();
QEQUAL(restext,text);
}
void QEditorTest::loadSave_data(){
QTest::addColumn<QString>("outCodecName");
QTest::addColumn<QString>("outLineEnding");
QTest::addColumn<bool>("autodetect");
QStringList codings = QStringList() << "latin1" << "utf-8" << "utf-16le" << "utf-16be" << "latin2"; //latin1 is not used
QStringList endings = QStringList() << "\n" << "\r" << "\r\n";
for (int i=0;i<codings.size();i++)
for (int j=0;j<endings.size();j++){
if (i!=codings.size()-1) //auto detect all except last one
QTest::newRow(qPrintable(QString("load with encoding %1 endings %2").arg(codings[i]).arg(j)))
<< codings[i]
<< endings[j]
<< true;
QTest::newRow(qPrintable(QString("load with encoding %1 endings %2").arg(codings[i]).arg(j)))
<< codings[i]
<< endings[j]
<< false;
}
Q_ASSERT(QTextCodec::codecForName("UTF-8")==QTextCodec::codecForMib(MIB_UTF8));
Q_ASSERT(QTextCodec::codecForName("UTF-16LE")==QTextCodec::codecForMib(MIB_UTF16LE));
Q_ASSERT(QTextCodec::codecForName("UTF-16BE")==QTextCodec::codecForMib(MIB_UTF16BE));
Q_ASSERT(QTextCodec::codecForName("ISO-8859-1")==QTextCodec::codecForMib(MIB_LATIN1));
Q_ASSERT(QTextCodec::codecForMib(MIB_UTF8)->mibEnum() == MIB_UTF8);
Q_ASSERT(QTextCodec::codecForMib(MIB_UTF16LE)->mibEnum() == MIB_UTF16LE);
Q_ASSERT(QTextCodec::codecForMib(MIB_UTF16BE)->mibEnum() == MIB_UTF16BE);
Q_ASSERT(QTextCodec::codecForMib(MIB_LATIN1)->mibEnum() == MIB_LATIN1);
}
//checks if load/saving preserves encoding and line endings
void QEditorTest::loadSave(){
QFETCH(QString, outCodecName);
QFETCH(QString, outLineEnding);
QFETCH(bool, autodetect);
QTextCodec* outCodec=QTextCodec::codecForName(qPrintable(outCodecName));
if (outCodecName=="latin1") outCodec = defaultCodec;
if (!allTests) {
qDebug("skipped load save test");
return;
}
const QString testText = QString::fromLatin1("hallo\n\xE4\xF6\xFC\n");
QString testTextWithLineEndings=testText;
testTextWithLineEndings.replace("\n",outLineEnding);
QTemporaryFile tf;//uncomment if you need to look at the files &tf=*(new QTemporaryFile);
tf.open();
QString tfn=tf.fileName();
tf.write(outCodec->fromUnicode(testTextWithLineEndings));
tf.close();
//Load
editor->setFileCodec(QTextCodec::codecForName("iso-8859-5"));
editor->load(tfn,autodetect?0:outCodec);
editor->document()->setLineEnding(editor->document()->originalLineEnding()); //TODO: find out why this line is only needed iff the editor passed by the testmanager is used and not if a new QEditor(0) is created
QEQUAL2(editor->document()->text(),testTextWithLineEndings,QString("File: %1 Got file codec: %2 ").arg(tfn).arg(editor->getFileCodec()?QString::fromLatin1(editor->getFileCodec()->name()):"<null>"));
QVERIFY2(editor->getFileCodec()==outCodec,qPrintable(QString("wrong encoding: got %1 wanted %2 by the sheriff %3").arg(QString::fromLatin1(editor->getFileCodec()->name())).arg(QString::fromLatin1(outCodec->name())).arg(autodetect)));
QEQUAL(editor->document()->lineEndingString(),outLineEnding);
//Save
editor->setText(editor->document()->text()+"Save test", false);
editor->save();
tf.open();
QString writtenText=outCodec->toUnicode( tf.readAll());
tf.close();
QEQUAL2(writtenText, testTextWithLineEndings+"Save test", "file text check, file:"+tfn);
QVERIFY2(writtenText.contains(outLineEnding), qPrintable("file don't contain right line ending, file"+tfn));
editor->setFileName(""); //reset filename so it won't get panically if the file is deleted
editor->document()->setLineEnding(QDocument::Conservative); //reset line ending so we won't screw up the other tests
}
void compareLists(const QList<int> actual, const QList<int> exp){
if (actual.length() != exp.length()) {
QFAIL(qPrintable(QString("length %1 != %2 ").arg(actual.length()).arg(exp.length())));
return;
}
for (int i=0;i<actual.length();i++)
QEQUAL(actual[i],exp[i]);
}
void QEditorTest::foldedText_data(){
QTest::addColumn<QString>("editorText");
QTest::addColumn<QList<int> >("foldAt");
QTest::addColumn<QList<int> >("hiddenLines");
QTest::addColumn<int>("froml");
QTest::addColumn<int>("fromc");
QTest::addColumn<int>("tol");
QTest::addColumn<int>("toc");
QTest::addColumn<QString>("operation");
QTest::addColumn<QString>("newEditorText");
QTest::addColumn<QList<int> >("newHiddenLines");
QTest::newRow("simple folded comment")
<< "0\n1{\n2abc\n3}\n4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 0 << 1 << 4 << 1
<< "comment"
<< "%0\n%1{\n%2abc\n%3}\n%4\n"
<< (QList<int>());
QTest::newRow("simple folded uncomment")
<< "0\n1{\n%2abc\n3}\n4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 0 << 1 << 4 << 1
<< "uncomment"
<< "0\n1{\n2abc\n3}\n4\n"
<< (QList<int>() << 2 << 3);
QTest::newRow("simple folded indent")
<< "0\n1{\n2abc\n3}\n4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 0 << 1 << 4 << 1
<< "indent"
<< "\t0\n\t1{\n\t2abc\n\t3}\n\t4\n"
<< (QList<int>() << 2 << 3);
QTest::newRow("simple folded unindent")
<< "\t0\n\t1{\n\t2abc\n\t3}\n\t4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 0 << 1 << 4 << 1
<< "unindent"
<< "0\n1{\n2abc\n3}\n4\n"
<< (QList<int>() << 2 << 3);
QTest::newRow("inner folded uncomment")
<< "{\n1{\n%2abc\n3}\n4\n5}\n6\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 0 << 1 << 5 << 1
<< "uncomment"
<< "{\n1{\n2abc\n3}\n4\n5}\n6\n"
<< (QList<int>() << 2 << 3);
QTest::newRow("fold block shorting uncomment")
<< "0\n1{\n%2}\n3}\n4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 0 << 1 << 4 << 1
<< "uncomment"
<< "0\n1{\n2}\n3}\n4\n"
<< (QList<int>() << 2);
QTest::newRow("fold block larging uncomment")
<< "0\n1{\n%2{\n3\n4}\n5\n6}\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4)
<< 0 << 1 << 6 << 1
<< "uncomment"
<< "0\n1{\n2{\n3\n4}\n5\n6}\n"
<< (QList<int>() << 2 << 3 << 4 << 5 << 6);
QTest::newRow("uncomment single line")
<< "0\n1{\n%2}\n3}\n4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 0 << 1 << 0 << 1
<< "uncomment"
<< "0\n1{\n%2}\n3}\n4\n"
<< (QList<int>() << 2 << 3);
QTest::newRow("last section folded commenting")
<< "\\section{1}\n1\n\\section{2}\n2\n\\section{3}\n3\n\\section{4}\n4\n"
<< (QList<int>() << 0 << 2 << 4 << 6)
<< (QList<int>() << 1 << 3 << 5 << 7 << 8)
<< 6 << 0 << 7 << 1
<< "comment"
<< "\\section{1}\n1\n\\section{2}\n2\n\\section{3}\n3\n%\\section{4}\n%4\n"
<< (QList<int>() << 1 << 3 << 5 << 6 << 7 << 8);
QTest::newRow("last section folded indent")
<< "\\section{1}\n1\n\\section{2}\n2\n\\section{3}\n3\n\\section{4}\n4\n"
<< (QList<int>() << 0 << 2 << 4 << 6)
<< (QList<int>() << 1 << 3 << 5 << 7 << 8)
<< 6 << 0 << 7 << 1
<< "indent"
<< "\\section{1}\n1\n\\section{2}\n2\n\\section{3}\n3\n\t\\section{4}\n\t4\n"
<< (QList<int>() << 1 << 3 << 5 << 7 << 8);
QTest::newRow("last section folded unindent")
<< "\\section{1}\n1\n\\section{2}\n2\n\\section{3}\n3\n\t\\section{4}\n\t4\n"
<< (QList<int>() << 0 << 2 << 4 << 6)
<< (QList<int>() << 1 << 3 << 5 << 7 << 8)
<< 6 << 0 << 7 << 1
<< "unindent"
<< "\\section{1}\n1\n\\section{2}\n2\n\\section{3}\n3\n\\section{4}\n4\n"
<< (QList<int>() << 1 << 3 << 5 << 7 << 8);
}
//checks if block-commands (e.g indent, comment) work with folded text
void QEditorTest::foldedText(){
QFETCH(QString, editorText);
QFETCH(QList<int>, foldAt);
QFETCH(QList<int>, hiddenLines);
QFETCH(int, froml);
QFETCH(int, fromc);
QFETCH(int, tol);
QFETCH(int, toc);
QFETCH(QString, operation);
QFETCH(QString, newEditorText);
QFETCH(QList<int>, newHiddenLines);
editor->setText(editorText, false);
foreach(const int &i, foldAt)
editor->document()->collapse(i);
for (int i=0;i<editor->document()->lines();i++)
QVERIFY2(editor->document()->line(i).isHidden() == hiddenLines.contains(i),qPrintable(QString::number(i)));
compareLists(editor->document()->impl()->testGetHiddenLines(), hiddenLines);
editor->setCursor(editor->document()->cursor(froml,fromc,tol,toc));
if (operation=="indent") editor->indentSelection();
else if (operation=="unindent") editor->unindentSelection();
else if (operation=="comment") editor->commentSelection();
else if (operation=="uncomment") editor->uncommentSelection();
else if (operation=="togglecomment") editor->toggleCommentSelection();
else qFatal("invalid operation");
QEQUAL(editor->document()->text(), newEditorText);
for (int i=0;i<editor->document()->lines();i++)
QVERIFY2(editor->document()->line(i).isHidden() == newHiddenLines.contains(i),qPrintable(QString::number(i)));
compareLists(editor->document()->impl()->testGetHiddenLines(), newHiddenLines);
}
void QEditorTest::passiveFolding_data(){
QTest::addColumn<QString>("editorText");
QTest::addColumn<QList<int> >("foldAt");
QTest::addColumn<QList<int> >("hiddenLines1");
QTest::addColumn<QList<int> >("unFoldAt");
QTest::addColumn<QList<int> >("hiddenLines2");
QTest::addColumn<QList<int> >("foldAtAgain");
QTest::addColumn<QList<int> >("hiddenLines3");
QTest::newRow("trivial")
<< "0\n1{\n2\n3\n4}\n5"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4)
<< (QList<int>() << 1)
<< (QList<int>())
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4);
QTest::newRow("trivial mixed brackets")
<< "0\n1${[](\n2\n3\n4)\\{\\}}$\n5"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4)
<< (QList<int>() << 1)
<< (QList<int>())
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4);
QTest::newRow("trivial open/close block")
<< "0\n1{\n2\n3\n4} {\n5\n6}\n"
<< (QList<int>() << 1 << 4)
<< (QList<int>() << 2 << 3 << 5 << 6)
<< (QList<int>() << 4)
<< (QList<int>() << 2 << 3)
<< (QList<int>() << 4)
<< (QList<int>() << 2 << 3 << 5 << 6);
QTest::newRow("open/close block with weight override")
<< "0\n1{\n2\n3\n4(} {)\n5\n6}\n"
<< (QList<int>() << 1 << 4)
<< (QList<int>() << 2 << 3 << 5 << 6)
<< (QList<int>() << 4)
<< (QList<int>() << 2 << 3)
<< (QList<int>() << 4)
<< (QList<int>() << 2 << 3 << 5 << 6);
QTest::newRow("trivial unclosed block")
<< "0\n1{\n2\n3\n4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4 << 5)
<< (QList<int>() << 1)
<< (QList<int>())
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4 << 5);
QTest::newRow("\\chapter overrides \\section")
<< "0\n1\\chapter\n2\n3\\section\n4\n5\\section\n6\n7\\chapter\n8\n9\n10\n"
<< (QList<int>() << 1 << 3)
<< (QList<int>() << 2 << 3 << 4 << 5 << 6)
<< (QList<int>() << 1)
<< (QList<int>() << 4)
<< (QList<int>() << 7)
<< (QList<int>() << 4 << 8 << 9 << 10 << 11);
QTest::newRow("\\section folds until \\chapter")
<< "0\n1\\chapter\n2\n3\\section\n4\n5\\section\n6\n7\\chapter\n8\n9\n10\n"
<< (QList<int>() << 5)
<< (QList<int>() << 6)
<< (QList<int>())
<< (QList<int>() << 6)
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3 << 4 << 5 << 6);
}
//checks if collapsing/expanding works
void QEditorTest::passiveFolding(){
QFETCH(QString, editorText);
QFETCH(QList<int>, foldAt);
QFETCH(QList<int>, hiddenLines1);
QFETCH(QList<int>, unFoldAt);
QFETCH(QList<int>, hiddenLines2);
QFETCH(QList<int>, foldAtAgain);
QFETCH(QList<int>, hiddenLines3);
editor->setText(editorText, false);
foreach(const int &i, foldAt)
editor->document()->collapse(i);
for (int i=0;i<editor->document()->lines();i++)
QVERIFY2(editor->document()->line(i).isHidden() == hiddenLines1.contains(i),qPrintable(QString::number(i)));
compareLists(editor->document()->impl()->testGetHiddenLines(), hiddenLines1);
foreach(const int &i, unFoldAt)
editor->document()->expand(i);
for (int i=0;i<editor->document()->lines();i++)
QVERIFY2(editor->document()->line(i).isHidden() == hiddenLines2.contains(i),qPrintable(QString::number(i)));
compareLists(editor->document()->impl()->testGetHiddenLines(), hiddenLines2);
foreach(const int &i, foldAtAgain)
editor->document()->collapse(i);
for (int i=0;i<editor->document()->lines();i++)
QVERIFY2(editor->document()->line(i).isHidden() == hiddenLines3.contains(i),qPrintable(QString::number(i)));
compareLists(editor->document()->impl()->testGetHiddenLines(), hiddenLines3);
}
void QEditorTest::activeFolding_data(){
QTest::addColumn<QString>("editorText");
QTest::addColumn<QList<int> >("foldAt");
QTest::addColumn<QList<int> >("hiddenLines");
QTest::addColumn<int>("cursorAL");
QTest::addColumn<int>("cursorAC");
QTest::addColumn<int>("cursorL");
QTest::addColumn<int>("cursorC");
QTest::addColumn<QString>("textToInsert");
QTest::addColumn<QString>("newEditorText");
QTest::addColumn<QList<int> >("newHiddenLines");
QTest::newRow("trivial insertion: shift down")
<< "0\n1\n2{\n3\n4}\n5\n"
<< (QList<int> () << 2)
<< (QList<int> () << 3 << 4)
<< 1 << 1 << 1 << 1
<< "insert\nnew line"
<< "0\n1insert\nnew line\n2{\n3\n4}\n5\n"
<< (QList<int> () << 4 << 5);
QTest::newRow("trivial insertion: shift up")
<< "0\n1\n2{\n3\n4}\n5\n"
<< (QList<int> () << 2)
<< (QList<int> () << 3 << 4)
<< 0 << 0 << 1 << 1
<< "remove line"
<< "remove line\n2{\n3\n4}\n5\n"
<< (QList<int> () << 2 << 3);
QTest::newRow("comment opened folding")
<< "0\n1\n2{\n3\n4}\n5\n"
<< (QList<int> () << 2)
<< (QList<int> () << 3 << 4)
<< 2 << 1 << 2 << 1
<< "%"
<< "0\n1\n2%{\n3\n4}\n5\n"
<< (QList<int> ());
QTest::newRow("comment opened folding partially")
<< "0\n1\n2{{{()}\n3\n4{\n5}\n6}\n7}\n"
<< (QList<int> () << 2 << 4)
<< (QList<int> () << 3 << 4 << 5 << 6 << 7)
<< 2 << 1 << 2 << 1
<< "%"
<< "0\n1\n2%{{{()}\n3\n4{\n5}\n6}\n7}\n"
<< (QList<int> () << 5);
QTest::newRow("comment merges blocks")
<< "0\n1\n2{\n3\n4} {\n5\n6}\n"
<< (QList<int> () << 2 << 4)
<< (QList<int> () << 3 << 5 << 6)
<< 4 << 1 << 4 << 1
<< "%"
<< "0\n1\n2{\n3\n4%} {\n5\n6}\n"
<< (QList<int> () << 3 << 4 << 5 << 6);
QTest::newRow("comment enlarge block")
<< "0\n1\n2{\n3\n4}\n5\n"
<< (QList<int> () << 2)
<< (QList<int> () << 3 << 4)
<< 4 << 1 << 4 << 1
<< "%"
<< "0\n1\n2{\n3\n4%}\n5\n"
<< (QList<int> () << 3 << 4 << 5 << 6);
QTest::newRow("line removing block merging")
<< "0\n1\n2{\n3\n4}\n5\n6{\n7\n8}\n"
<< (QList<int> () << 2 << 6)
<< (QList<int> () << 3 << 4 << 7 << 8)
<< 4 << 2 << 5 << 1
<< " "
<< "0\n1\n2{\n3\n4} \n6{\n7\n8}\n"
<< (QList<int> () << 3 << 4 << 6 << 7);
QTest::newRow("block override")
<< "0\n1$\n2{\n3\n4\n5}\n6\n"
<< (QList<int> () << 2 )
<< (QList<int> () << 3 << 4 << 5)
<< 4 << 1 << 4 << 1
<< "$"
<< "0\n1$\n2{\n3\n4$\n5}\n6\n"
<< (QList<int> () << 3);
QTest::newRow("multi line deletion")
<< "0\n1{\n2abc\n3}\n4\n"
<< (QList<int>() << 1)
<< (QList<int>() << 2 << 3)
<< 1 << 0 << 4 << 0
<< "x"
<< "0\nx4\n"
<< (QList<int>());
QTest::newRow("latex test")
<< "\\begin{document}\n\n\\subsection{Bilder}\n\n\\subsection{end}\n\n\\end{document}"
<< (QList<int>() << 2)
<< (QList<int>() << 3)
<< 1 << 0 << 5 << 0
<< "x"
<< "\\begin{document}\nx\n\\end{document}"
<< (QList<int>());
}
//tests if folded text can be edited
void QEditorTest::activeFolding(){
QFETCH(QString, editorText);
QFETCH(QList<int>, foldAt);
QFETCH(QList<int>, hiddenLines);
QFETCH(int, cursorAL);
QFETCH(int, cursorAC);
QFETCH(int, cursorL);
QFETCH(int, cursorC);
QFETCH(QString, textToInsert);
QFETCH(QString, newEditorText);
QFETCH(QList<int>, newHiddenLines);
editor->setText(editorText, false);
foreach(const int &i, foldAt)
editor->document()->collapse(i);
for (int i=0;i<editor->document()->lines();i++)
QVERIFY2(editor->document()->line(i).isHidden() == hiddenLines.contains(i),qPrintable(QString::number(i)));
compareLists(editor->document()->impl()->testGetHiddenLines(), hiddenLines);
QDocumentCursor editCursor = editor->document()->cursor(cursorAL,cursorAC,cursorL,cursorC);
editCursor.insertText(textToInsert);
QEQUAL(editor->document()->text(), newEditorText);
for (int i=0;i<editor->document()->lines();i++)
QVERIFY2(editor->document()->line(i).isHidden() == newHiddenLines.contains(i),qPrintable(QString::number(i)));
compareLists(editor->document()->impl()->testGetHiddenLines(), newHiddenLines);
}
void QEditorTest::insertTab_data() {
QTest::addColumn<bool>("replaceTextTabs");
QTest::addColumn<int>("tabWidth");
QTest::addColumn<QString>("line");
QTest::addColumn<int>("cursorCol");
QTest::addColumn<QString>("resultLine");
QTest::newRow("no replace") << false << 4 << "fooobar" << 4 << "fooo\tbar";
QTest::newRow("4 char tab 1") << true << 4 << "fooobar" << 4 << "fooo bar";
QTest::newRow("4 char tab 2") << true << 4 << "fooo1bar" << 5 << "fooo1 bar";
QTest::newRow("4 char tab 3") << true << 4 << "fooo12bar" << 6 << "fooo12 bar";
QTest::newRow("4 char tab 4") << true << 4 << "fooo123bar" << 7 << "fooo123 bar";
QTest::newRow("4 char tab 5") << true << 4 << "fooo1234bar" << 8 << "fooo1234 bar";
QTest::newRow("5 char tab 1") << true << 5 << "fooobar" << 4 << "fooo bar";
}
void QEditorTest::insertTab()
{
bool savedReplaceTextTabs = editor->flag(QEditor::ReplaceTextTabs);
int savedTabWidth = QDocument::tabStop();
QFETCH(bool, replaceTextTabs);
QFETCH(int, tabWidth);
QFETCH(QString, line);
QFETCH(int, cursorCol);
QFETCH(QString, resultLine);
editor->setFlag(QEditor::ReplaceTextTabs, replaceTextTabs);
QDocument::setTabStop(tabWidth);
editor->setText(line);
editor->setCursorPosition(0, cursorCol);
editor->insertTab();
QEQUAL(editor->text(), resultLine);
editor->setFlag(QEditor::ReplaceTextTabs, savedReplaceTextTabs);
QDocument::setTabStop(savedTabWidth);
}
void QEditorTest::indentation_data(){
editor->setFlag(QEditor::AutoIndent,true);
editor->setFlag(QEditor::ReplaceIndentTabs,false);
QTest::addColumn<QString>("baseText");
QTest::addColumn<bool>("weak");
QTest::addColumn<int>("line");
QTest::addColumn<int>("col");
QTest::addColumn<int>("anchorLine");
QTest::addColumn<int>("anchorCol");
QTest::addColumn<QString>("insert");
QTest::addColumn<QString>("result");
QTest::newRow("trivial")
<< "hello\nworld\n"
<< false << 0 << 5 << -1 << -1
<< "{\na\n}"
<< "hello{\n\ta\n}\nworld\n";
QTest::newRow("trivial weak")
<< "hello\nworld\n"
<< true << 0 << 5 << -1 << -1
<< "{\na\n}"
<< "hello{\na\n}\nworld\n";
QTest::newRow("trivial + \\t indentation")
<< "\thello\nworld\n"
<< false << 0 << 6 << -1 << -1
<< "{\na\n}"
<< "\thello{\n\t\ta\n\t}\nworld\n";
QTest::newRow("trivial + \\t indentation weak")
<< "\thello\nworld\n"
<< true << 0 << 6 << -1 << -1
<< "{\na\n}"
<< "\thello{\n\ta\n\t}\nworld\n";
QTest::newRow("trivial + space indentation")
<< " hello\nworld\n"
<< false << 0 << 8 << -1 << -1
<< "{\na\n}"
<< " hello{\n \ta\n }\nworld\n";
QTest::newRow("trivial + space indentation weak")
<< " hello\nworld\n"
<< true << 0 << 8 << -1 << -1
<< "{\na\n}"
<< " hello{\n a\n }\nworld\n";
QTest::newRow("trivial + \\t indentation")
<< "\thello\nworld\n"
<< false << 0 << 6 << -1 << -1
<< "{\na\n}"
<< "\thello{\n\t\ta\n\t}\nworld\n";
QTest::newRow("space test")
<< " \nworld\n"
<< false << 0 << 2 << -1 << -1
<< "\n"
<< " \n \nworld\n";
QTest::newRow("space test + weak")
<< " \nworld\n"
<< true << 0 << 2 << -1 << -1
<< "\n"
<< " \n \nworld\n";
QTest::newRow("block indentation")
<< " hello\nworld\n"
<< true << 0 << 6 << -1 << -1
<< " x\n y\n z"
<< " hel x\n y\n zlo\nworld\n";
QTest::newRow("block indentation")
<< " hello\nworld\n"
<< true << 0 << 6 << -1 << -1
<< " \\begin{abc}\n abcdef\n \\end{abc}"
<< " hel \\begin{abc}\n abcdef\n \\end{abc}lo\nworld\n";
QTest::newRow("block indentation multiline")
<< " hello\nworld\n"
<< false << 0 << 6 << -1 << -1
<< "\\begin{abc}\n abcdef\n sdfsdf\n \\end{abc}"
<< " hel\\begin{abc}\n \tabcdef\n \tsdfsdf\n \\end{abc}lo\nworld\n";
QTest::newRow("block indentation nested")
<< " \n"
<< false << 0 << 3 << -1 << -1
<< "{\n{\nabcdef\nsdfsdf\n}\n}"
<< " {\n \t{\n \t\tabcdef\n \t\tsdfsdf\n \t}\n }\n";
QTest::newRow("block indentation + 3 space")
<< " hello\nworld\n"
<< false << 0 << 6 << -1 << -1
<< " \\begin{abc}\n abcdef\n \\end{abc}"
<< " hel \\begin{abc}\n \tabcdef\n \\end{abc}lo\nworld\n";
QTest::newRow("block indentation + 4 space")
<< " hello\nworld\n"
<< false << 0 << 7 << -1 << -1
<< " \\begin{abc}\n abcdef\n \\end{abc}"
<< " hel \\begin{abc}\n \tabcdef\n \\end{abc}lo\nworld\n";
QTest::newRow("block indentation + tabs")
<< "\thello\nworld\n"
<< true << 0 << 4 << -1 << -1
<< "\t\\begin{abc}\n\t\tabcdef\n\t\\end{abc}"
<< "\thel\t\\begin{abc}\n\t\t\tabcdef\n\t\t\\end{abc}lo\nworld\n";
QTest::newRow("block indentation + tabs simple")
<< "hello\nworld\n"
<< true << 0 << 3 << -1 << -1
<< "\t\\begin{abc}\n\t\tabcdef\n\t\\end{abc}"
<< "hel\t\\begin{abc}\n\t\tabcdef\n\t\\end{abc}lo\nworld\n";
QTest::newRow("multi unindent with closing brackets after text")
<< "hello\nworld\n"
<< false << 0 << 5 << -1 << -1
<< "{{\na\nxx}}\nabc"
<< "hello{{\n\t\ta\n\t\txx}}\nabc\nworld\n"; //only unindent the next line
QTest::newRow("multi unindent with closing brackets after text with pre indentation")
<< " hello\n world\n"
<< false << 0 << 6 << -1 << -1
<< "{{\na\nxx}}\nabc"
<< " hello{{\n \t\ta\n \t\txx}}\n abc\n world\n";
QTest::newRow("no immediate unindent with closing bracket after text")
<< "\t\thello\n\t\tworld\n"
<< false << 0 << 7 << -1 << -1
<< "}}"
<< "\t\thello}}\n\t\tworld\n"; //no prapagation yet
QTest::newRow("2 openings and closings per line")
<< "A\nB"
<< false << 1 << 0 << -1 << -1
<< "{{\nTEXT\n}}\n"
<< "A\n{{\n\t\tTEXT\n}}\nB";
QTest::newRow("3 openings and closings per line")
<< "A\nB"
<< false << 1 << 0 << -1 << -1
<< "{{{\nTEXT\n}}}\n"
<< "A\n{{{\n\t\t\tTEXT\n}}}\nB";
QTest::newRow("multiple closings with unindent on a line")
<< "A\nB"
<< false << 1 << 0 << -1 << -1
<< "\\cmd{\\begin{env}\nTEXT\n\\end{env}}\nMORE\n"
<< "A\n\\cmd{\\begin{env}\n\t\tTEXT\n\\end{env}}\nMORE\nB";
QTest::newRow("pasting non-indented text with newline at end weak")
<< "\tfoo\n\tbar\n"
<< true << 1 << 0 << -1 << -1
<< "baz\n"
<< "\tfoo\nbaz\n\tbar\n";
QTest::newRow("pasting non-indented text with newline at end")
<< "\tfoo\n\tbar\n"
<< false << 1 << 0 << -1 << -1
<< "baz\n"
<< "\tfoo\nbaz\n\tbar\n";
QTest::newRow("pasting indented text with newline at end weak")
<< "\tfoo\n\tbar\n"
<< true << 1 << 0 << -1 << -1
<< "\tbaz\n"
<< "\tfoo\n\tbaz\n\tbar\n";
QTest::newRow("pasting indented text with newline at end")
<< "\tfoo\n\tbar\n"
<< false << 1 << 0 << -1 << -1
<< "\tbaz\n"
<< "\tfoo\n\tbaz\n\tbar\n";
QTest::newRow("selection block self replacement (line start) weak")
<< "\tAA\n\tBB\n\tCC\n"
<< true << 2 << 0 << 1 << 0
<< "\tBB\n"
<< "\tAA\n\tBB\n\tCC\n";
QTest::newRow("selection block self replacement (line start)")
<< "\tAA\n\tBB\n\tCC\n"
<< false << 2 << 0 << 1 << 0
<< "\tBB\n"
<< "\tAA\n\tBB\n\tCC\n";
QTest::newRow("reversed selection block self replacement (line start) weak")
<< "\tAA\n\tBB\n\tCC\n"
<< true << 1 << 0 << 2 << 0
<< "\tBB\n"
<< "\tAA\n\tBB\n\tCC\n";
QTest::newRow("reversed selection block self replacement (line start)")
<< "\tAA\n\tBB\n\tCC\n"
<< false << 1 << 0 << 2 << 0
<< "\tBB\n"
<< "\tAA\n\tBB\n\tCC\n";
QTest::newRow("selection block self replacement (text start) weak")
<< "\tAA\n\tBB\n\tCC\n"
<< true << 2 << 1 << 1 << 1
<< "BB\n\t"
<< "\tAA\n\tBB\n\tCC\n";
QTest::newRow("selection block self replacement (text start)")
<< "\tAA\n\tBB\n\tCC\n"
<< false << 2 << 1 << 1 << 1
<< "BB\n\t"
<< "\tAA\n\tBB\n\tCC\n";
QTest::newRow("reversed selection block self replacement (text start) weak")
<< "\tAA\n\tBB\n\tCC\n"
<< true << 1 << 1 << 2 << 1
<< "BB\n\t"
<< "\tAA\n\tBB\n\tCC\n";
QTest::newRow("reversed selection block self replacement (text start)")
<< "\tAA\n\tBB\n\tCC\n"
<< false << 1 << 1 << 2 << 1
<< "BB\n\t"
<< "\tAA\n\tBB\n\tCC\n";
/*
this is broken:
QTest::newRow("trivial 2")
<< "hello\nworld\n"
<< false << 0 << 5
<< "{{\na\n}}"
<< "hello{{\n\t\ta\n}}\nworld\n";
*/
}
void QEditorTest::indentation(){
QFETCH(QString, baseText);
QFETCH(bool, weak);
QFETCH(int, line);
QFETCH(int, col);
QFETCH(int, anchorLine);
QFETCH(int, anchorCol);
QFETCH(QString, insert);
QFETCH(QString, result);
editor->setFlag(QEditor::WeakIndent, weak);
editor->setText(baseText, false);
QDocumentCursor c=editor->document()->cursor(line,col,anchorLine,anchorCol);
editor->insertText(c, insert);
//QEXPECT_FAIL("2 openings and closings per line", "issue 1335", Continue);
//QEXPECT_FAIL("3 openings and closings per line", "issue 1335", Continue);
//QEXPECT_FAIL("multiple closings with unindent on a line", "issue 1335", Continue);
QEQUAL(editor->document()->text(), result);
}
void QEditorTest::autoClosing_data(){
editor->setFlag(QEditor::AutoIndent,true);
//editor->setFlag(QEditor::WeakIndent,false);
editor->setFlag(QEditor::ReplaceIndentTabs,false);
QTest::addColumn<QString>("baseText");
QTest::addColumn<int>("line");
QTest::addColumn<int>("col");
QTest::addColumn<QString>("insert");
QTest::addColumn<QString>("result");
QTest::newRow("-(-") << "><" << 0 << 1 << "(" << ">()<";
QTest::newRow("-)-") << "><" << 0 << 1 << ")" << ">)<";
QTest::newRow("-{-") << "><" << 0 << 1 << "{" << ">{}<";
QTest::newRow("-$-") << "><" << 0 << 1 << "$" << ">$<"; //this tests the absence of a feature
QTest::newRow("-\\begin{verbatim}-") << "><" << 0 << 1 << "\\begin{verbatim}" << ">\\begin{verbatim}<"; //too
QTest::newRow("existing )") << ">)<" << 0 << 1 << "(" << ">()<";
QTest::newRow("existing 2-)") << ">))<" << 0 << 1 << "(" << ">())<";
QTest::newRow("inserting-multiple-brackets") << "><" << 0 << 1 << "((" << ">((<"; // currently not supported - what would one expect?
QTest::newRow("counting 1") << ">())<" << 0 << 1 << "(" << ">(())<";
QTest::newRow("counting 2") << ">((())))<" << 0 << 1 << "(" << ">(((())))<";
QTest::newRow("counting 3") << ">((()()))())<" << 0 << 1 << "(" << ">(((()()))())<";
QTest::newRow("multi line search") << ">\n\n\\]<" << 0 << 1 << "\\[" << ">\\[\n\n\\]<";
QTest::newRow("insert-match-to-close-mixed") << ">[{}])<" << 0 << 1 << "(" << ">([{}])<";
QTest::newRow("insert-match-to-close-mixed-with-same") << ">([{}]))<" << 0 << 1 << "(" << ">(([{}]))<";
QTest::newRow("insert-match-to-close-mixed-with-same2") << ">([{}]))<" << 0 << 2 << "(" << ">(([{}]))<";
QTest::newRow("many") << "(((((())))))" << 0 << 1 << "(" << "((((((())))))";
QTest::newRow("following") << " ()" << 0 << 0 << "(" << "() ()";
QTest::newRow("following2") << " \\(\\)" << 0 << 0 << "\\(" << "\\(\\) \\(\\)";
QTest::newRow("following2withExistingMismatch") << " {\\(\\)" << 0 << 0 << "\\(" << "\\(\\) {\\(\\)";
QTest::newRow("mismatch") << "}" << 0 << 0 << "(" << "()}";
QTest::newRow("mismatch2") << "}" << 0 << 0 << "\\(" << "\\(\\)}";
QTest::newRow("inner") << "(..)" << 0 << 2 << "(" << "(.().)";
QTest::newRow("inner2") << "\\(..\\)" << 0 << 3 << "\\(" << "\\(.\\(\\).\\)";
}
void QEditorTest::autoClosing(){
QFETCH(QString, baseText);
QFETCH(int, line);
QFETCH(int, col);
QFETCH(QString, insert);
QFETCH(QString, result);
QEXPECT_FAIL("insert-match-to-close-mixed-with-same2", "currently not properly supported", Continue);
editor->cutBuffer.clear(); // need to start from a clean state (other tests may have put something there)
editor->setText(baseText, false);
QDocumentCursor c=editor->document()->cursor(line,col);
editor->insertText(c, insert);
QEQUAL(editor->document()->text(), result);
}
#endif
|