diff options
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | test-1-input.txt | 1 | ||||
| -rw-r--r-- | test-1-output.txt | 1 | ||||
| -rw-r--r-- | test-2-input.txt | 1 | ||||
| -rw-r--r-- | test-2-output.txt | 1 | ||||
| -rw-r--r-- | test-convert-markdown.c | 41 |
6 files changed, 36 insertions, 13 deletions
@@ -13,13 +13,13 @@ convert-markdown: $(OBJS) $(OBJS): config.h clean: - rm -f convert-markdown $(OBJS) test-convert-markdown + rm -f convert-markdown $(OBJS) test-convert-markdown $(TESTOBJS) distclean: clean rm -f Makefile.configure config.h config.log config.h.old config.log.old check: tests - @echo "# header" | ./test-convert-markdown + @./test-convert-markdown tests: test-convert-markdown diff --git a/test-1-input.txt b/test-1-input.txt new file mode 100644 index 0000000..6f83f34 --- /dev/null +++ b/test-1-input.txt @@ -0,0 +1 @@ +# header diff --git a/test-1-output.txt b/test-1-output.txt new file mode 100644 index 0000000..5b47623 --- /dev/null +++ b/test-1-output.txt @@ -0,0 +1 @@ +<h1 id="header">header</h1> diff --git a/test-2-input.txt b/test-2-input.txt new file mode 100644 index 0000000..1c2717d --- /dev/null +++ b/test-2-input.txt @@ -0,0 +1 @@ +*italic* diff --git a/test-2-output.txt b/test-2-output.txt new file mode 100644 index 0000000..73a5a0c --- /dev/null +++ b/test-2-output.txt @@ -0,0 +1 @@ +<p><em>italic</em></p> diff --git a/test-convert-markdown.c b/test-convert-markdown.c index 403a68a..4ef2e16 100644 --- a/test-convert-markdown.c +++ b/test-convert-markdown.c @@ -16,41 +16,60 @@ #include "parse-file.h" -static int test_parse_file(const char* testname, const char* input, const char* output) { - FILE* instream = tmpfile(); +static int test_parse_file(const char* testname, const char* infile, const char* outfile) { FILE* outstream = tmpfile(); + FILE* outptr = fopen(outfile, "r"); - /* for some reason, input isn't properly written to instream */ - fprintf(instream, "%s\n", input); + if (outptr == NULL) { + printf("ERROR: could not read test data file\n"); + return 1; + } + + // find length of expected output + fseek(outptr, 0L, SEEK_END); + const int length = ftell(outptr) + 1; + rewind(outptr); - const int length = strlen(output) + 1; char buffer[length]; + char output[length]; + + // read expected output from outfile + fgets(output, length, outptr); + fclose(outptr); - /* TODO: use instream instead of stdin */ - _parse_file(stdin, outstream); + // read expected input from infile and redirect to stdin + if (freopen(infile, "r", stdin) != NULL) { + _parse_file(stdin, outstream); + } else { + printf("ERROR: could not read test data file\n"); + return 1; + } const int tmp_length = ftell(outstream) + 1; rewind(outstream); + // grab program output fgets(buffer, length, outstream); - fclose(instream); fclose(outstream); + // compare actual output to expected output if (strcmp(buffer, output) == 0 && tmp_length == length) { printf("%s test succeeded\n", testname); return 0; } else { printf("%s test failed\n", testname); - printf("%s !=\n%s\n", buffer, output); + printf("%s\n!=\n%s\n", buffer, output); return 1; } } int main(void) { - printf("=============\nRunning Tests\n=============\n"); + printf("\n=============\nRunning Tests\n=============\n"); int errors = 0; - errors += test_parse_file("1. Header", "# header", "<h1 id=\"header\">header</h1>\n"); + //errors += test_parse_file("1. Header", "# header", "<h1 id=\"header\">header</h1>\n"); //errors += test_parse_file("2. Bold", "*bold*", "<strong>bold</strong>\n"); + errors += test_parse_file("1. Header", "test-1-input.txt", "test-1-output.txt"); + errors += test_parse_file("2. Emphasis", "test-2-input.txt", "test-2-output.txt"); printf("\n============\nTest Summary\n============\n"); |
