From: Scott Worley Date: Thu, 31 Aug 2023 18:49:54 +0000 (-0700) Subject: test: Use expectations for verifying output X-Git-Tag: v1.0.0~20 X-Git-Url: http://git.scottworley.com/tl-append/commitdiff_plain/71a7e5c5f2fa792715f1c4c9491ad8cc7e718388?ds=sidebyside test: Use expectations for verifying output --- diff --git a/Makefile b/Makefile index 03bc6c5..f5e1548 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ INSTALL = install tl-append: %: %.c - $(CC) -Wall -Wextra -pedantic -o $@ $^ + $(CC) -Wall -Wextra -pedantic -std=c99 -o $@ $^ .PHONY: check check: tl-append tl-append-test diff --git a/tl-append-test.c b/tl-append-test.c index 3eef960..2a5d233 100644 --- a/tl-append-test.c +++ b/tl-append-test.c @@ -4,13 +4,14 @@ #include #include -struct expectation { +typedef struct expectation { const char *message; -}; +} ex_t; -const struct expectation END = {NULL}; -static struct expectation expectation(const char *message) { - struct expectation exp; +const ex_t END = {NULL}; +static int is_end(ex_t exp) { return exp.message == END.message; } +static ex_t expectation(const char *message) { + ex_t exp; exp.message = message; return exp; } @@ -34,7 +35,7 @@ static void remove_logfile() { } } -static struct expectation write_to_tl_append(const char *content) { +static ex_t write_to_tl_append(const char *content) { FILE *p = popen("./tl-append", "w"); if (p == NULL) die_err("Couldn't run tl-append"); @@ -48,34 +49,37 @@ static struct expectation write_to_tl_append(const char *content) { return expectation(content); } -static void verify_log_contents(const char *contents) { - size_t len = strlen(contents); - char *buf = (char *)malloc(len + 2); +static void verify_log_contents(ex_t exps[]) { FILE *f = fopen("tl.log", "r"); if (f == NULL) die_err("Error opening log file"); - buf[fread(buf, 1, len + 1, f)] = '\0'; - if (ferror(f)) - die("Error reading log file"); - if (strncmp(contents, buf, len + 1) != 0) - die("Wrong contents in log file"); + for (size_t i = 0; !is_end(exps[i]); i++) { + size_t len = strlen(exps[i].message); + char *buf = (char *)malloc(len + 2); + if (fgets(buf, len + 1, f) == NULL) + die("Error reading log file"); + if (ferror(f)) + die("Error reading log file"); + if (strncmp(exps[i].message, buf, len + 1) != 0) + die("Wrong contents in log file"); + free(buf); + } if (fclose(f) != 0) die_err("Error closing log file"); - free(buf); } static void write_and_read_line() { remove_logfile(); - write_to_tl_append("foo\n"); - verify_log_contents("foo\n"); + ex_t e = write_to_tl_append("foo\n"); + verify_log_contents((ex_t[]){e, END}); } static void write_and_read_two_lines() { remove_logfile(); - write_to_tl_append("foo\n"); - write_to_tl_append("bar\n"); - verify_log_contents("foo\nbar\n"); + ex_t e1 = write_to_tl_append("foo\n"); + ex_t e2 = write_to_tl_append("bar\n"); + verify_log_contents((ex_t[]){e1, e2, END}); } int main() {