2013-02-06 14:58:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-02-06 14:58:09 +01:00
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <cplusplus/Token.h>
|
|
|
|
#include <cplusplus/SimpleLexer.h>
|
|
|
|
|
2013-02-06 14:58:09 +01:00
|
|
|
#include <QtTest>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2014-01-19 22:26:23 +02:00
|
|
|
//#define DEBUG_TOKENS
|
|
|
|
|
2014-01-18 20:38:18 +02:00
|
|
|
typedef QList<unsigned> List;
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(List)
|
2013-02-25 18:32:10 +01:00
|
|
|
|
2013-02-06 14:58:09 +01:00
|
|
|
//TESTED_COMPONENT=src/libs/cplusplus
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
|
|
class tst_SimpleLexer: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
2013-12-10 10:17:39 +01:00
|
|
|
void basic();
|
|
|
|
void basic_data();
|
2013-02-06 14:58:09 +01:00
|
|
|
};
|
|
|
|
|
2013-12-10 10:17:39 +01:00
|
|
|
void tst_SimpleLexer::basic()
|
2013-02-06 14:58:09 +01:00
|
|
|
{
|
|
|
|
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);
|
2014-01-19 22:26:23 +02:00
|
|
|
#ifdef DEBUG_TOKENS
|
|
|
|
qDebug("Comparing (i=%d): \"%s\" \"%s\"", i,
|
|
|
|
Token::name(token.kind()),
|
|
|
|
Token::name(expectedTokenKind));
|
|
|
|
#endif
|
2013-03-11 15:01:10 +01:00
|
|
|
QCOMPARE(token.kind(), expectedTokenKind);
|
2013-02-06 14:58:09 +01:00
|
|
|
}
|
|
|
|
QVERIFY2(i == expectedTokenKindList.size(), "Less tokens than expected.");
|
|
|
|
}
|
|
|
|
|
2013-12-10 10:17:39 +01:00
|
|
|
void tst_SimpleLexer::basic_data()
|
2013-02-06 14:58:09 +01:00
|
|
|
{
|
|
|
|
QTest::addColumn<QByteArray>("source");
|
2014-01-18 20:38:18 +02:00
|
|
|
QTest::addColumn<List>("expectedTokenKindList");
|
2013-02-06 14:58:09 +01:00
|
|
|
|
|
|
|
QByteArray source;
|
2014-01-18 20:38:18 +02:00
|
|
|
List expectedTokenKindList;
|
2013-02-06 14:58:09 +01:00
|
|
|
|
|
|
|
source = "// comment";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_CPP_COMMENT;
|
2013-02-06 14:58:09 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "//// comment";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
|
2013-02-06 14:58:09 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/// comment";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
|
2013-02-06 14:58:09 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "///< comment";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
|
2013-02-06 14:58:09 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "//! comment";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
|
2013-02-06 14:58:09 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "//!< comment";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
|
2013-02-06 14:58:09 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "///\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_CPP_DOXY_COMMENT;
|
2013-02-06 14:58:09 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
2013-03-11 15:01:10 +01:00
|
|
|
|
|
|
|
source = "///\n"
|
|
|
|
"int i;";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List()
|
2013-03-11 15:01:10 +01:00
|
|
|
<< T_CPP_DOXY_COMMENT
|
|
|
|
<< T_INT << T_IDENTIFIER << T_SEMICOLON;
|
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
2013-03-12 08:51:22 +01:00
|
|
|
|
|
|
|
source = "/* comment */\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_COMMENT;
|
2013-03-12 08:51:22 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/* comment\n"
|
|
|
|
" comment\n"
|
|
|
|
" */\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_COMMENT;
|
2013-03-12 08:51:22 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/** comment */";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_DOXY_COMMENT;
|
2013-03-12 08:51:22 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/** comment */\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_DOXY_COMMENT;
|
2013-03-12 08:51:22 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/** comment */ int i;\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List()
|
2013-03-12 08:51:22 +01:00
|
|
|
<< T_DOXY_COMMENT << T_INT << T_IDENTIFIER << T_SEMICOLON;
|
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/**\n"
|
|
|
|
" * comment\n"
|
|
|
|
" */\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_DOXY_COMMENT;
|
2013-03-12 08:51:22 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/*!\n"
|
|
|
|
" * comment\n"
|
|
|
|
" */\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_DOXY_COMMENT;
|
2013-03-12 08:51:22 +01:00
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
|
|
|
source = "/*!\n"
|
|
|
|
" comment\n"
|
|
|
|
"*/\n";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List() << T_DOXY_COMMENT;
|
2013-03-12 08:51:22 +01:00
|
|
|
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... ";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List()
|
2013-03-12 08:51:22 +01:00
|
|
|
<< 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;
|
2013-11-26 15:23:28 +01:00
|
|
|
|
2013-12-03 10:30:05 +01:00
|
|
|
source = "?" "?(?" "?)?" "?<?" "?>a?b:c";
|
2014-01-18 20:38:18 +02:00
|
|
|
expectedTokenKindList = List()
|
2013-11-26 15:23:28 +01:00
|
|
|
<< T_LBRACKET << T_RBRACKET << T_LBRACE << T_RBRACE
|
|
|
|
<< T_IDENTIFIER << T_QUESTION << T_IDENTIFIER << T_COLON << T_IDENTIFIER;
|
|
|
|
QTest::newRow(source) << source << expectedTokenKindList;
|
|
|
|
|
2013-02-06 14:58:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_APPLESS_MAIN(tst_SimpleLexer)
|
|
|
|
#include "tst_lexer.moc"
|