From c3e5a35a6edb876f0265065b9402e533055d938f Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Tue, 28 Apr 2015 21:37:01 +0200 Subject: [PATCH] Added string conversions for equality-comparable classes Provides a Catch::toString() overload for the following SFML classes, that support at least operator== and operator!=, and can thus be used in Catch assertion expressions: * String * Time * Vector2 * Vector3 * VideoMode * Color * Rect The intermediate header UnitTests.hpp is introduced to ensure string conversion visibility. Do not include directly any longer. --- test/CMakeLists.txt | 1 + test/src/Rect.cpp | 2 +- test/src/UnitTests.cpp | 42 ++++++++++++++++++++++++++++++ test/src/UnitTests.hpp | 58 ++++++++++++++++++++++++++++++++++++++++++ test/src/Vector2.cpp | 2 +- 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 test/src/UnitTests.cpp create mode 100644 test/src/UnitTests.hpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index aab0467e..2ea08f4a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,7 @@ set(SRCROOT "${PROJECT_SOURCE_DIR}/test/src") SET(SRC "${SRCROOT}/Main.cpp" + "${SRCROOT}/UnitTests.cpp" "${SRCROOT}/Vector2.cpp" "${SRCROOT}/Rect.cpp" ) diff --git a/test/src/Rect.cpp b/test/src/Rect.cpp index a6eabe06..f6b06ee2 100644 --- a/test/src/Rect.cpp +++ b/test/src/Rect.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include "UnitTests.hpp" TEST_CASE("sf::Rect class") { diff --git a/test/src/UnitTests.cpp b/test/src/UnitTests.cpp new file mode 100644 index 00000000..74f72b08 --- /dev/null +++ b/test/src/UnitTests.cpp @@ -0,0 +1,42 @@ +// Note: No need to increase compile time by including UnitTests.hpp +#include +#include +#include +#include +#include + + +// String conversions for Catch framework +namespace Catch +{ + std::string toString(const sf::String& string) + { + return string.toAnsiString(); + } + + std::string toString(sf::Time time) + { + std::ostringstream stream; + stream << time.asMicroseconds() << "us"; + return stream.str(); + } + + std::string toString(const sf::VideoMode& videoMode) + { + std::ostringstream stream; + stream << videoMode.width << "x" << videoMode.height << "x" << videoMode.bitsPerPixel; + return stream.str(); + } + + std::string toString(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) << ")"; + + return stream.str(); + } +} diff --git a/test/src/UnitTests.hpp b/test/src/UnitTests.hpp new file mode 100644 index 00000000..0e11c952 --- /dev/null +++ b/test/src/UnitTests.hpp @@ -0,0 +1,58 @@ +// Header for SFML unit tests. +// +// For a new test case, include this header and not directly. +// This ensures that string conversions are visible and can be used by Catch for debug output. + +#ifndef SFML_UNITTESTS_HPP +#define SFML_UNITTESTS_HPP + +#include +#include +#include +#include +#include + + +// Forward declarations for non-template types +namespace sf +{ + class Color; + class String; + class Time; + class VideoMode; +} + +// String conversions for Catch framework +namespace Catch +{ + std::string toString(const sf::String& string); + std::string toString(sf::Time time); + std::string toString(const sf::VideoMode& videoMode); + std::string toString(const sf::Color& color); + + template + std::string toString(const sf::Vector2& vector) + { + std::ostringstream stream; + stream << "(" << vector.x << ", " << vector.y << ")"; + return stream.str(); + } + + template + std::string toString(const sf::Vector3& vector) + { + std::ostringstream stream; + stream << "(" << vector.x << ", " << vector.y << ", " << vector.z << ")"; + return stream.str(); + } + + template + std::string toString(const sf::Rect& rect) + { + std::ostringstream stream; + stream << "(left=" << rect.left << ", top=" << rect.top << ", width=" << rect.width << ", height=" << rect.height << ")"; + return stream.str(); + } +} + +#endif // SFML_UNITTESTS_HPP diff --git a/test/src/Vector2.cpp b/test/src/Vector2.cpp index 92fde45b..50a53b4a 100644 --- a/test/src/Vector2.cpp +++ b/test/src/Vector2.cpp @@ -1,5 +1,5 @@ #include -#include +#include "UnitTests.hpp" // Use sf::Vector2i for tests. Test coverage is given, as there are no template specializations.