tst_lexer.cpp 6.24 KB
Newer Older
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
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
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

30
31
32
#include <cplusplus/Token.h>
#include <cplusplus/SimpleLexer.h>

33
34
35
#include <QtTest>
#include <QDebug>

36
37
//#define DEBUG_TOKENS

38
39
40
typedef QList<unsigned> List;

Q_DECLARE_METATYPE(List)
41

42
43
44
45
46
47
48
49
//TESTED_COMPONENT=src/libs/cplusplus
using namespace CPlusPlus;

class tst_SimpleLexer: public QObject
{
    Q_OBJECT

private slots:
50
51
    void basic();
    void basic_data();
52
53
};

54
void tst_SimpleLexer::basic()
55
56
57
58
59
60
61
62
63
64
65
66
67
{
    QFETCH(QByteArray, source);
    QFETCH(QList<unsigned>, expectedTokenKindList);

    SimpleLexer lexer;
    const QList<Token> tokenList = lexer(source);

    int i = 0;
    for (; i < tokenList.size(); ++i) {
        QVERIFY2(i < expectedTokenKindList.size(), "More tokens than expected.");

        const Token token = tokenList.at(i);
        const unsigned expectedTokenKind = expectedTokenKindList.at(i);
68
69
70
71
72
#ifdef DEBUG_TOKENS
        qDebug("Comparing (i=%d): \"%s\" \"%s\"", i,
               Token::name(token.kind()),
               Token::name(expectedTokenKind));
#endif
73
        QCOMPARE(token.kind(), expectedTokenKind);
74
75
76
77
    }
    QVERIFY2(i == expectedTokenKindList.size(), "Less tokens than expected.");
}

78
void tst_SimpleLexer::basic_data()
79
80
{
    QTest::addColumn<QByteArray>("source");
81
    QTest::addColumn<List>("expectedTokenKindList");
82
83

    QByteArray source;
84
    List expectedTokenKindList;
85
86

    source = "// comment";
87
    expectedTokenKindList = List() << T_CPP_COMMENT;
88
89
90
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "//// comment";
91
    expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
92
93
94
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/// comment";
95
    expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
96
97
98
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "///< comment";
99
    expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
100
101
102
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "//! comment";
103
    expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
104
105
106
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "//!< comment";
107
    expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
108
109
110
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "///\n";
111
    expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
112
    QTest::newRow(source) << source << expectedTokenKindList;
113
114
115

    source = "///\n"
             "int i;";
116
    expectedTokenKindList = List()
117
118
119
        << T_CPP_DOXY_COMMENT
        << T_INT << T_IDENTIFIER << T_SEMICOLON;
    QTest::newRow(source) << source << expectedTokenKindList;
120
121

    source = "/* comment */\n";
122
    expectedTokenKindList = List() << T_COMMENT;
123
124
125
126
127
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/* comment\n"
             "   comment\n"
             " */\n";
128
    expectedTokenKindList = List() << T_COMMENT;
129
130
131
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/** comment */";
132
    expectedTokenKindList = List() << T_DOXY_COMMENT;
133
134
135
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/** comment */\n";
136
    expectedTokenKindList = List() << T_DOXY_COMMENT;
137
138
139
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/** comment */ int i;\n";
140
    expectedTokenKindList = List()
141
142
143
144
145
146
        << T_DOXY_COMMENT << T_INT << T_IDENTIFIER << T_SEMICOLON;
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/**\n"
            "  * comment\n"
             " */\n";
147
    expectedTokenKindList = List() << T_DOXY_COMMENT;
148
149
150
151
152
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/*!\n"
            "  * comment\n"
             " */\n";
153
    expectedTokenKindList = List() << T_DOXY_COMMENT;
154
155
156
157
158
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "/*!\n"
             "    comment\n"
             "*/\n";
159
    expectedTokenKindList = List() << T_DOXY_COMMENT;
160
161
162
163
164
165
166
    QTest::newRow(source) << source << expectedTokenKindList;

    source = "int i; /*!< first counter */\n"
             "int j; /**< second counter */\n"
             "int k; ///< third counter\n"
             "int l; //!< fourth counter\n"
             "       //!< more details...  ";
167
    expectedTokenKindList = List()
168
169
170
171
172
        << T_INT << T_IDENTIFIER << T_SEMICOLON << T_DOXY_COMMENT
        << T_INT << T_IDENTIFIER << T_SEMICOLON << T_DOXY_COMMENT
        << T_INT << T_IDENTIFIER << T_SEMICOLON << T_CPP_DOXY_COMMENT
        << T_INT << T_IDENTIFIER << T_SEMICOLON << T_CPP_DOXY_COMMENT << T_CPP_DOXY_COMMENT;
    QTest::newRow(source) << source << expectedTokenKindList;
173

174
    source = "?" "?(?" "?)?" "?<?" "?>a?b:c";
175
    expectedTokenKindList = List()
176
177
178
179
        << T_LBRACKET << T_RBRACKET << T_LBRACE << T_RBRACE
        << T_IDENTIFIER << T_QUESTION << T_IDENTIFIER << T_COLON << T_IDENTIFIER;
    QTest::newRow(source) << source << expectedTokenKindList;

180
181
182
183
}

QTEST_APPLESS_MAIN(tst_SimpleLexer)
#include "tst_lexer.moc"