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()
74 doc->setHtml(QStringLiteral(
"This <b>text</b> is bold."));
78 md->processDocument(doc);
79 auto result = hb->getResult();
82 QVERIFY(regex.match(result).hasMatch());
85 void TestPlainMarkupOutput::testDoubleFormat()
90 doc->setHtml(QStringLiteral(
"Some <b><i>formatted</i></b> text."));
94 md->processDocument(doc);
95 auto result = hb->getResult();
97 QStringLiteral(
"^Some (\\*/|/\\*)formatted(\\*/|/\\*) text.\\n$"));
99 QVERIFY(regex.match(result).hasMatch());
102 void TestPlainMarkupOutput::testAnchor()
106 QStringLiteral(
"A <a href=\"http://www.kde.org\">link</a> to KDE."));
110 md->processDocument(doc);
111 auto result = hb->getResult();
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()
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();
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()
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();
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()
161 doc->setHtml(QStringLiteral(
"This <b>text is <i>italic</i> and</b> bold."));
165 md->processDocument(doc);
166 auto result = hb->getResult();
169 QStringLiteral(
"^This \\*text is /italic/ and\\* bold.\\n$"));
171 QVERIFY(regex.match(result).hasMatch());
174 void TestPlainMarkupOutput::testSpan()
177 doc->setHtml(QStringLiteral(
178 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
182 md->processDocument(doc);
183 auto result = hb->getResult();
187 QVERIFY(regex.match(result).hasMatch());
190 void TestPlainMarkupOutput::testDoubleSpan()
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();
204 QVERIFY(regex.match(result).hasMatch());
207 void TestPlainMarkupOutput::testSpanNesting()
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();
220 QStringLiteral(
"^Paragraph with some formatted nested text.\\n$"));
222 QVERIFY(regex.match(result).hasMatch());
225 void TestPlainMarkupOutput::testDoubleStartDifferentFinish()
229 QStringLiteral(
"Paragraph <i><b>with</b> some formatted</i> text."));
233 md->processDocument(doc);
234 auto result = hb->getResult();
237 QStringLiteral(
"^Paragraph /\\*with\\* some formatted/ text.\\n$"));
239 QVERIFY(regex.match(result).hasMatch());
242 void TestPlainMarkupOutput::testDoubleStartDifferentFinishReverseOrder()
246 QStringLiteral(
"Paragraph <b><i>with</i> some formatted</b> text."));
250 md->processDocument(doc);
251 auto result = hb->getResult();
254 QStringLiteral(
"^Paragraph \\*/with/ some formatted\\* text.\\n$"));
256 QVERIFY(regex.match(result).hasMatch());
259 void TestPlainMarkupOutput::testDifferentStartDoubleFinish()
263 QStringLiteral(
"Paragraph <i>with some <b>formatted<b></i> text."));
267 md->processDocument(doc);
268 auto result = hb->getResult();
271 QStringLiteral(
"^Paragraph /with some \\*formatted\\*/ text.\\n$"));
273 QVERIFY(regex.match(result).hasMatch());
276 void TestPlainMarkupOutput::testDifferentStartDoubleFinishReverseOrder()
280 QStringLiteral(
"Paragraph <b>with some <i>formatted</i></b> text."));
284 md->processDocument(doc);
285 auto result = hb->getResult();
288 QStringLiteral(
"^Paragraph \\*with some /formatted/\\* text.\\n$"));
290 QVERIFY(regex.match(result).hasMatch());
293 void TestPlainMarkupOutput::testOverlap()
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();
305 QStringLiteral(
"^Paragraph \\*with /some/\\*/ formatted/ text.\\n$"));
307 QVERIFY(regex.match(result).hasMatch());
310 void TestPlainMarkupOutput::testEdgeCaseLeft()
313 doc->setHtml(QStringLiteral(
"Paragraph <b>with some formatted text.</b>"));
317 md->processDocument(doc);
318 auto result = hb->getResult();
321 QStringLiteral(
"^Paragraph \\*with some formatted text.\\*\\n$"));
323 QVERIFY(regex.match(result).hasMatch());
326 void TestPlainMarkupOutput::testEdgeCaseRight()
329 doc->setHtml(QStringLiteral(
"<b>Paragraph with some formatted</b> text."));
333 md->processDocument(doc);
334 auto result = hb->getResult();
337 QStringLiteral(
"^\\*Paragraph with some formatted\\* text.\\n$"));
339 QVERIFY(regex.match(result).hasMatch());
342 void TestPlainMarkupOutput::testImage()
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();
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()
370 doc->setHtml(QStringLiteral(
371 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 372 "width=\"10\" /> image."));
380 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] " 381 "http://kde.org/img/kde41.png\\n$"));
385 doc->setHtml(QStringLiteral(
386 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 387 "height=\"10\" /> image."));
395 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] " 396 "http://kde.org/img/kde41.png\\n$"));
400 doc->setHtml(QStringLiteral(
401 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 402 "height=\"10\" width=\"10\" /> image."));
410 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] " 411 "http://kde.org/img/kde41.png\\n$"));
416 void TestPlainMarkupOutput::testEachFormatTagSingly()
425 doc->setHtml(QStringLiteral(
"Some <b>formatted</b> text."));
435 doc->setHtml(QStringLiteral(
"Some <i>formatted</i> text."));
445 doc->setHtml(QStringLiteral(
"Some <u>formatted</u> text."));
455 doc->setHtml(QStringLiteral(
"Some <s>formatted</s> text."));
465 doc->setHtml(QStringLiteral(
"Some <sup>formatted</sup> text."));
472 QStringLiteral(
"^Some \\^\\{formatted\\} text.\\n$"));
476 doc->setHtml(QStringLiteral(
"Some <sub>formatted</sub> text."));
487 doc->setHtml(QStringLiteral(
488 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
498 doc->setHtml(QStringLiteral(
499 "Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
509 doc->setHtml(QStringLiteral(
510 "Some <span style=\"font-family:courier;\">formatted</span> text."));
520 doc->setHtml(QStringLiteral(
521 "Some <span style=\"font-size:20pt;\">formatted</span> text."));
531 void TestPlainMarkupOutput::testHorizontalRule()
535 QStringLiteral(
"<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr " 536 "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
544 QStringLiteral(
"^Foo\\n--------------------\\nBar\\n$"));
549 void TestPlainMarkupOutput::testNewlines()
552 doc->setHtml(QStringLiteral(
"<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
564 void TestPlainMarkupOutput::testEmptyParagraphs()
567 doc->setHtml(QStringLiteral(
"<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
579 void TestPlainMarkupOutput::testNewlinesThroughQTextCursor()
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"));
600 void TestPlainMarkupOutput::testBrInsideParagraph()
604 doc->setHtml(QStringLiteral(
"<p>Foo<br /><br /><br />Bar</p>"));
618 void TestPlainMarkupOutput::testLongDocument()
622 QFile sourceHtml(QFINDTESTDATA(
"sourcehtml"));
632 "cutelee is used from kmail\n")),
640 #include "plainmarkupbuildertest.moc" Instructs a builder object to create markup output.
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
The Cutelee namespace holds all public Cutelee API.
Creates a simple marked up plain text document.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool hasMatch() const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
virtual void processDocument(QTextDocument *doc)
void setHtml(const QString &html)
QString fromLatin1(const char *str, int size)
QString getResult() override