1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/* This file is part of KDevelop
Copyright 2012 Olivier de Gaalon <olivier.jg@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "test_cppassistants.h"
#include <QtTest/QtTest>
#include <KTempDir>
#include <qtest_kde.h>
#include <tests/autotestshell.h>
#include <tests/testcore.h>
#include <interfaces/foregroundlock.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/iplugincontroller.h>
#include <interfaces/isourceformattercontroller.h>
#include <ktexteditor/view.h>
#include <ktexteditor/document.h>
#include <language/assistant/renameaction.h>
#include <language/assistant/staticassistantsmanager.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainutils.h>
#include <language/codegen/coderepresentation.h>
#include <shell/documentcontroller.h>
#include <interfaces/ilanguage.h>
#include <navigation/macronavigationcontext.h>
#include <navigation/navigationwidget.h>
using namespace KDevelop;
using namespace KTextEditor;
QTEST_KDEMAIN(TestCppAssistants, GUI)
ForegroundLock *globalTestLock = 0;
namespace {
StaticAssistantsManager* staticAssistantsManager() { return Core::self()->languageController()->staticAssistantsManager(); }
}
void TestCppAssistants::initTestCase()
{
AutoTestShell::init(QStringList() << "kdevcppsupport");
TestCore::initialize();
DUChain::self()->disablePersistentStorage();
Core::self()->languageController()->backgroundParser()->setDelay(0);
Core::self()->sourceFormatterController()->disableSourceFormatting(true);
CodeRepresentation::setDiskChangesForbidden(true);
globalTestLock = new ForegroundLock;
}
void TestCppAssistants::cleanupTestCase()
{
Core::self()->cleanup();
delete globalTestLock;
globalTestLock = 0;
}
static QString createFile(const QString& fileContents)
{
static KTempDir dirA;
static int i = 0;
++i;
QFile file(dirA.name() + QString::number(i) + ".cpp");
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write(fileContents.toUtf8());
file.close();
return file.fileName();
}
class Testbed
{
public:
enum TestDoc
{
HeaderDoc,
CppDoc
};
Testbed(const QString& headerContents, const QString& cppContents)
{
m_headerDocument.url = createFile(headerContents);
m_headerDocument.textDoc = openDocument(m_headerDocument.url);
m_cppDocument.url = createFile(QString("#include \"%1\"\n").arg(m_headerDocument.url) + cppContents);
m_cppDocument.textDoc = openDocument(m_cppDocument.url);
}
~Testbed()
{
Core::self()->documentController()->documentForUrl(m_cppDocument.url)->close(KDevelop::IDocument::Discard);
Core::self()->documentController()->documentForUrl(m_headerDocument.url)->close(KDevelop::IDocument::Discard);
staticAssistantsManager()->hideAssistant();
}
void changeDocument(TestDoc which, Range where, const QString& what, bool waitForUpdate = false)
{
TestDocument document;
if (which == CppDoc)
{
document = m_cppDocument;
where = Range(where.start().line() + 1, where.start().column(),
where.end().line() + 1, where.end().column()); //The include adds a line
}
else
document = m_headerDocument;
document.textDoc->activeView()->setSelection(where);
document.textDoc->activeView()->removeSelectionText();
document.textDoc->activeView()->setCursorPosition(where.start());
document.textDoc->activeView()->insertText(what);
QCoreApplication::processEvents();
if (waitForUpdate)
DUChain::self()->waitForUpdate(IndexedString(document.url), KDevelop::TopDUContext::AllDeclarationsAndContexts);
}
QString documentText(TestDoc which)
{
if (which == CppDoc)
{
//The CPP document text shouldn't include the autogenerated include line
QString text = m_cppDocument.textDoc->text();
return text.mid(text.indexOf("\n") + 1);
}
else
return m_headerDocument.textDoc->text();
}
private:
struct TestDocument {
QString url;
Document *textDoc;
};
Document* openDocument(const QString& url)
{
Core::self()->documentController()->openDocument(url);
DUChain::self()->waitForUpdate(IndexedString(url), KDevelop::TopDUContext::AllDeclarationsAndContexts);
return Core::self()->documentController()->documentForUrl(url)->textDocument();
}
TestDocument m_headerDocument;
TestDocument m_cppDocument;
};
/**
* A StateChange describes an insertion/deletion/replacement and the expected result
**/
struct StateChange
{
StateChange(){};
StateChange(Testbed::TestDoc document, const Range& range, const QString& newText, const QString& result)
: document(document)
, range(range)
, newText(newText)
, result(result)
{
}
Testbed::TestDoc document;
Range range;
QString newText;
QString result;
};
Q_DECLARE_METATYPE(StateChange)
Q_DECLARE_METATYPE(QList<StateChange>)
void TestCppAssistants::testRenameAssistant_data()
{
QTest::addColumn<QString>("fileContents");
QTest::addColumn<QString>("oldDeclarationName");
QTest::addColumn<QList<StateChange> >("stateChanges");
QTest::addColumn<QString>("finalFileContents");
QTest::newRow("Prepend Text")
<< "int foo(int i)\n { i = 0; return i; }"
<< "i"
<< (QList<StateChange>() << StateChange(Testbed::CppDoc, Range(0,12,0,12), "u", "ui"))
<< "int foo(int ui)\n { ui = 0; return ui; }";
QTest::newRow("Append Text")
<< "int foo(int i)\n { i = 0; return i; }"
<< "i"
<< (QList<StateChange>() << StateChange(Testbed::CppDoc, Range(0,13,0,13), "d", "id"))
<< "int foo(int id)\n { id = 0; return id; }";
QTest::newRow("Replace Text")
<< "int foo(int i)\n { i = 0; return i; }"
<< "i"
<< (QList<StateChange>() << StateChange(Testbed::CppDoc, Range(0,12,0,13), "u", "u"))
<< "int foo(int u)\n { u = 0; return u; }";
QTest::newRow("Letter-by-Letter")
<< "int foo(int i)\n { i = 0; return i; }"
<< "i"
<< (QList<StateChange>()
<< StateChange(Testbed::CppDoc, Range(0,12,0,13), "", "")
<< StateChange(Testbed::CppDoc, Range(0,12,0,12), "a", "a")
<< StateChange(Testbed::CppDoc, Range(0,13,0,13), "b", "ab")
<< StateChange(Testbed::CppDoc, Range(0,14,0,14), "c", "abc")
)
<< "int foo(int abc)\n { abc = 0; return abc; }";
QTest::newRow("Paste Replace")
<< "int foo(int abg)\n { abg = 0; return abg; }"
<< "abg"
<< (QList<StateChange>() << StateChange(Testbed::CppDoc, Range(0,12,0,15), "abcdefg", "abcdefg"))
<< "int foo(int abcdefg)\n { abcdefg = 0; return abcdefg; }";
QTest::newRow("Paste Insert")
<< "int foo(int abg)\n { abg = 0; return abg; }"
<< "abg"
<< (QList<StateChange>() << StateChange(Testbed::CppDoc, Range(0,14,0,14), "cdef", "abcdefg"))
<< "int foo(int abcdefg)\n { abcdefg = 0; return abcdefg; }";
QTest::newRow("Letter-by-Letter Insert")
<< "int foo(int abg)\n { abg = 0; return abg; }"
<< "abg"
<< (QList<StateChange>()
<< StateChange(Testbed::CppDoc, Range(0,14,0,14), "c", "abcg")
<< StateChange(Testbed::CppDoc, Range(0,15,0,15), "d", "abcdg")
<< StateChange(Testbed::CppDoc, Range(0,16,0,16), "e", "abcdeg")
<< StateChange(Testbed::CppDoc, Range(0,17,0,17), "f", "abcdefg")
)
<< "int foo(int abcdefg)\n { abcdefg = 0; return abcdefg; }";
}
void TestCppAssistants::testRenameAssistant()
{
QFETCH(QString, fileContents);
Testbed testbed("", fileContents);
QFETCH(QString, oldDeclarationName);
QFETCH(QList<StateChange>, stateChanges);
foreach(StateChange stateChange, stateChanges)
{
testbed.changeDocument(Testbed::CppDoc, stateChange.range, stateChange.newText);
if (stateChange.result.isEmpty())
{
QVERIFY(!staticAssistantsManager()->activeAssistant() || !staticAssistantsManager()->activeAssistant()->actions().size());
}
else
{
QVERIFY(staticAssistantsManager()->activeAssistant() && staticAssistantsManager()->activeAssistant()->actions().size());
RenameAction *r = dynamic_cast<RenameAction*>(staticAssistantsManager()->activeAssistant()->actions().first().data());
QCOMPARE(r->oldDeclarationName(), oldDeclarationName);
QCOMPARE(r->newDeclarationName(), stateChange.result);
}
}
if (staticAssistantsManager()->activeAssistant() && staticAssistantsManager()->activeAssistant()->actions().size())
staticAssistantsManager()->activeAssistant()->actions().first()->execute();
QFETCH(QString, finalFileContents);
QCOMPARE(testbed.documentText(Testbed::CppDoc), finalFileContents);
}
void TestCppAssistants::testRenameAssistantUndoRename()
{
Testbed testbed("", "int foo(int i)\n { i = 0; return i; }");
testbed.changeDocument(Testbed::CppDoc, Range(0,13,0,13), "d");
QVERIFY(staticAssistantsManager()->activeAssistant());
QVERIFY(staticAssistantsManager()->activeAssistant()->actions().size() > 0);
RenameAction *r = dynamic_cast<RenameAction*>(staticAssistantsManager()->activeAssistant()->actions().first().data());
QVERIFY(r);
// now rename the variable back to its original identifier
testbed.changeDocument(Testbed::CppDoc, Range(0,13,0,14), "");
// there should be no assistant anymore
QVERIFY(!staticAssistantsManager()->activeAssistant());
}
const QString SHOULD_ASSIST = "SHOULD_ASSIST"; //An assistant will be visible
const QString NO_ASSIST = "NO_ASSIST"; //No assistant visible
void TestCppAssistants::testSignatureAssistant_data()
{
QTest::addColumn<QString>("headerContents");
QTest::addColumn<QString>("cppContents");
QTest::addColumn<QList<StateChange> >("stateChanges");
QTest::addColumn<QString>("finalHeaderContents");
QTest::addColumn<QString>("finalCppContents");
QTest::newRow("Change Argument Type")
<< "class Foo {\nint bar(int a, char* b, int c = 10); \n};"
<< "int Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }"
<< (QList<StateChange>() << StateChange(Testbed::HeaderDoc, Range(1,8,1,11), "char", SHOULD_ASSIST))
<< "class Foo {\nint bar(char a, char* b, int c = 10); \n};"
<< "int Foo::bar(char a, char* b, int c)\n{ a = c; b = new char; return a + *b; }";
QTest::newRow("Change Default Parameter")
<< "class Foo {\nint bar(int a, char* b, int c = 10); \n};"
<< "int Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }"
<< (QList<StateChange>() << StateChange(Testbed::HeaderDoc, Range(1,29,1,34), "", NO_ASSIST))
<< "class Foo {\nint bar(int a, char* b, int c); \n};"
<< "int Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }";
QTest::newRow("Change Function Type")
<< "class Foo {\nint bar(int a, char* b, int c = 10); \n};"
<< "int Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }"
<< (QList<StateChange>() << StateChange(Testbed::CppDoc, Range(0,0,0,3), "char", SHOULD_ASSIST))
<< "class Foo {\nchar bar(int a, char* b, int c = 10); \n};"
<< "char Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }";
//Interesting corner case: trying to do this in only one change will fail, because the declarationBuilder
//reuses the declarations and doesn't reorganize the parameters internally (though it does change the ranges)
//This could be fixed by making the signature assistant reorganize according to range... if anyone cares.
QTest::newRow("Swap Args Definition Side")
<< "class Foo {\nint bar(int a, char* b, int c = 10); \n};"
<< "int Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }"
<< (QList<StateChange>()
<< StateChange(Testbed::CppDoc, Range(0,13,0,28), "char* b, ", SHOULD_ASSIST)
<< StateChange(Testbed::CppDoc, Range(0,22,0,22), "int a,", SHOULD_ASSIST)
)
<< "class Foo {\nint bar(char* b, int a, int c = 10); \n};"
<< "int Foo::bar(char* b, int a, int c)\n{ a = c; b = new char; return a + *b; }";
// see https://bugs.kde.org/show_bug.cgi?id=299393
// actually related to the whitespaces in the header...
QTest::newRow("Change Function Constness")
<< "class Foo {\nvoid bar( const Foo& ) const;\n};"
<< "void Foo::bar(const Foo&) const\n{}"
<< (QList<StateChange>() << StateChange(Testbed::CppDoc, Range(0,25,0,31), "", SHOULD_ASSIST))
<< "class Foo {\nvoid bar( const Foo& );\n};"
<< "void Foo::bar(const Foo&)\n{}";
QTest::newRow("Undo Change")
<< "class Foo {\nint bar(int a, char* b, int c = 10); \n};"
<< "int Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }"
<< (QList<StateChange>()
<< StateChange(Testbed::HeaderDoc, Range(1,8,1,11), "char", SHOULD_ASSIST)
<< StateChange(Testbed::HeaderDoc, Range(1,8,1,12), "int", NO_ASSIST)
)
<< "class Foo {\nint bar(int a, char* b, int c = 10); \n};"
<< "int Foo::bar(int a, char* b, int c)\n{ a = c; b = new char; return a + *b; }";
}
void TestCppAssistants::testSignatureAssistant()
{
QFETCH(QString, headerContents);
QFETCH(QString, cppContents);
Testbed testbed(headerContents, cppContents);
QFETCH(QList<StateChange>, stateChanges);
foreach(StateChange stateChange, stateChanges)
{
testbed.changeDocument(stateChange.document, stateChange.range, stateChange.newText, true);
if (stateChange.result == SHOULD_ASSIST) {
QVERIFY(staticAssistantsManager()->activeAssistant());
QVERIFY(staticAssistantsManager()->activeAssistant()->actions().size());
} else {
QVERIFY(!staticAssistantsManager()->activeAssistant() || !staticAssistantsManager()->activeAssistant()->actions().size());
}
}
if (staticAssistantsManager()->activeAssistant() && staticAssistantsManager()->activeAssistant()->actions().size())
staticAssistantsManager()->activeAssistant()->actions().first()->execute();
QFETCH(QString, finalHeaderContents);
QFETCH(QString, finalCppContents);
QCOMPARE(testbed.documentText(Testbed::HeaderDoc), finalHeaderContents);
QCOMPARE(testbed.documentText(Testbed::CppDoc), finalCppContents);
}
void TestCppAssistants::testMacroExpansion_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("expected");
QTest::addColumn<int>("macroLine");
QTest::newRow("nonewlines") << "#define FOO(arg1,arg2,arg3) BAR(arg1,arg2,arg3)\nint main(){\nFOO(1,2,3)\n}"
<< "BAR(1 ,2 ,3 ) "
<< 2;
QTest::newRow("newlines") << "#define FOO(arg1,arg2,arg3) BAR(arg1,arg2,arg3)\nint main(){\nFOO(1,\n2,\n3)\n}"
<< "BAR(1 ,2 ,3 ) "
<< 2;
QTest::newRow("invalid") << "#define FOO(arg1,arg2,arg3) BAR(arg1,arg2,arg3)\nint main(){\nFOO(1,\n2,\n3\n}"
<< "FOO"
<< 2;
QTest::newRow("multibrace") << "#define FOO(arg1,arg2,arg3) BAR(arg1,arg2,arg3)\nint main(){\nFOO((1),\n2,\n3)\n}"
<< "BAR((1) ,2 ,3 ) "
<< 2;
}
void TestCppAssistants::testMacroExpansion()
{
QFETCH(QString, input);
QFETCH(QString, expected);
QFETCH(int, macroLine);
KUrl url(createFile(input));
Core::self()->documentController()->openDocument(url);
DUChain::self()->waitForUpdate(IndexedString(url), KDevelop::TopDUContext::AllDeclarationsAndContexts);
ILanguage* language = ICore::self()->languageController()->languagesForUrl(url).at(0);
QVERIFY(language);
QWidget *macroWidget = language->languageSupport()->specialLanguageObjectNavigationWidget(url, SimpleCursor(macroLine,0));
QVERIFY(macroWidget);
Cpp::NavigationWidget *macroNavigationWidget = dynamic_cast<Cpp::NavigationWidget*>(macroWidget);
QVERIFY(macroNavigationWidget);
Cpp::MacroNavigationContext *macroContext = dynamic_cast<Cpp::MacroNavigationContext*>(macroNavigationWidget->context().data());
QVERIFY(macroContext);
QCOMPARE(macroContext->body(),expected);
Core::self()->documentController()->documentForUrl(url)->close(KDevelop::IDocument::Discard);
}
#include "test_cppassistants.moc"
|