File indexing completed on 2024-04-28 04:37:35
0001 /* 0002 SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com> 0003 SPDX-FileCopyrightText: 2011 Milian Wolff <mail@milianw.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #ifndef KDEVPLATFORM_TESTFILE_H 0009 #define KDEVPLATFORM_TESTFILE_H 0010 0011 #include <QObject> 0012 0013 #include <language/duchain/topducontext.h> 0014 0015 #include "testsexport.h" 0016 0017 namespace KDevelop { 0018 class TestProject; 0019 class TestFilePrivate; 0020 0021 /** 0022 * Helper file to parse a file using the full KDevelop architecture. 0023 * 0024 * The file will be added to the background parser, and eventually 0025 * parsed by a fitting language plugin, just like a normal file 0026 * would be in an actual KDevelop instance. 0027 * 0028 * Example usage: 0029 * \code 0030 * TestFile file("<?php function foo(){}", "php", project); 0031 * file.parse(TopDUContext::VisibleDeclarationsAndContexts); 0032 * QVERIFY(file.waitForParsed()); 0033 * QVERIFY(file.topContext()); 0034 * \endcode 0035 */ 0036 class KDEVPLATFORMTESTS_EXPORT TestFile 0037 : public QObject 0038 { 0039 Q_OBJECT 0040 0041 public: 0042 /** 0043 * Create a temporary file from @p contents with file extension @p extension. 0044 * 0045 * @param fileExtension the file extension without the dot. 0046 * @param project this file will be added to the project's fileset and gets 0047 * removed from there on destruction 0048 * @param dir optional path to a (sub-) directory in which this file should 0049 * be created. The directory must exist. 0050 * 0051 * Example: 0052 * @code 0053 * TestFile file("int i = 0;", "cpp"); 0054 * file.parse(...); 0055 * file.topContext()->... 0056 * @endcode 0057 */ 0058 TestFile(const QString& contents, const QString& fileExtension, KDevelop::TestProject* project = nullptr, 0059 const QString& dir = QString()); 0060 0061 /** 0062 * Create a temporary file from @p contents with the same file basename as 0063 * @p base but with the given @p fileExtension. 0064 * 0065 * @param fileExtension the new file extension without the dot. 0066 * @param base a different TestFile which is used for this file's basename 0067 * 0068 * This can be used to create e.g. .cpp/.h file pairs: 0069 * 0070 * @code 0071 * TestFile header("...", "h"); 0072 * TestFile impl("...", "cpp", &header); 0073 * @endcode 0074 */ 0075 TestFile(const QString& contents, const QString& fileExtension, const TestFile* base); 0076 0077 /** 0078 * Create a temporary file named @p fileName from @p contents with file extension @p extension. 0079 * 0080 * @param fileExtension the file extension without the dot. 0081 * @param fileName the name to use for the file 0082 * @param project this file will be added to the project's fileset and gets 0083 * removed from there on destruction 0084 * @param dir optional path to a (sub-) directory in which this file should 0085 * be created. The directory must exist. 0086 * 0087 * Example: 0088 * @code 0089 * TestFile file("int i = 0;", "h", "guard_test"); 0090 * @endcode 0091 */ TestFile(const QString& contents, const QString& fileExtension, const QString& fileName, 0092 KDevelop::TestProject* project = nullptr, const QString& dir = QString()); 0093 0094 /** 0095 * Removes temporary file and cleans up. 0096 */ 0097 ~TestFile() override; 0098 0099 /** 0100 * Returns the URL to this file. 0101 */ 0102 IndexedString url() const; 0103 0104 /** 0105 * Trigger (re-)parsing of this file with given @p features and @p priority. 0106 * 0107 * @see KDevelop::DUChain::updateContextForUrl 0108 */ 0109 void parse(TopDUContext::Features features = TopDUContext::AllDeclarationsContextsAndUses, int priority = 1); 0110 0111 /** 0112 * Convenience method: 0113 * Trigger parse and wait for the file to be parsed. Internally calls waitForParsed() 0114 * 0115 * @see waitForParsed() 0116 * @see parse() 0117 */ 0118 bool parseAndWait(TopDUContext::Features features = TopDUContext::AllDeclarationsContextsAndUses, 0119 int priority = 1, int timeout = 5000); 0120 0121 /** 0122 * Blocks current thread and waits until the file has been parsed. 0123 * 0124 * If it has waited longer than @p timeout ms, we return false 0125 * and assume something went wrong. 0126 * 0127 * Otherwise true is returned, indicating parsing finished 0128 * within the timeout interval. 0129 */ 0130 bool waitForParsed(int timeout = 5000); 0131 0132 /** 0133 * Check whether the file has been processed after the last call to @c parse(). 0134 */ 0135 bool isReady() const; 0136 0137 /** 0138 * Returns the @c TopDUContext for the current file, if it has been successfully parsed. 0139 */ 0140 KDevelop::ReferencedTopDUContext topContext(); 0141 0142 /** 0143 * Change the file contents to @p contents. 0144 * 0145 * Use this to test behavior of your parsing code over 0146 * file changes. 0147 */ 0148 void setFileContents(const QString& contents); 0149 0150 /** 0151 * Read the files contents and return them. 0152 */ 0153 QString fileContents() const; 0154 0155 /** 0156 * Set to true when you want to keep the DUChain data. 0157 * 0158 * By default the DUChain data is removed on destruction of the TestFile. 0159 */ 0160 void setKeepDUChainData(bool keep); 0161 bool keepDUChainData() const; 0162 0163 private: 0164 const QScopedPointer<class TestFilePrivate> d_ptr; 0165 Q_DECLARE_PRIVATE(TestFile) 0166 0167 Q_PRIVATE_SLOT(d_func(), void updateReady(const KDevelop::IndexedString& url, KDevelop::ReferencedTopDUContext topContext)) 0168 }; 0169 } 0170 0171 #endif // KDEVPLATFORM_TESTFILE_H