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()
66 auto doc =
new QTextDocument();
69 doc->setHtml(QStringLiteral(
"This <b>text</b> is bold."));
73 md->processDocument(doc);
74 auto result = hb->getResult();
75 QRegularExpression regex(
76 QStringLiteral(
"^<p>This <strong>text</strong> is bold.</p>\\n$"));
78 QVERIFY(regex.match(result).hasMatch());
81 void TestHtmlOutput::testDoubleFormat()
83 auto doc =
new QTextDocument();
86 doc->setHtml(QStringLiteral(
"Some <b><i>formatted</i></b> text."));
90 md->processDocument(doc);
91 auto result = hb->getResult();
92 QRegularExpression regex(
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()
102 auto doc =
new QTextDocument();
104 QStringLiteral(
"A <a href=\"http://www.kde.org\">link</a> to KDE."));
108 md->processDocument(doc);
109 auto result = hb->getResult();
111 QRegularExpression regex(QStringLiteral(
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()
119 auto doc =
new QTextDocument();
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();
128 QRegularExpression regex(QStringLiteral(
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()
137 auto doc =
new QTextDocument();
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();
147 QRegularExpression regex(QStringLiteral(
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()
156 auto doc =
new QTextDocument();
157 doc->setHtml(QStringLiteral(
"This <b>text is <i>italic</i> and</b> bold."));
161 md->processDocument(doc);
162 auto result = hb->getResult();
164 QRegularExpression regex(QStringLiteral(
165 "^<p>This <strong>text is <em>italic</em> and</strong> bold.</p>\\n$"));
167 QVERIFY(regex.match(result).hasMatch());
170 void TestHtmlOutput::testSpan()
172 auto doc =
new QTextDocument();
173 doc->setHtml(QStringLiteral(
174 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
178 md->processDocument(doc);
179 auto result = hb->getResult();
181 auto regex = QRegularExpression(
182 QStringLiteral(
"^<p>Some <span style=\"color:#ff0000;\">formatted</span> "
185 QVERIFY(regex.match(result).hasMatch());
188 void TestHtmlOutput::testDoubleSpan()
190 auto doc =
new QTextDocument();
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();
200 auto regex = QRegularExpression(QStringLiteral(
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()
211 auto doc =
new QTextDocument();
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();
221 auto regex = QRegularExpression(QStringLiteral(
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()
230 auto doc =
new QTextDocument();
232 QStringLiteral(
"Paragraph <i><b>with</b> some formatted</i> text."));
236 md->processDocument(doc);
237 auto result = hb->getResult();
239 auto regex = QRegularExpression(
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()
248 auto doc =
new QTextDocument();
250 QStringLiteral(
"Paragraph <b><i>with</i> some formatted</b> text."));
254 md->processDocument(doc);
255 auto result = hb->getResult();
257 auto regex = QRegularExpression(
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()
266 auto doc =
new QTextDocument();
268 QStringLiteral(
"Paragraph <i>with some <b>formatted<b></i> text."));
272 md->processDocument(doc);
273 auto result = hb->getResult();
275 auto regex = QRegularExpression(
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()
284 auto doc =
new QTextDocument();
286 QStringLiteral(
"Paragraph <b>with some <i>formatted</i></b> text."));
290 md->processDocument(doc);
291 auto result = hb->getResult();
293 auto regex = QRegularExpression(
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()
302 auto doc =
new QTextDocument();
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();
311 auto regex = QRegularExpression(
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()
320 auto doc =
new QTextDocument();
321 doc->setHtml(QStringLiteral(
"Paragraph <b>with some formatted text.</b>"));
325 md->processDocument(doc);
326 auto result = hb->getResult();
328 auto regex = QRegularExpression(QStringLiteral(
329 "^<p>Paragraph <strong>with some formatted text.</strong></p>\\n$"));
331 QVERIFY(regex.match(result).hasMatch());
334 void TestHtmlOutput::testEdgeCaseRight()
336 auto doc =
new QTextDocument();
337 doc->setHtml(QStringLiteral(
"<b>Paragraph with some formatted</b> text."));
341 md->processDocument(doc);
342 auto result = hb->getResult();
344 auto regex = QRegularExpression(QStringLiteral(
345 "^<p><strong>Paragraph with some formatted</strong> text.</p>\\n$"));
347 QVERIFY(regex.match(result).hasMatch());
350 void TestHtmlOutput::testImage()
352 auto doc =
new QTextDocument();
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();
362 auto regex = QRegularExpression(
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()
372 QRegularExpression regex;
375 auto doc =
new QTextDocument();
378 doc->setHtml(QStringLiteral(
379 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
380 "width=\"10\" /> image."));
387 regex = QRegularExpression(QStringLiteral(
388 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
389 "width=\"10\" /> image.</p>\\n$"));
390 QVERIFY(regex.match(result).hasMatch());
393 doc->setHtml(QStringLiteral(
394 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
395 "height=\"10\" /> image."));
402 regex = QRegularExpression(QStringLiteral(
403 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
404 "height=\"10\" /> image.</p>\\n$"));
405 QVERIFY(regex.match(result).hasMatch());
408 doc->setHtml(QStringLiteral(
409 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
410 "height=\"10\" width=\"10\" /> image."));
417 regex = QRegularExpression(QStringLiteral(
418 "^<p>Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
419 "width=\"10\" height=\"10\" /> image.</p>\\n$"));
421 QVERIFY(regex.match(result).hasMatch());
424 void TestHtmlOutput::testEachFormatTagSingly()
427 QRegularExpression regex;
430 auto doc =
new QTextDocument();
433 doc->setHtml(QStringLiteral(
"Some <b>formatted</b> text."));
439 regex = QRegularExpression(
440 QStringLiteral(
"^<p>Some <strong>formatted</strong> text.</p>\\n$"));
441 QVERIFY(regex.match(result).hasMatch());
444 doc->setHtml(QStringLiteral(
"Some <i>formatted</i> text."));
450 regex = QRegularExpression(
451 QStringLiteral(
"^<p>Some <em>formatted</em> text.</p>\\n$"));
452 QVERIFY(regex.match(result).hasMatch());
455 doc->setHtml(QStringLiteral(
"Some <u>formatted</u> text."));
461 regex = QRegularExpression(
462 QStringLiteral(
"^<p>Some <u>formatted</u> text.</p>\\n$"));
463 QVERIFY(regex.match(result).hasMatch());
466 doc->setHtml(QStringLiteral(
"Some <s>formatted</s> text."));
472 regex = QRegularExpression(
473 QStringLiteral(
"^<p>Some <s>formatted</s> text.</p>\\n$"));
474 QVERIFY(regex.match(result).hasMatch());
477 doc->setHtml(QStringLiteral(
"Some <sup>formatted</sup> text."));
483 regex = QRegularExpression(
484 QStringLiteral(
"^<p>Some <sup>formatted</sup> text.</p>\\n$"));
485 QVERIFY(regex.match(result).hasMatch());
488 doc->setHtml(QStringLiteral(
"Some <sub>formatted</sub> text."));
494 regex = QRegularExpression(
495 QStringLiteral(
"^<p>Some <sub>formatted</sub> text.</p>\\n$"));
496 QVERIFY(regex.match(result).hasMatch());
499 doc->setHtml(QStringLiteral(
500 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
506 regex = QRegularExpression(
507 QStringLiteral(
"^<p>Some <span style=\"color:#ff0000;\">formatted</span> "
509 QVERIFY(regex.match(result).hasMatch());
512 doc->setHtml(QStringLiteral(
513 "Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
519 regex = QRegularExpression(QStringLiteral(
520 "^<p>Some <span style=\"background-color:#ff0000;\">formatted</span> "
522 QVERIFY(regex.match(result).hasMatch());
525 doc->setHtml(QStringLiteral(
526 "Some <span style=\"font-family:courier;\">formatted</span> text."));
532 regex = QRegularExpression(QStringLiteral(
533 "^<p>Some <span style=\"font-family:courier;\">formatted</span> "
535 QVERIFY(regex.match(result).hasMatch());
538 doc->setHtml(QStringLiteral(
539 "Some <span style=\"font-size:20pt;\">formatted</span> text."));
545 regex = QRegularExpression(QStringLiteral(
546 "^<p>Some <span style=\"font-size:20pt;\">formatted</span> "
548 QVERIFY(regex.match(result).hasMatch());
551 void TestHtmlOutput::testHorizontalRule()
553 auto doc =
new QTextDocument();
555 QStringLiteral(
"<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr "
556 "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
563 auto regex = QRegularExpression(
564 QStringLiteral(
"^<p>Foo</p>\\n<hr />\\n<p>Bar</p>\\n$"));
566 QVERIFY(regex.match(result).hasMatch());
569 void TestHtmlOutput::testNewlines()
571 auto doc =
new QTextDocument();
572 doc->setHtml(QStringLiteral(
"<p>Foo<br /><br />\n<p>Bar</p>"));
579 auto regex = QRegularExpression(
580 QStringLiteral(
"^<p>Foo</p>\\n<p> <p> </p>\\n<p>Bar</p>\\n$"));
582 QVERIFY(regex.match(result).hasMatch());
585 void TestHtmlOutput::testNewlinesThroughQTextCursor()
587 auto doc =
new QTextDocument(
this);
588 QTextCursor cursor(doc);
589 cursor.movePosition(QTextCursor::Start);
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"));
601 auto regex = QRegularExpression(
602 QStringLiteral(
"^<p>Foo</p>\\n<p> <p> <p>Bar</p>\\n$"));
604 QVERIFY(regex.match(result).hasMatch());
608 #include "htmlbuildertest.moc"
Instructs a builder object to create markup output.
virtual void processDocument(QTextDocument *doc)
The TextHTMLBuilder creates a clean html markup output.
QString getResult() override
The Cutelee namespace holds all public Cutelee API.