File indexing completed on 2024-04-28 04:35:54

0001 /*
0002  * This file is part of KDevelop
0003  * Copyright (C) 2012-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 
0020 // Qt + KDevelop
0021 #include <QtTest/QTest>
0022 #include <tests/autotestshell.h>
0023 #include <tests/testcore.h>
0024 #include <language/duchain/duchain.h>
0025 #include <language/codegen/coderepresentation.h>
0026 
0027 // Ruby
0028 #include <duchain/helpers.h>
0029 #include <duchain/tests/duchaintestbase.h>
0030 #include <duchain/editorintegrator.h>
0031 #include <duchain/builders/declarationbuilder.h>
0032 #include <parser/parser.h>
0033 #include <parser/ast.h>
0034 
0035 
0036 using namespace KDevelop;
0037 namespace ruby
0038 {
0039 
0040 DUChainTestBase::DUChainTestBase()
0041 {
0042     /* There's nothing to do here */
0043 }
0044 
0045 TopDUContext * DUChainTestBase::parse(const QByteArray &code, const QString &id)
0046 {
0047     QUrl url("/tmp/kdevruby_" + id + ".rb");
0048     QFile f(url.path());
0049     f.open(QIODevice::WriteOnly);
0050     f.write(code);
0051     f.close();
0052 
0053     return DUChain::self()->waitForUpdate(KDevelop::IndexedString(url.path()),
0054                                           TopDUContext::AllDeclarationsContextsAndUses | TopDUContext::ForceUpdate);
0055 }
0056 
0057 Declaration * DUChainTestBase::getBuiltinDeclaration(const QString &name, TopDUContext *top, DUContext *ctx)
0058 {
0059     QStringList list = name.split("#");
0060     DUContext *context = (ctx) ? ctx : top->childContexts().first();
0061     AbstractType::Ptr type = getBuiltinsType(list.first(), context);
0062     auto sType = type.staticCast<StructureType>();
0063     Declaration *d = sType->declaration(top);
0064 
0065     QualifiedIdentifier id(list.first() + "::" + list.last());
0066     QList<Declaration *> decls = d->internalContext()->findDeclarations(id);
0067     return (decls.isEmpty()) ? nullptr : decls.last();
0068 }
0069 
0070 void DUChainTestBase::initTestCase()
0071 {
0072     AutoTestShell::init();
0073     TestCore::initialize(Core::NoUi);
0074 
0075     DUChain::self()->disablePersistentStorage();
0076     KDevelop::CodeRepresentation::setDiskChangesForbidden(true);
0077 }
0078 
0079 void DUChainTestBase::cleanupTestCase()
0080 {
0081     TestCore::shutdown();
0082 }
0083 
0084 }