OSDN Git Service

a493270e2ba297ce667667e675dd018030bcfa98
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / wizards / testwizard.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
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.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include "testwizard.h"
31 #include "testwizarddialog.h"
32
33 #include <cpptools/cppmodelmanager.h>
34 #include <projectexplorer/projectexplorerconstants.h>
35
36 #include <utils/qtcassert.h>
37
38 #include <QtCore/QTextStream>
39 #include <QtCore/QFileInfo>
40
41 #include <QtGui/QIcon>
42
43 namespace Qt4ProjectManager {
44 namespace Internal {
45
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),
51              tr("Qt Unit Test"),
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")))
56 {
57 }
58
59 QWizard *TestWizard::createWizardDialog(QWidget *parent,
60                                         const QString &defaultPath,
61                                         const WizardPageList &extensionPages) const
62 {
63     TestWizardDialog *dialog = new TestWizardDialog(displayName(), icon(), extensionPages, parent);
64     dialog->setPath(defaultPath);
65     dialog->setProjectName(TestWizardDialog::uniqueProjectName(defaultPath));
66     return dialog;
67 }
68
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";
74
75 static inline void writeVoidMemberDeclaration(QTextStream &str,
76                                               const QString &indent,
77                                               const QString &methodName)
78 {
79     str << indent << "void " << methodName << "();\n";
80 }
81
82 static inline void writeVoidMemberBody(QTextStream &str,
83                                        const QString &className,
84                                        const QString &methodName,
85                                        bool close = true)
86 {
87     str << "void " << className << "::" << methodName << "()\n{\n";
88     if (close)
89         str << closeFunctionC;
90 }
91
92 static QString generateTestCode(const TestWizardParameters &testParams,
93                                 const QString &sourceBaseName)
94 {
95     QString rc;
96     const QString indent = QString(4, QLatin1Char(' '));
97     QTextStream str(&rc);
98     // Includes
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";
103     // Class declaration
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));
110     }
111     const QString dataSlot = testParams.testSlot + QLatin1String("_data");
112     writeVoidMemberDeclaration(str, indent, testParams.testSlot);
113     if (testParams.useDataSet)
114         writeVoidMemberDeclaration(str, indent, dataSlot);
115     str << "};\n\n";
116     // Code: Constructor
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));
122     }
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";
127     }
128     switch (testParams.type) {
129     case TestWizardParameters::Test:
130         str << indent << "QVERIFY2(true, \"Failure\");\n";
131         break;
132     case TestWizardParameters::Benchmark:
133         str << indent << "QBENCHMARK {\n" << indent << "}\n";
134         break;
135     }
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"
142             << closeFunctionC;
143     }
144     // Main & moc include
145     str << (testParams.requiresQApplication ? "QTEST_MAIN" : "QTEST_APPLESS_MAIN")
146         << '(' << testParams.className << ");\n\n"
147         << "#include \"" << sourceBaseName << ".moc\"\n";
148     return rc;
149 }
150
151 Core::GeneratedFiles TestWizard::generateFiles(const QWizard *w, QString *errorMessage) const
152 {
153     Q_UNUSED(errorMessage)
154
155     const TestWizardDialog *wizardDialog = qobject_cast<const TestWizardDialog *>(w);
156     QTC_ASSERT(wizardDialog, return Core::GeneratedFiles())
157
158     const QtProjectParameters projectParams = wizardDialog->projectParameters();
159     const TestWizardParameters testParams = wizardDialog->testParameters();
160     const QString projectPath = projectParams.projectPath();
161
162     // Create files: Source
163     const QString sourceFilePath = Core::BaseFileWizard::buildFileName(projectPath, testParams.fileName, sourceSuffix());
164     const QFileInfo sourceFileInfo(sourceFilePath);
165
166     Core::GeneratedFile source(sourceFilePath);
167     source.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
168     source.setContents(generateTestCode(testParams, sourceFileInfo.baseName()));
169
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);
174     QString contents;
175     {
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";
181     }
182     profile.setContents(contents);
183
184     return Core::GeneratedFiles() <<  source << profile;
185 }
186
187 } // namespace Internal
188 } // namespace Qt4ProjectManager