]> git.draconx.ca Git - cdecl99.git/blobdiff - test/crossparse.c
Add better --help text to the test programs.
[cdecl99.git] / test / crossparse.c
index 75344cb6d911e7529e5aff0bdfaedfaf250829d8..05c3095a2d1953220e9860b934a1e2f5962b8c51 100644 (file)
@@ -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 <https://www.gnu.org/licenses/>.
+ */
+
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdbool.h>
 #include <getopt.h>
 #include <cdecl.h>
 #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;
 }