+/*
+ * 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"
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 {
}
#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;
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;
}