1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
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 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "testwizard.h"
35 #include "testwizarddialog.h"
37 #include <cpptools/abstracteditorsupport.h>
38 #include <projectexplorer/projectexplorerconstants.h>
40 #include <utils/qtcassert.h>
42 #include <QtCore/QTextStream>
43 #include <QtCore/QFileInfo>
45 #include <QtGui/QIcon>
47 namespace Qt4ProjectManager {
50 TestWizard::TestWizard() :
51 QtWizard(QLatin1String("L.Qt4Test"),
52 QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_CATEGORY),
53 QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_TR_SCOPE),
54 QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_TR_CATEGORY),
56 tr("Creates a QTestLib-based unit test for a feature or a class. "
57 "Unit tests allow you to verify that the code is fit for use "
58 "and that there are no regressions."),
59 QIcon(QLatin1String(":/wizards/images/console.png")))
63 QWizard *TestWizard::createWizardDialog(QWidget *parent,
64 const QString &defaultPath,
65 const WizardPageList &extensionPages) const
67 TestWizardDialog *dialog = new TestWizardDialog(displayName(), icon(), extensionPages, parent);
68 dialog->setPath(defaultPath);
69 dialog->setProjectName(TestWizardDialog::uniqueProjectName(defaultPath));
73 // ---------------- code generation helpers
74 static const char initTestCaseC[] = "initTestCase";
75 static const char cleanupTestCaseC[] = "cleanupTestCase";
76 static const char closeFunctionC[] = "}\n\n";
77 static const char testDataTypeC[] = "QString";
79 static inline void writeVoidMemberDeclaration(QTextStream &str,
80 const QString &indent,
81 const QString &methodName)
83 str << indent << "void " << methodName << "();\n";
86 static inline void writeVoidMemberBody(QTextStream &str,
87 const QString &className,
88 const QString &methodName,
91 str << "void " << className << "::" << methodName << "()\n{\n";
93 str << closeFunctionC;
96 static QString generateTestCode(const TestWizardParameters &testParams,
97 const QString &sourceBaseName)
100 const QString indent = QString(4, QLatin1Char(' '));
101 QTextStream str(&rc);
103 str << CppTools::AbstractEditorSupport::licenseTemplate(testParams.fileName, testParams.className)
104 << "#include <QtCore/QString>\n#include <QtTest/QtTest>\n";
105 if (testParams.requiresQApplication)
106 str << "#include <QtCore/QCoreApplication>\n";
108 str << "\nclass " << testParams.className << " : public QObject\n"
109 "{\n" << indent << "Q_OBJECT\n\npublic:\n"
110 << indent << testParams.className << "();\n\nprivate Q_SLOTS:\n";
111 if (testParams.initializationCode) {
112 writeVoidMemberDeclaration(str, indent, QLatin1String(initTestCaseC));
113 writeVoidMemberDeclaration(str, indent, QLatin1String(cleanupTestCaseC));
115 const QString dataSlot = testParams.testSlot + QLatin1String("_data");
116 writeVoidMemberDeclaration(str, indent, testParams.testSlot);
117 if (testParams.useDataSet)
118 writeVoidMemberDeclaration(str, indent, dataSlot);
121 str << testParams.className << "::" << testParams.className << "()\n{\n}\n\n";
122 // Code: Initialization slots
123 if (testParams.initializationCode) {
124 writeVoidMemberBody(str, testParams.className, QLatin1String(initTestCaseC));
125 writeVoidMemberBody(str, testParams.className, QLatin1String(cleanupTestCaseC));
127 // Test slot with data or dummy
128 writeVoidMemberBody(str, testParams.className, testParams.testSlot, false);
129 if (testParams.useDataSet) {
130 str << indent << "QFETCH(" << testDataTypeC << ", data);\n";
132 switch (testParams.type) {
133 case TestWizardParameters::Test:
134 str << indent << "QVERIFY2(true, \"Failure\");\n";
136 case TestWizardParameters::Benchmark:
137 str << indent << "QBENCHMARK {\n" << indent << "}\n";
140 str << closeFunctionC;
141 // test data generation slot
142 if (testParams.useDataSet) {
143 writeVoidMemberBody(str, testParams.className, dataSlot, false);
144 str << indent << "QTest::addColumn<" << testDataTypeC << ">(\"data\");\n"
145 << indent << "QTest::newRow(\"0\") << " << testDataTypeC << "();\n"
148 // Main & moc include
149 str << (testParams.requiresQApplication ? "QTEST_MAIN" : "QTEST_APPLESS_MAIN")
150 << '(' << testParams.className << ");\n\n"
151 << "#include \"" << sourceBaseName << ".moc\"\n";
155 Core::GeneratedFiles TestWizard::generateFiles(const QWizard *w, QString *errorMessage) const
157 Q_UNUSED(errorMessage)
159 const TestWizardDialog *wizardDialog = qobject_cast<const TestWizardDialog *>(w);
160 QTC_ASSERT(wizardDialog, return Core::GeneratedFiles())
162 const QtProjectParameters projectParams = wizardDialog->projectParameters();
163 const TestWizardParameters testParams = wizardDialog->testParameters();
164 const QString projectPath = projectParams.projectPath();
166 // Create files: Source
167 const QString sourceFilePath = Core::BaseFileWizard::buildFileName(projectPath, testParams.fileName, sourceSuffix());
168 const QFileInfo sourceFileInfo(sourceFilePath);
170 Core::GeneratedFile source(sourceFilePath);
171 source.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
172 source.setContents(generateTestCode(testParams, sourceFileInfo.baseName()));
174 // Create profile with define for base dir to find test data
175 const QString profileName = Core::BaseFileWizard::buildFileName(projectPath, projectParams.fileName, profileSuffix());
176 Core::GeneratedFile profile(profileName);
177 profile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
180 QTextStream proStr(&contents);
181 QtProjectParameters::writeProFileHeader(proStr);
182 projectParams.writeProFile(proStr);
183 proStr << "\n\nSOURCES += " << QFileInfo(sourceFilePath).fileName() << '\n'
184 << "DEFINES += SRCDIR=\\\\\\\"$$PWD/\\\\\\\"\n";
186 profile.setContents(contents);
188 return Core::GeneratedFiles() << source << profile;
191 } // namespace Internal
192 } // namespace Qt4ProjectManager