Cutelee 6.1.0
plainmarkupbuildertest.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2.1 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include <QtCore/QRegularExpression>
22#include <QtCore>
23#include <QtGui/QTextCursor>
24#include <QtGui/QTextDocument>
25#include <QtGui/QTextLine>
26#include <QtTest/QtTest>
27#include <QtTest/qtestevent.h>
28
29#include "coverageobject.h"
30#include "markupdirector.h"
31#include "plaintextmarkupbuilder.h"
32#include "texthtmlbuilder.h"
33
34using namespace Cutelee;
35
37{
38 Q_OBJECT
39private Q_SLOTS:
40
41 // Test paragraph contents:
42 void testSingleFormat();
43 void testDoubleFormat();
44 void testDoubleStartDifferentFinish();
45 void testDoubleStartDifferentFinishReverseOrder();
46 void testDifferentStartDoubleFinish();
47 void testDifferentStartDoubleFinishReverseOrder();
48 void testOverlap();
49 void testAnchor();
50 void testAnchorWithFormattedContent();
51 void testAdjacentAnchors();
52 void testNestedFormatting();
53 void testSpan();
54 void testDoubleSpan();
55 void testSpanNesting();
56 void testEdgeCaseLeft();
57 void testEdgeCaseRight();
58 void testImage();
59 void testImageResized();
60 void testEachFormatTagSingly();
61 void testHorizontalRule();
62 void testNewlines();
63 void testEmptyParagraphs();
64 void testNewlinesThroughQTextCursor();
65 void testBrInsideParagraph();
66 void testLongDocument();
67};
68
69void TestPlainMarkupOutput::testSingleFormat()
70{
71 auto doc = new QTextDocument();
72
73 // One format
74 doc->setHtml(QStringLiteral("This <b>text</b> is bold."));
75
76 auto hb = new PlainTextMarkupBuilder();
77 auto md = new MarkupDirector(hb);
78 md->processDocument(doc);
79 auto result = hb->getResult();
80 QRegularExpression regex(QStringLiteral("^This \\*text\\* is bold.\\n$"));
81
82 QVERIFY(regex.match(result).hasMatch());
83}
84
85void TestPlainMarkupOutput::testDoubleFormat()
86{
87 auto doc = new QTextDocument();
88
89 // One format
90 doc->setHtml(QStringLiteral("Some <b><i>formatted</i></b> text."));
91
92 auto hb = new PlainTextMarkupBuilder();
93 auto md = new MarkupDirector(hb);
94 md->processDocument(doc);
95 auto result = hb->getResult();
96 QRegularExpression regex(
97 QStringLiteral("^Some (\\*/|/\\*)formatted(\\*/|/\\*) text.\\n$"));
98
99 QVERIFY(regex.match(result).hasMatch());
100}
101
102void TestPlainMarkupOutput::testAnchor()
103{
104 auto doc = new QTextDocument();
105 doc->setHtml(
106 QStringLiteral("A <a href=\"http://www.kde.org\">link</a> to KDE."));
107
108 auto hb = new PlainTextMarkupBuilder();
109 auto md = new MarkupDirector(hb);
110 md->processDocument(doc);
111 auto result = hb->getResult();
112
113 QRegularExpression regex(QStringLiteral(
114 "^A link\\[1\\] to KDE.\\n\\n--------\\n\\[1\\] http://www.kde.org\\n$"));
115
116 regex.match(result).hasMatch();
117
118 QVERIFY(regex.match(result).hasMatch());
119}
120
121void TestPlainMarkupOutput::testAnchorWithFormattedContent()
122{
123 auto doc = new QTextDocument();
124 doc->setHtml(QStringLiteral(
125 "A <a href=\"http://www.kde.org\"><b>formatted</b> link</a> to KDE."));
126
127 auto hb = new PlainTextMarkupBuilder();
128 auto md = new MarkupDirector(hb);
129 md->processDocument(doc);
130 auto result = hb->getResult();
131
132 QRegularExpression regex(
133 QStringLiteral("^A \\*formatted\\* link\\[1\\] to "
134 "KDE.\\n\\n--------\\n\\[1\\] http://www.kde.org\\n$"));
135
136 QVERIFY(regex.match(result).hasMatch());
137}
138
139void TestPlainMarkupOutput::testAdjacentAnchors()
140{
141 auto doc = new QTextDocument();
142 doc->setHtml(
143 QStringLiteral("Two <a href=\"http://www.kde.org\">links</a><a "
144 "href=\"http://www.google.com\">next</a> to eachother."));
145
146 auto hb = new PlainTextMarkupBuilder();
147 auto md = new MarkupDirector(hb);
148 md->processDocument(doc);
149 auto result = hb->getResult();
150
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$"));
154
155 QVERIFY(regex.match(result).hasMatch());
156}
157
158void TestPlainMarkupOutput::testNestedFormatting()
159{
160 auto doc = new QTextDocument();
161 doc->setHtml(QStringLiteral("This <b>text is <i>italic</i> and</b> bold."));
162
163 auto hb = new PlainTextMarkupBuilder();
164 auto md = new MarkupDirector(hb);
165 md->processDocument(doc);
166 auto result = hb->getResult();
167
168 QRegularExpression regex(
169 QStringLiteral("^This \\*text is /italic/ and\\* bold.\\n$"));
170
171 QVERIFY(regex.match(result).hasMatch());
172}
173
174void TestPlainMarkupOutput::testSpan()
175{
176 auto doc = new QTextDocument();
177 doc->setHtml(QStringLiteral(
178 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
179
180 auto hb = new PlainTextMarkupBuilder();
181 auto md = new MarkupDirector(hb);
182 md->processDocument(doc);
183 auto result = hb->getResult();
184
185 auto regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
186
187 QVERIFY(regex.match(result).hasMatch());
188}
189
190void TestPlainMarkupOutput::testDoubleSpan()
191{
192 auto doc = new QTextDocument();
193 doc->setHtml(QStringLiteral("Some <span "
194 "style=\"color:#ff0000;background-color:#00ff00;"
195 "\">formatted</span> text."));
196
197 auto hb = new PlainTextMarkupBuilder();
198 auto md = new MarkupDirector(hb);
199 md->processDocument(doc);
200 auto result = hb->getResult();
201
202 auto regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
203
204 QVERIFY(regex.match(result).hasMatch());
205}
206
207void TestPlainMarkupOutput::testSpanNesting()
208{
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."));
213
214 auto hb = new PlainTextMarkupBuilder();
215 auto md = new MarkupDirector(hb);
216 md->processDocument(doc);
217 auto result = hb->getResult();
218
219 auto regex = QRegularExpression(
220 QStringLiteral("^Paragraph with some formatted nested text.\\n$"));
221
222 QVERIFY(regex.match(result).hasMatch());
223}
224
225void TestPlainMarkupOutput::testDoubleStartDifferentFinish()
226{
227 auto doc = new QTextDocument();
228 doc->setHtml(
229 QStringLiteral("Paragraph <i><b>with</b> some formatted</i> text."));
230
231 auto hb = new PlainTextMarkupBuilder();
232 auto md = new MarkupDirector(hb);
233 md->processDocument(doc);
234 auto result = hb->getResult();
235
236 auto regex = QRegularExpression(
237 QStringLiteral("^Paragraph /\\*with\\* some formatted/ text.\\n$"));
238
239 QVERIFY(regex.match(result).hasMatch());
240}
241
242void TestPlainMarkupOutput::testDoubleStartDifferentFinishReverseOrder()
243{
244 auto doc = new QTextDocument();
245 doc->setHtml(
246 QStringLiteral("Paragraph <b><i>with</i> some formatted</b> text."));
247
248 auto hb = new PlainTextMarkupBuilder();
249 auto md = new MarkupDirector(hb);
250 md->processDocument(doc);
251 auto result = hb->getResult();
252
253 auto regex = QRegularExpression(
254 QStringLiteral("^Paragraph \\*/with/ some formatted\\* text.\\n$"));
255
256 QVERIFY(regex.match(result).hasMatch());
257}
258
259void TestPlainMarkupOutput::testDifferentStartDoubleFinish()
260{
261 auto doc = new QTextDocument();
262 doc->setHtml(
263 QStringLiteral("Paragraph <i>with some <b>formatted<b></i> text."));
264
265 auto hb = new PlainTextMarkupBuilder();
266 auto md = new MarkupDirector(hb);
267 md->processDocument(doc);
268 auto result = hb->getResult();
269
270 auto regex = QRegularExpression(
271 QStringLiteral("^Paragraph /with some \\*formatted\\*/ text.\\n$"));
272
273 QVERIFY(regex.match(result).hasMatch());
274}
275
276void TestPlainMarkupOutput::testDifferentStartDoubleFinishReverseOrder()
277{
278 auto doc = new QTextDocument();
279 doc->setHtml(
280 QStringLiteral("Paragraph <b>with some <i>formatted</i></b> text."));
281
282 auto hb = new PlainTextMarkupBuilder();
283 auto md = new MarkupDirector(hb);
284 md->processDocument(doc);
285 auto result = hb->getResult();
286
287 auto regex = QRegularExpression(
288 QStringLiteral("^Paragraph \\*with some /formatted/\\* text.\\n$"));
289
290 QVERIFY(regex.match(result).hasMatch());
291}
292
293void TestPlainMarkupOutput::testOverlap()
294{
295 auto doc = new QTextDocument();
296 doc->setHtml(QStringLiteral(
297 "Paragraph <b>with <i>some</i></b><i> formatted</i> text."));
298
299 auto hb = new PlainTextMarkupBuilder();
300 auto md = new MarkupDirector(hb);
301 md->processDocument(doc);
302 auto result = hb->getResult();
303
304 auto regex = QRegularExpression(
305 QStringLiteral("^Paragraph \\*with /some/\\*/ formatted/ text.\\n$"));
306
307 QVERIFY(regex.match(result).hasMatch());
308}
309
310void TestPlainMarkupOutput::testEdgeCaseLeft()
311{
312 auto doc = new QTextDocument();
313 doc->setHtml(QStringLiteral("Paragraph <b>with some formatted text.</b>"));
314
315 auto hb = new PlainTextMarkupBuilder();
316 auto md = new MarkupDirector(hb);
317 md->processDocument(doc);
318 auto result = hb->getResult();
319
320 auto regex = QRegularExpression(
321 QStringLiteral("^Paragraph \\*with some formatted text.\\*\\n$"));
322
323 QVERIFY(regex.match(result).hasMatch());
324}
325
326void TestPlainMarkupOutput::testEdgeCaseRight()
327{
328 auto doc = new QTextDocument();
329 doc->setHtml(QStringLiteral("<b>Paragraph with some formatted</b> text."));
330
331 auto hb = new PlainTextMarkupBuilder();
332 auto md = new MarkupDirector(hb);
333 md->processDocument(doc);
334 auto result = hb->getResult();
335
336 auto regex = QRegularExpression(
337 QStringLiteral("^\\*Paragraph with some formatted\\* text.\\n$"));
338
339 QVERIFY(regex.match(result).hasMatch());
340}
341
342void TestPlainMarkupOutput::testImage()
343{
344 auto doc = new QTextDocument();
345 doc->setHtml(
346 QStringLiteral("Paragraph with an inline <img "
347 "src=\"http://kde.org/img/kde41.png\" /> image."));
348
349 auto hb = new PlainTextMarkupBuilder();
350 auto md = new MarkupDirector(hb);
351 md->processDocument(doc);
352 auto result = hb->getResult();
353
354 auto regex = QRegularExpression(QStringLiteral(
355 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
356 "http://kde.org/img/kde41.png\\n$"));
357
358 QVERIFY(regex.match(result).hasMatch());
359}
360
361void TestPlainMarkupOutput::testImageResized()
362{
363 QString result;
364 QRegularExpression regex;
366 MarkupDirector *md;
367 auto doc = new QTextDocument();
368
369 // width
370 doc->setHtml(QStringLiteral(
371 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
372 "width=\"10\" /> image."));
373
374 hb = new PlainTextMarkupBuilder();
375 md = new MarkupDirector(hb);
376 md->processDocument(doc);
377 result = hb->getResult();
378
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());
383
384 // height
385 doc->setHtml(QStringLiteral(
386 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
387 "height=\"10\" /> image."));
388
389 hb = new PlainTextMarkupBuilder();
390 md = new MarkupDirector(hb);
391 md->processDocument(doc);
392 result = hb->getResult();
393
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());
398
399 // height and width
400 doc->setHtml(QStringLiteral(
401 "Paragraph with an inline <img src=\"http://kde.org/img/kde41.png\" "
402 "height=\"10\" width=\"10\" /> image."));
403
404 hb = new PlainTextMarkupBuilder();
405 md = new MarkupDirector(hb);
406 md->processDocument(doc);
407 result = hb->getResult();
408
409 regex = QRegularExpression(QStringLiteral(
410 "^Paragraph with an inline \\[1\\] image.\\n\\n--------\\n\\[1\\] "
411 "http://kde.org/img/kde41.png\\n$"));
412
413 QVERIFY(regex.match(result).hasMatch());
414}
415
416void TestPlainMarkupOutput::testEachFormatTagSingly()
417{
418 QString result;
419 QRegularExpression regex;
421 MarkupDirector *md;
422 auto doc = new QTextDocument();
423
424 // Test bold
425 doc->setHtml(QStringLiteral("Some <b>formatted</b> text."));
426 hb = new PlainTextMarkupBuilder();
427 md = new MarkupDirector(hb);
428 md->processDocument(doc);
429 result = hb->getResult();
430
431 regex = QRegularExpression(QStringLiteral("^Some \\*formatted\\* text.\\n$"));
432 QVERIFY(regex.match(result).hasMatch());
433
434 // Test Italic
435 doc->setHtml(QStringLiteral("Some <i>formatted</i> text."));
436 hb = new PlainTextMarkupBuilder();
437 md = new MarkupDirector(hb);
438 md->processDocument(doc);
439 result = hb->getResult();
440
441 regex = QRegularExpression(QStringLiteral("^Some /formatted/ text.\\n$"));
442 QVERIFY(regex.match(result).hasMatch());
443
444 // Test Underline
445 doc->setHtml(QStringLiteral("Some <u>formatted</u> text."));
446 hb = new PlainTextMarkupBuilder();
447 md = new MarkupDirector(hb);
448 md->processDocument(doc);
449 result = hb->getResult();
450
451 regex = QRegularExpression(QStringLiteral("^Some _formatted_ text.\\n$"));
452 QVERIFY(regex.match(result).hasMatch());
453
454 // Test Strikeout
455 doc->setHtml(QStringLiteral("Some <s>formatted</s> text."));
456 hb = new PlainTextMarkupBuilder();
457 md = new MarkupDirector(hb);
458 md->processDocument(doc);
459 result = hb->getResult();
460
461 regex = QRegularExpression(QStringLiteral("^Some -formatted- text.\\n$"));
462 QVERIFY(regex.match(result).hasMatch());
463
464 // Test Superscript
465 doc->setHtml(QStringLiteral("Some <sup>formatted</sup> text."));
466 hb = new PlainTextMarkupBuilder();
467 md = new MarkupDirector(hb);
468 md->processDocument(doc);
469 result = hb->getResult();
470
471 regex = QRegularExpression(
472 QStringLiteral("^Some \\^\\{formatted\\} text.\\n$"));
473 QVERIFY(regex.match(result).hasMatch());
474
475 // Test Subscript
476 doc->setHtml(QStringLiteral("Some <sub>formatted</sub> text."));
477 hb = new PlainTextMarkupBuilder();
478 md = new MarkupDirector(hb);
479 md->processDocument(doc);
480 result = hb->getResult();
481
482 regex
483 = QRegularExpression(QStringLiteral("^Some _\\{formatted\\} text.\\n$"));
484 QVERIFY(regex.match(result).hasMatch());
485
486 // Test Foreground
487 doc->setHtml(QStringLiteral(
488 "Some <span style=\"color:#ff0000;\">formatted</span> text."));
489 hb = new PlainTextMarkupBuilder();
490 md = new MarkupDirector(hb);
491 md->processDocument(doc);
492 result = hb->getResult();
493
494 regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
495 QVERIFY(regex.match(result).hasMatch());
496
497 // Test Background
498 doc->setHtml(QStringLiteral(
499 "Some <span style=\"background-color:#ff0000;\">formatted</span> text."));
500 hb = new PlainTextMarkupBuilder();
501 md = new MarkupDirector(hb);
502 md->processDocument(doc);
503 result = hb->getResult();
504
505 regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
506 QVERIFY(regex.match(result).hasMatch());
507
508 // Test Font Family
509 doc->setHtml(QStringLiteral(
510 "Some <span style=\"font-family:courier;\">formatted</span> text."));
511 hb = new PlainTextMarkupBuilder();
512 md = new MarkupDirector(hb);
513 md->processDocument(doc);
514 result = hb->getResult();
515
516 regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
517 QVERIFY(regex.match(result).hasMatch());
518
519 // Test Font Size
520 doc->setHtml(QStringLiteral(
521 "Some <span style=\"font-size:20pt;\">formatted</span> text."));
522 hb = new PlainTextMarkupBuilder();
523 md = new MarkupDirector(hb);
524 md->processDocument(doc);
525 result = hb->getResult();
526
527 regex = QRegularExpression(QStringLiteral("^Some formatted text.\\n$"));
528 QVERIFY(regex.match(result).hasMatch());
529}
530
531void TestPlainMarkupOutput::testHorizontalRule()
532{
533 auto doc = new QTextDocument();
534 doc->setHtml(
535 QStringLiteral("<p style=\"margin-top:0;margin-bottom:0;\">Foo</p><hr "
536 "/><p style=\"margin-top:0;margin-bottom:0;\">Bar</p>"));
537
538 auto hb = new PlainTextMarkupBuilder();
539 auto md = new MarkupDirector(hb);
540 md->processDocument(doc);
541 auto result = hb->getResult();
542
543 auto regex = QRegularExpression(
544 QStringLiteral("^Foo\\n--------------------\\nBar\\n$"));
545
546 QVERIFY(regex.match(result).hasMatch());
547}
548
549void TestPlainMarkupOutput::testNewlines()
550{
551 auto doc = new QTextDocument();
552 doc->setHtml(QStringLiteral("<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
553
554 auto hb = new PlainTextMarkupBuilder();
555 auto md = new MarkupDirector(hb);
556 md->processDocument(doc);
557 auto result = hb->getResult();
558
559 auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
560
561 QVERIFY(regex.match(result).hasMatch());
562}
563
564void TestPlainMarkupOutput::testEmptyParagraphs()
565{
566 auto doc = new QTextDocument();
567 doc->setHtml(QStringLiteral("<p>Foo</p>\n<br /><br />\n<p>Bar</p>"));
568
569 auto hb = new PlainTextMarkupBuilder();
570 auto md = new MarkupDirector(hb);
571 md->processDocument(doc);
572 auto result = hb->getResult();
573
574 auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
575
576 QVERIFY(regex.match(result).hasMatch());
577}
578
579void TestPlainMarkupOutput::testNewlinesThroughQTextCursor()
580{
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"));
589
590 auto hb = new PlainTextMarkupBuilder();
591 auto md = new MarkupDirector(hb);
592 md->processDocument(doc);
593 auto result = hb->getResult();
594
595 auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
596
597 QVERIFY(regex.match(result).hasMatch());
598}
599
600void TestPlainMarkupOutput::testBrInsideParagraph()
601{
602
603 auto doc = new QTextDocument();
604 doc->setHtml(QStringLiteral("<p>Foo<br /><br /><br />Bar</p>"));
605
606 auto hb = new PlainTextMarkupBuilder();
607 auto md = new MarkupDirector(hb);
608 md->processDocument(doc);
609 auto result = hb->getResult();
610
611 // Two paragraphs separated by two line breaks
612
613 auto regex = QRegularExpression(QStringLiteral("^Foo\\n\\n\\nBar\\n$"));
614
615 QVERIFY(regex.match(result).hasMatch());
616}
617
618void TestPlainMarkupOutput::testLongDocument()
619{
620 QTextDocument doc;
621
622 QFile sourceHtml(QFINDTESTDATA("sourcehtml"));
623 QVERIFY(sourceHtml.open(QIODevice::ReadOnly));
624 doc.setHtml(QString::fromLatin1(sourceHtml.readAll().constData()));
625
626 auto hb = new PlainTextMarkupBuilder();
627 auto md = new MarkupDirector(hb);
628 md->processDocument(&doc);
629 auto result = hb->getResult();
630 QVERIFY2(
631 result.startsWith(QLatin1String("Hello,\nThis is some text\nIt shows how "
632 "cutelee is used from kmail\n")),
633 qPrintable(result));
634 QVERIFY2(result.endsWith(QLatin1String("This is the end of the signature\n")),
635 qPrintable(result));
636 // qDebug() << result;
637}
638
639QTEST_MAIN(TestPlainMarkupOutput)
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.
The Cutelee namespace holds all public Cutelee API.
Definition Mainpage.dox:8