2009-12-16 14:19:34 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2009-12-16 14:19:34 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2009-12-16 14:19:34 +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.
|
2009-12-16 14:19:34 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +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
|
2010-12-17 16:01:08 +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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2009-12-16 14:19:34 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "testwizard.h"
|
|
|
|
|
#include "testwizarddialog.h"
|
2010-01-13 18:44:15 +01:00
|
|
|
|
2010-12-03 13:49:35 +01:00
|
|
|
#include <cpptools/abstracteditorsupport.h>
|
2010-01-13 18:44:15 +01:00
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
2012-01-18 13:50:14 +01:00
|
|
|
#include <qtsupport/qtsupportconstants.h>
|
2009-12-16 14:19:34 +01:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QTextStream>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
|
|
|
|
|
#include <QtGui/QIcon>
|
|
|
|
|
|
|
|
|
|
namespace Qt4ProjectManager {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
TestWizard::TestWizard() :
|
2010-01-13 18:44:15 +01:00
|
|
|
QtWizard(QLatin1String("L.Qt4Test"),
|
2011-11-18 12:54:41 +01:00
|
|
|
QLatin1String(ProjectExplorer::Constants::QT_PROJECT_WIZARD_CATEGORY),
|
|
|
|
|
QLatin1String(ProjectExplorer::Constants::QT_PROJECT_WIZARD_CATEGORY_DISPLAY),
|
2010-01-13 18:44:15 +01:00
|
|
|
tr("Qt Unit Test"),
|
2010-05-12 10:39:49 +02:00
|
|
|
tr("Creates a QTestLib-based unit test for a feature or a class. "
|
|
|
|
|
"Unit tests allow you to verify that the code is fit for use "
|
|
|
|
|
"and that there are no regressions."),
|
2009-12-16 14:19:34 +01:00
|
|
|
QIcon(QLatin1String(":/wizards/images/console.png")))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWizard *TestWizard::createWizardDialog(QWidget *parent,
|
|
|
|
|
const QString &defaultPath,
|
|
|
|
|
const WizardPageList &extensionPages) const
|
|
|
|
|
{
|
2010-01-07 18:17:24 +01:00
|
|
|
TestWizardDialog *dialog = new TestWizardDialog(displayName(), icon(), extensionPages, parent);
|
2009-12-16 14:19:34 +01:00
|
|
|
dialog->setPath(defaultPath);
|
2010-01-07 18:17:24 +01:00
|
|
|
dialog->setProjectName(TestWizardDialog::uniqueProjectName(defaultPath));
|
2009-12-16 14:19:34 +01:00
|
|
|
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
|
2010-08-09 12:23:21 +02:00
|
|
|
str << CppTools::AbstractEditorSupport::licenseTemplate(testParams.fileName, testParams.className)
|
2009-12-16 14:19:34 +01:00
|
|
|
<< "#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")
|
2011-07-08 08:12:39 +02:00
|
|
|
<< '(' << testParams.className << ")\n\n"
|
2009-12-16 14:19:34 +01:00
|
|
|
<< "#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);
|
2010-04-16 15:55:32 +02:00
|
|
|
source.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
|
2009-12-16 14:19:34 +01:00
|
|
|
source.setContents(generateTestCode(testParams, sourceFileInfo.baseName()));
|
|
|
|
|
|
|
|
|
|
// Create profile with define for base dir to find test data
|
2010-01-07 18:17:24 +01:00
|
|
|
const QString profileName = Core::BaseFileWizard::buildFileName(projectPath, projectParams.fileName, profileSuffix());
|
2009-12-16 14:19:34 +01:00
|
|
|
Core::GeneratedFile profile(profileName);
|
2010-04-16 15:55:32 +02:00
|
|
|
profile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
|
2009-12-16 14:19:34 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-18 13:50:14 +01:00
|
|
|
Core::FeatureSet TestWizard::requiredFeatures() const
|
|
|
|
|
{
|
|
|
|
|
return Core::Feature(QtSupport::Constants::FEATURE_GENERIC_CPP_ENTRY_POINT) |
|
|
|
|
|
Core::Feature(QtSupport::Constants::FEATURE_QT);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-16 14:19:34 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Qt4ProjectManager
|