texthtmlbuildertest.cpp 3.25 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
   Copyright (C) 2020 Laurent Montel <montel@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "texthtmlbuildertest.h"
#include "grantleebuilder/texthtmlbuilder.h"
#include <QRegularExpression>
#include <QTest>
#include <QTextDocument>
#include <grantlee/markupdirector.h>
QTEST_MAIN(TextHTMLBuilderTest)
TextHTMLBuilderTest::TextHTMLBuilderTest(QObject *parent)
    : QObject(parent)
{

}

Laurent Montel's avatar
Laurent Montel committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void TextHTMLBuilderTest::testHtmlWithTab()
{
    auto doc = new QTextDocument(this);
    QTextCursor cursor(doc);
    cursor.movePosition(QTextCursor::Start);
    cursor.insertText(QStringLiteral("\n"));
    cursor.insertText(QStringLiteral("\t"));
    cursor.insertText(QStringLiteral("foo"));

    auto hb = new KPIMTextEdit::TextHTMLBuilder();
    auto md = new Grantlee::MarkupDirector(hb);
    md->processDocument(doc);
    auto result = hb->getResult();

    auto regex = QRegularExpression(
        QStringLiteral("^<p>Foo</p>\\n<p>&nbsp;<p>&nbsp;<p>Bar</p>\\n$"));

    const bool regexpHasResult = regex.match(result).hasMatch();
    if (!regexpHasResult) {
        qDebug() << " result found " << result;
    }
    QVERIFY(regexpHasResult);
    delete md;
    delete hb;
    delete doc;

}

Laurent Montel's avatar
Laurent Montel committed
61
62
63
64
void TextHTMLBuilderTest::testHtmlText_data()
{
    QTest::addColumn<QString>("text");
    QTest::addColumn<QString>("regexpText");
Laurent Montel's avatar
Laurent Montel committed
65
66
67
68
    QTest::addColumn<bool>("htmlFormat");
    QTest::newRow("link") <<  QStringLiteral("A <a href=\"http://www.kde.org\">link</a> to KDE.") << QStringLiteral("^<p>A <a href=\"http://www.kde.org\">link</a>&nbsp;to KDE.</p>\\n$") << true;
    QTest::newRow("text with espace") <<  QStringLiteral("         foo") << QStringLiteral("^<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foo</p>\\n$") << false;
    QTest::newRow("text with espace at begin") <<  QStringLiteral(" foo") << QStringLiteral("^<p>&nbsp;foo</p>\\n$") << false;
Laurent Montel's avatar
Laurent Montel committed
69
70
}

Laurent Montel's avatar
Laurent Montel committed
71
72
void TextHTMLBuilderTest::testHtmlText()
{
Laurent Montel's avatar
Laurent Montel committed
73
74
    QFETCH(QString, text);
    QFETCH(QString, regexpText);
Laurent Montel's avatar
Laurent Montel committed
75
    QFETCH(bool, htmlFormat);
Laurent Montel's avatar
Laurent Montel committed
76

Laurent Montel's avatar
Laurent Montel committed
77
    auto doc = new QTextDocument();
Laurent Montel's avatar
Laurent Montel committed
78
79
80
81
82
    if (htmlFormat) {
        doc->setHtml(text);
    } else {
        doc->setPlainText(text);
    }
Laurent Montel's avatar
Laurent Montel committed
83
84
85
86

    auto hb = new KPIMTextEdit::TextHTMLBuilder();
    auto md = new Grantlee::MarkupDirector(hb);
    md->processDocument(doc);
Laurent Montel's avatar
Laurent Montel committed
87
    const auto result = hb->getResult();
Laurent Montel's avatar
Laurent Montel committed
88

Laurent Montel's avatar
Laurent Montel committed
89
    const QRegularExpression regex(regexpText);
Laurent Montel's avatar
Laurent Montel committed
90

Laurent Montel's avatar
Laurent Montel committed
91
92
93
94
95
    const bool regexpHasResult = regex.match(result).hasMatch();
    if (!regexpHasResult) {
        qDebug() << " result found " << result;
    }
    QVERIFY(regexpHasResult);
Laurent Montel's avatar
Laurent Montel committed
96
97
98
99
    delete md;
    delete hb;
    delete doc;
}