From 206dcfb234147c76f3953a6ae611418754343156 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Wed, 12 Jun 2024 13:02:49 -0500 Subject: [PATCH] Add tests for expected error messages --- test/Graphics/Image.test.cpp | 60 +++++++++++++++++++++++-------- test/TestUtilities/SystemUtil.cpp | 16 +++++++++ test/TestUtilities/SystemUtil.hpp | 13 +++++++ 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/test/Graphics/Image.test.cpp b/test/Graphics/Image.test.cpp index 00e74e857..c6460c3b5 100644 --- a/test/Graphics/Image.test.cpp +++ b/test/Graphics/Image.test.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -78,12 +79,21 @@ TEST_CASE("[Graphics] sf::Image") } } + ErrReader errReader; + SECTION("loadFromFile()") { - SECTION("Invalid file") + SECTION("Invalid path") { CHECK(!sf::Image::loadFromFile(".")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to load image")); + } + + SECTION("Nonexistent file") + { CHECK(!sf::Image::loadFromFile("this/does/not/exist.jpg")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to load image")); + CHECK_THAT(errReader.get(), Catch::Matchers::ContainsSubstring("this/does/not/exist.jpg")); } SECTION("Successful load") @@ -132,6 +142,7 @@ TEST_CASE("[Graphics] sf::Image") CHECK(image->getSize() == sf::Vector2u(1001, 304)); CHECK(image->getPixelsPtr() != nullptr); + CHECK(errReader.get().empty()); } } @@ -140,12 +151,14 @@ TEST_CASE("[Graphics] sf::Image") SECTION("Invalid pointer") { CHECK(!sf::Image::loadFromMemory(nullptr, 1)); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to load image from memory, no data provided")); } SECTION("Invalid size") { const std::byte testByte{0xAB}; CHECK(!sf::Image::loadFromMemory(&testByte, 0)); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to load image from memory, no data provided")); } SECTION("Failed load") @@ -154,15 +167,19 @@ TEST_CASE("[Graphics] sf::Image") SECTION("Empty") { - memory.clear(); + CHECK(!sf::Image::loadFromMemory(memory.data(), memory.size())); + CHECK_THAT(errReader.get(), + Catch::Matchers::StartsWith("Failed to load image from memory, no data provided")); } SECTION("Junk data") { memory = {1, 2, 3, 4}; + CHECK(!sf::Image::loadFromMemory(memory.data(), memory.size())); + CHECK_THAT(errReader.get(), + Catch::Matchers::StartsWith("Failed to load image from memory. Reason: Image not of any " + "known type, or corrupt")); } - - CHECK(!sf::Image::loadFromMemory(memory.data(), memory.size())); } SECTION("Successful load") @@ -173,6 +190,7 @@ TEST_CASE("[Graphics] sf::Image") CHECK(image.getPixelsPtr() != nullptr); CHECK(image.getPixel({0, 0}) == sf::Color::Green); CHECK(image.getPixel({23, 23}) == sf::Color::Green); + CHECK(errReader.get().empty()); } } @@ -184,6 +202,7 @@ TEST_CASE("[Graphics] sf::Image") CHECK(image.getPixelsPtr() != nullptr); CHECK(image.getPixel({0, 0}) == sf::Color(255, 255, 255, 0)); CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62)); + CHECK(errReader.get().empty()); } SECTION("saveToFile()") @@ -191,6 +210,9 @@ TEST_CASE("[Graphics] sf::Image") SECTION("Invalid size") { CHECK(!sf::Image({10, 0}, sf::Color::Magenta).saveToFile("test.jpg")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to save image")); + CHECK_THAT(errReader.get(), Catch::Matchers::ContainsSubstring("test.jpg")); + CHECK(!sf::Image({0, 10}, sf::Color::Magenta).saveToFile("test.jpg")); } @@ -199,12 +221,19 @@ TEST_CASE("[Graphics] sf::Image") SECTION("No extension") { CHECK(!image.saveToFile("wheresmyextension")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Image file extension \"\" not supported")); + CHECK_THAT(errReader.get(), Catch::Matchers::ContainsSubstring("wheresmyextension")); + CHECK(!image.saveToFile("pls/add/extension")); + CHECK_THAT(errReader.get(), Catch::Matchers::ContainsSubstring("pls/add/extension")); } SECTION("Invalid extension") { CHECK(!image.saveToFile("test.ps")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Image file extension \".ps\" not supported")); + CHECK_THAT(errReader.get(), Catch::Matchers::ContainsSubstring("test.ps")); + CHECK(!image.saveToFile("test.foo")); } @@ -235,6 +264,7 @@ TEST_CASE("[Graphics] sf::Image") const auto loadedImage = sf::Image::loadFromFile(filename).value(); CHECK(loadedImage.getSize() == sf::Vector2u(256, 256)); CHECK(loadedImage.getPixelsPtr() != nullptr); + CHECK(errReader.get().empty()); CHECK(std::filesystem::remove(filename)); } @@ -245,6 +275,9 @@ TEST_CASE("[Graphics] sf::Image") SECTION("Invalid size") { CHECK(!sf::Image({10, 0}, sf::Color::Magenta).saveToMemory("test.jpg")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to save image with format \"test.jpg\"")); + CHECK_THAT(errReader.get(), Catch::Matchers::ContainsSubstring("test.jpg")); + CHECK(!sf::Image({0, 10}, sf::Color::Magenta).saveToMemory("test.jpg")); } @@ -253,24 +286,23 @@ TEST_CASE("[Graphics] sf::Image") SECTION("No extension") { CHECK(!image.saveToMemory("")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to save image with format \"\"")); } SECTION("Invalid extension") { CHECK(!image.saveToMemory(".")); + CHECK_THAT(errReader.get(), Catch::Matchers::StartsWith("Failed to save image with format \".\"")); + CHECK(!image.saveToMemory("gif")); CHECK(!image.saveToMemory(".jpg")); // Supposed to be "jpg" } SECTION("Successful save") { - std::optional> maybeOutput; - SECTION("To bmp") { - maybeOutput = image.saveToMemory("bmp"); - REQUIRE(maybeOutput.has_value()); - const auto& output = *maybeOutput; + const auto output = image.saveToMemory("bmp").value(); REQUIRE(output.size() == 1146); CHECK(output[0] == 66); CHECK(output[1] == 77); @@ -284,9 +316,7 @@ TEST_CASE("[Graphics] sf::Image") SECTION("To tga") { - maybeOutput = image.saveToMemory("tga"); - REQUIRE(maybeOutput.has_value()); - const auto& output = *maybeOutput; + const auto output = image.saveToMemory("tga").value(); REQUIRE(output.size() == 98); CHECK(output[0] == 0); CHECK(output[1] == 0); @@ -296,9 +326,7 @@ TEST_CASE("[Graphics] sf::Image") SECTION("To png") { - maybeOutput = image.saveToMemory("png"); - REQUIRE(maybeOutput.has_value()); - const auto& output = *maybeOutput; + const auto output = image.saveToMemory("png").value(); REQUIRE(output.size() == 92); CHECK(output[0] == 137); CHECK(output[1] == 80); @@ -306,6 +334,8 @@ TEST_CASE("[Graphics] sf::Image") CHECK(output[3] == 71); } + CHECK(errReader.get().empty()); + // Cannot test JPEG encoding due to it triggering UB in stbiw__jpg_writeBits } } diff --git a/test/TestUtilities/SystemUtil.cpp b/test/TestUtilities/SystemUtil.cpp index de2b25c3f..3b4180849 100644 --- a/test/TestUtilities/SystemUtil.cpp +++ b/test/TestUtilities/SystemUtil.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -92,3 +93,18 @@ std::vector loadIntoMemory(const std::filesystem::path& path) assert(result); return buffer; } + +ErrReader::ErrReader() : m_stringBuffer(sf::err().rdbuf()) +{ + sf::err().rdbuf(m_stream.rdbuf()); +} + +ErrReader::~ErrReader() +{ + sf::err().rdbuf(m_stringBuffer); +} + +std::string ErrReader::get() +{ + return m_stream.str(); +} diff --git a/test/TestUtilities/SystemUtil.hpp b/test/TestUtilities/SystemUtil.hpp index de00cc7a9..0db88efe3 100644 --- a/test/TestUtilities/SystemUtil.hpp +++ b/test/TestUtilities/SystemUtil.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -64,3 +65,15 @@ std::ostream& operator<<(std::ostream& os, const Approx& approx) } [[nodiscard]] std::vector loadIntoMemory(const std::filesystem::path& path); + +class ErrReader +{ +public: + ErrReader(); + ~ErrReader(); + std::string get(); + +private: + std::streambuf* m_stringBuffer; + std::stringstream m_stream; +};