From dd41762b70e0efa5705c49ad65b6402cf0568e9c Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 23 Sep 2020 22:49:33 +0200 Subject: [PATCH] Add testDirPath() for finding directories with test files The same as testFilePath(), just for directories. --- tests/testutils.cpp | 26 ++++++++++++++++++++++---- tests/testutils.h | 10 ++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/testutils.cpp b/tests/testutils.cpp index f41e1dd..c9ec063 100644 --- a/tests/testutils.cpp +++ b/tests/testutils.cpp @@ -233,16 +233,34 @@ TestApplication::~TestApplication() * 3. The subdirectory "testfiles" within the source directory, if it could be determined via "srcref"-file. * 4. The subdirectory "testfiles" within present working directory. */ -string TestApplication::testFilePath(const string &relativeTestFilePath) const +std::string TestApplication::testFilePath(const std::string &relativeTestFilePath) const { - string path; + std::string path; for (const auto &testFilesPath : m_testFilesPaths) { if (fileExists(path = testFilesPath + relativeTestFilePath)) { return path; } } - throw runtime_error("The testfile \"" % relativeTestFilePath % "\" can not be located. Was looking under:" - + joinStrings(m_testFilesPaths, "\n", false, string(), relativeTestFilePath)); + throw std::runtime_error("The test file \"" % relativeTestFilePath % "\" can not be located. Was looking under:" + + joinStrings(m_testFilesPaths, "\n", false, std::string(), relativeTestFilePath)); +} + +/*! + * \brief Returns the full path of the test directory with the specified \a relativeTestDirPath. + * + * This is the same as TestApplication::testFilePath() but for directories. Checkout the documentation of + * TestApplication::testFilePath() for details about the lookup. + */ +std::string TestApplication::testDirPath(const std::string &relativeTestDirPath) const +{ + std::string path; + for (const auto &testFilesPath : m_testFilesPaths) { + if (dirExists(path = testFilesPath + relativeTestDirPath)) { + return path; + } + } + throw std::runtime_error("The test directory \"" % relativeTestDirPath % "\" can not be located. Was looking under:" + + joinStrings(m_testFilesPaths, "\n", false, std::string(), relativeTestDirPath)); } /*! diff --git a/tests/testutils.h b/tests/testutils.h index 4d16901..5a97210 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -28,6 +28,7 @@ public: // helper for tests std::string testFilePath(const std::string &relativeTestFilePath) const; + std::string testDirPath(const std::string &relativeTestDirPath) const; std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy) const; std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy) const; @@ -149,6 +150,15 @@ inline CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relative return TestApplication::instance()->testFilePath(relativeTestFilePath); } +/*! + * \brief Convenience function to invoke TestApplication::testDirPath(). + * \remarks A TestApplication must be present. + */ +inline CPP_UTILITIES_EXPORT std::string testDirPath(const std::string &relativeTestDirPath) +{ + return TestApplication::instance()->testDirPath(relativeTestDirPath); +} + /*! * \brief Convenience function to invoke TestApplication::workingCopyPath(). * \remarks A TestApplication must be present.