File indexing completed on 2024-05-05 04:35:22

0001 /* This file is part of KDevelop
0002     Copyright 2006 Hamish Rodda <rodda@kde.org>
0003     Copyright 2008 Niko Sams <niko.sams@gmail.com>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License version 2 as published by the Free Software Foundation.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "duchaintestbase.h"
0021 
0022 #include <QTest>
0023 
0024 #include <language/duchain/duchaindumper.h>
0025 #include <language/duchain/parsingenvironment.h>
0026 #include <language/duchain/duchainlock.h>
0027 #include <language/duchain/topducontext.h>
0028 #include <serialization/indexedstring.h>
0029 #include <language/codegen/coderepresentation.h>
0030 #include <tests/autotestshell.h>
0031 #include <tests/testcore.h>
0032 
0033 #include "../../parser/parsesession.h"
0034 #include "../../parser/editorintegrator.h"
0035 #include "cssdebugvisitor.h"
0036 #include "../builders/declarationbuilder.h"
0037 
0038 using namespace Css;
0039 using namespace KDevelop;
0040 
0041 DUChainTestBase::DUChainTestBase()
0042 {
0043 }
0044 
0045 void DUChainTestBase::initTestCase()
0046 {
0047     AutoTestShell::init();
0048     TestCore::initialize(Core::NoUi);
0049 
0050     DUChain::self()->disablePersistentStorage();
0051     CodeRepresentation::setDiskChangesForbidden(true);
0052 }
0053 
0054 void DUChainTestBase::cleanupTestCase()
0055 {
0056     TestCore::shutdown();
0057 }
0058 
0059 TopDUContext* DUChainTestBase::parse(const QByteArray& unit, DumpAreas dump, QString url)
0060 {
0061     if (dump)
0062         qDebug() << "==== Beginning new test case...:" << endl << unit;
0063 
0064     ParseSession* session = new ParseSession();
0065     session->setContents(unit);
0066     StartAst* ast = nullptr;
0067     if (!session->parse(&ast)) {
0068         qDebug() << "Parse failed";
0069         return nullptr;
0070     }
0071 
0072     if (dump & DumpAST) {
0073         qDebug() << "===== AST:";
0074         DebugVisitor debugVisitor(session->tokenStream(), session->contents());
0075         debugVisitor.visitNode(ast);
0076     }
0077 
0078     static int testNumber = 0;
0079     if (url.isEmpty()) url = QString("file:///internal/%1").arg(testNumber++);
0080 
0081     EditorIntegrator editor;
0082     editor.setParseSession(session);
0083     DeclarationBuilder builder(&editor);
0084     TopDUContext* top = builder.build(IndexedString(url), ast);
0085 
0086     if (dump & DumpDUChain) {
0087         qDebug() << "===== DUChain:";
0088 
0089         DUChainReadLocker lock;
0090         DUChainDumper dumper;
0091         dumper.dump(top);
0092     }
0093 
0094     if (dump)
0095         qDebug() << "===== Finished test case.";
0096 
0097     delete session;
0098 
0099     return top;
0100 }
0101