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 #ifndef RUBY_DUCHAINTESTBASE_H
0021 #define RUBY_DUCHAINTESTBASE_H
0022 
0023 
0024 // Qt + KDevelop
0025 #include <QtCore/QObject>
0026 #include <language/duchain/duchain.h>
0027 #include <language/duchain/duchainlock.h>
0028 
0029 /*
0030  * This macro is used to annotate that the only purpose of a test is to
0031  * check that the code does not crash.
0032  */
0033 #define DOES_NOT_CRASH QVERIFY(true)
0034 
0035 
0036 namespace ruby {
0037 
0038 /**
0039  * Manage pointer to TopDUContexts and release them properly, even if a test
0040  * fails. Shamelessly copied from the PHP plugin :)
0041  */
0042 struct DUChainReleaser {
0043     DUChainReleaser(KDevelop::TopDUContext *top) : m_top(top) {}
0044     ~DUChainReleaser()
0045     {
0046         KDevelop::DUChainWriteLocker lock;
0047         KDevelop::DUChain::self()->removeDocumentChain(m_top);
0048     }
0049     KDevelop::TopDUContext *m_top;
0050 };
0051 
0052 
0053 /**
0054  * @class DUChainTestBase
0055  * The base class for all DUChain Test classes.
0056  */
0057 class DUChainTestBase : public QObject
0058 {
0059     Q_OBJECT
0060 
0061 public:
0062     DUChainTestBase();
0063 
0064 protected:
0065     /**
0066      * Parse the given @p code that belongs to the test identified by @p id.
0067      *
0068      * @returns the KDevelop::TopDUContext for the given code, or nullptr if
0069      * something failed (i.e. parse error).
0070      */
0071     virtual KDevelop::TopDUContext * parse(const QByteArray &code, const QString &id);
0072 
0073 
0074     /**
0075      * Get a builtin method declaration.
0076      *
0077      * @param name The name of the method in a format such as "String#bytesize"
0078      * @param top The TopDUContext that we've got from parsing.
0079      * @param ctx Optional DUContext. Set this when you don't want a child
0080      * context from the @p top to be used.
0081      * @returns the Declaration of the required builtin method.
0082      */
0083     KDevelop::Declaration * getBuiltinDeclaration(const QString &name, KDevelop::TopDUContext *top,
0084                                                   KDevelop::DUContext *ctx = nullptr);
0085 
0086 public slots:
0087     void initTestCase();
0088     void cleanupTestCase();
0089 };
0090 
0091 }
0092 
0093 
0094 #endif /* RUBY_DUCHAINTESTBASE_H */