From f23ff655fd9e90d451586bd6e0a681692d3b2ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 4 Aug 2023 12:36:40 +0200 Subject: [PATCH] Text: improve and test an assertion. --- src/Magnum/Text/AbstractFont.cpp | 2 +- src/Magnum/Text/Test/AbstractLayouterTest.cpp | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Magnum/Text/AbstractFont.cpp b/src/Magnum/Text/AbstractFont.cpp index 7d25a4ffa..6c8f4c7f0 100644 --- a/src/Magnum/Text/AbstractFont.cpp +++ b/src/Magnum/Text/AbstractFont.cpp @@ -309,7 +309,7 @@ AbstractLayouter::AbstractLayouter(UnsignedInt glyphCount): _glyphCount(glyphCou AbstractLayouter::~AbstractLayouter() = default; std::pair AbstractLayouter::renderGlyph(const UnsignedInt i, Vector2& cursorPosition, Range2D& rectangle) { - CORRADE_ASSERT(i < glyphCount(), "Text::AbstractLayouter::renderGlyph(): glyph index out of bounds", {}); + CORRADE_ASSERT(i < glyphCount(), "Text::AbstractLayouter::renderGlyph(): index" << i << "out of range for" << glyphCount() << "glyphs", {}); /* Render the glyph */ Range2D quadPosition, textureCoordinates; diff --git a/src/Magnum/Text/Test/AbstractLayouterTest.cpp b/src/Magnum/Text/Test/AbstractLayouterTest.cpp index 1bd6aa887..b768dd522 100644 --- a/src/Magnum/Text/Test/AbstractLayouterTest.cpp +++ b/src/Magnum/Text/Test/AbstractLayouterTest.cpp @@ -23,7 +23,9 @@ DEALINGS IN THE SOFTWARE. */ +#include #include +#include /** @todo remove once Debug is stream-free */ #include "Magnum/Math/Range.h" #include "Magnum/Text/AbstractFont.h" @@ -34,10 +36,12 @@ struct AbstractLayouterTest: TestSuite::Tester { explicit AbstractLayouterTest(); void renderGlyph(); + void renderGlyphOutOfBounds(); }; AbstractLayouterTest::AbstractLayouterTest() { - addTests({&AbstractLayouterTest::renderGlyph}); + addTests({&AbstractLayouterTest::renderGlyph, + &AbstractLayouterTest::renderGlyphOutOfBounds}); } void AbstractLayouterTest::renderGlyph() { @@ -78,6 +82,22 @@ void AbstractLayouterTest::renderGlyph() { CORRADE_COMPARE(rectangle, Range2D({2.0f, 0.5f}, {6.1f, 3.0f})); } +void AbstractLayouterTest::renderGlyphOutOfBounds() { + struct Layouter: AbstractLayouter { + explicit Layouter(): AbstractLayouter{3} {} + + std::tuple doRenderGlyph(UnsignedInt) override { return {}; } + } layouter; + + Range2D rectangle; + Vector2 cursorPosition; + + std::ostringstream out; + Error redirectError{&out}; + layouter.renderGlyph(3, cursorPosition, rectangle); + CORRADE_COMPARE(out.str(), "Text::AbstractLayouter::renderGlyph(): index 3 out of range for 3 glyphs\n"); +} + }}}} CORRADE_TEST_MAIN(Magnum::Text::Test::AbstractLayouterTest)