File: foldingtest.cpp

package info (click to toggle)
kf6-syntax-highlighting 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 47,568 kB
  • sloc: xml: 197,750; cpp: 12,850; python: 3,023; sh: 955; perl: 546; ruby: 488; pascal: 393; javascript: 161; php: 150; jsp: 132; lisp: 131; haskell: 124; ada: 119; ansic: 107; makefile: 96; f90: 94; ml: 85; cobol: 81; yacc: 71; csh: 62; erlang: 54; sql: 51; java: 47; objc: 37; awk: 31; asm: 30; tcl: 29; fortran: 18; cs: 10
file content (209 lines) | stat: -rw-r--r-- 6,576 bytes parent folder | download
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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include "test-config.h"

#include <KSyntaxHighlighting/AbstractHighlighter>
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/FoldingRegion>
#include <KSyntaxHighlighting/Repository>
#include <KSyntaxHighlighting/State>

#include <QDir>
#include <QFile>
#include <QObject>
#include <QStandardPaths>
#include <QTest>
#include <QTextStream>

#include <unordered_map>

using namespace KSyntaxHighlighting;

class FoldingHighlighter : public AbstractHighlighter
{
public:
    void highlightFile(const QString &inFileName, const QString &outFileName)
    {
        QFile outFile(outFileName);
        if (!outFile.open(QFile::WriteOnly | QFile::Truncate)) {
            qWarning() << "Failed to open output file" << outFileName << ":" << outFile.errorString();
            return;
        }
        m_out.setDevice(&outFile);

        QFile f(inFileName);
        if (!f.open(QFile::ReadOnly)) {
            qWarning() << "Failed to open input file" << inFileName << ":" << f.errorString();
            return;
        }

        QTextStream in(&f);
        State state;
        bool indentationFoldEnabled = definition().indentationBasedFoldingEnabled();
        if (indentationFoldEnabled) {
            m_out << "<indentfold>";
        }
        while (!in.atEnd()) {
            const auto currentLine = in.readLine();
            state = highlightLine(currentLine, state);

            if (indentationFoldEnabled != state.indentationBasedFoldingEnabled()) {
                indentationFoldEnabled = state.indentationBasedFoldingEnabled();
                if (indentationFoldEnabled) {
                    m_out << "<indentfold>";
                } else {
                    m_out << "</indentfold>";
                }
            }

            int offset = 0;
            for (const auto &fold : std::as_const(m_folds)) {
                // use stable ids for output, see below docs for m_stableFoldingIds
                const auto stableId = m_stableFoldingIds[fold.region.id()];
                m_out << currentLine.mid(offset, fold.offset - offset);
                if (fold.region.type() == FoldingRegion::Begin) {
                    m_out << "<beginfold id='" << stableId << "'>";
                } else {
                    m_out << "<endfold id='" << stableId << "'>";
                }
                m_out << currentLine.mid(fold.offset, fold.length);
                if (fold.region.type() == FoldingRegion::Begin) {
                    m_out << "</beginfold id='" << stableId << "'>";
                } else {
                    m_out << "</endfold id='" << stableId << "'>";
                }
                offset = fold.offset + fold.length;
            }
            m_out << currentLine.mid(offset) << '\n';
            m_folds.clear();
        }

        m_out.flush();
    }

protected:
    void applyFormat(int offset, int length, const Format &format) override
    {
        Q_UNUSED(offset);
        Q_UNUSED(length);
        Q_UNUSED(format);
    }

    void applyFolding(int offset, int length, FoldingRegion region) override
    {
        Q_ASSERT(region.isValid());
        m_folds.push_back({offset, length, region});

        // create stable id if needed, see below m_stableFoldingIds docs for details
        // start with 1
        m_stableFoldingIds.emplace(region.id(), m_stableFoldingIds.size() + 1);
    }

private:
    QTextStream m_out;
    struct Fold {
        int offset;
        int length;
        FoldingRegion region;
    };
    QList<Fold> m_folds;

    // we use one repository for all tests
    // => the folding ids might change even if just unrelated highlighings are added
    // => construct some stable id per test based on occurrence of id
    std::unordered_map<uint32_t, size_t> m_stableFoldingIds;
};

class FoldingTest : public QObject
{
    Q_OBJECT
public:
    explicit FoldingTest(QObject *parent = nullptr)
        : QObject(parent)
        , m_repo(nullptr)
    {
    }

private:
    Repository *m_repo;

private Q_SLOTS:
    void initTestCase()
    {
        QStandardPaths::setTestModeEnabled(true);
        m_repo = new Repository;
        initRepositorySearchPaths(*m_repo);
    }

    void cleanupTestCase()
    {
        delete m_repo;
        m_repo = nullptr;
    }

    void testFolding_data()
    {
        QTest::addColumn<QString>("inFile");
        QTest::addColumn<QString>("outFile");
        QTest::addColumn<QString>("refFile");
        QTest::addColumn<QString>("syntax");

        const QDir dir(QStringLiteral(TESTSRCDIR "/input"));
        for (const auto &fileName : dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable | QDir::Hidden, QDir::Name)) {
            // skip .clang-format file we use to avoid formatting test files
            if (fileName == QLatin1String(".clang-format")) {
                continue;
            }

            const auto inFile = dir.absoluteFilePath(fileName);
            if (inFile.endsWith(QLatin1String(".syntax"))) {
                continue;
            }

            QString syntax;
            QFile syntaxOverride(inFile + QStringLiteral(".syntax"));
            if (syntaxOverride.exists() && syntaxOverride.open(QFile::ReadOnly)) {
                syntax = QString::fromUtf8(syntaxOverride.readAll()).trimmed();
            }

            QTest::newRow(fileName.toUtf8().constData()) << inFile << (QStringLiteral(TESTBUILDDIR "/folding.out/") + fileName + QStringLiteral(".fold"))
                                                         << (QStringLiteral(TESTSRCDIR "/folding/") + fileName + QStringLiteral(".fold")) << syntax;
        }

        // cleanup before we test
        QDir(QStringLiteral(TESTBUILDDIR "/folding.out/")).removeRecursively();
        QDir().mkpath(QStringLiteral(TESTBUILDDIR "/folding.out/"));
    }

    void testFolding()
    {
        QFETCH(QString, inFile);
        QFETCH(QString, outFile);
        QFETCH(QString, refFile);
        QFETCH(QString, syntax);
        QVERIFY(m_repo);

        auto def = m_repo->definitionForFileName(inFile);
        if (!syntax.isEmpty()) {
            def = m_repo->definitionForName(syntax);
        }

        FoldingHighlighter highlighter;
        QVERIFY(def.isValid());
        highlighter.setDefinition(def);
        highlighter.highlightFile(inFile, outFile);

        /**
         * compare results
         */
        compareFiles(refFile, outFile);
    }
};

QTEST_GUILESS_MAIN(FoldingTest)

#include "foldingtest.moc"