From 3d01efb80a294333f8e82c22d2380e2151cf7fa2 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Tue, 28 Dec 2021 22:03:15 -0700 Subject: [PATCH] Use operator<< for printing during test failure --- test/Graphics/Color.cpp | 2 ++ test/Graphics/Rect.cpp | 2 ++ test/Graphics/Vertex.cpp | 2 ++ test/System/FileInputStream.cpp | 2 ++ test/System/Time.cpp | 2 ++ test/System/Vector2.cpp | 2 ++ test/System/Vector3.cpp | 2 ++ test/TestUtilities/GraphicsUtil.cpp | 18 ++++++++---------- test/TestUtilities/GraphicsUtil.hpp | 9 ++------- test/TestUtilities/SystemUtil.cpp | 15 +++++++-------- test/TestUtilities/SystemUtil.hpp | 24 +++++++++++------------- test/TestUtilities/WindowUtil.cpp | 11 ++++------- test/TestUtilities/WindowUtil.hpp | 13 +++++-------- test/Window/VideoMode.cpp | 2 ++ 14 files changed, 53 insertions(+), 53 deletions(-) diff --git a/test/Graphics/Color.cpp b/test/Graphics/Color.cpp index c2fd0ad33..9cf115a56 100644 --- a/test/Graphics/Color.cpp +++ b/test/Graphics/Color.cpp @@ -1,6 +1,8 @@ #include #include "GraphicsUtil.hpp" +#include + TEST_CASE("sf::Color class - [graphics]") { SUBCASE("Construction") diff --git a/test/Graphics/Rect.cpp b/test/Graphics/Rect.cpp index 6aa54c453..834681c5a 100644 --- a/test/Graphics/Rect.cpp +++ b/test/Graphics/Rect.cpp @@ -2,6 +2,8 @@ #include #include "GraphicsUtil.hpp" +#include + TEST_CASE("sf::Rect class template - [graphics]") { SUBCASE("Construction") diff --git a/test/Graphics/Vertex.cpp b/test/Graphics/Vertex.cpp index 5eb3ed317..b6ea904c3 100644 --- a/test/Graphics/Vertex.cpp +++ b/test/Graphics/Vertex.cpp @@ -1,6 +1,8 @@ #include #include "GraphicsUtil.hpp" +#include + TEST_CASE("sf::Vertex class - [graphics]") { SUBCASE("Construction") diff --git a/test/System/FileInputStream.cpp b/test/System/FileInputStream.cpp index 22f29f5f8..4bbd069dd 100644 --- a/test/System/FileInputStream.cpp +++ b/test/System/FileInputStream.cpp @@ -3,6 +3,8 @@ #include #include +#include + TEST_CASE("sf::FileInputStream class - [system]") { SUBCASE("Empty stream") diff --git a/test/System/Time.cpp b/test/System/Time.cpp index d32f83412..aaed19d9e 100644 --- a/test/System/Time.cpp +++ b/test/System/Time.cpp @@ -1,6 +1,8 @@ #include #include "SystemUtil.hpp" +#include + using doctest::Approx; TEST_CASE("sf::Time class - [system]") diff --git a/test/System/Vector2.cpp b/test/System/Vector2.cpp index 1795d10b1..dd1ebe812 100644 --- a/test/System/Vector2.cpp +++ b/test/System/Vector2.cpp @@ -2,6 +2,8 @@ #include "SystemUtil.hpp" #include +#include + // Use sf::Vector2i for tests. Test coverage is given, as there are no template specializations. TEST_CASE("sf::Vector2 class template - [system]") diff --git a/test/System/Vector3.cpp b/test/System/Vector3.cpp index 55bb08a36..dd96e3c45 100644 --- a/test/System/Vector3.cpp +++ b/test/System/Vector3.cpp @@ -2,6 +2,8 @@ #include "SystemUtil.hpp" #include +#include + // Use sf::Vector3i for tests. Test coverage is given, as there are no template specializations. TEST_CASE("sf::Vector3 class template - [system]") diff --git a/test/TestUtilities/GraphicsUtil.cpp b/test/TestUtilities/GraphicsUtil.cpp index 45879fce2..ebff0532a 100644 --- a/test/TestUtilities/GraphicsUtil.cpp +++ b/test/TestUtilities/GraphicsUtil.cpp @@ -1,19 +1,17 @@ // Note: No need to increase compile time by including TestUtilities/Graphics.hpp #include -#include -#include +#include namespace sf { - doctest::String toString(const sf::Color& color) + std::ostream& operator <<(std::ostream& os, const sf::Color& color) { - std::ostringstream stream; - stream << "0x" << std::hex << color.toInteger() << std::dec - << " (r=" << static_cast(color.r) - << ", g=" << static_cast(color.g) - << ", b=" << static_cast(color.b) - << ", a=" << static_cast(color.a) << ")"; + os << "0x" << std::hex << color.toInteger() << std::dec + << " (r=" << static_cast(color.r) + << ", g=" << static_cast(color.g) + << ", b=" << static_cast(color.b) + << ", a=" << static_cast(color.a) << ")"; - return stream.str().c_str(); + return os; } } diff --git a/test/TestUtilities/GraphicsUtil.hpp b/test/TestUtilities/GraphicsUtil.hpp index 287c72514..9547c1503 100644 --- a/test/TestUtilities/GraphicsUtil.hpp +++ b/test/TestUtilities/GraphicsUtil.hpp @@ -1,6 +1,6 @@ // Header for SFML unit tests. // -// For a new graphics module test case, include this header and not directly. +// For a new graphics module test case, include this header. // This ensures that string conversions are visible and can be used by doctest for debug output. #ifndef SFML_TESTUTILITIES_GRAPHICS_HPP @@ -8,15 +8,10 @@ #include "WindowUtil.hpp" -namespace doctest -{ - class String; -} - namespace sf { class Color; - doctest::String toString(const Color& color); + std::ostream& operator <<(std::ostream& os, const Color& color); } #endif // SFML_TESTUTILITIES_GRAPHICS_HPP diff --git a/test/TestUtilities/SystemUtil.cpp b/test/TestUtilities/SystemUtil.cpp index 7306e1a98..c1c14ea44 100644 --- a/test/TestUtilities/SystemUtil.cpp +++ b/test/TestUtilities/SystemUtil.cpp @@ -9,23 +9,22 @@ #endif // !defined(__GNUC__) || (__GNUC__ >= 9) #include +#include #include #include -#include - namespace sf { - doctest::String toString(const sf::String& string) + std::ostream& operator <<(std::ostream& os, const sf::String& string) { - return string.toAnsiString().c_str(); + os << string.toAnsiString(); + return os; } - doctest::String toString(sf::Time time) + std::ostream& operator <<(std::ostream& os, sf::Time time) { - std::ostringstream stream; - stream << time.asMicroseconds() << "us"; - return stream.str().c_str(); + os << time.asMicroseconds() << "us"; + return os; } } diff --git a/test/TestUtilities/SystemUtil.hpp b/test/TestUtilities/SystemUtil.hpp index c5eb989f4..1c0c1b685 100644 --- a/test/TestUtilities/SystemUtil.hpp +++ b/test/TestUtilities/SystemUtil.hpp @@ -1,15 +1,15 @@ // Header for SFML unit tests. // -// For a new system module test case, include this header and not directly. +// For a new system module test case, include this header. // This ensures that string conversions are visible and can be used by doctest for debug output. #ifndef SFML_TESTUTILITIES_SYSTEM_HPP #define SFML_TESTUTILITIES_SYSTEM_HPP -#include - #include #include + +#include #include #include @@ -19,23 +19,21 @@ namespace sf class String; class Time; - doctest::String toString(const sf::String& string); - doctest::String toString(sf::Time time); + std::ostream& operator <<(std::ostream& os, const sf::String& string); + std::ostream& operator <<(std::ostream& os, sf::Time time); template - doctest::String toString(const sf::Vector2& vector) + std::ostream& operator <<(std::ostream& os, const sf::Vector2& vector) { - std::ostringstream stream; - stream << "(" << vector.x << ", " << vector.y << ")"; - return stream.str().c_str(); + os << "(" << vector.x << ", " << vector.y << ")"; + return os; } template - doctest::String toString(const sf::Vector3& vector) + std::ostream& operator <<(std::ostream& os, const sf::Vector3& vector) { - std::ostringstream stream; - stream << "(" << vector.x << ", " << vector.y << ", " << vector.z << ")"; - return stream.str().c_str(); + os << "(" << vector.x << ", " << vector.y << ", " << vector.z << ")"; + return os; } } diff --git a/test/TestUtilities/WindowUtil.cpp b/test/TestUtilities/WindowUtil.cpp index b280f8553..637e328c1 100644 --- a/test/TestUtilities/WindowUtil.cpp +++ b/test/TestUtilities/WindowUtil.cpp @@ -1,15 +1,12 @@ // Note: No need to increase compile time by including TestUtilities/Window.hpp #include -#include - -#include +#include namespace sf { - doctest::String toString(const sf::VideoMode& videoMode) + std::ostream& operator <<(std::ostream& os, const sf::VideoMode& videoMode) { - std::ostringstream stream; - stream << videoMode.width << "x" << videoMode.height << "x" << videoMode.bitsPerPixel; - return stream.str().c_str(); + os << videoMode.width << "x" << videoMode.height << "x" << videoMode.bitsPerPixel; + return os; } } diff --git a/test/TestUtilities/WindowUtil.hpp b/test/TestUtilities/WindowUtil.hpp index 586ee39cd..ae5630afa 100644 --- a/test/TestUtilities/WindowUtil.hpp +++ b/test/TestUtilities/WindowUtil.hpp @@ -1,6 +1,6 @@ // Header for SFML unit tests. // -// For a new window module test case, include this header and not directly. +// For a new window module test case, include this header. // This ensures that string conversions are visible and can be used by doctest for debug output. #ifndef SFML_TESTUTILITIES_WINDOW_HPP @@ -10,21 +10,18 @@ #include -#include - // String conversions for doctest framework namespace sf { class VideoMode; - doctest::String toString(const sf::VideoMode& videoMode); + std::ostream& operator <<(std::ostream& os, const sf::VideoMode& videoMode); template - doctest::String toString(const sf::Rect& rect) + std::ostream& operator <<(std::ostream& os, const sf::Rect& rect) { - std::ostringstream stream; - stream << "(left=" << rect.left << ", top=" << rect.top << ", width=" << rect.width << ", height=" << rect.height << ")"; - return stream.str().c_str(); + os << "(left=" << rect.left << ", top=" << rect.top << ", width=" << rect.width << ", height=" << rect.height << ")"; + return os; } } diff --git a/test/Window/VideoMode.cpp b/test/Window/VideoMode.cpp index 2f6bab7a0..6f5e05121 100644 --- a/test/Window/VideoMode.cpp +++ b/test/Window/VideoMode.cpp @@ -1,6 +1,8 @@ #include #include "WindowUtil.hpp" +#include + TEST_CASE("sf::VideoMode class - [window]") { SUBCASE("Construction")