From: Richard Russon Date: Tue, 30 Apr 2019 23:12:05 +0000 (+0100) Subject: test: move file tests X-Git-Tag: 2019-10-25~228^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d29827f587219f4d7f4cacc895ea418e9179bf67;p=neomutt test: move file tests --- diff --git a/test/Makefile.autosetup b/test/Makefile.autosetup index 839151d89..ff1cb1a62 100644 --- a/test/Makefile.autosetup +++ b/test/Makefile.autosetup @@ -3,8 +3,7 @@ TEST_OBJS = test/main.o \ test/md5.o \ test/rfc2047.o \ test/address.o \ - test/url.o \ - test/file.o + test/url.o ADDRESS_OBJS = test/address/main.o \ test/address/mutt_addr_append.o \ @@ -144,6 +143,7 @@ ENVLIST_OBJS = test/envlist/main.o \ test/envlist/mutt_envlist_unset.o FILE_OBJS = test/file/main.o \ + test/file/common.o \ test/file/mutt_file_check_empty.o \ test/file/mutt_file_chmod_add.o \ test/file/mutt_file_chmod_add_stat.o \ diff --git a/test/file.c b/test/file.c deleted file mode 100644 index 74371d7e2..000000000 --- a/test/file.c +++ /dev/null @@ -1,140 +0,0 @@ -/** - * @file - * Test code for the file operations - * - * @authors - * Copyright (C) 2018 Ian Zimmerman - * - * @copyright - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 2 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#define TEST_NO_MAIN -#include "acutest.h" -#include -#include -#include -#include "mutt/file.h" - -static const char *lines[] = { - "This is the first line.", - "The second line.", - "And the third line", - NULL, -}; - -#define NUM_TEST_LINES (sizeof(lines) / sizeof(const char *) - 1) -#define BOOLIFY(x) ((x) ? "true" : "false") -#define SET_UP() (set_up(__func__)) -#define TEAR_DOWN(fp) (tear_down((fp), __func__)) - -static FILE *set_up(const char *funcname) -{ - int res = 0; - FILE *fp = tmpfile(); - const char **linep = NULL; - if (!fp) - goto err1; - for (linep = lines; *linep; linep++) - { - res = fputs(*linep, fp); - if (res == EOF) - goto err2; - res = fputc('\n', fp); - if (res == EOF) - goto err2; - } - rewind(fp); - return fp; -err2: - fclose(fp); -err1: - TEST_MSG("Failed to set up test %s", funcname); - return NULL; -} - -static void tear_down(FILE *fp, const char *funcname) -{ - int res = fclose(fp); - if (res == EOF) - TEST_MSG("Failed to tear down test %s", funcname); -} - -void test_file_iter_line(void) -{ - FILE *fp = SET_UP(); - if (!fp) - return; - struct MuttFileIter iter = { 0 }; - bool res; - for (int i = 0; i < NUM_TEST_LINES; i++) - { - res = mutt_file_iter_line(&iter, fp, 0); - if (!TEST_CHECK(res)) - { - TEST_MSG("Expected: true"); - TEST_MSG("Actual: false"); - } - if (!TEST_CHECK(strcmp(iter.line, lines[i]) == 0)) - { - TEST_MSG("Expected: %s", lines[i]); - TEST_MSG("Actual: %s", iter.line); - } - if (!TEST_CHECK(iter.line_num == i + 1)) - { - TEST_MSG("Expected: %d", i + 1); - TEST_MSG("Actual: %d", iter.line_num); - } - } - res = mutt_file_iter_line(&iter, fp, 0); - if (!TEST_CHECK(!res)) - { - TEST_MSG("Expected: false"); - TEST_MSG("Actual: true"); - } - TEAR_DOWN(fp); -} - -static bool mapping_func(char *line, int line_num, void *user_data) -{ - const int *p_last_line_num = (const int *) (user_data); - if (!TEST_CHECK(strcmp(line, lines[line_num - 1]) == 0)) - { - TEST_MSG("Expected: %s", lines[line_num - 1]); - TEST_MSG("Actual: %s", line); - } - return (line_num < *p_last_line_num); -} - -static void test_file_map_lines_breaking_after(int last_line, bool expected) -{ - FILE *fp = SET_UP(); - if (!fp) - return; - bool res = mutt_file_map_lines(mapping_func, &last_line, fp, 0); - if (!TEST_CHECK(res == expected)) - { - TEST_MSG("Expected: %s", BOOLIFY(expected)); - TEST_MSG("Actual: %s", BOOLIFY(res)); - } - TEAR_DOWN(fp); -} - -void test_file_map_lines(void) -{ - test_file_map_lines_breaking_after(NUM_TEST_LINES + 1, true); - test_file_map_lines_breaking_after(0, false); - test_file_map_lines_breaking_after(1, false); - test_file_map_lines_breaking_after(NUM_TEST_LINES, false); -} diff --git a/test/file/common.c b/test/file/common.c new file mode 100644 index 000000000..45d3f8dce --- /dev/null +++ b/test/file/common.c @@ -0,0 +1,70 @@ +/** + * @file + * Common code for file tests + * + * @authors + * Copyright (C) 2019 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#define TEST_NO_MAIN +#include "acutest.h" +#include "config.h" +#include "mutt/mutt.h" + +const char *file_lines[] = { + "This is the first line.", + "The second line.", + "And the third line", + NULL, +}; + +size_t file_num_test_lines(void) +{ + return (sizeof(file_lines) / sizeof(const char *) - 1); +} + +FILE *file_set_up(const char *funcname) +{ + int res = 0; + const char **linep = NULL; + FILE *fp = tmpfile(); + if (!fp) + goto err1; + for (linep = file_lines; *linep; linep++) + { + res = fputs(*linep, fp); + if (res == EOF) + goto err2; + res = fputc('\n', fp); + if (res == EOF) + goto err2; + } + rewind(fp); + return fp; +err2: + fclose(fp); +err1: + TEST_MSG("Failed to set up test %s", funcname); + return NULL; +} + +void file_tear_down(FILE *fp, const char *funcname) +{ + int res = fclose(fp); + if (res == EOF) + TEST_MSG("Failed to tear down test %s", funcname); +} diff --git a/test/file/common.h b/test/file/common.h new file mode 100644 index 000000000..9ff0d5b74 --- /dev/null +++ b/test/file/common.h @@ -0,0 +1,36 @@ +/** + * @file + * Common code for file tests + * + * @authors + * Copyright (C) 2019 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "acutest.h" +#include "config.h" +#include +#include "mutt/mutt.h" + +extern const char *file_lines[]; + +FILE *file_set_up(const char *funcname); +void file_tear_down(FILE *fp, const char *funcname); +size_t file_num_test_lines(void); + +#define SET_UP() (file_set_up(__func__)) +#define TEAR_DOWN(fp) (file_tear_down((fp), __func__)) + diff --git a/test/file/mutt_file_iter_line.c b/test/file/mutt_file_iter_line.c index 439c74211..9770e847c 100644 --- a/test/file/mutt_file_iter_line.c +++ b/test/file/mutt_file_iter_line.c @@ -24,6 +24,7 @@ #include "acutest.h" #include "config.h" #include "mutt/mutt.h" +#include "common.h" void test_mutt_file_iter_line(void) { @@ -38,4 +39,38 @@ void test_mutt_file_iter_line(void) struct MuttFileIter muttfileiter = { 0 }; TEST_CHECK(!mutt_file_iter_line(&muttfileiter, NULL, 0)); } + + { + FILE *fp = SET_UP(); + if (!fp) + return; + struct MuttFileIter iter = { 0 }; + bool res; + for (int i = 0; file_lines[i]; i++) + { + res = mutt_file_iter_line(&iter, fp, 0); + if (!TEST_CHECK(res)) + { + TEST_MSG("Expected: true"); + TEST_MSG("Actual: false"); + } + if (!TEST_CHECK(strcmp(iter.line, file_lines[i]) == 0)) + { + TEST_MSG("Expected: %s", file_lines[i]); + TEST_MSG("Actual: %s", iter.line); + } + if (!TEST_CHECK(iter.line_num == (i + 1))) + { + TEST_MSG("Expected: %d", i + 1); + TEST_MSG("Actual: %d", iter.line_num); + } + } + res = mutt_file_iter_line(&iter, fp, 0); + if (!TEST_CHECK(!res)) + { + TEST_MSG("Expected: false"); + TEST_MSG("Actual: true"); + } + TEAR_DOWN(fp); + } } diff --git a/test/file/mutt_file_map_lines.c b/test/file/mutt_file_map_lines.c index 98475caf2..1bf7d3dd0 100644 --- a/test/file/mutt_file_map_lines.c +++ b/test/file/mutt_file_map_lines.c @@ -24,12 +24,40 @@ #include "acutest.h" #include "config.h" #include "mutt/mutt.h" +#include "common.h" + +#define BOOLIFY(x) ((x) ? "true" : "false") bool map_dummy(char *line, int line_num, void *user_data) { return false; } +static bool mapping_func(char *line, int line_num, void *user_data) +{ + const int *p_last_line_num = (const int *) (user_data); + if (!TEST_CHECK(strcmp(line, file_lines[line_num - 1]) == 0)) + { + TEST_MSG("Expected: %s", file_lines[line_num - 1]); + TEST_MSG("Actual: %s", line); + } + return (line_num < *p_last_line_num); +} + +static void test_file_map_lines_breaking_after(int last_line, bool expected) +{ + FILE *fp = SET_UP(); + if (!fp) + return; + bool res = mutt_file_map_lines(mapping_func, &last_line, fp, 0); + if (!TEST_CHECK(res == expected)) + { + TEST_MSG("Expected: %s", BOOLIFY(expected)); + TEST_MSG("Actual: %s", BOOLIFY(res)); + } + TEAR_DOWN(fp); +} + void test_mutt_file_map_lines(void) { // bool mutt_file_map_lines(mutt_file_map_t func, void *user_data, FILE *fp, int flags); @@ -50,4 +78,12 @@ void test_mutt_file_map_lines(void) mutt_file_map_t map = map_dummy; TEST_CHECK(!mutt_file_map_lines(map, "apple", NULL, 0)); } + + { + const size_t num = file_num_test_lines(); + test_file_map_lines_breaking_after(num + 1, true); + test_file_map_lines_breaking_after(0, false); + test_file_map_lines_breaking_after(1, false); + test_file_map_lines_breaking_after(num, false); + } } diff --git a/test/main.c b/test/main.c index aa4ff1a91..e251c900a 100644 --- a/test/main.c +++ b/test/main.c @@ -26,8 +26,6 @@ * Add your test cases to this list. *****************************************************************************/ #define NEOMUTT_TEST_LIST \ - NEOMUTT_TEST_ITEM(test_file_iter_line) \ - NEOMUTT_TEST_ITEM(test_file_map_lines) \ NEOMUTT_TEST_ITEM(test_rfc2047) \ NEOMUTT_TEST_ITEM(test_md5) \ NEOMUTT_TEST_ITEM(test_md5_ctx) \