21 #include <QtCore/QRegularExpression>
23 #include <QtGui/QTextCursor>
24 #include <QtGui/QTextDocument>
25 #include <QtGui/QTextLine>
26 #include <QtTest/QtTest>
27 #include <QtTest/qtestevent.h>
29 #include "coverageobject.h"
30 #include "markupdirector.h"
31 #include "plaintextmarkupbuilder.h"
32 #include "texthtmlbuilder.h"
42 void testSingleFormat();
43 void testDoubleFormat();
44 void testDoubleStartDifferentFinish();
45 void testDoubleStartDifferentFinishReverseOrder();
46 void testDifferentStartDoubleFinish();
47 void testDifferentStartDoubleFinishReverseOrder();
50 void testAnchorWithFormattedContent();
51 void testAdjacentAnchors();
52 void testNestedFormatting();
54 void testDoubleSpan();
55 void testSpanNesting();
56 void testEdgeCaseLeft();
57 void testEdgeCaseRight();
59 void testImageResized();
60 void testEachFormatTagSingly();
61 void testHorizontalRule();
63 void testEmptyParagraphs();
64 void testNewlinesThroughQTextCursor();
65 void testBrInsideParagraph();
66 void testLongDocument();
69 void TestPlainMarkupOutput::testSingleFormat()
71 auto doc =
new QTextDocument();
74 doc->setHtml(QStringLiteral(
"This <b>text</b> is bold."));
78 md->processDocument(doc);
79 auto result = hb->getResult();
80 QRegularExpression regex(QStringLiteral(
"^This \\*text\\* is bold.\\n$"));
82 QVERIFY(regex.match(result).hasMatch());
85 void TestPlainMarkupOutput::testDoubleFormat()
87 auto doc =
new QTextDocument();
90 doc->setHtml(QStringLiteral(
"Some <b><i>formatted</i></b> text."));
94 md->processDocument(doc);
95 auto result = hb->getResult();
96 QRegularExpression regex(
97 QStringLiteral(
"^Some (\\*/|/\\*)formatted(\\*/|/\\*) text.\\n$"));
99 QVERIFY(regex.match(result).hasMatch());
102 void TestPlainMarkupOutput::testAnchor()
104 auto doc =
new QTextDocument();
106 QStringLiteral(
"A <a href=\"http://www.kde.org\">link</a> to KDE."));
110 md->processDocument(doc);
111 auto result = hb->getResult();
113 QRegularExpression regex(QStringLiteral(
114 "^A link\\[1\\] to KDE.\\n\\n--------\\n\\[1\\] http://www.kde.org\\n$"));
116 regex.match(result).hasMatch();
118 QVERIFY(regex.match(result).hasMatch());
121 void TestPlainMarkupOutput::testAnchorWithFormattedContent()
123 auto doc =
new QTextDocument();
124 doc->setHtml(QStringLiteral(
125 "A <a href=\"http://www.kde.org\"><b>formatted</b> link</a> to KDE."));
129 md->processDocument(doc);
130 auto result = hb->getResult();
132 QRegularExpression regex(
133 QStringLiteral(
"^A \\*formatted\\* link\\[1\\] to "
134 "KDE.\\n\\n--------\\n\\[1\\] http://www.kde.org\\n$"));
136 QVERIFY(regex.match(result).hasMatch());
139 void TestPlainMarkupOutput::testAdjacentAnchors()
141 auto doc =
new QTextDocument();
143 QStringLiteral(
"Two <a href=\"http://www.kde.org\">links</a><a "
144 "href=\"http://www.google.com\">next</a> to eachother."));
148 md->processDocument(doc);
149 auto result = hb->getResult();
151 QRegularExpression regex(QStringLiteral(
152 "^Two links\\[1\\]next\\[2\\] to eachother.\\n\\n--------\\n\\[1\\] "
153 "http://www.kde.org\\n\\[2\\] http://www.google.com\\n$"));
155 QVERIFY(regex.match(result).hasMatch());
158 void TestPlainMarkupOutput::testNestedFormatting()
160 auto doc =
new QTextDocument();
161 doc->setHtml(QStringLiteral(
"This <b>text is <i>italic</i> and</b> bold."));
165 md->processDocument(doc);
166 auto result = hb->getResult();
168 QRegularExpression regex(
169 QStringLiteral(
"^This \\*text is /italic/ and\\* bold.\\n$"));
171 QVERIFY(regex.match(result).hasMatch());
174 void TestPlainMarkupOutput::testSpan()
176 auto doc =
new QTextDocument();
177 doc->setHtml(QStringLiteral(
178 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
182 md->processDocument(doc);
183 auto result = hb->getResult();
185 auto regex = QRegularExpression(QStringLiteral(
"^Some formatted text.\\n$"));
187 QVERIFY(regex.match(result).hasMatch());
190 void TestPlainMarkupOutput::testDoubleSpan()
192 auto doc =
new QTextDocument();
193 doc->setHtml(QStringLiteral(
"Some <span "
194 "style=\"color:#ff0000;background-color:#00ff00;"
195 "\">formatted</span> text."));
199 md->processDocument(doc);
200 auto result = hb->getResult();
202 auto regex = QRegularExpression(QStringLiteral(
"^Some formatted text.\\n$"));
204 QVERIFY(regex.match(result).hasMatch());
207 void TestPlainMarkupOutput::testSpanNesting()
209 auto doc =
new QTextDocument();
210 doc->setHtml(QStringLiteral(
211 "Paragraph <span style=\"background-color:#00ff00;\">with some <span "
212 "style=\"color:#ff0000;\">formatted</span> nested</span> text."));
216 md->processDocument(doc);
217 auto result = hb->getResult();
219 auto regex = QRegularExpression(
220 QStringLiteral(
"^Paragraph with some formatted nested text.\\n$"));
222 QVERIFY(regex.match(result).hasMatch());
225 void TestPlainMarkupOutput::testDoubleStartDifferentFinish()
227 auto doc =
new QTextDocument();
229 QStringLiteral(
"Paragraph <i><b>with</b> some formatted</i> text."));
233 md->processDocument(doc);
234 auto result = hb->getResult();
236 auto regex = QRegularExpression(
237 QStringLiteral(
"^Paragraph /\\*with\\* some formatted/ text.\\n$"));
239 QVERIFY(regex.match(result).hasMatch());
242 void TestPlainMarkupOutput::testDoubleStartDifferentFinishReverseOrder()
244 auto doc =
new QTextDocument();
246 QStringLiteral(
"Paragraph <b><i>with</i> some formatted</b> text."));
250 md->processDocument(doc);
251 auto result = hb->getResult();
253 auto regex = QRegularExpression(
254 QStringLiteral(
"^Paragraph \\*/with/ some formatted\\* text.\\n$"));
256 QVERIFY(regex.match(result).hasMatch());
259 void TestPlainMarkupOutput::testDifferentStartDoubleFinish()
261 auto doc =
new QTextDocument();
263 QStringLiteral(
"Paragraph <i>with some <b>formatted<b></i> text."));
267 md->processDocument(doc);
268 auto result = hb->getResult();
270 auto regex = QRegularExpression(
271 QStringLiteral(
"^Paragraph /with some \\*formatted\\*/ text.\\n$"));
273 QVERIFY(regex.match(result).hasMatch());
276 void TestPlainMarkupOutput::testDifferentStartDoubleFinishReverseOrder()
278 auto doc =
new QTextDocument();
280 QStringLiteral(
"Paragraph <b>with some <i>formatted</i></b> text."));
284 md->processDocument(doc);
285 auto result = hb->getResult();
287 auto regex = QRegularExpression(
288 QStringLiteral(
"^Paragraph \\*with some /formatted/\\* text.\\n$"));
290 QVERIFY(regex.match(result).hasMatch());
293 void TestPlainMarkupOutput::testOverlap()
295 auto doc =
new QTextDocument();
296 doc->setHtml(QStringLiteral(
297 "Paragraph <b>with <i>some</i></b><i> formatted</i> text."));
301 md->processDocument(doc);
302 auto result = hb->getResult();
304 auto regex = QRegularExpression(
305 QStringLiteral(
"^Paragraph \\*with /some/\\*/ formatted/ text.\\n$"));
307 QVERIFY(regex.match(result).hasMatch());
310 void TestPlainMarkupOutput::testEdgeCaseLeft()
312 auto doc =
new QTextDocument();
313 doc->setHtml(QStringLiteral(
"Paragraph <b>with some formatted text.</b>"));
317 md->processDocument(doc);
318 auto result = hb->getResult();
320 auto regex = QRegularExpression(
321 QStringLiteral(
"^Paragraph \\*with some formatted text.\\*\\n$"));
323 QVERIFY(regex.match(result).hasMatch());
326 void TestPlainMarkupOutput::testEdgeCaseRight()
328 auto doc =
new QTextDocument();
329 doc->setHtml(QStringLiteral(
"<b>Paragraph with some formatted</b> text."));
333 md->processDocument(doc);
334 auto result = hb->getResult();
336 auto regex = QRegularExpression(
337 QStringLiteral(
"^\\*Paragraph with some formatted\\* text.\\n$"));
339 QVERIFY(regex.match(result).hasMatch());
342 void TestPlainMarkupOutput::testImage()
344 auto doc =
new QTextDocument();
346 QStringLiteral(
"Paragraph with an inline <img "
347 "src=\"http://kde.org/img/kde41.png\" /> image."));
351 md->processDocument(doc);
352 auto result = hb->getResult();
354 auto regex = QRegularExpression(QStringLiteral(
355 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
356 "http://kde.org/img/kde41.png\\n$"));
358 QVERIFY(regex.match(result).hasMatch());
361 void TestPlainMarkupOutput::testImageResized()
364 QRegularExpression regex;
367 auto doc =
new QTextDocument();
370 doc->setHtml(QStringLiteral(
371 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
372 "width=\"10\" /> image."));
379 regex = QRegularExpression(QStringLiteral(
380 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
381 "http://kde.org/img/kde41.png\\n$"));
382 QVERIFY(regex.match(result).hasMatch());
385 doc->setHtml(QStringLiteral(
386 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
387 "height=\"10\" /> image."));
394 regex = QRegularExpression(QStringLiteral(
395 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
396 "http://kde.org/img/kde41.png\\n$"));
397 QVERIFY(regex.match(result).hasMatch());
400 doc->setHtml(QStringLiteral(
401 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
402 "height=\"10\" width=\"10\" /> image."));
409 regex = QRegularExpression(QStringLiteral(
410 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
411 "http://kde.org/img/kde41.png\\n$"));
413 QVERIFY(regex.match(result).hasMatch());
416 void TestPlainMarkupOutput::testEachFormatTagSingly()
419 QRegularExpression regex;
422 auto doc =
new QTextDocument();
425 doc->setHtml(QStringLiteral(
"Some <b>formatted</b> text."));
431 regex = QRegularExpression(QStringLiteral(
"^Some \\*formatted\\* text.\\n$"));
432 QVERIFY(regex.match(result).hasMatch());
435 doc->setHtml(QStringLiteral(
"Some <i>formatted</i> text."));
441 regex = QRegularExpression(QStringLiteral(
"^Some /formatted/ text.\\n$"));
442 QVERIFY(regex.match(result).hasMatch());
445 doc->setHtml(QStringLiteral(
"Some <u>formatted</u> text."));
451 regex = QRegularExpression(QStringLiteral(
"^Some _formatted_ text.\\n$"));
452 QVERIFY(regex.match(result).hasMatch());
455 doc->setHtml(QStringLiteral(
"Some <s>formatted</s> text."));
461 regex = QRegularExpression(QStringLiteral(
"^Some -formatted- text.\\n$"));
462 QVERIFY(regex.match(result).hasMatch());
465 doc->setHtml(QStringLiteral(
"Some <sup>formatted</sup> text."));
471 regex = QRegularExpression(
472 QStringLiteral(
"^Some \\^\\{formatted\\} text.\\n$"));
473 QVERIFY(regex.match(result).hasMatch());
476 doc->setHtml(QStringLiteral(
"Some <sub>formatted</sub> text."));
483 = QRegularExpression(QStringLiteral(
"^Some _\\{formatted\\} text.\\n$"));
484 QVERIFY(regex.match(result).hasMatch());
487 doc->setHtml(QStringLiteral(
488 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
494 regex = QRegularExpression(QStringLiteral(
"^Some formatted text.\\n$"));
495 QVERIFY(regex.match(result).hasMatch());
498 doc->setHtml(QStringLiteral(
499 "Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
505 regex = QRegularExpression(QStringLiteral(
"^Some formatted text.\\n$"));
506 QVERIFY(regex.match(result).hasMatch());
509 doc->setHtml(QStringLiteral(
510 "Some <span style=\"font-family:courier;\">formatted</span> text."));
516 regex = QRegularExpression(QStringLiteral(
"^Some formatted text.\\n$"));
517 QVERIFY(regex.match(result).hasMatch());
520 doc->setHtml(QStringLiteral(
521 "Some <span style=\"font-size:20pt;\">formatted</span> text."));
527 regex = QRegularExpression(QStringLiteral(
"^Some formatted text.\\n$"));
528 QVERIFY(regex.match(result).hasMatch());
531 void TestPlainMarkupOutput::testHorizontalRule()
533 auto doc =
new QTextDocument();
535 QStringLiteral(
"<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr "
536 "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
543 auto regex = QRegularExpression(
544 QStringLiteral(
"^Foo\\n--------------------\\nBar\\n$"));
546 QVERIFY(regex.match(result).hasMatch());
549 void TestPlainMarkupOutput::testNewlines()
551 auto doc =
new QTextDocument();
552 doc->setHtml(QStringLiteral(
"<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
559 auto regex = QRegularExpression(QStringLiteral(
"^Foo\\n\\n\\nBar\\n$"));
561 QVERIFY(regex.match(result).hasMatch());
564 void TestPlainMarkupOutput::testEmptyParagraphs()
566 auto doc =
new QTextDocument();
567 doc->setHtml(QStringLiteral(
"<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
574 auto regex = QRegularExpression(QStringLiteral(
"^Foo\\n\\n\\nBar\\n$"));
576 QVERIFY(regex.match(result).hasMatch());
579 void TestPlainMarkupOutput::testNewlinesThroughQTextCursor()
581 auto doc =
new QTextDocument(
this);
582 QTextCursor cursor(doc);
583 cursor.movePosition(QTextCursor::Start);
584 cursor.insertText(QStringLiteral(
"Foo"));
585 cursor.insertText(QStringLiteral(
"\n"));
586 cursor.insertText(QStringLiteral(
"\n"));
587 cursor.insertText(QStringLiteral(
"\n"));
588 cursor.insertText(QStringLiteral(
"Bar"));
595 auto regex = QRegularExpression(QStringLiteral(
"^Foo\\n\\n\\nBar\\n$"));
597 QVERIFY(regex.match(result).hasMatch());
600 void TestPlainMarkupOutput::testBrInsideParagraph()
603 auto doc =
new QTextDocument();
604 doc->setHtml(QStringLiteral(
"<p>Foo<br /><br /><br />Bar</p>"));
613 auto regex = QRegularExpression(QStringLiteral(
"^Foo\\n\\n\\nBar\\n$"));
615 QVERIFY(regex.match(result).hasMatch());
618 void TestPlainMarkupOutput::testLongDocument()
622 QFile sourceHtml(QFINDTESTDATA(
"sourcehtml"));
623 QVERIFY(sourceHtml.open(QIODevice::ReadOnly));
624 doc.setHtml(QString::fromLatin1(sourceHtml.readAll().constData()));
631 result.startsWith(QLatin1String(
"Hello,\nThis is some text\nIt shows how "
632 "cutelee is used from kmail\n")),
634 QVERIFY2(result.endsWith(QLatin1String(
"This is the end of the signature\n")),
640 #include "plainmarkupbuildertest.moc"
Instructs a builder object to create markup output.
virtual void processDocument(QTextDocument *doc)
Creates a simple marked up plain text document.
QString getResult() override
The Cutelee namespace holds all public Cutelee API.