1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
28 **************************************************************************/
30 #include "testwizard.h"
31 #include "testwizarddialog.h"
33 #include <cpptools/cppmodelmanager.h>
34 #include <projectexplorer/projectexplorerconstants.h>
36 #include <utils/qtcassert.h>
38 #include <QtCore/QTextStream>
39 #include <QtCore/QFileInfo>
41 #include <QtGui/QIcon>
43 namespace Qt4ProjectManager {
46 TestWizard::TestWizard() :
47 QtWizard(QLatin1String("L.Qt4Test"),
48 QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_CATEGORY),
49 QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_TR_SCOPE),
50 QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_TR_CATEGORY),
52 tr("Creates a QTestLib-based unit test for a feature or a class. "
53 "Unit tests allow you to verify that the code is fit for use "
54 "and that there are no regressions."),
55 QIcon(QLatin1String(":/wizards/images/console.png")))
59 QWizard *TestWizard::createWizardDialog(QWidget *parent,
60 const QString &defaultPath,
61 const WizardPageList &extensionPages) const
63 TestWizardDialog *dialog = new TestWizardDialog(displayName(), icon(), extensionPages, parent);
64 dialog->setPath(defaultPath);
65 dialog->setProjectName(TestWizardDialog::uniqueProjectName(defaultPath));
69 // ---------------- code generation helpers
70 static const char initTestCaseC[] = "initTestCase";
71 static const char cleanupTestCaseC[] = "cleanupTestCase";
72 static const char closeFunctionC[] = "}\n\n";
73 static const char testDataTypeC[] = "QString";
75 static inline void writeVoidMemberDeclaration(QTextStream &str,
76 const QString &indent,
77 const QString &methodName)
79 str << indent << "void " << methodName << "();\n";
82 static inline void writeVoidMemberBody(QTextStream &str,
83 const QString &className,
84 const QString &methodName,
87 str << "void " << className << "::" << methodName << "()\n{\n";
89 str << closeFunctionC;
92 static QString generateTestCode(const TestWizardParameters &testParams,
93 const QString &sourceBaseName)
96 const QString indent = QString(4, QLatin1Char(' '));
99 str << CppTools::AbstractEditorSupport::licenseTemplate(testParams.fileName, testParams.className)
100 << "#include <QtCore/QString>\n#include <QtTest/QtTest>\n";
101 if (testParams.requiresQApplication)
102 str << "#include <QtCore/QCoreApplication>\n";
104 str << "\nclass " << testParams.className << " : public QObject\n"
105 "{\n" << indent << "Q_OBJECT\n\npublic:\n"
106 << indent << testParams.className << "();\n\nprivate Q_SLOTS:\n";
107 if (testParams.initializationCode) {
108 writeVoidMemberDeclaration(str, indent, QLatin1String(initTestCaseC));
109 writeVoidMemberDeclaration(str, indent, QLatin1String(cleanupTestCaseC));
111 const QString dataSlot = testParams.testSlot + QLatin1String("_data");
112 writeVoidMemberDeclaration(str, indent, testParams.testSlot);
113 if (testParams.useDataSet)
114 writeVoidMemberDeclaration(str, indent, dataSlot);
117 str << testParams.className << "::" << testParams.className << "()\n{\n}\n\n";
118 // Code: Initialization slots
119 if (testParams.initializationCode) {
120 writeVoidMemberBody(str, testParams.className, QLatin1String(initTestCaseC));
121 writeVoidMemberBody(str, testParams.className, QLatin1String(cleanupTestCaseC));
123 // Test slot with data or dummy
124 writeVoidMemberBody(str, testParams.className, testParams.testSlot, false);
125 if (testParams.useDataSet) {
126 str << indent << "QFETCH(" << testDataTypeC << ", data);\n";
128 switch (testParams.type) {
129 case TestWizardParameters::Test:
130 str << indent << "QVERIFY2(true, \"Failure\");\n";
132 case TestWizardParameters::Benchmark:
133 str << indent << "QBENCHMARK {\n" << indent << "}\n";
136 str << closeFunctionC;
137 // test data generation slot
138 if (testParams.useDataSet) {
139 writeVoidMemberBody(str, testParams.className, dataSlot, false);
140 str << indent << "QTest::addColumn<" << testDataTypeC << ">(\"data\");\n"
141 << indent << "QTest::newRow(\"0\") << " << testDataTypeC << "();\n"
144 // Main & moc include
145 str << (testParams.requiresQApplication ? "QTEST_MAIN" : "QTEST_APPLESS_MAIN")
146 << '(' << testParams.className << ");\n\n"
147 << "#include \"" << sourceBaseName << ".moc\"\n";
151 Core::GeneratedFiles TestWizard::generateFiles(const QWizard *w, QString *errorMessage) const
153 Q_UNUSED(errorMessage)
155 const TestWizardDialog *wizardDialog = qobject_cast<const TestWizardDialog *>(w);
156 QTC_ASSERT(wizardDialog, return Core::GeneratedFiles())
158 const QtProjectParameters projectParams = wizardDialog->projectParameters();
159 const TestWizardParameters testParams = wizardDialog->testParameters();
160 const QString projectPath = projectParams.projectPath();
162 // Create files: Source
163 const QString sourceFilePath = Core::BaseFileWizard::buildFileName(projectPath, testParams.fileName, sourceSuffix());
164 const QFileInfo sourceFileInfo(sourceFilePath);
166 Core::GeneratedFile source(sourceFilePath);
167 source.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
168 source.setContents(generateTestCode(testParams, sourceFileInfo.baseName()));
170 // Create profile with define for base dir to find test data
171 const QString profileName = Core::BaseFileWizard::buildFileName(projectPath, projectParams.fileName, profileSuffix());
172 Core::GeneratedFile profile(profileName);
173 profile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
176 QTextStream proStr(&contents);
177 QtProjectParameters::writeProFileHeader(proStr);
178 projectParams.writeProFile(proStr);
179 proStr << "\n\nSOURCES += " << QFileInfo(sourceFilePath).fileName() << '\n'
180 << "DEFINES += SRCDIR=\\\\\\\"$$PWD/\\\\\\\"\n";
182 profile.setContents(contents);
184 return Core::GeneratedFiles() << source << profile;
187 } // namespace Internal
188 } // namespace Qt4ProjectManager