Commit b9927f99 authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

Wizards: Add a simple wizard for unit tests.

parent c04ece53
......@@ -643,7 +643,7 @@ QString BaseFileWizard::buildFileName(const QString &path,
return rc;
}
QString BaseFileWizard::preferredSuffix(const QString &mimeType) const
QString BaseFileWizard::preferredSuffix(const QString &mimeType)
{
const QString rc = Core::ICore::instance()->mimeDatabase()->preferredSuffixByType(mimeType);
if (rc.isEmpty())
......
......@@ -190,7 +190,7 @@ protected:
virtual bool postGenerateFiles(const GeneratedFiles &l, QString *errorMessage);
// Utility that returns the preferred suffix for a mime type
QString preferredSuffix(const QString &mimeType) const;
static QString preferredSuffix(const QString &mimeType);
// Utility that performs an overwrite check on a set of files. It checks if
// the file exists, can be overwritten at all and prompts the user.
......
......@@ -21,6 +21,9 @@ HEADERS += qt4projectmanagerplugin.h \
wizards/guiappwizarddialog.h \
wizards/emptyprojectwizard.h \
wizards/emptyprojectwizarddialog.h \
wizards/testwizard.h \
wizards/testwizarddialog.h \
wizards/testwizardpage.h \
wizards/modulespage.h \
wizards/filespage.h \
wizards/qtwizard.h \
......@@ -58,6 +61,9 @@ SOURCES += qt4projectmanagerplugin.cpp \
wizards/guiappwizarddialog.cpp \
wizards/emptyprojectwizard.cpp \
wizards/emptyprojectwizarddialog.cpp \
wizards/testwizard.cpp \
wizards/testwizarddialog.cpp \
wizards/testwizardpage.cpp \
wizards/modulespage.cpp \
wizards/filespage.cpp \
wizards/qtwizard.cpp \
......@@ -81,7 +87,8 @@ FORMS += makestep.ui \
qt4projectconfigwidget.ui \
qtversionmanager.ui \
showbuildlog.ui \
gettingstartedwelcomepagewidget.ui
gettingstartedwelcomepagewidget.ui \
wizards/testwizardpage.ui
RESOURCES += qt4projectmanager.qrc \
wizards/wizards.qrc
include(../../shared/proparser/proparser.pri)
......
......@@ -33,6 +33,7 @@
#include "wizards/consoleappwizard.h"
#include "wizards/guiappwizard.h"
#include "wizards/librarywizard.h"
#include "wizards/testwizard.h"
#include "wizards/emptyprojectwizard.h"
#include "customwidgetwizard/customwidgetwizard.h"
#include "profileeditorfactory.h"
......@@ -132,6 +133,7 @@ bool Qt4ProjectManagerPlugin::initialize(const QStringList &arguments, QString *
LibraryWizard *libWizard = new LibraryWizard;
addAutoReleasedObject(libWizard);
addAutoReleasedObject(new TestWizard);
addAutoReleasedObject(new CustomWidgetWizard);
addAutoReleasedObject(new QMakeStepFactory);
......
......@@ -60,8 +60,9 @@ void QtProjectParameters::writeProFile(QTextStream &str) const
str << "QT += " << selectedModules << "\n\n";
if (!deselectedModules.isEmpty())
str << "QT -= " << deselectedModules << "\n\n";
if (!name.isEmpty())
str << "TARGET = " << name << '\n';
const QString &effectiveTarget = target.isEmpty() ? name : target;
if (!effectiveTarget.isEmpty())
str << "TARGET = " << effectiveTarget << '\n';
switch (type) {
case ConsoleApp:
// Mac: Command line apps should not be bundles
......
......@@ -62,6 +62,7 @@ struct QtProjectParameters {
Type type;
QString name;
QString target;
QString path;
QString selectedModules;
QString deselectedModules;
......
......@@ -71,22 +71,22 @@ QtWizard::QtWizard(const QString &id, const QString &name,
{
}
QString QtWizard::sourceSuffix() const
QString QtWizard::sourceSuffix()
{
return preferredSuffix(QLatin1String(Constants::CPP_SOURCE_MIMETYPE));
}
QString QtWizard::headerSuffix() const
QString QtWizard::headerSuffix()
{
return preferredSuffix(QLatin1String(Constants::CPP_HEADER_MIMETYPE));
}
QString QtWizard::formSuffix() const
QString QtWizard::formSuffix()
{
return preferredSuffix(QLatin1String(Constants::FORM_MIMETYPE));
}
QString QtWizard::profileSuffix() const
QString QtWizard::profileSuffix()
{
return preferredSuffix(QLatin1String(Constants::PROFILE_MIMETYPE));
}
......@@ -102,7 +102,7 @@ bool QtWizard::postGenerateFiles(const Core::GeneratedFiles &l, QString *errorMe
return true;
}
QString QtWizard::templateDir() const
QString QtWizard::templateDir()
{
QString rc = Core::ICore::instance()->resourcePath();
rc += QLatin1String("/templates/qt4project");
......
......@@ -60,12 +60,14 @@ protected:
const QString &description,
const QIcon &icon);
QString templateDir() const;
public:
static QString templateDir();
QString sourceSuffix() const;
QString headerSuffix() const;
QString formSuffix() const;
QString profileSuffix() const;
static QString sourceSuffix();
static QString headerSuffix();
static QString formSuffix();
static QString profileSuffix();
// Query CppTools settings for the class wizard settings
static bool lowerCaseFiles();
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "testwizard.h"
#include "testwizarddialog.h"
#include <cpptools/cppmodelmanager.h>
#include <utils/qtcassert.h>
#include <QtCore/QTextStream>
#include <QtCore/QFileInfo>
#include <QtGui/QIcon>
namespace Qt4ProjectManager {
namespace Internal {
TestWizard::TestWizard() :
QtWizard(QLatin1String("E.Qt4Test"),
tr("Qt4 Unit Test"),
tr("Creates a Qt4 Unit Test."),
QIcon(QLatin1String(":/wizards/images/console.png")))
{
}
QWizard *TestWizard::createWizardDialog(QWidget *parent,
const QString &defaultPath,
const WizardPageList &extensionPages) const
{
TestWizardDialog *dialog = new TestWizardDialog(name(), icon(), extensionPages, parent);
dialog->setPath(defaultPath);
dialog->setName(TestWizardDialog::projectName(defaultPath));
return dialog;
}
// ---------------- code generation helpers
static const char initTestCaseC[] = "initTestCase";
static const char cleanupTestCaseC[] = "cleanupTestCase";
static const char closeFunctionC[] = "}\n\n";
static const char testDataTypeC[] = "QString";
static inline void writeVoidMemberDeclaration(QTextStream &str,
const QString &indent,
const QString &methodName)
{
str << indent << "void " << methodName << "();\n";
}
static inline void writeVoidMemberBody(QTextStream &str,
const QString &className,
const QString &methodName,
bool close = true)
{
str << "void " << className << "::" << methodName << "()\n{\n";
if (close)
str << closeFunctionC;
}
static QString generateTestCode(const TestWizardParameters &testParams,
const QString &sourceBaseName)
{
QString rc;
const QString indent = QString(4, QLatin1Char(' '));
QTextStream str(&rc);
// Includes
str << CppTools::AbstractEditorSupport::licenseTemplate()
<< "#include <QtCore/QString>\n#include <QtTest/QtTest>\n";
if (testParams.requiresQApplication)
str << "#include <QtCore/QCoreApplication>\n";
// Class declaration
str << "\nclass " << testParams.className << " : public QObject\n"
"{\n" << indent << "Q_OBJECT\n\npublic:\n"
<< indent << testParams.className << "();\n\nprivate Q_SLOTS:\n";
if (testParams.initializationCode) {
writeVoidMemberDeclaration(str, indent, QLatin1String(initTestCaseC));
writeVoidMemberDeclaration(str, indent, QLatin1String(cleanupTestCaseC));
}
const QString dataSlot = testParams.testSlot + QLatin1String("_data");
writeVoidMemberDeclaration(str, indent, testParams.testSlot);
if (testParams.useDataSet)
writeVoidMemberDeclaration(str, indent, dataSlot);
str << "};\n\n";
// Code: Constructor
str << testParams.className << "::" << testParams.className << "()\n{\n}\n\n";
// Code: Initialization slots
if (testParams.initializationCode) {
writeVoidMemberBody(str, testParams.className, QLatin1String(initTestCaseC));
writeVoidMemberBody(str, testParams.className, QLatin1String(cleanupTestCaseC));
}
// Test slot with data or dummy
writeVoidMemberBody(str, testParams.className, testParams.testSlot, false);
if (testParams.useDataSet) {
str << indent << "QFETCH(" << testDataTypeC << ", data);\n";
}
switch (testParams.type) {
case TestWizardParameters::Test:
str << indent << "QVERIFY2(true, \"Failure\");\n";
break;
case TestWizardParameters::Benchmark:
str << indent << "QBENCHMARK {\n" << indent << "}\n";
break;
}
str << closeFunctionC;
// test data generation slot
if (testParams.useDataSet) {
writeVoidMemberBody(str, testParams.className, dataSlot, false);
str << indent << "QTest::addColumn<" << testDataTypeC << ">(\"data\");\n"
<< indent << "QTest::newRow(\"0\") << " << testDataTypeC << "();\n"
<< closeFunctionC;
}
// Main & moc include
str << (testParams.requiresQApplication ? "QTEST_MAIN" : "QTEST_APPLESS_MAIN")
<< '(' << testParams.className << ");\n\n"
<< "#include \"" << sourceBaseName << ".moc\"\n";
return rc;
}
Core::GeneratedFiles TestWizard::generateFiles(const QWizard *w, QString *errorMessage) const
{
Q_UNUSED(errorMessage)
const TestWizardDialog *wizardDialog = qobject_cast<const TestWizardDialog *>(w);
QTC_ASSERT(wizardDialog, return Core::GeneratedFiles())
const QtProjectParameters projectParams = wizardDialog->projectParameters();
const TestWizardParameters testParams = wizardDialog->testParameters();
const QString projectPath = projectParams.projectPath();
// Create files: Source
const QString sourceFilePath = Core::BaseFileWizard::buildFileName(projectPath, testParams.fileName, sourceSuffix());
const QFileInfo sourceFileInfo(sourceFilePath);
Core::GeneratedFile source(sourceFilePath);
source.setContents(generateTestCode(testParams, sourceFileInfo.baseName()));
// Create profile with define for base dir to find test data
const QString profileName = Core::BaseFileWizard::buildFileName(projectPath, projectParams.name, profileSuffix());
Core::GeneratedFile profile(profileName);
QString contents;
{
QTextStream proStr(&contents);
QtProjectParameters::writeProFileHeader(proStr);
projectParams.writeProFile(proStr);
proStr << "\n\nSOURCES += " << QFileInfo(sourceFilePath).fileName() << '\n'
<< "DEFINES += SRCDIR=\\\\\\\"$$PWD/\\\\\\\"\n";
}
profile.setContents(contents);
return Core::GeneratedFiles() << source << profile;
}
} // namespace Internal
} // namespace Qt4ProjectManager
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef TESTWIZARD_H
#define TESTWIZARD_H
#include "qtwizard.h"
namespace Qt4ProjectManager {
namespace Internal {
class TestWizard : public QtWizard
{
Q_OBJECT
Q_DISABLE_COPY(TestWizard)
public:
TestWizard();
protected:
virtual QWizard *createWizardDialog(QWidget *parent,
const QString &defaultPath,
const WizardPageList &extensionPages) const;
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
signals:
public slots:
};
} // namespace Internal
} // namespace Qt4ProjectManager
#endif // TESTWIZARD_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "testwizarddialog.h"
#include "testwizardpage.h"
#include <QtCore/QFileInfo>
enum PageIds { StartPageId, TestPageId, ModulesPageId };
namespace Qt4ProjectManager {
namespace Internal {
const char *TestWizardParameters::filePrefix = "tst_";
TestWizardParameters::TestWizardParameters() :
type(Test),
initializationCode(false),
useDataSet(false),
requiresQApplication(requiresQApplicationDefault)
{
}
TestWizardDialog::TestWizardDialog(const QString &templateName,
const QIcon &icon,
const QList<QWizardPage*> &extensionPages,
QWidget *parent) :
BaseQt4ProjectWizardDialog(true, parent),
m_testPage(new TestWizardPage)
{
setWindowIcon(icon);
setWindowTitle(templateName);
setSelectedModules(QLatin1String("core testlib"), true);
setPage(TestPageId, m_testPage);
addModulesPage(ModulesPageId);
foreach (QWizardPage *p, extensionPages)
addPage(p);
connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(slotCurrentIdChanged(int)));
}
void TestWizardDialog::slotCurrentIdChanged(int id)
{
if (id == TestPageId)
m_testPage->setProjectName(name());
}
TestWizardParameters TestWizardDialog::testParameters() const
{
return m_testPage->parameters();
}
QtProjectParameters TestWizardDialog::projectParameters() const
{
QtProjectParameters rc;
rc.type = QtProjectParameters::ConsoleApp;
rc.name = name();
rc.path = path();
// Name binary "tst_xx" after the main source
rc.target = QFileInfo(m_testPage->sourcefileName()).baseName();
rc.selectedModules = selectedModules();
rc.deselectedModules = deselectedModules();
return rc;
}
} // namespace Internal
} // namespace Qt4ProjectManager
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef TESTWIZARDDIALOG_H
#define TESTWIZARDDIALOG_H
#include "qtwizard.h"
namespace Qt4ProjectManager {
namespace Internal {
class TestWizardPage;
// Parameters of the test wizard.
struct TestWizardParameters {
enum { requiresQApplicationDefault = 0 };
static const char *filePrefix;
TestWizardParameters();
enum Type { Test, Benchmark };
Type type;
bool initializationCode;
bool useDataSet;
bool requiresQApplication;
QString className;
QString testSlot;
QString fileName;
};
class TestWizardDialog : public BaseQt4ProjectWizardDialog
{
Q_OBJECT
public:
explicit TestWizardDialog(const QString &templateName,
const QIcon &icon,
const QList<QWizardPage*> &extensionPages,
QWidget *parent = 0);
TestWizardParameters testParameters() const;
QtProjectParameters projectParameters() const;
private slots:
void slotCurrentIdChanged(int id);
private:
TestWizardPage *m_testPage;
};
} // namespace Internal
} // namespace Qt4ProjectManager
#endif // TESTWIZARDDIALOG_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "testwizardpage.h"
#include "testwizarddialog.h"
#include "qtwizard.h"