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 **************************************************************************/
36 #include <CppDocument.h>
37 #include <DiagnosticClient.h>
39 #include <TranslationUnit.h>
43 #include <cpptools/insertionpointlocator.h>
44 #include <cpptools/cpprefactoringchanges.h>
45 #include <cpptools/cpptoolsplugin.h>
46 #include <extensionsystem/pluginmanager.h>
50 #include <QTextDocument>
52 //TESTED_COMPONENT=src/libs/cplusplus
55 Tests for various parts of the code generation. Well, okay, currently it only
56 tests the InsertionPointLocator.
58 using namespace CPlusPlus;
59 using namespace CppTools;
61 class tst_Codegen: public QObject
67 void cleanupTestCase();
68 void public_in_empty_class();
69 void public_in_nonempty_class();
70 void public_before_protected();
71 void private_after_protected();
72 void protected_in_nonempty_class();
73 void protected_betwee_public_and_private();
74 void qtdesigner_integration();
76 ExtensionSystem::PluginManager *pluginManager;
79 void tst_Codegen::initTestCase()
81 pluginManager = new ExtensionSystem::PluginManager;
82 QSettings *settings = new QSettings(QSettings::IniFormat, QSettings::UserScope,
83 QLatin1String("Nokia"), QLatin1String("QtCreator"));
84 pluginManager->setSettings(settings);
85 pluginManager->setFileExtension(QLatin1String("pluginspec"));
86 pluginManager->setPluginPaths(QStringList() << QLatin1String(Q_PLUGIN_PATH));
87 pluginManager->loadPlugins();
90 void tst_Codegen::cleanupTestCase()
92 pluginManager->shutdown();
96 Should insert at line 3, column 1, with "public:\n" as prefix and without suffix.
98 void tst_Codegen::public_in_empty_class()
100 const QByteArray src = "\n"
101 "class Foo\n" // line 1
106 Document::Ptr doc = Document::create("public_in_empty_class");
111 QCOMPARE(doc->diagnosticMessages().size(), 0);
112 QCOMPARE(doc->globalSymbolCount(), 1U);
114 Class *foo = doc->globalSymbolAt(0)->asClass();
116 QCOMPARE(foo->line(), 1U);
117 QCOMPARE(foo->column(), 7U);
120 snapshot.insert(doc);
121 CppRefactoringChanges changes(snapshot);
122 InsertionPointLocator find(&changes);
123 InsertionLocation loc = find.methodDeclarationInClass(
126 InsertionPointLocator::Public);
127 QVERIFY(loc.isValid());
128 QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
129 QVERIFY(loc.suffix().isEmpty());
130 QCOMPARE(loc.line(), 3U);
131 QCOMPARE(loc.column(), 1U);
135 Should insert at line 3, column 1, without prefix and without suffix.
137 void tst_Codegen::public_in_nonempty_class()
139 const QByteArray src = "\n"
140 "class Foo\n" // line 1
142 "public:\n" // line 3
146 Document::Ptr doc = Document::create("public_in_nonempty_class");
151 QCOMPARE(doc->diagnosticMessages().size(), 0);
152 QCOMPARE(doc->globalSymbolCount(), 1U);
154 Class *foo = doc->globalSymbolAt(0)->asClass();
156 QCOMPARE(foo->line(), 1U);
157 QCOMPARE(foo->column(), 7U);
160 snapshot.insert(doc);
161 CppRefactoringChanges changes(snapshot);
162 InsertionPointLocator find(&changes);
163 InsertionLocation loc = find.methodDeclarationInClass(
166 InsertionPointLocator::Public);
167 QVERIFY(loc.isValid());
168 QVERIFY(loc.prefix().isEmpty());
169 QVERIFY(loc.suffix().isEmpty());
170 QCOMPARE(loc.line(), 4U);
171 QCOMPARE(loc.column(), 1U);
175 Should insert at line 3, column 1, with "public:\n" as prefix and "\n suffix.
177 void tst_Codegen::public_before_protected()
179 const QByteArray src = "\n"
180 "class Foo\n" // line 1
182 "protected:\n" // line 3
186 Document::Ptr doc = Document::create("public_before_protected");
191 QCOMPARE(doc->diagnosticMessages().size(), 0);
192 QCOMPARE(doc->globalSymbolCount(), 1U);
194 Class *foo = doc->globalSymbolAt(0)->asClass();
196 QCOMPARE(foo->line(), 1U);
197 QCOMPARE(foo->column(), 7U);
200 snapshot.insert(doc);
201 CppRefactoringChanges changes(snapshot);
202 InsertionPointLocator find(&changes);
203 InsertionLocation loc = find.methodDeclarationInClass(
206 InsertionPointLocator::Public);
207 QVERIFY(loc.isValid());
208 QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
209 QCOMPARE(loc.suffix(), QLatin1String("\n"));
210 QCOMPARE(loc.column(), 1U);
211 QCOMPARE(loc.line(), 3U);
215 Should insert at line 4, column 1, with "private:\n" as prefix and without
218 void tst_Codegen::private_after_protected()
220 const QByteArray src = "\n"
221 "class Foo\n" // line 1
223 "protected:\n" // line 3
227 Document::Ptr doc = Document::create("private_after_protected");
232 QCOMPARE(doc->diagnosticMessages().size(), 0);
233 QCOMPARE(doc->globalSymbolCount(), 1U);
235 Class *foo = doc->globalSymbolAt(0)->asClass();
237 QCOMPARE(foo->line(), 1U);
238 QCOMPARE(foo->column(), 7U);
241 snapshot.insert(doc);
242 CppRefactoringChanges changes(snapshot);
243 InsertionPointLocator find(&changes);
244 InsertionLocation loc = find.methodDeclarationInClass(
247 InsertionPointLocator::Private);
248 QVERIFY(loc.isValid());
249 QCOMPARE(loc.prefix(), QLatin1String("private:\n"));
250 QVERIFY(loc.suffix().isEmpty());
251 QCOMPARE(loc.column(), 1U);
252 QCOMPARE(loc.line(), 4U);
256 Should insert at line 4, column 1, with "protected:\n" as prefix and without
259 void tst_Codegen::protected_in_nonempty_class()
261 const QByteArray src = "\n"
262 "class Foo\n" // line 1
264 "public:\n" // line 3
268 Document::Ptr doc = Document::create("protected_in_nonempty_class");
273 QCOMPARE(doc->diagnosticMessages().size(), 0);
274 QCOMPARE(doc->globalSymbolCount(), 1U);
276 Class *foo = doc->globalSymbolAt(0)->asClass();
278 QCOMPARE(foo->line(), 1U);
279 QCOMPARE(foo->column(), 7U);
282 snapshot.insert(doc);
283 CppRefactoringChanges changes(snapshot);
284 InsertionPointLocator find(&changes);
285 InsertionLocation loc = find.methodDeclarationInClass(
288 InsertionPointLocator::Protected);
289 QVERIFY(loc.isValid());
290 QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
291 QVERIFY(loc.suffix().isEmpty());
292 QCOMPARE(loc.column(), 1U);
293 QCOMPARE(loc.line(), 4U);
297 Should insert at line 4, column 1, with "protected\n" as prefix and "\n" suffix.
299 void tst_Codegen::protected_betwee_public_and_private()
301 const QByteArray src = "\n"
302 "class Foo\n" // line 1
304 "public:\n" // line 3
305 "private:\n" // line 4
309 Document::Ptr doc = Document::create("protected_betwee_public_and_private");
314 QCOMPARE(doc->diagnosticMessages().size(), 0);
315 QCOMPARE(doc->globalSymbolCount(), 1U);
317 Class *foo = doc->globalSymbolAt(0)->asClass();
319 QCOMPARE(foo->line(), 1U);
320 QCOMPARE(foo->column(), 7U);
323 snapshot.insert(doc);
324 CppRefactoringChanges changes(snapshot);
325 InsertionPointLocator find(&changes);
326 InsertionLocation loc = find.methodDeclarationInClass(
329 InsertionPointLocator::Protected);
330 QVERIFY(loc.isValid());
331 QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
332 QCOMPARE(loc.suffix(), QLatin1String("\n"));
333 QCOMPARE(loc.column(), 1U);
334 QCOMPARE(loc.line(), 4U);
338 Should insert at line 18, column 1, with "private slots:\n" as prefix and "\n"
341 This is the typical Qt Designer case, with test-input like what the integration
344 void tst_Codegen::qtdesigner_integration()
346 const QByteArray src = "/**** Some long (C)opyright notice ****/\n"
347 "#ifndef MAINWINDOW_H\n"
348 "#define MAINWINDOW_H\n"
350 "#include <QMainWindow>\n"
353 " class MainWindow;\n"
356 "class MainWindow : public QMainWindow\n" // line 10
360 "public:\n" // line 14
361 " explicit MainWindow(QWidget *parent = 0);\n"
364 "private:\n" // line 18
365 " Ui::MainWindow *ui;\n"
368 "#endif // MAINWINDOW_H\n";
370 Document::Ptr doc = Document::create("qtdesigner_integration");
375 QCOMPARE(doc->diagnosticMessages().size(), 0);
376 QCOMPARE(doc->globalSymbolCount(), 2U);
378 Class *foo = doc->globalSymbolAt(1)->asClass();
380 QCOMPARE(foo->line(), 10U);
381 QCOMPARE(foo->column(), 7U);
384 snapshot.insert(doc);
385 CppRefactoringChanges changes(snapshot);
386 InsertionPointLocator find(&changes);
387 InsertionLocation loc = find.methodDeclarationInClass(
390 InsertionPointLocator::PrivateSlot);
391 QVERIFY(loc.isValid());
392 QCOMPARE(loc.prefix(), QLatin1String("private slots:\n"));
393 QCOMPARE(loc.suffix(), QLatin1String("\n"));
394 QCOMPARE(loc.line(), 18U);
395 QCOMPARE(loc.column(), 1U);
398 QTEST_MAIN(tst_Codegen)
399 #include "tst_codegen.moc"