Improve test coverage

This commit is contained in:
Chris Thrasher 2024-04-11 10:29:03 -06:00
parent d31dad8300
commit efcfaf06fb
6 changed files with 101 additions and 19 deletions

View File

@ -81,17 +81,7 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests())
SECTION("Successful load") SECTION("Successful load")
{ {
const auto memory = []() const auto memory = loadIntoMemory("Graphics/tuffy.ttf");
{
std::ifstream file("Graphics/tuffy.ttf", std::ios::binary | std::ios::ate);
REQUIRE(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(static_cast<std::size_t>(size));
REQUIRE(file.read(reinterpret_cast<char*>(buffer.data()), size));
return buffer;
}();
REQUIRE(font.loadFromMemory(memory.data(), memory.size())); REQUIRE(font.loadFromMemory(memory.data(), memory.size()));
CHECK(font.getInfo().family == "Tuffy"); CHECK(font.getInfo().family == "Tuffy");
const auto& glyph = font.getGlyph(0x45, 16, false); const auto& glyph = font.getGlyph(0x45, 16, false);
@ -158,6 +148,7 @@ TEST_CASE("[Graphics] sf::Font", runDisplayTests())
SECTION("Set/get smooth") SECTION("Set/get smooth")
{ {
sf::Font font; sf::Font font;
REQUIRE(font.loadFromFile("Graphics/tuffy.ttf"));
font.setSmooth(false); font.setSmooth(false);
CHECK(!font.isSmooth()); CHECK(!font.isSmooth());
} }

View File

@ -61,15 +61,15 @@ TEST_CASE("[Graphics] sf::RenderTarget")
CHECK(renderTarget.setActive(true)); CHECK(renderTarget.setActive(true));
} }
const auto makeView = [](const auto& viewport)
{
sf::View view;
view.setViewport(viewport);
return view;
};
SECTION("getViewport(const View&)") SECTION("getViewport(const View&)")
{ {
const auto makeView = [](const auto& viewport)
{
sf::View view;
view.setViewport(viewport);
return view;
};
const RenderTarget renderTarget; const RenderTarget renderTarget;
CHECK(renderTarget.getViewport(makeView(sf::FloatRect({0, 0}, {1, 1}))) == sf::IntRect({0, 0}, {640, 480})); CHECK(renderTarget.getViewport(makeView(sf::FloatRect({0, 0}, {1, 1}))) == sf::IntRect({0, 0}, {640, 480}));
CHECK(renderTarget.getViewport(makeView(sf::FloatRect({1, 1}, {.5f, .25f}))) == CHECK(renderTarget.getViewport(makeView(sf::FloatRect({1, 1}, {.5f, .25f}))) ==
@ -78,6 +78,15 @@ TEST_CASE("[Graphics] sf::RenderTarget")
sf::IntRect({320, 240}, {160, 360})); sf::IntRect({320, 240}, {160, 360}));
} }
SECTION("getScissor(const View&)")
{
const RenderTarget renderTarget;
CHECK(renderTarget.getScissor(makeView(sf::FloatRect({0, 0}, {1, 1}))) == sf::IntRect({0, 0}, {640, 480}));
CHECK(renderTarget.getScissor(makeView(sf::FloatRect({1, 1}, {.5f, .25f}))) == sf::IntRect({0, 0}, {640, 480}));
CHECK(renderTarget.getScissor(makeView(sf::FloatRect({.5f, .5f}, {.25f, .75f}))) ==
sf::IntRect({0, 0}, {640, 480}));
}
SECTION("mapPixelToCoords(const Vector2i&)") SECTION("mapPixelToCoords(const Vector2i&)")
{ {
sf::View view; sf::View view;

View File

@ -172,6 +172,24 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests())
CHECK(shader.getNativeHandle() == 0); CHECK(shader.getNativeHandle() == 0);
} }
SECTION("Move semantics")
{
SECTION("Construction")
{
sf::Shader movedShader;
const sf::Shader shader = std::move(movedShader);
CHECK(shader.getNativeHandle() == 0);
}
SECTION("Assignment")
{
sf::Shader movedShader;
sf::Shader shader;
shader = std::move(movedShader);
CHECK(shader.getNativeHandle() == 0);
}
}
SECTION("loadFromFile()") SECTION("loadFromFile()")
{ {
sf::Shader shader; sf::Shader shader;
@ -229,19 +247,24 @@ TEST_CASE("[Graphics] sf::Shader", skipShaderFullTests())
sf::FileInputStream geometryShaderStream; sf::FileInputStream geometryShaderStream;
REQUIRE(geometryShaderStream.open("Graphics/shader.geom")); REQUIRE(geometryShaderStream.open("Graphics/shader.geom"));
sf::FileInputStream emptyStream;
SECTION("One shader") SECTION("One shader")
{ {
REQUIRE(!shader.loadFromStream(emptyStream, sf::Shader::Type::Vertex));
REQUIRE(shader.loadFromStream(vertexShaderStream, sf::Shader::Type::Vertex) == sf::Shader::isAvailable()); REQUIRE(shader.loadFromStream(vertexShaderStream, sf::Shader::Type::Vertex) == sf::Shader::isAvailable());
REQUIRE(shader.loadFromStream(fragmentShaderStream, sf::Shader::Type::Fragment) == sf::Shader::isAvailable());
} }
SECTION("Two shaders") SECTION("Two shaders")
{ {
REQUIRE(!shader.loadFromStream(emptyStream, fragmentShaderStream));
REQUIRE(!shader.loadFromStream(vertexShaderStream, emptyStream));
REQUIRE(shader.loadFromStream(vertexShaderStream, fragmentShaderStream) == sf::Shader::isAvailable()); REQUIRE(shader.loadFromStream(vertexShaderStream, fragmentShaderStream) == sf::Shader::isAvailable());
} }
SECTION("Three shaders") SECTION("Three shaders")
{ {
sf::FileInputStream emptyStream;
REQUIRE(!shader.loadFromStream(emptyStream, geometryShaderStream, fragmentShaderStream)); REQUIRE(!shader.loadFromStream(emptyStream, geometryShaderStream, fragmentShaderStream));
REQUIRE(!shader.loadFromStream(vertexShaderStream, emptyStream, fragmentShaderStream)); REQUIRE(!shader.loadFromStream(vertexShaderStream, emptyStream, fragmentShaderStream));
REQUIRE(!shader.loadFromStream(vertexShaderStream, geometryShaderStream, emptyStream)); REQUIRE(!shader.loadFromStream(vertexShaderStream, geometryShaderStream, emptyStream));

View File

@ -32,6 +32,32 @@ TEST_CASE("[Graphics] sf::Texture", runDisplayTests())
CHECK(texture.getNativeHandle() == 0); CHECK(texture.getNativeHandle() == 0);
} }
SECTION("Move semantics")
{
SECTION("Construction")
{
sf::Texture movedTexture;
const sf::Texture texture = std::move(movedTexture);
CHECK(texture.getSize() == sf::Vector2u());
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() == 0);
}
SECTION("Assignment")
{
sf::Texture movedTexture;
sf::Texture texture;
texture = std::move(movedTexture);
CHECK(texture.getSize() == sf::Vector2u());
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() == 0);
}
}
SECTION("create()") SECTION("create()")
{ {
sf::Texture texture; sf::Texture texture;
@ -68,6 +94,18 @@ TEST_CASE("[Graphics] sf::Texture", runDisplayTests())
CHECK(texture.getNativeHandle() != 0); CHECK(texture.getNativeHandle() != 0);
} }
SECTION("loadFromMemory()")
{
const auto memory = loadIntoMemory("Graphics/sfml-logo-big.png");
sf::Texture texture;
REQUIRE(texture.loadFromMemory(memory.data(), memory.size()));
CHECK(texture.getSize() == sf::Vector2u(1001, 304));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("loadFromStream()") SECTION("loadFromStream()")
{ {
sf::Texture texture; sf::Texture texture;

View File

@ -6,9 +6,12 @@
#include <GraphicsUtil.hpp> #include <GraphicsUtil.hpp>
#include <SystemUtil.hpp> #include <SystemUtil.hpp>
#include <fstream>
#include <limits> #include <limits>
#include <ostream> #include <ostream>
#include <cassert>
namespace sf namespace sf
{ {
std::ostream& operator<<(std::ostream& os, const BlendMode& blendMode) std::ostream& operator<<(std::ostream& os, const BlendMode& blendMode)
@ -113,3 +116,15 @@ 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]);
} }
std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path)
{
std::ifstream file(path, std::ios::binary | std::ios::ate);
assert(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(static_cast<std::size_t>(size));
[[maybe_unused]] const auto& result = file.read(reinterpret_cast<char*>(buffer.data()), size);
assert(result);
return buffer;
}

View File

@ -6,7 +6,11 @@
#pragma once #pragma once
#include <SystemUtil.hpp> #include <SystemUtil.hpp>
#include <filesystem>
#include <iosfwd> #include <iosfwd>
#include <vector>
#include <cstddef>
namespace sf namespace sf
{ {
@ -35,3 +39,5 @@ 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) && 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); lhs.width == Approx(rhs.value.width) && lhs.height == Approx(rhs.value.height);
} }
[[nodiscard]] std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path);