Reduce the scope of helper class

This commit is contained in:
Chris Thrasher 2022-06-25 11:54:57 -06:00 committed by Vittorio Romeo
parent 0812054e02
commit 44d3d63ade
3 changed files with 54 additions and 73 deletions

View File

@ -1,10 +1,62 @@
#include <SFML/System/FileInputStream.hpp> #include <SFML/System/FileInputStream.hpp>
#include "SystemUtil.hpp" #include <cassert>
#include <fstream>
#include <sstream>
#include <string_view> #include <string_view>
#include <utility> #include <utility>
#include <doctest.h> #include <doctest.h>
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]") TEST_CASE("sf::FileInputStream class - [system]")
{ {
SUBCASE("Empty stream") SUBCASE("Empty stream")
@ -20,7 +72,7 @@ TEST_CASE("sf::FileInputStream class - [system]")
{ {
const std::string fileContents = "hello world"; const std::string fileContents = "hello world";
sf::Testing::TemporaryFile tmpFile(fileContents); TemporaryFile tmpFile(fileContents);
sf::FileInputStream fis; sf::FileInputStream fis;
REQUIRE(fis.open(tmpFile.getPath())); REQUIRE(fis.open(tmpFile.getPath()));

View File

@ -5,13 +5,6 @@
#include <SFML/System/Time.hpp> #include <SFML/System/Time.hpp>
#include <doctest.h> // for Approx #include <doctest.h> // for Approx
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <limits>
#include <ostream>
#include <sstream>
namespace sf namespace sf
{ {
@ -54,42 +47,3 @@ bool operator==(const sf::Angle& lhs, const Approx<sf::Angle>& rhs)
{ {
return lhs.asDegrees() == Approx(rhs.value.asDegrees()); 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;
}
}

View File

@ -12,8 +12,6 @@
#include <iomanip> #include <iomanip>
#include <limits> #include <limits>
#include <ostream> #include <ostream>
#include <sstream>
#include <string>
// String conversions for doctest framework // String conversions for doctest framework
namespace sf namespace sf
@ -65,27 +63,4 @@ std::ostream& operator <<(std::ostream& os, const Approx<T>& approx)
return os << approx.value; 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 #endif // SFML_TESTUTILITIES_SYSTEM_HPP