From 9d8e83d957519ef741894a55c914acdd071ac28f Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 19 May 2022 17:35:10 +0300 Subject: [PATCH] Fix tagged_text escaping & add parse error tests --- libs/ui/source/tagged_text.cpp | 7 +++---- libs/ui/tests/tagged_text.cpp | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libs/ui/source/tagged_text.cpp b/libs/ui/source/tagged_text.cpp index 7d3f15a8..359bd9ad 100644 --- a/libs/ui/source/tagged_text.cpp +++ b/libs/ui/source/tagged_text.cpp @@ -56,20 +56,19 @@ namespace psemek::ui } else { + auto start = current; + if (*current == '\\') { ++current; if (current == text.end()) error("unexpected end"); - - if (*current != '[' && *current != ']' && *current != '\\') - error("unknown escape sequence \\" + std::string(1, *current)); } auto append = [&](std::string_view & target) { if (target.empty()) - target = {current, current + 1}; + target = {start, current + 1}; else target = {target.data(), current + 1}; }; diff --git a/libs/ui/tests/tagged_text.cpp b/libs/ui/tests/tagged_text.cpp index 87d1739d..9efb2f4a 100644 --- a/libs/ui/tests/tagged_text.cpp +++ b/libs/ui/tests/tagged_text.cpp @@ -35,6 +35,11 @@ static void test(std::string_view text, tagged_text const & expected) compare(tagged_text::parse(text), expected); } +static void test_throw(std::string_view text) +{ + expect_throw(tagged_text::parse(text), tagged_text::parse_error); +} + test_case(ui_tagged__text_empty) { test("", {}); @@ -88,9 +93,20 @@ test_case(ui_tagged__text_tag__attribute) test_case(ui_tagged__text_escape) { - test("\\[", {{"["}}); - test("\\]", {{"]"}}); - test("\\\\", {{"\\"}}); + test("\\[", {{"\\["}}); + test("\\]", {{"\\]"}}); + test("\\\\", {{"\\\\"}}); + test("\\[\\]", {{"\\[\\]"}}); +} + +test_case(ui_tagged__text_error) +{ + test_throw("[["); + test_throw("[ab[cd"); + test_throw("]"); + test_throw("]["); + test_throw("[]]"); + test_throw("[::]"); } test_case(ui_tagged__text_random)