From 44d3d63adedac1855ddaf44dbbc059c666c0b4b1 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sat, 25 Jun 2022 11:54:57 -0600 Subject: [PATCH] Reduce the scope of helper class --- test/System/FileInputStream.cpp | 56 +++++++++++++++++++++++++++++-- test/TestUtilities/SystemUtil.cpp | 46 ------------------------- test/TestUtilities/SystemUtil.hpp | 25 -------------- 3 files changed, 54 insertions(+), 73 deletions(-) diff --git a/test/System/FileInputStream.cpp b/test/System/FileInputStream.cpp index 7eab360d8..c169656a3 100644 --- a/test/System/FileInputStream.cpp +++ b/test/System/FileInputStream.cpp @@ -1,9 +1,61 @@ #include -#include "SystemUtil.hpp" +#include +#include +#include #include #include #include + +static std::string getTemporaryFilePath() +{ + static int counter = 0; + + std::ostringstream oss; + oss << "sfmltemp" << counter << ".tmp"; + ++counter; + + std::filesystem::path result; + result /= std::filesystem::temp_directory_path(); + result /= oss.str(); + + return result.string(); +} + +class TemporaryFile +{ +private: + std::string m_path; + +public: + // Create a temporary file with a randomly generated path, containing 'contents'. + TemporaryFile(const std::string& contents) + : m_path(getTemporaryFilePath()) + { + std::ofstream ofs(m_path); + assert(ofs); + + ofs << contents; + assert(ofs); + } + + // Close and delete the generated file. + ~TemporaryFile() + { + [[maybe_unused]] const bool removed = std::filesystem::remove(m_path); + assert(removed); + } + + // Prevent copies. + TemporaryFile(const TemporaryFile&) = delete; + TemporaryFile& operator=(const TemporaryFile&) = delete; + + // Return the randomly generated path. + const std::string& getPath() const + { + return m_path; + } +}; TEST_CASE("sf::FileInputStream class - [system]") { @@ -20,7 +72,7 @@ TEST_CASE("sf::FileInputStream class - [system]") { const std::string fileContents = "hello world"; - sf::Testing::TemporaryFile tmpFile(fileContents); + TemporaryFile tmpFile(fileContents); sf::FileInputStream fis; REQUIRE(fis.open(tmpFile.getPath())); diff --git a/test/TestUtilities/SystemUtil.cpp b/test/TestUtilities/SystemUtil.cpp index e64ca4a33..2bd554bca 100644 --- a/test/TestUtilities/SystemUtil.cpp +++ b/test/TestUtilities/SystemUtil.cpp @@ -5,13 +5,6 @@ #include #include // for Approx -#include -#include -#include -#include -#include -#include -#include namespace sf { @@ -54,42 +47,3 @@ bool operator==(const sf::Angle& lhs, const Approx& rhs) { return lhs.asDegrees() == Approx(rhs.value.asDegrees()); } - -namespace sf::Testing -{ - static std::string getTemporaryFilePath() - { - static int counter = 0; - - std::ostringstream oss; - oss << "sfmltemp" << counter << ".tmp"; - ++counter; - - std::filesystem::path result; - result /= std::filesystem::temp_directory_path(); - result /= oss.str(); - - return result.string(); - } - - TemporaryFile::TemporaryFile(const std::string& contents) - : m_path(getTemporaryFilePath()) - { - std::ofstream ofs(m_path); - assert(ofs); - - ofs << contents; - assert(ofs); - } - - TemporaryFile::~TemporaryFile() - { - [[maybe_unused]] const bool removed = std::filesystem::remove(m_path); - assert(removed); - } - - const std::string& TemporaryFile::getPath() const - { - return m_path; - } -} diff --git a/test/TestUtilities/SystemUtil.hpp b/test/TestUtilities/SystemUtil.hpp index bed407efa..9b268548a 100644 --- a/test/TestUtilities/SystemUtil.hpp +++ b/test/TestUtilities/SystemUtil.hpp @@ -12,8 +12,6 @@ #include #include #include -#include -#include // String conversions for doctest framework namespace sf @@ -65,27 +63,4 @@ std::ostream& operator <<(std::ostream& os, const Approx& approx) return os << approx.value; } -namespace sf::Testing -{ - class TemporaryFile - { - private: - std::string m_path; - - public: - // Create a temporary file with a randomly generated path, containing 'contents'. - TemporaryFile(const std::string& contents); - - // Close and delete the generated file. - ~TemporaryFile(); - - // Prevent copies. - TemporaryFile(const TemporaryFile&) = delete; - TemporaryFile& operator=(const TemporaryFile&) = delete; - - // Return the randomly generated path. - const std::string& getPath() const; - }; -} - #endif // SFML_TESTUTILITIES_SYSTEM_HPP