Add more tests for sf::Text

This commit is contained in:
Chris Thrasher 2023-09-27 22:27:20 -06:00
parent 3f6403e279
commit dbb87db026
5 changed files with 81 additions and 27 deletions

View File

@ -5,7 +5,7 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <WindowUtil.hpp> #include <GraphicsUtil.hpp>
#include <type_traits> #include <type_traits>
TEST_CASE("[Graphics] sf::Text", runDisplayTests()) TEST_CASE("[Graphics] sf::Text", runDisplayTests())
@ -19,10 +19,29 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests())
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Text>); STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Text>);
} }
const sf::Font font; sf::Font font;
REQUIRE(font.loadFromFile("Graphics/tuffy.ttf"));
SECTION("Construction") 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") SECTION("Font constructor")
{ {
const sf::Text text(font); const sf::Text text(font);
@ -53,8 +72,8 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests())
CHECK(text.getOutlineColor() == sf::Color::Black); CHECK(text.getOutlineColor() == sf::Color::Black);
CHECK(text.getOutlineThickness() == 0); CHECK(text.getOutlineThickness() == 0);
CHECK(text.findCharacterPos(0) == sf::Vector2f()); CHECK(text.findCharacterPos(0) == sf::Vector2f());
CHECK(text.getLocalBounds() == sf::FloatRect({0, 30}, {0, 0})); CHECK(text.getLocalBounds() == sf::FloatRect({1, 8}, {357, 28}));
CHECK(text.getGlobalBounds() == sf::FloatRect({0, 30}, {0, 0})); CHECK(text.getGlobalBounds() == sf::FloatRect({1, 8}, {357, 28}));
} }
SECTION("Font, string, and character size constructor") 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.getOutlineColor() == sf::Color::Black);
CHECK(text.getOutlineThickness() == 0); CHECK(text.getOutlineThickness() == 0);
CHECK(text.findCharacterPos(0) == sf::Vector2f()); CHECK(text.findCharacterPos(0) == sf::Vector2f());
CHECK(text.getLocalBounds() == sf::FloatRect({0, 24}, {0, 0})); CHECK(text.getLocalBounds() == sf::FloatRect({1, 7}, {290, 22}));
CHECK(text.getGlobalBounds() == sf::FloatRect({0, 24}, {0, 0})); CHECK(text.getGlobalBounds() == sf::FloatRect({1, 7}, {290, 22}));
} }
} }
@ -120,14 +139,14 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests())
SECTION("Set/get fill color") SECTION("Set/get fill color")
{ {
sf::Text text(font); sf::Text text(font, "Fill color");
text.setFillColor(sf::Color::Red); text.setFillColor(sf::Color::Red);
CHECK(text.getFillColor() == sf::Color::Red); CHECK(text.getFillColor() == sf::Color::Red);
} }
SECTION("Set/get outline color") SECTION("Set/get outline color")
{ {
sf::Text text(font); sf::Text text(font, "Outline color");
text.setOutlineColor(sf::Color::Green); text.setOutlineColor(sf::Color::Green);
CHECK(text.getOutlineColor() == sf::Color::Green); CHECK(text.getOutlineColor() == sf::Color::Green);
} }
@ -138,4 +157,47 @@ TEST_CASE("[Graphics] sf::Text", runDisplayTests())
text.setOutlineThickness(3.14f); text.setOutlineThickness(3.14f);
CHECK(text.getOutlineThickness() == 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})));
}
}
} }

View File

@ -57,9 +57,3 @@ bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs)
lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) && lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) &&
lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]); lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]);
} }
template <>
std::ostream& operator<<(std::ostream& os, const Approx<sf::Transform>& approx)
{
return os << approx.value;
}

View File

@ -27,3 +27,10 @@ std::ostream& operator<<(std::ostream& os, const Rect<T>& rect);
} // namespace sf } // namespace sf
bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs); bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs);
template <typename T>
bool operator==(const sf::Rect<T>& lhs, const Approx<sf::Rect<T>>& 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);
}

View File

@ -76,15 +76,3 @@ bool operator==(const sf::Angle& lhs, const Approx<sf::Angle>& rhs)
{ {
return lhs.asDegrees() == Approx(rhs.value.asDegrees()); return lhs.asDegrees() == Approx(rhs.value.asDegrees());
} }
template <typename T>
std::ostream& operator<<(std::ostream& os, const Approx<T>& approx)
{
return os << approx.value;
}
template std::ostream& operator<<(std::ostream&, const Approx<int>&);
template std::ostream& operator<<(std::ostream&, const Approx<float>&);
template std::ostream& operator<<(std::ostream&, const Approx<sf::Vector2<float>>&);
template std::ostream& operator<<(std::ostream&, const Approx<sf::Vector3<float>>&);
template std::ostream& operator<<(std::ostream&, const Approx<sf::Angle>&);

View File

@ -54,4 +54,7 @@ bool operator==(const sf::Vector3<float>& lhs, const Approx<sf::Vector3<float>>&
bool operator==(const sf::Angle& lhs, const Approx<sf::Angle>& rhs); bool operator==(const sf::Angle& lhs, const Approx<sf::Angle>& rhs);
template <typename T> template <typename T>
std::ostream& operator<<(std::ostream& os, const Approx<T>& approx); std::ostream& operator<<(std::ostream& os, const Approx<T>& approx)
{
return os << approx.value;
}