2011-02-18 14:48:16 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-02-18 14:48:16 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2011-02-18 14:48:16 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-07-06 10:31:07 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2011-02-18 14:48:16 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2010-07-28 12:09:50 +02:00
|
|
|
#include <AST.h>
|
|
|
|
|
#include <Control.h>
|
|
|
|
|
#include <CppDocument.h>
|
|
|
|
|
#include <DiagnosticClient.h>
|
|
|
|
|
#include <Scope.h>
|
|
|
|
|
#include <TranslationUnit.h>
|
|
|
|
|
#include <Literals.h>
|
2010-08-13 16:22:25 +02:00
|
|
|
#include <Bind.h>
|
2010-07-28 12:09:50 +02:00
|
|
|
#include <Symbols.h>
|
2010-10-20 14:16:04 +10:00
|
|
|
#include <cpptools/insertionpointlocator.h>
|
|
|
|
|
#include <cpptools/cpprefactoringchanges.h>
|
2010-10-21 14:52:56 +10:00
|
|
|
#include <cpptools/cpptoolsplugin.h>
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2011-08-29 09:53:52 +02:00
|
|
|
#include <utils/fileutils.h>
|
2010-07-28 12:09:50 +02:00
|
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
2010-09-02 15:32:04 +10:00
|
|
|
//TESTED_COMPONENT=src/libs/cplusplus
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Tests for various parts of the code generation. Well, okay, currently it only
|
|
|
|
|
tests the InsertionPointLocator.
|
|
|
|
|
*/
|
2010-07-28 12:09:50 +02:00
|
|
|
using namespace CPlusPlus;
|
2010-10-20 14:16:04 +10:00
|
|
|
using namespace CppTools;
|
2010-07-28 12:09:50 +02:00
|
|
|
|
|
|
|
|
class tst_Codegen: public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
private slots:
|
2010-10-21 14:52:56 +10:00
|
|
|
void initTestCase();
|
|
|
|
|
void cleanupTestCase();
|
2010-07-28 12:09:50 +02:00
|
|
|
void public_in_empty_class();
|
|
|
|
|
void public_in_nonempty_class();
|
|
|
|
|
void public_before_protected();
|
|
|
|
|
void private_after_protected();
|
|
|
|
|
void protected_in_nonempty_class();
|
|
|
|
|
void protected_betwee_public_and_private();
|
|
|
|
|
void qtdesigner_integration();
|
2011-08-29 09:53:52 +02:00
|
|
|
void definition_empty_class();
|
|
|
|
|
void definition_first_member();
|
|
|
|
|
void definition_last_member();
|
|
|
|
|
void definition_middle_member();
|
|
|
|
|
|
2010-10-21 14:52:56 +10:00
|
|
|
private:
|
|
|
|
|
ExtensionSystem::PluginManager *pluginManager;
|
2010-07-28 12:09:50 +02:00
|
|
|
};
|
|
|
|
|
|
2010-10-21 14:52:56 +10:00
|
|
|
void tst_Codegen::initTestCase()
|
|
|
|
|
{
|
|
|
|
|
pluginManager = new ExtensionSystem::PluginManager;
|
2010-12-03 15:04:19 +10:00
|
|
|
QSettings *settings = new QSettings(QSettings::IniFormat, QSettings::UserScope,
|
|
|
|
|
QLatin1String("Nokia"), QLatin1String("QtCreator"));
|
|
|
|
|
pluginManager->setSettings(settings);
|
2010-10-21 14:52:56 +10:00
|
|
|
pluginManager->setFileExtension(QLatin1String("pluginspec"));
|
|
|
|
|
pluginManager->setPluginPaths(QStringList() << QLatin1String(Q_PLUGIN_PATH));
|
|
|
|
|
pluginManager->loadPlugins();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Codegen::cleanupTestCase()
|
|
|
|
|
{
|
2011-08-29 09:53:52 +02:00
|
|
|
// gives me a qFatal...
|
|
|
|
|
// pluginManager->shutdown();
|
|
|
|
|
// delete pluginManager;
|
2010-10-21 14:52:56 +10:00
|
|
|
}
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Should insert at line 3, column 1, with "public:\n" as prefix and without suffix.
|
|
|
|
|
*/
|
|
|
|
|
void tst_Codegen::public_in_empty_class()
|
2010-07-28 12:09:50 +02:00
|
|
|
{
|
|
|
|
|
const QByteArray src = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
2010-07-28 17:33:21 +02:00
|
|
|
"};\n"
|
2010-07-28 12:09:50 +02:00
|
|
|
"\n";
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
Document::Ptr doc = Document::create("public_in_empty_class");
|
2010-07-28 12:09:50 +02:00
|
|
|
doc->setSource(src);
|
|
|
|
|
doc->parse();
|
|
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(doc->globalSymbolCount(), 1U);
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Class *foo = doc->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(doc);
|
2010-10-20 14:16:04 +10:00
|
|
|
CppRefactoringChanges changes(snapshot);
|
2011-08-29 09:53:52 +02:00
|
|
|
InsertionPointLocator find(changes);
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionLocation loc = find.methodDeclarationInClass(
|
2010-09-27 18:01:04 +02:00
|
|
|
doc->fileName(),
|
|
|
|
|
foo,
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionPointLocator::Public);
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.isValid());
|
2010-07-28 17:33:21 +02:00
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.suffix().isEmpty());
|
2010-07-28 17:33:21 +02:00
|
|
|
QCOMPARE(loc.line(), 3U);
|
2010-07-28 12:09:50 +02:00
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Should insert at line 3, column 1, without prefix and without suffix.
|
|
|
|
|
*/
|
|
|
|
|
void tst_Codegen::public_in_nonempty_class()
|
2010-07-28 12:09:50 +02:00
|
|
|
{
|
|
|
|
|
const QByteArray src = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
2010-07-28 17:33:21 +02:00
|
|
|
"public:\n" // line 3
|
|
|
|
|
"};\n" // line 4
|
2010-07-28 12:09:50 +02:00
|
|
|
"\n";
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
Document::Ptr doc = Document::create("public_in_nonempty_class");
|
2010-07-28 12:09:50 +02:00
|
|
|
doc->setSource(src);
|
|
|
|
|
doc->parse();
|
|
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(doc->globalSymbolCount(), 1U);
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Class *foo = doc->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
2010-07-28 12:09:50 +02:00
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(doc);
|
2010-10-20 14:16:04 +10:00
|
|
|
CppRefactoringChanges changes(snapshot);
|
2011-08-29 09:53:52 +02:00
|
|
|
InsertionPointLocator find(changes);
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionLocation loc = find.methodDeclarationInClass(
|
2010-09-27 18:01:04 +02:00
|
|
|
doc->fileName(),
|
|
|
|
|
foo,
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionPointLocator::Public);
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.isValid());
|
2010-07-28 17:33:21 +02:00
|
|
|
QVERIFY(loc.prefix().isEmpty());
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.suffix().isEmpty());
|
2010-07-28 17:33:21 +02:00
|
|
|
QCOMPARE(loc.line(), 4U);
|
2010-07-28 12:09:50 +02:00
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Should insert at line 3, column 1, with "public:\n" as prefix and "\n suffix.
|
|
|
|
|
*/
|
2010-07-28 12:09:50 +02:00
|
|
|
void tst_Codegen::public_before_protected()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray src = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"protected:\n" // line 3
|
|
|
|
|
"};\n"
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr doc = Document::create("public_before_protected");
|
|
|
|
|
doc->setSource(src);
|
|
|
|
|
doc->parse();
|
|
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(doc->globalSymbolCount(), 1U);
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Class *foo = doc->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
2010-07-28 12:09:50 +02:00
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(doc);
|
2010-10-20 14:16:04 +10:00
|
|
|
CppRefactoringChanges changes(snapshot);
|
2011-08-29 09:53:52 +02:00
|
|
|
InsertionPointLocator find(changes);
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionLocation loc = find.methodDeclarationInClass(
|
2010-09-27 18:01:04 +02:00
|
|
|
doc->fileName(),
|
|
|
|
|
foo,
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionPointLocator::Public);
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.isValid());
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
|
|
|
|
|
QCOMPARE(loc.suffix(), QLatin1String("\n"));
|
|
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
QCOMPARE(loc.line(), 3U);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Should insert at line 4, column 1, with "private:\n" as prefix and without
|
|
|
|
|
suffix.
|
|
|
|
|
*/
|
2010-07-28 12:09:50 +02:00
|
|
|
void tst_Codegen::private_after_protected()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray src = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"protected:\n" // line 3
|
|
|
|
|
"};\n"
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr doc = Document::create("private_after_protected");
|
|
|
|
|
doc->setSource(src);
|
|
|
|
|
doc->parse();
|
|
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(doc->globalSymbolCount(), 1U);
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Class *foo = doc->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
2010-07-28 12:09:50 +02:00
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(doc);
|
2010-10-20 14:16:04 +10:00
|
|
|
CppRefactoringChanges changes(snapshot);
|
2011-08-29 09:53:52 +02:00
|
|
|
InsertionPointLocator find(changes);
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionLocation loc = find.methodDeclarationInClass(
|
2010-09-27 18:01:04 +02:00
|
|
|
doc->fileName(),
|
|
|
|
|
foo,
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionPointLocator::Private);
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.isValid());
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("private:\n"));
|
|
|
|
|
QVERIFY(loc.suffix().isEmpty());
|
|
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
QCOMPARE(loc.line(), 4U);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Should insert at line 4, column 1, with "protected:\n" as prefix and without
|
|
|
|
|
suffix.
|
|
|
|
|
*/
|
2010-07-28 12:09:50 +02:00
|
|
|
void tst_Codegen::protected_in_nonempty_class()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray src = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"public:\n" // line 3
|
|
|
|
|
"};\n" // line 4
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr doc = Document::create("protected_in_nonempty_class");
|
|
|
|
|
doc->setSource(src);
|
|
|
|
|
doc->parse();
|
|
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(doc->globalSymbolCount(), 1U);
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Class *foo = doc->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
2010-07-28 12:09:50 +02:00
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(doc);
|
2010-10-20 14:16:04 +10:00
|
|
|
CppRefactoringChanges changes(snapshot);
|
2011-08-29 09:53:52 +02:00
|
|
|
InsertionPointLocator find(changes);
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionLocation loc = find.methodDeclarationInClass(
|
2010-09-27 18:01:04 +02:00
|
|
|
doc->fileName(),
|
|
|
|
|
foo,
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionPointLocator::Protected);
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.isValid());
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
|
|
|
|
|
QVERIFY(loc.suffix().isEmpty());
|
|
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
QCOMPARE(loc.line(), 4U);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Should insert at line 4, column 1, with "protected\n" as prefix and "\n" suffix.
|
|
|
|
|
*/
|
2010-07-28 12:09:50 +02:00
|
|
|
void tst_Codegen::protected_betwee_public_and_private()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray src = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"public:\n" // line 3
|
|
|
|
|
"private:\n" // line 4
|
|
|
|
|
"};\n" // line 5
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr doc = Document::create("protected_betwee_public_and_private");
|
|
|
|
|
doc->setSource(src);
|
|
|
|
|
doc->parse();
|
|
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(doc->globalSymbolCount(), 1U);
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Class *foo = doc->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
2010-07-28 12:09:50 +02:00
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(doc);
|
2010-10-20 14:16:04 +10:00
|
|
|
CppRefactoringChanges changes(snapshot);
|
2011-08-29 09:53:52 +02:00
|
|
|
InsertionPointLocator find(changes);
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionLocation loc = find.methodDeclarationInClass(
|
2010-09-27 18:01:04 +02:00
|
|
|
doc->fileName(),
|
|
|
|
|
foo,
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionPointLocator::Protected);
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.isValid());
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
|
|
|
|
|
QCOMPARE(loc.suffix(), QLatin1String("\n"));
|
|
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
QCOMPARE(loc.line(), 4U);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-28 17:33:21 +02:00
|
|
|
/*!
|
|
|
|
|
Should insert at line 18, column 1, with "private slots:\n" as prefix and "\n"
|
|
|
|
|
as suffix.
|
|
|
|
|
|
|
|
|
|
This is the typical Qt Designer case, with test-input like what the integration
|
|
|
|
|
generates.
|
|
|
|
|
*/
|
2010-07-28 12:09:50 +02:00
|
|
|
void tst_Codegen::qtdesigner_integration()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray src = "/**** Some long (C)opyright notice ****/\n"
|
|
|
|
|
"#ifndef MAINWINDOW_H\n"
|
|
|
|
|
"#define MAINWINDOW_H\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"#include <QMainWindow>\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"namespace Ui {\n"
|
|
|
|
|
" class MainWindow;\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"class MainWindow : public QMainWindow\n" // line 10
|
|
|
|
|
"{\n"
|
|
|
|
|
" Q_OBJECT\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"public:\n" // line 14
|
|
|
|
|
" explicit MainWindow(QWidget *parent = 0);\n"
|
|
|
|
|
" ~MainWindow();\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"private:\n" // line 18
|
|
|
|
|
" Ui::MainWindow *ui;\n"
|
|
|
|
|
"};\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"#endif // MAINWINDOW_H\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr doc = Document::create("qtdesigner_integration");
|
|
|
|
|
doc->setSource(src);
|
|
|
|
|
doc->parse();
|
|
|
|
|
doc->check();
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(doc->globalSymbolCount(), 2U);
|
|
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Class *foo = doc->globalSymbolAt(1)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 10U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
2010-07-28 12:09:50 +02:00
|
|
|
|
2010-09-27 18:01:04 +02:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(doc);
|
2010-10-20 14:16:04 +10:00
|
|
|
CppRefactoringChanges changes(snapshot);
|
2011-08-29 09:53:52 +02:00
|
|
|
InsertionPointLocator find(changes);
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionLocation loc = find.methodDeclarationInClass(
|
2010-09-27 18:01:04 +02:00
|
|
|
doc->fileName(),
|
|
|
|
|
foo,
|
2010-07-28 17:33:21 +02:00
|
|
|
InsertionPointLocator::PrivateSlot);
|
2010-07-28 12:09:50 +02:00
|
|
|
QVERIFY(loc.isValid());
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("private slots:\n"));
|
|
|
|
|
QCOMPARE(loc.suffix(), QLatin1String("\n"));
|
|
|
|
|
QCOMPARE(loc.line(), 18U);
|
|
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-29 09:53:52 +02:00
|
|
|
void tst_Codegen::definition_empty_class()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray srcText = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"void foo();\n" // line 3
|
|
|
|
|
"};\n"
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
const QByteArray dstText = "\n"
|
|
|
|
|
"int x;\n" // line 1
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr src = Document::create(QLatin1String("/tmp/file.h"));
|
|
|
|
|
Utils::FileSaver srcSaver(src->fileName());
|
|
|
|
|
srcSaver.write(srcText);
|
|
|
|
|
srcSaver.finalize();
|
|
|
|
|
src->setSource(srcText);
|
|
|
|
|
src->parse();
|
|
|
|
|
src->check();
|
|
|
|
|
QCOMPARE(src->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(src->globalSymbolCount(), 1U);
|
|
|
|
|
|
|
|
|
|
Document::Ptr dst = Document::create(QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
Utils::FileSaver dstSaver(dst->fileName());
|
|
|
|
|
dstSaver.write(dstText);
|
|
|
|
|
dstSaver.finalize();
|
|
|
|
|
dst->setSource(dstText);
|
|
|
|
|
dst->parse();
|
|
|
|
|
dst->check();
|
|
|
|
|
QCOMPARE(dst->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(dst->globalSymbolCount(), 1U);
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(src);
|
|
|
|
|
snapshot.insert(dst);
|
|
|
|
|
|
|
|
|
|
Class *foo = src->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
|
|
|
|
QCOMPARE(foo->memberCount(), 1U);
|
|
|
|
|
Declaration *decl = foo->memberAt(0)->asDeclaration();
|
|
|
|
|
QVERIFY(decl);
|
|
|
|
|
QCOMPARE(decl->line(), 3U);
|
|
|
|
|
QCOMPARE(decl->column(), 6U);
|
|
|
|
|
|
|
|
|
|
CppRefactoringChanges changes(snapshot);
|
|
|
|
|
InsertionPointLocator find(changes);
|
|
|
|
|
QList<InsertionLocation> locList = find.methodDefinition(decl);
|
|
|
|
|
QVERIFY(locList.size() == 1);
|
|
|
|
|
InsertionLocation loc = locList.first();
|
|
|
|
|
QCOMPARE(loc.fileName(), QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
|
|
|
|
|
QCOMPARE(loc.suffix(), QString());
|
|
|
|
|
QCOMPARE(loc.line(), 1U);
|
|
|
|
|
QCOMPARE(loc.column(), 7U);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Codegen::definition_first_member()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray srcText = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"void foo();\n" // line 3
|
|
|
|
|
"void bar();\n" // line 4
|
|
|
|
|
"};\n"
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
const QByteArray dstText = "\n"
|
|
|
|
|
"#include \"/tmp/file.h\"\n" // line 1
|
|
|
|
|
"int x;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"void Foo::bar()\n" // line 4
|
|
|
|
|
"{\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"int y;\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr src = Document::create(QLatin1String("/tmp/file.h"));
|
|
|
|
|
Utils::FileSaver srcSaver(src->fileName());
|
|
|
|
|
srcSaver.write(srcText);
|
|
|
|
|
srcSaver.finalize();
|
|
|
|
|
src->setSource(srcText);
|
|
|
|
|
src->parse();
|
|
|
|
|
src->check();
|
|
|
|
|
QCOMPARE(src->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(src->globalSymbolCount(), 1U);
|
|
|
|
|
|
|
|
|
|
Document::Ptr dst = Document::create(QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
dst->addIncludeFile("/tmp/file.h", 1);
|
|
|
|
|
Utils::FileSaver dstSaver(dst->fileName());
|
|
|
|
|
dstSaver.write(dstText);
|
|
|
|
|
dstSaver.finalize();
|
|
|
|
|
dst->setSource(dstText);
|
|
|
|
|
dst->parse();
|
|
|
|
|
dst->check();
|
|
|
|
|
QCOMPARE(dst->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(dst->globalSymbolCount(), 3U);
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(src);
|
|
|
|
|
snapshot.insert(dst);
|
|
|
|
|
|
|
|
|
|
Class *foo = src->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
|
|
|
|
QCOMPARE(foo->memberCount(), 2U);
|
|
|
|
|
Declaration *decl = foo->memberAt(0)->asDeclaration();
|
|
|
|
|
QVERIFY(decl);
|
|
|
|
|
QCOMPARE(decl->line(), 3U);
|
|
|
|
|
QCOMPARE(decl->column(), 6U);
|
|
|
|
|
|
|
|
|
|
CppRefactoringChanges changes(snapshot);
|
|
|
|
|
InsertionPointLocator find(changes);
|
|
|
|
|
QList<InsertionLocation> locList = find.methodDefinition(decl);
|
|
|
|
|
QVERIFY(locList.size() == 1);
|
|
|
|
|
InsertionLocation loc = locList.first();
|
|
|
|
|
QCOMPARE(loc.fileName(), QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
QCOMPARE(loc.line(), 4U);
|
|
|
|
|
QCOMPARE(loc.column(), 1U);
|
|
|
|
|
QCOMPARE(loc.suffix(), QLatin1String("\n\n"));
|
|
|
|
|
QCOMPARE(loc.prefix(), QString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Codegen::definition_last_member()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray srcText = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"void foo();\n" // line 3
|
|
|
|
|
"void bar();\n" // line 4
|
|
|
|
|
"};\n"
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
const QByteArray dstText = "\n"
|
|
|
|
|
"#include \"/tmp/file.h\"\n" // line 1
|
|
|
|
|
"int x;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"void Foo::foo()\n" // line 4
|
|
|
|
|
"{\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"}\n" // line 7
|
|
|
|
|
"\n"
|
|
|
|
|
"int y;\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr src = Document::create(QLatin1String("/tmp/file.h"));
|
|
|
|
|
Utils::FileSaver srcSaver(src->fileName());
|
|
|
|
|
srcSaver.write(srcText);
|
|
|
|
|
srcSaver.finalize();
|
|
|
|
|
src->setSource(srcText);
|
|
|
|
|
src->parse();
|
|
|
|
|
src->check();
|
|
|
|
|
QCOMPARE(src->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(src->globalSymbolCount(), 1U);
|
|
|
|
|
|
|
|
|
|
Document::Ptr dst = Document::create(QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
dst->addIncludeFile("/tmp/file.h", 1);
|
|
|
|
|
Utils::FileSaver dstSaver(dst->fileName());
|
|
|
|
|
dstSaver.write(dstText);
|
|
|
|
|
dstSaver.finalize();
|
|
|
|
|
dst->setSource(dstText);
|
|
|
|
|
dst->parse();
|
|
|
|
|
dst->check();
|
|
|
|
|
QCOMPARE(dst->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(dst->globalSymbolCount(), 3U);
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(src);
|
|
|
|
|
snapshot.insert(dst);
|
|
|
|
|
|
|
|
|
|
Class *foo = src->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
|
|
|
|
QCOMPARE(foo->memberCount(), 2U);
|
|
|
|
|
Declaration *decl = foo->memberAt(1)->asDeclaration();
|
|
|
|
|
QVERIFY(decl);
|
|
|
|
|
QCOMPARE(decl->line(), 4U);
|
|
|
|
|
QCOMPARE(decl->column(), 6U);
|
|
|
|
|
|
|
|
|
|
CppRefactoringChanges changes(snapshot);
|
|
|
|
|
InsertionPointLocator find(changes);
|
|
|
|
|
QList<InsertionLocation> locList = find.methodDefinition(decl);
|
|
|
|
|
QVERIFY(locList.size() == 1);
|
|
|
|
|
InsertionLocation loc = locList.first();
|
|
|
|
|
QCOMPARE(loc.fileName(), QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
QCOMPARE(loc.line(), 7U);
|
|
|
|
|
QCOMPARE(loc.column(), 2U);
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
|
|
|
|
|
QCOMPARE(loc.suffix(), QString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_Codegen::definition_middle_member()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray srcText = "\n"
|
|
|
|
|
"class Foo\n" // line 1
|
|
|
|
|
"{\n"
|
|
|
|
|
"void foo();\n" // line 3
|
|
|
|
|
"void bar();\n" // line 4
|
|
|
|
|
"void car();\n" // line 5
|
|
|
|
|
"};\n"
|
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
const QByteArray dstText = "\n"
|
|
|
|
|
"#include \"/tmp/file.h\"\n" // line 1
|
|
|
|
|
"int x;\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"void Foo::foo()\n" // line 4
|
|
|
|
|
"{\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"}\n" // line 7
|
|
|
|
|
"\n"
|
|
|
|
|
"void Foo::car()\n" // line 9
|
|
|
|
|
"{\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"}\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"int y;\n";
|
|
|
|
|
|
|
|
|
|
Document::Ptr src = Document::create(QLatin1String("/tmp/file.h"));
|
|
|
|
|
Utils::FileSaver srcSaver(src->fileName());
|
|
|
|
|
srcSaver.write(srcText);
|
|
|
|
|
srcSaver.finalize();
|
|
|
|
|
src->setSource(srcText);
|
|
|
|
|
src->parse();
|
|
|
|
|
src->check();
|
|
|
|
|
QCOMPARE(src->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(src->globalSymbolCount(), 1U);
|
|
|
|
|
|
|
|
|
|
Document::Ptr dst = Document::create(QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
dst->addIncludeFile("/tmp/file.h", 1);
|
|
|
|
|
Utils::FileSaver dstSaver(dst->fileName());
|
|
|
|
|
dstSaver.write(dstText);
|
|
|
|
|
dstSaver.finalize();
|
|
|
|
|
dst->setSource(dstText);
|
|
|
|
|
dst->parse();
|
|
|
|
|
dst->check();
|
|
|
|
|
QCOMPARE(dst->diagnosticMessages().size(), 0);
|
|
|
|
|
QCOMPARE(dst->globalSymbolCount(), 4U);
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
snapshot.insert(src);
|
|
|
|
|
snapshot.insert(dst);
|
|
|
|
|
|
|
|
|
|
Class *foo = src->globalSymbolAt(0)->asClass();
|
|
|
|
|
QVERIFY(foo);
|
|
|
|
|
QCOMPARE(foo->line(), 1U);
|
|
|
|
|
QCOMPARE(foo->column(), 7U);
|
|
|
|
|
QCOMPARE(foo->memberCount(), 3U);
|
|
|
|
|
Declaration *decl = foo->memberAt(1)->asDeclaration();
|
|
|
|
|
QVERIFY(decl);
|
|
|
|
|
QCOMPARE(decl->line(), 4U);
|
|
|
|
|
QCOMPARE(decl->column(), 6U);
|
|
|
|
|
|
|
|
|
|
CppRefactoringChanges changes(snapshot);
|
|
|
|
|
InsertionPointLocator find(changes);
|
|
|
|
|
QList<InsertionLocation> locList = find.methodDefinition(decl);
|
|
|
|
|
QVERIFY(locList.size() == 1);
|
|
|
|
|
InsertionLocation loc = locList.first();
|
|
|
|
|
QCOMPARE(loc.fileName(), QLatin1String("/tmp/file.cpp"));
|
|
|
|
|
QCOMPARE(loc.line(), 7U);
|
|
|
|
|
QCOMPARE(loc.column(), 2U);
|
|
|
|
|
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
|
|
|
|
|
QCOMPARE(loc.suffix(), QString());
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-21 14:52:56 +10:00
|
|
|
QTEST_MAIN(tst_Codegen)
|
2010-07-28 12:09:50 +02:00
|
|
|
#include "tst_codegen.moc"
|