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

0001 /*
0002     SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com>
0003     SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KDEVPLATFORM_TEST_PROJECT_H
0009 #define KDEVPLATFORM_TEST_PROJECT_H
0010 
0011 #include <QSet>
0012 
0013 #include <interfaces/iproject.h>
0014 
0015 #include <serialization/indexedstring.h>
0016 #include <shell/projectcontroller.h>
0017 
0018 #include "testsexport.h"
0019 #include <util/path.h>
0020 
0021 namespace KDevelop {
0022 /**
0023  * Dummy Project than can be used for Unit Tests.
0024  *
0025  * Currently only FileSet methods are implemented.
0026  */
0027 class KDEVPLATFORMTESTS_EXPORT TestProject
0028     : public IProject
0029 {
0030     Q_OBJECT
0031 
0032 public:
0033     /**
0034      * @p url Path to project directory.
0035      */
0036     explicit TestProject(const Path& url = Path(), QObject* parent = nullptr);
0037     ~TestProject() override;
0038     IProjectFileManager* projectFileManager() const override { return nullptr; }
0039     IBuildSystemManager* buildSystemManager() const override { return nullptr; }
0040     IPlugin* managerPlugin() const override { return nullptr; }
0041     IPlugin* versionControlPlugin() const override { return nullptr; }
0042     ProjectFolderItem* projectItem() const override;
0043     void setProjectItem(ProjectFolderItem* item);
0044     int fileCount() const { return 0; }
0045     ProjectFileItem* fileAt(int) const { return nullptr; }
0046     QList<ProjectFileItem*> files() const;
0047     QList<ProjectBaseItem*> itemsForPath(const IndexedString&) const override { return QList<ProjectBaseItem*>(); }
0048     QList<ProjectFileItem*> filesForPath(const IndexedString&) const override;
0049     QList<ProjectFolderItem*> foldersForPath(const IndexedString&) const override
0050     {
0051         return QList<ProjectFolderItem*>();
0052     }
0053     void reloadModel() override { }
0054     void close() override {}
0055     Path projectFile() const override;
0056     KSharedConfigPtr projectConfiguration() const override { return m_projectConfiguration; }
0057     void addToFileSet(ProjectFileItem* file) override;
0058     void removeFromFileSet(ProjectFileItem* file) override;
0059     QSet<IndexedString> fileSet() const override { return m_fileSet; }
0060     bool isReady() const override { return true; }
0061 
0062     void setPath(const Path& path);
0063 
0064     Path path() const override;
0065     QString name() const override { return QStringLiteral("Test Project"); }
0066     bool inProject(const IndexedString& path) const override;
0067     void setReloadJob(KJob*) override {}
0068 
0069 private:
0070     QSet<IndexedString> m_fileSet;
0071     Path m_path;
0072     ProjectFolderItem* m_root = nullptr;
0073     KSharedConfigPtr m_projectConfiguration;
0074 };
0075 
0076 /**
0077  * ProjectController that can clear open projects. Useful in Unit Tests.
0078  */
0079 class KDEVPLATFORMTESTS_EXPORT TestProjectController
0080     : public ProjectController
0081 {
0082     Q_OBJECT
0083 
0084 public:
0085     explicit TestProjectController(Core* core) : ProjectController(core) {}
0086 
0087 public:
0088     using ProjectController::addProject;
0089     using ProjectController::takeProject;
0090 
0091     void initialize() override;
0092 };
0093 
0094 namespace TestProjectUtils {
0095 template<class T>
0096 T* createChild(ProjectFolderItem* parent, const QString& childName)
0097 {
0098     return new T(childName, parent);
0099 }
0100 
0101 /**
0102  * Iterate over all files and directories inside the given directory
0103  * in the file system recursively and create a project item for each.
0104  */
0105 KDEVPLATFORMTESTS_EXPORT void addChildrenFromFileSystem(ProjectFolderItem* parent);
0106 } // namespace TestProjectUtils
0107 } // namespace KDevelop
0108 #endif