SFML/test/Graphics/Shape.test.cpp

135 lines
4.2 KiB
C++
Raw Permalink Normal View History

2022-03-17 00:05:48 +08:00
#include <SFML/Graphics/Shape.hpp>
2023-10-30 12:41:59 +08:00
// Other 1st party headers
#include <SFML/Graphics/Texture.hpp>
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-03-17 00:05:48 +08:00
2022-07-05 00:20:58 +08:00
#include <GraphicsUtil.hpp>
2024-02-08 21:28:57 +08:00
#include <WindowUtil.hpp>
#include <type_traits>
2022-03-17 00:05:48 +08:00
class TriangleShape : public sf::Shape
{
public:
2022-07-05 00:20:58 +08:00
explicit TriangleShape(const sf::Vector2f& size) : m_size(size)
2022-03-17 00:05:48 +08:00
{
update();
}
std::size_t getPointCount() const override
{
return 3;
}
sf::Vector2f getPoint(std::size_t index) const override
{
switch (index)
{
default:
2022-07-05 00:20:58 +08:00
case 0:
return {m_size.x / 2, 0};
2022-07-05 00:20:58 +08:00
case 1:
return {0, m_size.y};
2022-07-05 00:20:58 +08:00
case 2:
return {m_size.x, m_size.y};
2022-03-17 00:05:48 +08:00
}
}
private:
sf::Vector2f m_size;
};
2023-10-30 12:41:59 +08:00
TEST_CASE("[Graphics] sf::Shape", runDisplayTests())
2022-03-17 00:05:48 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
{
STATIC_CHECK(!std::is_constructible_v<sf::Shape>);
STATIC_CHECK(!std::is_copy_constructible_v<sf::Shape>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Shape>);
STATIC_CHECK(!std::is_move_constructible_v<sf::Shape>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Shape>);
STATIC_CHECK(std::has_virtual_destructor_v<sf::Shape>);
}
SECTION("Default constructor")
2022-03-17 00:05:48 +08:00
{
const TriangleShape triangleShape({0, 0});
CHECK(triangleShape.getTexture() == nullptr);
CHECK(triangleShape.getTextureRect() == sf::IntRect());
CHECK(triangleShape.getFillColor() == sf::Color::White);
CHECK(triangleShape.getOutlineColor() == sf::Color::White);
CHECK(triangleShape.getOutlineThickness() == 0.0f);
CHECK(triangleShape.getLocalBounds() == sf::FloatRect());
CHECK(triangleShape.getGlobalBounds() == sf::FloatRect());
}
2023-10-30 12:41:59 +08:00
SECTION("Set/get texture")
{
const auto texture = sf::Texture::create({64, 64}).value();
TriangleShape triangleShape({});
2023-10-30 12:41:59 +08:00
triangleShape.setTexture(&texture, true);
CHECK(triangleShape.getTexture() == &texture);
}
2023-01-18 12:51:08 +08:00
SECTION("Set/get texture rect")
2022-03-17 00:05:48 +08:00
{
TriangleShape triangleShape({});
triangleShape.setTextureRect({{4, 5}, {6, 7}});
CHECK(triangleShape.getTextureRect() == sf::IntRect({4, 5}, {6, 7}));
}
2023-01-18 12:51:08 +08:00
SECTION("Set/get fill color")
2022-03-17 00:05:48 +08:00
{
TriangleShape triangleShape({});
triangleShape.setFillColor(sf::Color::Cyan);
CHECK(triangleShape.getFillColor() == sf::Color::Cyan);
}
2023-01-18 12:51:08 +08:00
SECTION("Set/get outline color")
2022-03-17 00:05:48 +08:00
{
TriangleShape triangleShape({});
triangleShape.setOutlineColor(sf::Color::Magenta);
CHECK(triangleShape.getOutlineColor() == sf::Color::Magenta);
}
2023-01-18 12:51:08 +08:00
SECTION("Set/get outline thickness")
2022-03-17 00:05:48 +08:00
{
TriangleShape triangleShape({});
triangleShape.setOutlineThickness(3.14f);
CHECK(triangleShape.getOutlineThickness() == 3.14f);
}
SECTION("Virtual functions: getPoint, getPointCount, getGeometricCenter")
2022-03-17 00:05:48 +08:00
{
const TriangleShape triangleShape({2, 2});
CHECK(triangleShape.getPointCount() == 3);
CHECK(triangleShape.getPoint(0) == sf::Vector2f(1, 0));
CHECK(triangleShape.getPoint(1) == sf::Vector2f(0, 2));
CHECK(triangleShape.getPoint(2) == sf::Vector2f(2, 2));
CHECK(triangleShape.getGeometricCenter() == sf::Vector2f(1.f, 4.f / 3.f));
2022-03-17 00:05:48 +08:00
}
2023-01-18 12:51:08 +08:00
SECTION("Get bounds")
2022-03-17 00:05:48 +08:00
{
2023-10-30 12:41:59 +08:00
TriangleShape triangleShape({30, 40});
CHECK(triangleShape.getLocalBounds() == sf::FloatRect({0, 0}, {30, 40}));
CHECK(triangleShape.getGlobalBounds() == sf::FloatRect({0, 0}, {30, 40}));
SECTION("Move and rotate")
{
triangleShape.move({1, 1});
triangleShape.rotate(sf::degrees(90));
CHECK(triangleShape.getLocalBounds() == sf::FloatRect({0, 0}, {30, 40}));
CHECK(triangleShape.getGlobalBounds() == Approx(sf::FloatRect({-39, 1}, {40, 30})));
}
SECTION("Add outline")
{
triangleShape.setOutlineThickness(5);
CHECK(triangleShape.getLocalBounds() == Approx(sf::FloatRect({-7.2150f, -14.2400f}, {44.4300f, 59.2400f})));
CHECK(triangleShape.getGlobalBounds() == Approx(sf::FloatRect({-7.2150f, -14.2400f}, {44.4300f, 59.2400f})));
}
2022-03-17 00:05:48 +08:00
}
}