diff --git a/NAS2D/Math/Point.h b/NAS2D/Math/Point.h index e034c75f..a320a913 100644 --- a/NAS2D/Math/Point.h +++ b/NAS2D/Math/Point.h @@ -12,8 +12,6 @@ #include "Vector.h" -#include - namespace NAS2D { @@ -84,11 +82,6 @@ namespace NAS2D { return Point(*this); } - - explicit operator std::string() const - { - return "(" + std::to_string(x) + ", " + std::to_string(y) + ")"; - } }; diff --git a/NAS2D/Math/Vector.h b/NAS2D/Math/Vector.h index e6eeef44..beca3c7f 100644 --- a/NAS2D/Math/Vector.h +++ b/NAS2D/Math/Vector.h @@ -12,8 +12,6 @@ #include "IsZero.h" -#include - namespace NAS2D { @@ -125,11 +123,6 @@ namespace NAS2D { return Vector(*this); } - - explicit operator std::string() const - { - return "(" + std::to_string(x) + ", " + std::to_string(y) + ")"; - } }; diff --git a/NAS2D/Resource/Font.cpp b/NAS2D/Resource/Font.cpp index dfb498b2..623a1713 100644 --- a/NAS2D/Resource/Font.cpp +++ b/NAS2D/Resource/Font.cpp @@ -11,6 +11,7 @@ #include "../Filesystem.h" #include "../Utility.h" +#include "../StringFrom.h" #include "../Math/MathUtils.h" #include "../Math/PointInRectangleRange.h" @@ -272,7 +273,7 @@ namespace if (fontSurfaceSize != glyphSize * GlyphMatrixSize) { SDL_FreeSurface(fontSurface); - throw std::runtime_error("Unexpected font image size. Image dimensions " + std::string{fontSurfaceSize} + " must both be evenly divisible by " + std::to_string(GlyphMatrixSize)); + throw std::runtime_error("Unexpected font image size. Image dimensions " + stringFrom(fontSurfaceSize) + " must both be evenly divisible by " + std::to_string(GlyphMatrixSize)); } Font::FontInfo fontInfo; diff --git a/NAS2D/Resource/Image.cpp b/NAS2D/Resource/Image.cpp index 72d296a8..463710ce 100644 --- a/NAS2D/Resource/Image.cpp +++ b/NAS2D/Resource/Image.cpp @@ -13,6 +13,7 @@ #include "../Math/Rectangle.h" #include "../Filesystem.h" #include "../Utility.h" +#include "../StringFrom.h" #if defined(__XCODE_BUILD__) #include @@ -148,7 +149,7 @@ Color Image::pixelColor(Point point) const { if (!Rectangle{{0, 0}, mSize}.contains(point)) { - throw std::runtime_error("Pixel coordinates out of bounds: " + std::string{point}); + throw std::runtime_error("Pixel coordinates out of bounds: " + stringFrom(point)); } if (!mSurface) { throw std::runtime_error("Image has no allocated surface"); } diff --git a/NAS2D/StringFrom.h b/NAS2D/StringFrom.h index f07dec08..ff52659f 100644 --- a/NAS2D/StringFrom.h +++ b/NAS2D/StringFrom.h @@ -6,6 +6,10 @@ namespace NAS2D { + template struct Point; + template struct Vector; + + template std::string stringFrom(T value) { @@ -22,4 +26,18 @@ namespace NAS2D return std::to_string(value); } } + + + template + std::string stringFrom(Point point) + { + return "(" + std::to_string(point.x) + ", " + std::to_string(point.y) + ")"; + } + + + template + std::string stringFrom(Vector vector) + { + return "(" + std::to_string(vector.x) + ", " + std::to_string(vector.y) + ")"; + } } diff --git a/test/Math/Point.test.cpp b/test/Math/Point.test.cpp index 62282174..0d95340a 100644 --- a/test/Math/Point.test.cpp +++ b/test/Math/Point.test.cpp @@ -89,12 +89,6 @@ TEST(Point, to) { EXPECT_EQ((NAS2D::Point{1.0, 2.0}), (NAS2D::Point{1, 2}.to())); } -TEST(Point, stringConversion) { - EXPECT_EQ("(0, 0)", (std::string{NAS2D::Point{0, 0}})); - EXPECT_EQ("(1, 0)", (std::string{NAS2D::Point{1, 0}})); - EXPECT_EQ("(0, 1)", (std::string{NAS2D::Point{0, 1}})); -} - TEST(Point, VectorPointAdd) { EXPECT_EQ((NAS2D::Point{2, 3}), (NAS2D::Vector{1, 2} + NAS2D::Point{1, 1})); } diff --git a/test/Math/Vector.test.cpp b/test/Math/Vector.test.cpp index 275b533e..99a23c70 100644 --- a/test/Math/Vector.test.cpp +++ b/test/Math/Vector.test.cpp @@ -208,12 +208,6 @@ TEST(Vector, to) { EXPECT_EQ((NAS2D::Vector{1.0, 2.0}), (NAS2D::Vector{1, 2}.to())); } -TEST(Vector, stringConversion) { - EXPECT_EQ("(0, 0)", (std::string{NAS2D::Vector{0, 0}})); - EXPECT_EQ("(1, 0)", (std::string{NAS2D::Vector{1, 0}})); - EXPECT_EQ("(0, 1)", (std::string{NAS2D::Vector{0, 1}})); -} - TEST(Vector, scalarVectorMultiply) { EXPECT_EQ((NAS2D::Vector{2, 4}), (2 * NAS2D::Vector{1, 2})); } diff --git a/test/StringFrom.test.cpp b/test/StringFrom.test.cpp index 5e651809..7599ae6e 100644 --- a/test/StringFrom.test.cpp +++ b/test/StringFrom.test.cpp @@ -1,5 +1,8 @@ #include "NAS2D/StringFrom.h" +#include "NAS2D/Math/Point.h" +#include "NAS2D/Math/Vector.h" + #include #include @@ -21,13 +24,22 @@ namespace } -TEST(StringFrom, stringFrom) { +TEST(StringFrom, implicitConversion) { + ImplicitStringConversionTestFixture implicitStringConversionTestFixture{"testString"}; + EXPECT_EQ("testString", NAS2D::stringFrom(implicitStringConversionTestFixture)); +} + +TEST(StringFrom, string) { EXPECT_EQ("SomeStringValue", NAS2D::stringFrom("SomeStringValue")); EXPECT_EQ("SomeStringValue", NAS2D::stringFrom(std::string{"SomeStringValue"})); +} +TEST(StringFrom, boolean) { EXPECT_EQ("false", NAS2D::stringFrom(false)); EXPECT_EQ("true", NAS2D::stringFrom(true)); +} +TEST(StringFrom, integers) { using signedChar = signed char; using unsignedChar = unsigned char; EXPECT_EQ("-1", NAS2D::stringFrom(signedChar{-1})); @@ -51,13 +63,23 @@ TEST(StringFrom, stringFrom) { EXPECT_EQ("-1", NAS2D::stringFrom(-1ll)); EXPECT_EQ("0", NAS2D::stringFrom(0ll)); EXPECT_EQ("1", NAS2D::stringFrom(1ull)); +} +TEST(StringFrom, floatingPoint) { // Ignore precision beyond one decimal place EXPECT_THAT(NAS2D::stringFrom(0.0f), testing::StartsWith("0.0")); EXPECT_THAT(NAS2D::stringFrom(0.0), testing::StartsWith("0.0")); EXPECT_THAT(NAS2D::stringFrom(0.0l), testing::StartsWith("0.0")); +} - // Implicit string conversion - ImplicitStringConversionTestFixture implicitStringConversionTestFixture{"testString"}; - EXPECT_EQ("testString", NAS2D::stringFrom(implicitStringConversionTestFixture)); +TEST(StringFrom, point) { + EXPECT_EQ("(0, 0)", NAS2D::stringFrom(NAS2D::Point{0, 0})); + EXPECT_EQ("(1, 0)", NAS2D::stringFrom(NAS2D::Point{1, 0})); + EXPECT_EQ("(0, 1)", NAS2D::stringFrom(NAS2D::Point{0, 1})); +} + +TEST(StringFrom, vector) { + EXPECT_EQ("(0, 0)", NAS2D::stringFrom(NAS2D::Vector{0, 0})); + EXPECT_EQ("(1, 0)", NAS2D::stringFrom(NAS2D::Vector{1, 0})); + EXPECT_EQ("(0, 1)", NAS2D::stringFrom(NAS2D::Vector{0, 1})); }