From f37124f5af7fedd384ca25d8a346611add2028e5 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Sat, 5 Mar 2022 13:06:19 +0100 Subject: [PATCH] renderer: Enforce valid alignment No renderer_interface function (except change_alignment) should be called with a NONE alignment in the context. --- src/components/renderer.cpp | 4 ++++ tests/unit_tests/tags/dispatch.cpp | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/components/renderer.cpp b/src/components/renderer.cpp index 4da65ec9..7da9a386 100644 --- a/src/components/renderer.cpp +++ b/src/components/renderer.cpp @@ -673,6 +673,7 @@ void renderer::fill_borders() { * Draw text contents */ void renderer::render_text(const tags::context& ctxt, const string&& contents) { + assert(ctxt.get_alignment() != alignment::NONE && ctxt.get_alignment() == m_align); m_log.trace_x("renderer: text(%s)", contents.c_str()); cairo::abspos origin{}; @@ -742,6 +743,7 @@ void renderer::draw_offset(rgba color, double x, double w) { } void renderer::render_offset(const tags::context& ctxt, const extent_val offset) { + assert(ctxt.get_alignment() != alignment::NONE && ctxt.get_alignment() == m_align); m_log.trace_x("renderer: offset_pixel(%f)", offset); int offset_width = units_utils::extent_to_pixel(offset, m_bar.dpi_x); @@ -752,6 +754,7 @@ void renderer::render_offset(const tags::context& ctxt, const extent_val offset) void renderer::change_alignment(const tags::context& ctxt) { auto align = ctxt.get_alignment(); + assert(align != alignment::NONE); if (align != m_align) { m_log.trace_x("renderer: change_alignment(%i)", static_cast(align)); @@ -772,6 +775,7 @@ void renderer::change_alignment(const tags::context& ctxt) { } double renderer::get_x(const tags::context& ctxt) const { + assert(ctxt.get_alignment() != alignment::NONE && ctxt.get_alignment() == m_align); return m_blocks.at(ctxt.get_alignment()).x; } diff --git a/tests/unit_tests/tags/dispatch.cpp b/tests/unit_tests/tags/dispatch.cpp index 2ead75b1..a0394551 100644 --- a/tests/unit_tests/tags/dispatch.cpp +++ b/tests/unit_tests/tags/dispatch.cpp @@ -31,16 +31,21 @@ class FakeRenderer : public renderer_interface { FakeRenderer(action_context& action_ctxt) : renderer_interface(action_ctxt){}; void render_offset(const tags::context& ctxt, const extent_val offset) override { + EXPECT_NE(alignment::NONE, ctxt.get_alignment()); block_x[ctxt.get_alignment()] += offset.value; }; void render_text(const tags::context& ctxt, const string&& str) override { + EXPECT_NE(alignment::NONE, ctxt.get_alignment()); block_x[ctxt.get_alignment()] += str.size(); }; - void change_alignment(const tags::context&) override{}; + void change_alignment(const tags::context& ctxt) override { + EXPECT_NE(alignment::NONE, ctxt.get_alignment()); + }; double get_x(const tags::context& ctxt) const override { + EXPECT_NE(alignment::NONE, ctxt.get_alignment()); return block_x.at(ctxt.get_alignment()); }; @@ -120,7 +125,7 @@ TEST_F(DispatchTest, ignoreFormatting) { } bar_settings settings; - m_dispatch->parse(settings, r, "%{O10}%{F#ff0000}abc%{F-}foo"); + m_dispatch->parse(settings, r, "%{l}%{O10}%{F#ff0000}abc%{F-}foo"); } TEST_F(DispatchTest, formatting) { @@ -135,6 +140,7 @@ TEST_F(DispatchTest, formatting) { { InSequence seq; + EXPECT_CALL(r, change_alignment(match_left_align)).Times(1); EXPECT_CALL(r, render_offset(_, extent_val{extent_type::PIXEL, 10})).Times(1); EXPECT_CALL(r, render_text(match_fg(c1), string{"abc"})).Times(1); EXPECT_CALL(r, render_text(match_fg(bar_fg), string{"foo"})).Times(1); @@ -149,7 +155,7 @@ TEST_F(DispatchTest, formatting) { } m_dispatch->parse(settings, r, - "%{O10}%{F#ff0000}abc%{F-}foo%{l}%{B#00ff00}bar%{R}123%{c}baz%{r}%{T123}def%{PR}" + "%{l}%{O10}%{F#ff0000}abc%{F-}foo%{l}%{B#00ff00}bar%{R}123%{c}baz%{r}%{T123}def%{PR}" "ghi"); }