Fix tagged_text escaping & add parse error tests

This commit is contained in:
Nikita Lisitsa 2022-05-19 17:35:10 +03:00
parent b524600da0
commit 9d8e83d957
2 changed files with 22 additions and 7 deletions

View file

@ -56,20 +56,19 @@ namespace psemek::ui
} }
else else
{ {
auto start = current;
if (*current == '\\') if (*current == '\\')
{ {
++current; ++current;
if (current == text.end()) if (current == text.end())
error("unexpected end"); error("unexpected end");
if (*current != '[' && *current != ']' && *current != '\\')
error("unknown escape sequence \\" + std::string(1, *current));
} }
auto append = [&](std::string_view & target) auto append = [&](std::string_view & target)
{ {
if (target.empty()) if (target.empty())
target = {current, current + 1}; target = {start, current + 1};
else else
target = {target.data(), current + 1}; target = {target.data(), current + 1};
}; };

View file

@ -35,6 +35,11 @@ static void test(std::string_view text, tagged_text const & expected)
compare(tagged_text::parse(text), 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_case(ui_tagged__text_empty)
{ {
test("", {}); test("", {});
@ -88,9 +93,20 @@ test_case(ui_tagged__text_tag__attribute)
test_case(ui_tagged__text_escape) 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) test_case(ui_tagged__text_random)