Add 'NetworkUtil' for network module tests
This commit is contained in:
parent
ff4fc05b18
commit
3dc8fc0923
@ -36,13 +36,10 @@
|
||||
#include <cstdio>
|
||||
|
||||
#ifdef SFML_SYSTEM_ANDROID
|
||||
namespace sf
|
||||
{
|
||||
namespace priv
|
||||
namespace sf::priv
|
||||
{
|
||||
class SFML_SYSTEM_API ResourceStream;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@ -79,6 +76,18 @@ public:
|
||||
////////////////////////////////////////////////////////////
|
||||
FileInputStream& operator=(const FileInputStream&) = delete;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Move constructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
FileInputStream(FileInputStream&&);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Move assignment
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
FileInputStream& operator=(FileInputStream&&);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Open the stream from a file path
|
||||
///
|
||||
|
@ -31,7 +31,6 @@
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Export.hpp>
|
||||
#include <chrono>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
namespace sf
|
||||
|
@ -42,6 +42,7 @@ void FileInputStream::FileCloser::operator()(std::FILE* file)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
FileInputStream::FileInputStream() = default;
|
||||
|
||||
@ -50,6 +51,14 @@ FileInputStream::FileInputStream() = default;
|
||||
FileInputStream::~FileInputStream() = default;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
FileInputStream::FileInputStream(FileInputStream&&) = default;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
FileInputStream& FileInputStream::operator=(FileInputStream&&) = default;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool FileInputStream::open(const std::string& filename)
|
||||
{
|
||||
|
@ -8,6 +8,7 @@ target_compile_features(sfml-test-main PRIVATE cxx_std_17)
|
||||
|
||||
# System is always built
|
||||
SET(SYSTEM_SRC
|
||||
"${SRCROOT}/System/FileInputStream.cpp"
|
||||
"${SRCROOT}/System/Time.cpp"
|
||||
"${SRCROOT}/System/Vector2.cpp"
|
||||
"${SRCROOT}/System/Vector3.cpp"
|
||||
|
42
test/System/FileInputStream.cpp
Normal file
42
test/System/FileInputStream.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include <SFML/System/FileInputStream.hpp>
|
||||
#include "SystemUtil.hpp"
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
TEST_CASE("sf::FileInputStream class - [system]")
|
||||
{
|
||||
SUBCASE("Empty stream")
|
||||
{
|
||||
sf::FileInputStream fis;
|
||||
|
||||
CHECK(fis.read(nullptr, 0) == -1);
|
||||
CHECK(fis.seek(0) == -1);
|
||||
CHECK(fis.tell() == -1);
|
||||
}
|
||||
|
||||
// Work around GCC 8.x bug with `<filesystem>`.
|
||||
#if !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
SUBCASE("Temporary file stream")
|
||||
{
|
||||
const std::string fileContents = "hello world";
|
||||
|
||||
sf::Testing::TemporaryFile tmpFile(fileContents);
|
||||
sf::FileInputStream fis;
|
||||
|
||||
REQUIRE(fis.open(tmpFile.getPath()));
|
||||
|
||||
char buffer[32];
|
||||
|
||||
CHECK(fis.read(buffer, 5) == 5);
|
||||
CHECK(std::string_view(buffer, 5) == std::string_view(fileContents.c_str(), 5));
|
||||
|
||||
SUBCASE("Move semantics")
|
||||
{
|
||||
sf::FileInputStream fis2 = std::move(fis);
|
||||
|
||||
CHECK(fis2.read(buffer, 6) == 6);
|
||||
CHECK(std::string_view(buffer, 6) == std::string_view(fileContents.c_str() + 5, 6));
|
||||
}
|
||||
}
|
||||
#endif // !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
}
|
@ -1,7 +1,16 @@
|
||||
// Note: No need to increase compile time by including TestUtilities/System.hpp
|
||||
#include "SystemUtil.hpp"
|
||||
|
||||
#include <SFML/System/String.hpp>
|
||||
#include <SFML/System/Time.hpp>
|
||||
|
||||
// Work around GCC 8.x bug with `<filesystem>`.
|
||||
#if !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
#include <filesystem>
|
||||
#endif // !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
@ -19,3 +28,45 @@ namespace sf
|
||||
return stream.str().c_str();
|
||||
}
|
||||
}
|
||||
|
||||
// Work around GCC 8.x bug with `<filesystem>`.
|
||||
#if !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
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;
|
||||
}
|
||||
}
|
||||
#endif // !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/System/Vector3.hpp>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
// String conversions for doctest framework
|
||||
namespace sf
|
||||
@ -38,4 +39,30 @@ namespace sf
|
||||
}
|
||||
}
|
||||
|
||||
// Work around GCC 8.x bug with `<filesystem>`.
|
||||
#if !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
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 // !defined(__GNUC__) || (__GNUC__ >= 9)
|
||||
|
||||
#endif // SFML_TESTUTILITIES_SYSTEM_HPP
|
||||
|
Loading…
Reference in New Issue
Block a user