From 28e4bc13996073a8f7777abba06d53396dfe9d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Thu, 10 Dec 2015 20:01:41 +0100 Subject: [PATCH] degesch: add more tests, bump liberty The UTF-8 common prefix test discovered a bug in UTF-8 parsing. Made $[1-9] in aliases insert nothing if there's no argument at that index. --- degesch.c | 44 +++++++++++++++++++++++++++++++++++--------- liberty | 2 +- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/degesch.c b/degesch.c index 2977e09..96d0dc1 100644 --- a/degesch.c +++ b/degesch.c @@ -9491,10 +9491,12 @@ expand_alias_escape (const char *p, const char *arguments, struct str *output) cstr_split_ignore_empty (arguments, ' ', &words); // TODO: eventually also add support for argument ranges - int as_number = *p - '0'; - if (as_number > 0 && as_number <= 9 - && (size_t) as_number <= words.len) - str_append (output, words.vector[as_number - 1]); + if (*p >= '1' && *p <= '9') + { + size_t offset = *p - '1'; + if (offset < words.len) + str_append (output, words.vector[offset]); + } else if (*p == '*') str_append (output, arguments); else if (strchr ("$;", *p)) @@ -9507,14 +9509,14 @@ expand_alias_escape (const char *p, const char *arguments, struct str *output) } static void -expand_alias_definition (const struct str *definition, const char *arguments, +expand_alias_definition (const char *definition, const char *arguments, struct str_vector *commands) { struct str expanded; str_init (&expanded); bool escape = false; - for (const char *p = definition->str; *p; p++) + for (const char *p = definition; *p; p++) { if (escape) { @@ -9550,7 +9552,7 @@ expand_alias (struct app_context *ctx, return false; } - expand_alias_definition (&entry->value.string, input, commands); + expand_alias_definition (entry->value.string.str, input, commands); return true; } @@ -11199,10 +11201,25 @@ init_poller_events (struct app_context *ctx) // --- Tests ------------------------------------------------------------------- -// The application is quite monolithic and can only be partially unit-tested +// The application is quite monolithic and can only be partially unit-tested. +// Locale-, terminal- and filesystem-dependent tests are also somewhat tricky. #ifdef TESTING +static void +test_aliases (void) +{ + struct str_vector v; + str_vector_init (&v); + expand_alias_definition ("/foo; /bar $* $$$;;;$1$2$3$4", "foo bar baz", &v); + hard_assert (v.len == 4); + hard_assert (!strcmp (v.vector[0], "/foo")); + hard_assert (!strcmp (v.vector[1], " /bar foo bar baz $;")); + hard_assert (!strcmp (v.vector[2], "")); + hard_assert (!strcmp (v.vector[3], "foobarbaz")); + str_vector_free (&v); +} + static void test_wrapping (void) { @@ -11221,12 +11238,21 @@ test_wrapping (void) str_vector_free (&v); } +static void +test_utf8_prefix (void) +{ + static const char *a[] = { "fřoo", "Fřooř", "fřOOŘ" }; + hard_assert (utf8_common_prefix (a, N_ELEMENTS (a)) == 5); +} + int main (int argc, char *argv[]) { struct test test; test_init (&test, argc, argv); - test_add_simple (&test, "/wrapping", NULL, test_wrapping); + test_add_simple (&test, "/aliases", NULL, test_aliases); + test_add_simple (&test, "/wrapping", NULL, test_wrapping); + test_add_simple (&test, "/utf8-prefix", NULL, test_utf8_prefix); return test_run (&test); } diff --git a/liberty b/liberty index 0adcaf6..5d3e911 160000 --- a/liberty +++ b/liberty @@ -1 +1 @@ -Subproject commit 0adcaf67c23fdc2a5082aa11aefd4fdc0aafd70a +Subproject commit 5d3e911f015b33d0b4dcc3aa94f7af630438cbf8