X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/e031b4f0220432aef6e1864ea6359353dbd8aed3..879d69d46fdf7ddf33beac6c32c7f2646d97d0ca:/test/crossparse.c diff --git a/test/crossparse.c b/test/crossparse.c index 75344cb..05c3095 100644 --- a/test/crossparse.c +++ b/test/crossparse.c @@ -1,7 +1,26 @@ +/* + * Test that libcdecl can parse its own output. + * Copyright © 2012, 2020 Nick Bowler + * + * 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 3 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 #include #include #include +#include #include #include #include "test.h" @@ -24,8 +43,18 @@ static void print_usage(FILE *f) static void print_help(void) { + const struct option *opt; + print_usage(stdout); - puts("Detailed help coming soon."); + puts("Test that libcdecl can parse its own output.\n"); + + puts("Options:"); + for (opt = lopts; opt->val; opt++) { + int w = print_option_start(opt, NULL); + + if (w) + putchar('\n'); + } } enum { @@ -69,9 +98,44 @@ err: } #define rerender(str, p, r) (rerender(str, #p, p, #r, r)) -int main(int argc, char **argv) +static bool test_crossparse(const char *str, int mode) { char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL; + bool ret = false; + + if (mode == MODE_ENGLISH) { + buf1 = rerender(str, cdecl_parse_english, cdecl_declare); + if (!buf1) + goto out; + buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain); + if (!buf2) + goto out; + buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare); + if (!buf3) + goto out; + } else { + buf1 = rerender(str, cdecl_parse_decl, cdecl_explain); + if (!buf1) + goto out; + buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare); + if (!buf2) + goto out; + buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain); + if (!buf3) + goto out; + } + + if (!strcmp(buf1, buf3)) + ret = true; +out: + free(buf1); + free(buf2); + free(buf3); + return ret; +} + +int main(int argc, char **argv) +{ int opt, mode = MODE_CDECL; int ret = EXIT_FAILURE; @@ -103,33 +167,13 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - if (mode == MODE_ENGLISH) { - buf1 = rerender(argv[optind], cdecl_parse_english, cdecl_declare); - if (!buf1) - goto out; - buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain); - if (!buf2) - goto out; - buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare); - if (!buf3) - goto out; - } else { - buf1 = rerender(argv[optind], cdecl_parse_decl, cdecl_explain); - if (!buf1) - goto out; - buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare); - if (!buf2) - goto out; - buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain); - if (!buf3) - goto out; + for (int i = optind; i < argc; i++) { + if (!test_crossparse(argv[i], mode)) { + fprintf(stderr, "%s: failed cross-parse check of: %s\n", + progname, argv[i]); + return EXIT_FAILURE; + } } - if (!strcmp(buf1, buf3)) - ret = EXIT_SUCCESS; -out: - free(buf1); - free(buf2); - free(buf3); - return ret; + return EXIT_SUCCESS; }