From: Richard Russon Date: Fri, 24 May 2019 22:24:04 +0000 (+0100) Subject: test: improve test_mutt_buffer_concat_path() X-Git-Tag: 2019-10-25~188^2~13 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4fab7a5c5d278368a85937a1b8f61b78f7723683;p=neomutt test: improve test_mutt_buffer_concat_path() --- diff --git a/mutt/buffer.c b/mutt/buffer.c index 4ecb1f510..56d3087cd 100644 --- a/mutt/buffer.c +++ b/mutt/buffer.c @@ -379,12 +379,24 @@ size_t mutt_buffer_len(const struct Buffer *buf) */ void mutt_buffer_concat_path(struct Buffer *buf, const char *dir, const char *fname) { - if (!buf || !dir || !fname) + if (!buf) return; - const char *fmt = "%s/%s"; + if (!dir) + dir = ""; + if (!fname) + fname = ""; + + const bool d_set = (dir[0] != '\0'); + const bool f_set = (fname[0] != '\0'); + if (!d_set && !f_set) + return; + + const int d_len = strlen(dir); + const bool slash = d_set && (dir[d_len - 1] == '/'); - if ((fname[0] == '\0') || ((dir[0] != '\0') && (dir[strlen(dir) - 1] == '/'))) + const char *fmt = "%s/%s"; + if (!f_set || !d_set || slash) fmt = "%s%s"; mutt_buffer_printf(buf, fmt, dir, fname); diff --git a/test/buffer/mutt_buffer_concat_path.c b/test/buffer/mutt_buffer_concat_path.c index 061d8d57d..74aa8429f 100644 --- a/test/buffer/mutt_buffer_concat_path.c +++ b/test/buffer/mutt_buffer_concat_path.c @@ -34,22 +34,61 @@ void test_mutt_buffer_concat_path(void) TEST_CHECK_(1, "mutt_buffer_concat_path(NULL, \"apple\", \"banana\")"); } - { - struct Buffer buf = { 0 }; - mutt_buffer_concat_path(&buf, NULL, "banana"); - TEST_CHECK_(1, "mutt_buffer_concat_path(&buf, NULL, \"banana\")"); - } + // clang-format off + const char *dirs[] = { NULL, "", "dir", "dir/" }; + const char *files[] = { NULL, "", "file" }; + const char *concat_test[][3] = { + { dirs[0], files[0], NULL }, + { dirs[0], files[1], NULL }, + { dirs[0], files[2], "file" }, + { dirs[1], files[0], NULL }, + { dirs[1], files[1], NULL }, + { dirs[1], files[2], "file" }, + { dirs[2], files[0], "dir" }, + { dirs[2], files[1], "dir" }, + { dirs[2], files[2], "dir/file" }, + { dirs[3], files[0], "dir/" }, + { dirs[3], files[1], "dir/" }, + { dirs[3], files[2], "dir/file" }, + }; + // clang-format on { - struct Buffer buf = { 0 }; - mutt_buffer_concat_path(&buf, "apple", NULL); - TEST_CHECK_(1, "mutt_buffer_concat_path(&buf, \"apple\", NULL)"); - } + for (size_t i = 0; i < mutt_array_size(concat_test); i++) + { + TEST_CASE_("DIR: '%s' FILE: '%s'", NONULL(concat_test[i][0]), NONULL(concat_test[i][1])); + { + struct Buffer *buf = mutt_buffer_new(); + mutt_buffer_concat_path(buf, concat_test[i][0], concat_test[i][1]); + if (concat_test[i][2]) + { + TEST_CHECK(strcmp(mutt_b2s(buf), concat_test[i][2]) == 0); + } + else + { + if (!TEST_CHECK(strlen(mutt_b2s(buf)) == 0)) + { + TEST_MSG("len = %ld", strlen(mutt_b2s(buf))); + } + } + mutt_buffer_free(&buf); + } - { - struct Buffer *buf = mutt_buffer_new(); - mutt_buffer_concat_path(buf, "apple", "banana"); - TEST_CHECK(strcmp(mutt_b2s(buf), "apple/banana") == 0); - mutt_buffer_free(&buf); + { + const char *str = "test"; + struct Buffer *buf = mutt_buffer_from(str); + mutt_buffer_concat_path(buf, concat_test[i][0], concat_test[i][1]); + if (concat_test[i][2]) + { + TEST_CHECK(strcmp(mutt_b2s(buf), concat_test[i][2]) == 0); + } + else + { + TEST_CHECK(strcmp(mutt_b2s(buf), str) == 0); + } + mutt_buffer_free(&buf); + } + } } } +