aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/linebylinelex/tst_linebylinelex.cpp
blob: 0d1ad180b928245fb481a021ba97040308ec1cb5 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtTest/QtTest>
#include <QtQuickTestUtils/private/qmlutils_p.h>
#include <QtCore/qplugin.h>
#include <QtQml/private/qqmljslexer_p.h>

QT_USE_NAMESPACE
using namespace Qt::StringLiterals;
using namespace QQmlJS;

class TestLineByLineLex : public QQmlDataTest
{
    Q_OBJECT

public:
    TestLineByLineLex();

private Q_SLOTS:
    void initTestCase() override;

    void testLineByLineLex_data();
    void testLineByLineLex();

    void testFormatter_data();
    void testFormatter();

private:
    void runLex(const QString &fileToLex);

    QString m_qmljsrootgenPath;
    QString m_qmltyperegistrarPath;
    QString m_baseDir;
};

TestLineByLineLex::TestLineByLineLex()
    : QQmlDataTest(QT_QMLTEST_DATADIR), m_baseDir(QString::fromLocal8Bit(QT_QMLTEST_DATADIR))
{
}

void TestLineByLineLex::initTestCase()
{
    QQmlDataTest::initTestCase();
}

void TestLineByLineLex::testLineByLineLex_data()
{
    QTest::addColumn<QString>("filename");
    QTest::newRow("Simple_QML") << QStringLiteral("Simple.qml");
    QTest::newRow("QML_importing_JS") << QStringLiteral("importing_js.qml");
    QTest::newRow("JS_with_pragma_and_import") << QStringLiteral("QTBUG-45916.js");
}

void TestLineByLineLex::testLineByLineLex()
{
    QFETCH(QString, filename);

    QString filePath = m_baseDir + u"/linebylinelex/data/"_s + filename;
    runLex(filePath);
}

void TestLineByLineLex::testFormatter_data()
{
    QTest::addColumn<QString>("filename");
    QDir formatData(m_baseDir + u"/qmlformat/data"_s);
    for (const QFileInfo &fInfo :
         formatData.entryInfoList(QStringList({ u"*.qml"_s, u"*.js"_s }), QDir::Files)) {
        QTest::newRow(qPrintable(fInfo.fileName())) << fInfo.absoluteFilePath();
    }
}

void TestLineByLineLex::testFormatter()
{
    QFETCH(QString, filename);

    runLex(filename);
}

void TestLineByLineLex::runLex(const QString &fileToLex)
{
    QFile f(fileToLex);
    QVERIFY2(f.open(QFile::ReadOnly), qPrintable(fileToLex));
    QString contents = QString::fromUtf8(f.readAll());
    bool isQml = fileToLex.endsWith(u".qml"_s);
    QQmlJS::Lexer lexer(nullptr);
    lexer.setCode(contents, 1, isQml, QQmlJS::Lexer::CodeContinuation::Reset);
    f.seek(0);
    QString line = QString::fromUtf8(f.readLine());
    QQmlJS::Lexer llLexer(nullptr, QQmlJS::Lexer::LexMode::LineByLine);
    QQmlJS::Lexer::State oldState = llLexer.state();
    llLexer.setCode(line, 1, isQml, QQmlJS::Lexer::CodeContinuation::Reset);
    int iLine = 0;
    int tokenKind = lexer.lex();
    QList<int> extraTokens({ QQmlJSGrammar::T_COMMENT, QQmlJSGrammar::T_PARTIAL_COMMENT,
                             QQmlJSGrammar::T_PARTIAL_DOUBLE_QUOTE_STRING_LITERAL,
                             QQmlJSGrammar::T_PARTIAL_SINGLE_QUOTE_STRING_LITERAL,
                             QQmlJSGrammar::T_PARTIAL_TEMPLATE_HEAD,
                             QQmlJSGrammar::T_PARTIAL_TEMPLATE_MIDDLE });
    while (!line.isEmpty()) {
        QQmlJS::Lexer llLexer2(nullptr, QQmlJS::Lexer::LexMode::LineByLine);
        llLexer2.setState(oldState);
        ++iLine;
        llLexer2.setCode(line, iLine, isQml,
                         ((iLine == 1) ? QQmlJS::Lexer::CodeContinuation::Reset
                                       : QQmlJS::Lexer::CodeContinuation::Continue));
        int llTokenKind = llLexer.lex();
        int ll2TokenKind = llLexer2.lex();
        QCOMPARE(llTokenKind, ll2TokenKind);
        QCOMPARE(llLexer.state(), llLexer2.state());
        while (llTokenKind != QQmlJSGrammar::T_EOL) {
            if (!extraTokens.contains(llTokenKind)) {
                QCOMPARE(llTokenKind, tokenKind);
                QCOMPARE(llLexer.state(), lexer.state());
                tokenKind = lexer.lex();
            }
            llTokenKind = llLexer.lex();
            ll2TokenKind = llLexer2.lex();
            QCOMPARE(llTokenKind, ll2TokenKind);
            QCOMPARE(llLexer.state(), llLexer2.state());
        }
        oldState = llLexer.state();
        line = QString::fromUtf8(f.readLine());
        llLexer.setCode(line, -1, isQml, QQmlJS::Lexer::CodeContinuation::Continue);
    }
    QCOMPARE(llLexer.lex(), QQmlJSGrammar::EOF_SYMBOL);
    if (tokenKind != QQmlJSGrammar::EOF_SYMBOL)
        tokenKind = lexer.lex();
    QCOMPARE(tokenKind, QQmlJSGrammar::EOF_SYMBOL);
}

QTEST_MAIN(TestLineByLineLex)
#include "tst_linebylinelex.moc"