OSDN Git Service

f5bb8727683de3a62f509ba0e0dd0ebdc851afeb
[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) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
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 ** 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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "testwizard.h"
35 #include "testwizarddialog.h"
36
37 #include <cpptools/abstracteditorsupport.h>
38 #include <projectexplorer/projectexplorerconstants.h>
39
40 #include <utils/qtcassert.h>
41
42 #include <QtCore/QTextStream>
43 #include <QtCore/QFileInfo>
44
45 #include <QtGui/QIcon>
46
47 namespace Qt4ProjectManager {
48 namespace Internal {
49
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),
55              tr("Qt Unit Test"),
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")))
60 {
61 }
62
63 QWizard *TestWizard::createWizardDialog(QWidget *parent,
64                                         const QString &defaultPath,
65                                         const WizardPageList &extensionPages) const
66 {
67     TestWizardDialog *dialog = new TestWizardDialog(displayName(), icon(), extensionPages, parent);
68     dialog->setPath(defaultPath);
69     dialog->setProjectName(TestWizardDialog::uniqueProjectName(defaultPath));
70     return dialog;
71 }
72
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";
78
79 static inline void writeVoidMemberDeclaration(QTextStream &str,
80                                               const QString &indent,
81                                               const QString &methodName)
82 {
83     str << indent << "void " << methodName << "();\n";
84 }
85
86 static inline void writeVoidMemberBody(QTextStream &str,
87                                        const QString &className,
88                                        const QString &methodName,
89                                        bool close = true)
90 {
91     str << "void " << className << "::" << methodName << "()\n{\n";
92     if (close)
93         str << closeFunctionC;
94 }
95
96 static QString generateTestCode(const TestWizardParameters &testParams,
97                                 const QString &sourceBaseName)
98 {
99     QString rc;
100     const QString indent = QString(4, QLatin1Char(' '));
101     QTextStream str(&rc);
102     // Includes
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";
107     // Class declaration
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));
114     }
115     const QString dataSlot = testParams.testSlot + QLatin1String("_data");
116     writeVoidMemberDeclaration(str, indent, testParams.testSlot);
117     if (testParams.useDataSet)
118         writeVoidMemberDeclaration(str, indent, dataSlot);
119     str << "};\n\n";
120     // Code: Constructor
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));
126     }
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";
131     }
132     switch (testParams.type) {
133     case TestWizardParameters::Test:
134         str << indent << "QVERIFY2(true, \"Failure\");\n";
135         break;
136     case TestWizardParameters::Benchmark:
137         str << indent << "QBENCHMARK {\n" << indent << "}\n";
138         break;
139     }
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"
146             << closeFunctionC;
147     }
148     // Main & moc include
149     str << (testParams.requiresQApplication ? "QTEST_MAIN" : "QTEST_APPLESS_MAIN")
150         << '(' << testParams.className << ");\n\n"
151         << "#include \"" << sourceBaseName << ".moc\"\n";
152     return rc;
153 }
154
155 Core::GeneratedFiles TestWizard::generateFiles(const QWizard *w, QString *errorMessage) const
156 {
157     Q_UNUSED(errorMessage)
158
159     const TestWizardDialog *wizardDialog = qobject_cast<const TestWizardDialog *>(w);
160     QTC_ASSERT(wizardDialog, return Core::GeneratedFiles())
161
162     const QtProjectParameters projectParams = wizardDialog->projectParameters();
163     const TestWizardParameters testParams = wizardDialog->testParameters();
164     const QString projectPath = projectParams.projectPath();
165
166     // Create files: Source
167     const QString sourceFilePath = Core::BaseFileWizard::buildFileName(projectPath, testParams.fileName, sourceSuffix());
168     const QFileInfo sourceFileInfo(sourceFilePath);
169
170     Core::GeneratedFile source(sourceFilePath);
171     source.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
172     source.setContents(generateTestCode(testParams, sourceFileInfo.baseName()));
173
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);
178     QString contents;
179     {
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";
185     }
186     profile.setContents(contents);
187
188     return Core::GeneratedFiles() <<  source << profile;
189 }
190
191 } // namespace Internal
192 } // namespace Qt4ProjectManager