diff options
Diffstat (limited to 'test-convert-markdown.c')
| -rw-r--r-- | test-convert-markdown.c | 41 |
1 files changed, 30 insertions, 11 deletions
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"); |
