mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
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<T> * Vector3<T> * VideoMode * Color * Rect<T> The intermediate header UnitTests.hpp is introduced to ensure string conversion visibility. Do not include <catch.hpp> directly any longer.
This commit is contained in:
parent
620c9989d6
commit
c3e5a35a6e
@ -2,6 +2,7 @@ set(SRCROOT "${PROJECT_SOURCE_DIR}/test/src")
|
|||||||
|
|
||||||
SET(SRC
|
SET(SRC
|
||||||
"${SRCROOT}/Main.cpp"
|
"${SRCROOT}/Main.cpp"
|
||||||
|
"${SRCROOT}/UnitTests.cpp"
|
||||||
"${SRCROOT}/Vector2.cpp"
|
"${SRCROOT}/Vector2.cpp"
|
||||||
"${SRCROOT}/Rect.cpp"
|
"${SRCROOT}/Rect.cpp"
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include <SFML/Graphics/Rect.hpp>
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <catch.hpp>
|
#include "UnitTests.hpp"
|
||||||
|
|
||||||
TEST_CASE("sf::Rect class")
|
TEST_CASE("sf::Rect class")
|
||||||
{
|
{
|
||||||
|
42
test/src/UnitTests.cpp
Normal file
42
test/src/UnitTests.cpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// Note: No need to increase compile time by including UnitTests.hpp
|
||||||
|
#include <SFML/System/String.hpp>
|
||||||
|
#include <SFML/System/Time.hpp>
|
||||||
|
#include <SFML/Window/VideoMode.hpp>
|
||||||
|
#include <SFML/Graphics/Color.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
// 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<int>(color.r)
|
||||||
|
<< ", g=" << static_cast<int>(color.g)
|
||||||
|
<< ", b=" << static_cast<int>(color.b)
|
||||||
|
<< ", a=" << static_cast<int>(color.a) << ")";
|
||||||
|
|
||||||
|
return stream.str();
|
||||||
|
}
|
||||||
|
}
|
58
test/src/UnitTests.hpp
Normal file
58
test/src/UnitTests.hpp
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// Header for SFML unit tests.
|
||||||
|
//
|
||||||
|
// For a new test case, include this header and not <catch.hpp> 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 <SFML/System/Vector2.hpp>
|
||||||
|
#include <SFML/System/Vector3.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
// 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 <typename T>
|
||||||
|
std::string toString(const sf::Vector2<T>& vector)
|
||||||
|
{
|
||||||
|
std::ostringstream stream;
|
||||||
|
stream << "(" << vector.x << ", " << vector.y << ")";
|
||||||
|
return stream.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::string toString(const sf::Vector3<T>& vector)
|
||||||
|
{
|
||||||
|
std::ostringstream stream;
|
||||||
|
stream << "(" << vector.x << ", " << vector.y << ", " << vector.z << ")";
|
||||||
|
return stream.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::string toString(const sf::Rect<T>& rect)
|
||||||
|
{
|
||||||
|
std::ostringstream stream;
|
||||||
|
stream << "(left=" << rect.left << ", top=" << rect.top << ", width=" << rect.width << ", height=" << rect.height << ")";
|
||||||
|
return stream.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SFML_UNITTESTS_HPP
|
@ -1,5 +1,5 @@
|
|||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <catch.hpp>
|
#include "UnitTests.hpp"
|
||||||
|
|
||||||
// Use sf::Vector2i for tests. Test coverage is given, as there are no template specializations.
|
// Use sf::Vector2i for tests. Test coverage is given, as there are no template specializations.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user