From dbb87db0265b3943b677027c78d9ff977908b979 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Wed, 27 Sep 2023 22:27:20 -0600 Subject: [PATCH] Add more tests for `sf::Text` --- test/Graphics/Text.test.cpp | 78 ++++++++++++++++++++++++++--- test/TestUtilities/GraphicsUtil.cpp | 6 --- test/TestUtilities/GraphicsUtil.hpp | 7 +++ test/TestUtilities/SystemUtil.cpp | 12 ----- test/TestUtilities/SystemUtil.hpp | 5 +- 5 files changed, 81 insertions(+), 27 deletions(-) diff --git a/test/Graphics/Text.test.cpp b/test/Graphics/Text.test.cpp index 23616e6c8..f65b328b7 100644 --- a/test/Graphics/Text.test.cpp +++ b/test/Graphics/Text.test.cpp @@ -5,7 +5,7 @@ #include -#include +#include #include TEST_CASE("[Graphics] sf::Text", runDisplayTests()) @@ -19,10 +19,29 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests()) STATIC_CHECK(std::is_nothrow_move_assignable_v); } - const sf::Font font; + sf::Font font; + REQUIRE(font.loadFromFile("Graphics/tuffy.ttf")); SECTION("Construction") { + SECTION("Font constructor with empty font") + { + const sf::Font emptyFont; + const sf::Text text(emptyFont); + CHECK(text.getString() == ""); + CHECK(&text.getFont() == &emptyFont); + CHECK(text.getCharacterSize() == 30); + CHECK(text.getLetterSpacing() == 1.f); + CHECK(text.getLineSpacing() == 1.f); + CHECK(text.getStyle() == sf::Text::Regular); + CHECK(text.getFillColor() == sf::Color::White); + CHECK(text.getOutlineColor() == sf::Color::Black); + CHECK(text.getOutlineThickness() == 0); + CHECK(text.findCharacterPos(0) == sf::Vector2f()); + CHECK(text.getLocalBounds() == sf::FloatRect()); + CHECK(text.getGlobalBounds() == sf::FloatRect()); + } + SECTION("Font constructor") { const sf::Text text(font); @@ -53,8 +72,8 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests()) CHECK(text.getOutlineColor() == sf::Color::Black); CHECK(text.getOutlineThickness() == 0); CHECK(text.findCharacterPos(0) == sf::Vector2f()); - CHECK(text.getLocalBounds() == sf::FloatRect({0, 30}, {0, 0})); - CHECK(text.getGlobalBounds() == sf::FloatRect({0, 30}, {0, 0})); + CHECK(text.getLocalBounds() == sf::FloatRect({1, 8}, {357, 28})); + CHECK(text.getGlobalBounds() == sf::FloatRect({1, 8}, {357, 28})); } SECTION("Font, string, and character size constructor") @@ -70,8 +89,8 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests()) CHECK(text.getOutlineColor() == sf::Color::Black); CHECK(text.getOutlineThickness() == 0); CHECK(text.findCharacterPos(0) == sf::Vector2f()); - CHECK(text.getLocalBounds() == sf::FloatRect({0, 24}, {0, 0})); - CHECK(text.getGlobalBounds() == sf::FloatRect({0, 24}, {0, 0})); + CHECK(text.getLocalBounds() == sf::FloatRect({1, 7}, {290, 22})); + CHECK(text.getGlobalBounds() == sf::FloatRect({1, 7}, {290, 22})); } } @@ -120,14 +139,14 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests()) SECTION("Set/get fill color") { - sf::Text text(font); + sf::Text text(font, "Fill color"); text.setFillColor(sf::Color::Red); CHECK(text.getFillColor() == sf::Color::Red); } SECTION("Set/get outline color") { - sf::Text text(font); + sf::Text text(font, "Outline color"); text.setOutlineColor(sf::Color::Green); CHECK(text.getOutlineColor() == sf::Color::Green); } @@ -138,4 +157,47 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests()) text.setOutlineThickness(3.14f); CHECK(text.getOutlineThickness() == 3.14f); } + + SECTION("findCharacterPos()") + { + sf::Text text(font, "\tabcdefghijklmnopqrstuvwxyz \n"); + text.setPosition({120, 240}); + CHECK(text.findCharacterPos(0) == sf::Vector2f(120, 240)); + CHECK(text.findCharacterPos(1) == sf::Vector2f(156, 240)); + CHECK(text.findCharacterPos(2) == sf::Vector2f(170, 240)); + CHECK(text.findCharacterPos(3) == sf::Vector2f(185, 240)); + CHECK(text.findCharacterPos(4) == sf::Vector2f(198, 240)); + + // Indices that are too large are capped at maximum valid index + CHECK(text.findCharacterPos(1'000) == sf::Vector2f(120, 277)); + } + + SECTION("Get bounds") + { + sf::Text text(font, "Test", 18); + text.setPosition({100, 200}); + CHECK(text.getLocalBounds() == sf::FloatRect({1, 5}, {33, 13})); + CHECK(text.getGlobalBounds() == sf::FloatRect({101, 205}, {33, 13})); + + SECTION("Add underline") + { + text.setStyle(sf::Text::Underlined); + CHECK(text.getLocalBounds() == sf::FloatRect({1, 5}, {33, 13})); + CHECK(text.getGlobalBounds() == sf::FloatRect({101, 205}, {33, 13})); + } + + SECTION("Add strikethrough") + { + text.setStyle(sf::Text::StrikeThrough); + CHECK(text.getLocalBounds() == sf::FloatRect({1, 5}, {33, 13})); + CHECK(text.getGlobalBounds() == sf::FloatRect({101, 205}, {33, 13})); + } + + SECTION("Change rotation") + { + text.setRotation(sf::degrees(180)); + CHECK(text.getLocalBounds() == sf::FloatRect({1, 5}, {33, 13})); + CHECK(text.getGlobalBounds() == Approx(sf::FloatRect({66, 182}, {33, 13}))); + } + } } diff --git a/test/TestUtilities/GraphicsUtil.cpp b/test/TestUtilities/GraphicsUtil.cpp index e5ea8ef4b..23c40efad 100644 --- a/test/TestUtilities/GraphicsUtil.cpp +++ b/test/TestUtilities/GraphicsUtil.cpp @@ -57,9 +57,3 @@ bool operator==(const sf::Transform& lhs, const Approx& rhs) lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) && lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]); } - -template <> -std::ostream& operator<<(std::ostream& os, const Approx& approx) -{ - return os << approx.value; -} diff --git a/test/TestUtilities/GraphicsUtil.hpp b/test/TestUtilities/GraphicsUtil.hpp index c58c5db9b..79d6b6530 100644 --- a/test/TestUtilities/GraphicsUtil.hpp +++ b/test/TestUtilities/GraphicsUtil.hpp @@ -27,3 +27,10 @@ std::ostream& operator<<(std::ostream& os, const Rect& rect); } // namespace sf bool operator==(const sf::Transform& lhs, const Approx& rhs); + +template +bool operator==(const sf::Rect& lhs, const Approx>& rhs) +{ + return lhs.left == Approx(rhs.value.left) && lhs.top == Approx(rhs.value.top) && + lhs.width == Approx(rhs.value.width) && lhs.height == Approx(rhs.value.height); +} diff --git a/test/TestUtilities/SystemUtil.cpp b/test/TestUtilities/SystemUtil.cpp index c6dd5af3c..e1f879ee1 100644 --- a/test/TestUtilities/SystemUtil.cpp +++ b/test/TestUtilities/SystemUtil.cpp @@ -76,15 +76,3 @@ bool operator==(const sf::Angle& lhs, const Approx& rhs) { return lhs.asDegrees() == Approx(rhs.value.asDegrees()); } - -template -std::ostream& operator<<(std::ostream& os, const Approx& approx) -{ - return os << approx.value; -} - -template std::ostream& operator<<(std::ostream&, const Approx&); -template std::ostream& operator<<(std::ostream&, const Approx&); -template std::ostream& operator<<(std::ostream&, const Approx>&); -template std::ostream& operator<<(std::ostream&, const Approx>&); -template std::ostream& operator<<(std::ostream&, const Approx&); diff --git a/test/TestUtilities/SystemUtil.hpp b/test/TestUtilities/SystemUtil.hpp index ecb76babf..3267df6f7 100644 --- a/test/TestUtilities/SystemUtil.hpp +++ b/test/TestUtilities/SystemUtil.hpp @@ -54,4 +54,7 @@ bool operator==(const sf::Vector3& lhs, const Approx>& bool operator==(const sf::Angle& lhs, const Approx& rhs); template -std::ostream& operator<<(std::ostream& os, const Approx& approx); +std::ostream& operator<<(std::ostream& os, const Approx& approx) +{ + return os << approx.value; +}