21 #include <QtCore/QRegularExpression> 23 #include <QtGui/QTextCursor> 24 #include <QtGui/QTextDocument> 25 #include <QtTest/QtTest> 26 #include <QtTest/qtestevent.h> 28 #include "coverageobject.h" 29 #include "markupdirector.h" 30 #include "texthtmlbuilder.h" 40 void testSingleFormat();
41 void testDoubleFormat();
42 void testDoubleStartDifferentFinish();
43 void testDoubleStartDifferentFinishReverseOrder();
44 void testDifferentStartDoubleFinish();
45 void testDifferentStartDoubleFinishReverseOrder();
48 void testAnchorWithFormattedContent();
49 void testAdjacentAnchors();
50 void testNestedFormatting();
52 void testDoubleSpan();
53 void testSpanNesting();
54 void testEdgeCaseLeft();
55 void testEdgeCaseRight();
57 void testImageResized();
58 void testEachFormatTagSingly();
59 void testHorizontalRule();
61 void testNewlinesThroughQTextCursor();
64 void TestHtmlOutput::testSingleFormat()
69 doc->setHtml(QStringLiteral(
"This <b>text</b> is bold."));
73 md->processDocument(doc);
74 auto result = hb->getResult();
76 QStringLiteral(
"^<p>This <strong>text</strong> is bold.</p>\\n$"));
78 QVERIFY(regex.match(result).hasMatch());
81 void TestHtmlOutput::testDoubleFormat()
86 doc->setHtml(QStringLiteral(
"Some <b><i>formatted</i></b> text."));
90 md->processDocument(doc);
91 auto result = hb->getResult();
93 QStringLiteral(
"^<p>Some " 94 "(<strong><em>|<em><strong>)formatted(</em></strong>|</" 95 "strong></em>) text.</p>\\n$"));
97 QVERIFY(regex.match(result).hasMatch());
100 void TestHtmlOutput::testAnchor()
104 QStringLiteral(
"A <a href=\"http://www.kde.org\">link</a> to KDE."));
108 md->processDocument(doc);
109 auto result = hb->getResult();
112 "^<p>A <a href=\"http://www.kde.org\">link</a> to KDE.</p>\\n$"));
114 QVERIFY(regex.match(result).hasMatch());
117 void TestHtmlOutput::testAnchorWithFormattedContent()
120 doc->setHtml(QStringLiteral(
121 "A <a href=\"http://www.kde.org\"><b>formatted</b> link</a> to KDE."));
125 md->processDocument(doc);
126 auto result = hb->getResult();
129 "^<p>A <a href=\"http://www.kde.org\"><strong>formatted</strong> " 130 "link</a> to KDE.</p>\\n$"));
132 QVERIFY(regex.match(result).hasMatch());
135 void TestHtmlOutput::testAdjacentAnchors()
139 QStringLiteral(
"Two <a href=\"http://www.kde.org\">links</a><a " 140 "href=\"http://www.google.com\">next</a> to eachother."));
144 md->processDocument(doc);
145 auto result = hb->getResult();
148 "^<p>Two <a href=\"http://www.kde.org\">links</a><a " 149 "href=\"http://www.google.com\">next</a> to eachother.</p>\\n$"));
151 QVERIFY(regex.match(result).hasMatch());
154 void TestHtmlOutput::testNestedFormatting()
157 doc->setHtml(QStringLiteral(
"This <b>text is <i>italic</i> and</b> bold."));
161 md->processDocument(doc);
162 auto result = hb->getResult();
165 "^<p>This <strong>text is <em>italic</em> and</strong> bold.</p>\\n$"));
167 QVERIFY(regex.match(result).hasMatch());
170 void TestHtmlOutput::testSpan()
173 doc->setHtml(QStringLiteral(
174 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
178 md->processDocument(doc);
179 auto result = hb->getResult();
182 QStringLiteral(
"^<p>Some <span style=\"color:#ff0000;\">formatted</span> " 185 QVERIFY(regex.match(result).hasMatch());
188 void TestHtmlOutput::testDoubleSpan()
191 doc->setHtml(QStringLiteral(
"Some <span " 192 "style=\"color:#ff0000;background-color:#00ff00;" 193 "\">formatted</span> text."));
197 md->processDocument(doc);
198 auto result = hb->getResult();
202 "style=\"(color:#ff0000|background-color:#00ff00);\"><span " 203 "style=\"(color:#ff0000|background-color:#00ff00);\">formatted</span></" 204 "span> text.</p>\\n$"));
206 QVERIFY(regex.match(result).hasMatch());
209 void TestHtmlOutput::testSpanNesting()
212 doc->setHtml(QStringLiteral(
213 "Paragraph <span style=\"background-color:#00ff00;\">with some <span " 214 "style=\"color:#ff0000;\">formatted</span> nested</span> text."));
218 md->processDocument(doc);
219 auto result = hb->getResult();
222 "^<p>Paragraph <span style=\"background-color:#00ff00;\">with some <span " 223 "style=\"color:#ff0000;\">formatted</span> nested</span> text.</p>\\n$"));
225 QVERIFY(regex.match(result).hasMatch());
228 void TestHtmlOutput::testDoubleStartDifferentFinish()
232 QStringLiteral(
"Paragraph <i><b>with</b> some formatted</i> text."));
236 md->processDocument(doc);
237 auto result = hb->getResult();
240 QStringLiteral(
"^<p>Paragraph <em><strong>with</strong> some " 241 "formatted</em> text.</p>\\n$"));
243 QVERIFY(regex.match(result).hasMatch());
246 void TestHtmlOutput::testDoubleStartDifferentFinishReverseOrder()
250 QStringLiteral(
"Paragraph <b><i>with</i> some formatted</b> text."));
254 md->processDocument(doc);
255 auto result = hb->getResult();
258 QStringLiteral(
"^<p>Paragraph <strong><em>with</em> some " 259 "formatted</strong> text.</p>\\n$"));
261 QVERIFY(regex.match(result).hasMatch());
264 void TestHtmlOutput::testDifferentStartDoubleFinish()
268 QStringLiteral(
"Paragraph <i>with some <b>formatted<b></i> text."));
272 md->processDocument(doc);
273 auto result = hb->getResult();
276 QStringLiteral(
"^<p>Paragraph <em>with some " 277 "<strong>formatted</strong></em> text.</p>\\n$"));
279 QVERIFY(regex.match(result).hasMatch());
282 void TestHtmlOutput::testDifferentStartDoubleFinishReverseOrder()
286 QStringLiteral(
"Paragraph <b>with some <i>formatted</i></b> text."));
290 md->processDocument(doc);
291 auto result = hb->getResult();
294 QStringLiteral(
"^<p>Paragraph <strong>with some " 295 "<em>formatted</em></strong> text.</p>\\n$"));
297 QVERIFY(regex.match(result).hasMatch());
300 void TestHtmlOutput::testOverlap()
303 doc->setHtml(QStringLiteral(
304 "Paragraph <b>with <i>some</i></b><i> formatted</i> text."));
308 md->processDocument(doc);
309 auto result = hb->getResult();
312 QStringLiteral(
"^<p>Paragraph <strong>with <em>some</em></strong><em> " 313 "formatted</em> text.</p>\\n$"));
315 QVERIFY(regex.match(result).hasMatch());
318 void TestHtmlOutput::testEdgeCaseLeft()
321 doc->setHtml(QStringLiteral(
"Paragraph <b>with some formatted text.</b>"));
325 md->processDocument(doc);
326 auto result = hb->getResult();
329 "^<p>Paragraph <strong>with some formatted text.</strong></p>\\n$"));
331 QVERIFY(regex.match(result).hasMatch());
334 void TestHtmlOutput::testEdgeCaseRight()
337 doc->setHtml(QStringLiteral(
"<b>Paragraph with some formatted</b> text."));
341 md->processDocument(doc);
342 auto result = hb->getResult();
345 "^<p><strong>Paragraph with some formatted</strong> text.</p>\\n$"));
347 QVERIFY(regex.match(result).hasMatch());
350 void TestHtmlOutput::testImage()
354 QStringLiteral(
"Paragraph with an inline <img " 355 "src=\"http://kde.org/img/kde41.png\" /> image."));
359 md->processDocument(doc);
360 auto result = hb->getResult();
363 QStringLiteral(
"^<p>Paragraph with an inline <img " 364 "src=\"http://kde.org/img/kde41.png\" /> image.</p>\\n$"));
366 QVERIFY(regex.match(result).hasMatch());
369 void TestHtmlOutput::testImageResized()
378 doc->setHtml(QStringLiteral(
379 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 380 "width=\"10\" /> image."));
388 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 389 "width=\"10\" /> image.</p>\\n$"));
393 doc->setHtml(QStringLiteral(
394 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 395 "height=\"10\" /> image."));
403 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 404 "height=\"10\" /> image.</p>\\n$"));
408 doc->setHtml(QStringLiteral(
409 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 410 "height=\"10\" width=\"10\" /> image."));
418 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" " 419 "width=\"10\" height=\"10\" /> image.</p>\\n$"));
424 void TestHtmlOutput::testEachFormatTagSingly()
433 doc->setHtml(QStringLiteral(
"Some <b>formatted</b> text."));
440 QStringLiteral(
"^<p>Some <strong>formatted</strong> text.</p>\\n$"));
444 doc->setHtml(QStringLiteral(
"Some <i>formatted</i> text."));
451 QStringLiteral(
"^<p>Some <em>formatted</em> text.</p>\\n$"));
455 doc->setHtml(QStringLiteral(
"Some <u>formatted</u> text."));
462 QStringLiteral(
"^<p>Some <u>formatted</u> text.</p>\\n$"));
466 doc->setHtml(QStringLiteral(
"Some <s>formatted</s> text."));
473 QStringLiteral(
"^<p>Some <s>formatted</s> text.</p>\\n$"));
477 doc->setHtml(QStringLiteral(
"Some <sup>formatted</sup> text."));
484 QStringLiteral(
"^<p>Some <sup>formatted</sup> text.</p>\\n$"));
488 doc->setHtml(QStringLiteral(
"Some <sub>formatted</sub> text."));
495 QStringLiteral(
"^<p>Some <sub>formatted</sub> text.</p>\\n$"));
499 doc->setHtml(QStringLiteral(
500 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
507 QStringLiteral(
"^<p>Some <span style=\"color:#ff0000;\">formatted</span> " 512 doc->setHtml(QStringLiteral(
513 "Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
520 "^<p>Some <span style=\"background-color:#ff0000;\">formatted</span> " 525 doc->setHtml(QStringLiteral(
526 "Some <span style=\"font-family:courier;\">formatted</span> text."));
533 "^<p>Some <span style=\"font-family:courier;\">formatted</span> " 538 doc->setHtml(QStringLiteral(
539 "Some <span style=\"font-size:20pt;\">formatted</span> text."));
546 "^<p>Some <span style=\"font-size:20pt;\">formatted</span> " 551 void TestHtmlOutput::testHorizontalRule()
555 QStringLiteral(
"<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr " 556 "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
564 QStringLiteral(
"^<p>Foo</p>\\n<hr />\\n<p>Bar</p>\\n$"));
569 void TestHtmlOutput::testNewlines()
572 doc->setHtml(QStringLiteral(
"<p>Foo<br /><br />\n<p>Bar</p>"));
580 QStringLiteral(
"^<p>Foo</p>\\n<p> <p> </p>\\n<p>Bar</p>\\n$"));
585 void TestHtmlOutput::testNewlinesThroughQTextCursor()
590 cursor.insertText(QStringLiteral(
"Foo"));
591 cursor.insertText(QStringLiteral(
"\n"));
592 cursor.insertText(QStringLiteral(
"\n"));
593 cursor.insertText(QStringLiteral(
"\n"));
594 cursor.insertText(QStringLiteral(
"Bar"));
602 QStringLiteral(
"^<p>Foo</p>\\n<p> <p> <p>Bar</p>\\n$"));
608 #include "htmlbuildertest.moc" Instructs a builder object to create markup output.
The Cutelee namespace holds all public Cutelee API.
QString getResult() override
bool hasMatch() const const
virtual void processDocument(QTextDocument *doc)
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
The TextHTMLBuilder creates a clean html markup output.