File: test-config.h.in

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 (93 lines) | stat: -rw-r--r-- 2,737 bytes parent folder | download | duplicates (3)
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
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#ifndef TEST_CONFIG_H
#define TEST_CONFIG_H

#include <QFile>
#include <QProcess>
#include <QStandardPaths>
#include <QString>
#include <QTextStream>
#include <QTest>

#include <KSyntaxHighlighting/Repository>

#define TESTSRCDIR "@CMAKE_CURRENT_SOURCE_DIR@"
#define TESTBUILDDIR "@CMAKE_CURRENT_BINARY_DIR@"

/**
 * init repository with right search paths, is needed if the files are not compiled in
 * as resources
 *
 * @param repository repository to init search paths
 */
inline void initRepositorySearchPaths(KSyntaxHighlighting::Repository &repository)
{
    // add extra search path for e.g. broken highlighting test
    repository.addCustomSearchPath(QStringLiteral(TESTSRCDIR "/input"));

    // handle no-resources case
#ifndef HAS_SYNTAX_RESOURCE
    repository.addCustomSearchPath(QStringLiteral("@CMAKE_SOURCE_DIR@/data"));
    repository.addCustomSearchPath(QStringLiteral("@CMAKE_BINARY_DIR@/data/generated"));
#endif
}

/**
 * helper to compare files
 * @param refFile reference file
 * @param outFile output file
 */
inline void compareFiles(const QString &refFile, const QString &outFile)
{
    /**
     * quick compare, all fine, if no diffs!
     * use text mode + text streams to avoid unix/windows mismatches
     */
    QFile ref(refFile);
    QFile out(outFile);
    QVERIFY(ref.open(QIODevice::ReadOnly | QIODevice::Text));
    QVERIFY(out.open(QIODevice::ReadOnly | QIODevice::Text));
    QTextStream refIn(&ref);
    QTextStream outIn(&out);
    const QString refContent = refIn.readAll();
    const QString outContent = outIn.readAll();
    const bool equalResults = refContent == outContent;
    if (equalResults) {
        return;
    }

    /**
     * elaborate diff output, if possible
     */
    const QString diffExecutable = QStandardPaths::findExecutable(QStringLiteral("diff"));
    if (!diffExecutable.isEmpty()) {
        QProcess proc;
        proc.setProcessChannelMode(QProcess::ForwardedChannels);
        proc.start(diffExecutable, {QStringLiteral("-u"), refFile, outFile});
        QVERIFY(proc.waitForFinished());
        QCOMPARE(proc.exitCode(), 0);
    }

    /**
     * else: trivial output of mismatching characters, e.g. for windows testing without diff
     */
    else {
        qDebug() << "Trivial differences output as the 'diff' executable is not in the PATH";
        for (int i = 0; i < refContent.size() && i < outContent.size(); ++i) {
            QCOMPARE(refContent[i], outContent[i]);
        }
        QCOMPARE(refContent.size(), outContent.size());
    }

    /**
     * if we arrive here, all lost!
     */
    QVERIFY(equalResults);
}

#endif // TEST_CONFIG_H