]> git.draconx.ca Git - cdecl99.git/blob - t/crossparse.c
e19b43852f77e6a28d984376f8e45b9e688cf989
[cdecl99.git] / t / crossparse.c
1 /*
2  * Test that libcdecl can parse its own output.
3  * Copyright © 2012, 2020 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <getopt.h>
26 #include <cdecl.h>
27 #include "test.h"
28
29 #define PROGNAME "crossparse"
30 static const char *progname = PROGNAME;
31 static const char sopts[] = "f:ECVH";
32 static const struct option lopts[] = {
33         { "cdecl",   0, NULL, 'C' },
34         { "english", 0, NULL, 'E' },
35         { "file",    2, NULL, 'f' },
36         { "version", 0, NULL, 'V' },
37         { "help",    0, NULL, 'H' },
38         { 0 }
39 };
40
41 static void print_usage(FILE *f)
42 {
43         fprintf(f, "Usage: %s [options]\n", progname);
44 }
45
46 static void print_help(void)
47 {
48         const struct option *opt;
49
50         print_usage(stdout);
51         puts("Test that libcdecl can parse its own output.\n");
52         test_print_options(lopts);
53 }
54
55 enum {
56         MODE_CDECL,
57         MODE_ENGLISH,
58 };
59
60 typedef struct cdecl *parse_func(const char *);
61 typedef size_t render_func(char *, size_t, struct cdecl *);
62
63 char *rerender(const char *str, const char *parse_name,  parse_func *parse,
64                                 const char *render_name, render_func *render)
65 {
66         struct cdecl *decl = NULL;
67         char *buf = NULL;
68         size_t len;
69
70         decl = parse(str);
71         if (!decl) {
72                 fprintf(stderr, "%s: %s: failed to parse input: %s\n",
73                                 progname, parse_name, cdecl_get_error()->str);
74                 goto err;
75         }
76
77         len = render(NULL, 0, decl);
78         buf = malloc_nofail(len+1);
79         if (render(buf, len+1, decl) != len) {
80                 fprintf(stderr, "%s: %s: inconsistent length returned\n",
81                                 progname, render_name);
82                 goto err;
83         }
84
85         cdecl_free(decl);
86         return buf;
87 err:
88         cdecl_free(decl);
89         free(buf);
90
91         fprintf(stderr, "%s: the failed input was: %s\n", progname, str);
92         return NULL;
93 }
94 #define rerender(str, p, r) (rerender(str, #p, p, #r, r))
95
96 static bool test_crossparse(const char *str, int mode)
97 {
98         char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
99         bool ret = false;
100
101         if (mode == MODE_ENGLISH) {
102                 buf1 = rerender(str, cdecl_parse_english, cdecl_declare);
103                 if (!buf1)
104                         goto out;
105                 buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
106                 if (!buf2)
107                         goto out;
108                 buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
109                 if (!buf3)
110                         goto out;
111         } else {
112                 buf1 = rerender(str, cdecl_parse_decl, cdecl_explain);
113                 if (!buf1)
114                         goto out;
115                 buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
116                 if (!buf2)
117                         goto out;
118                 buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
119                 if (!buf3)
120                         goto out;
121         }
122
123         if (!strcmp(buf1, buf3))
124                 ret = true;
125 out:
126         free(buf1);
127         free(buf2);
128         free(buf3);
129
130         if (!ret) {
131                 fprintf(stderr, "%s: failed cross-parse check of: %s\n",
132                                 progname, str);
133         }
134
135         return ret;
136 }
137
138 int main(int argc, char **argv)
139 {
140         int opt, mode = MODE_CDECL;
141         int ret = EXIT_SUCCESS;
142
143         const char *filename = NULL;
144         FILE *infile = NULL;
145
146         if (argc > 0)
147                 progname = argv[0];
148
149         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
150                 switch (opt) {
151                 case 'C':
152                         mode = MODE_CDECL;
153                         break;
154                 case 'E':
155                         mode = MODE_ENGLISH;
156                         break;
157                 case 'f':
158                         infile = stdin;
159                         filename = optarg;
160                         break;
161                 case 'V':
162                         test_print_version(PROGNAME);
163                         return EXIT_SUCCESS;
164                 case 'H':
165                         print_help();
166                         return EXIT_SUCCESS;
167                 default:
168                         print_usage(stderr);
169                         return EXIT_FAILURE;
170                 }
171         }
172
173         if (infile) {
174                 char *line = NULL;
175                 size_t n;
176
177                 if (filename) {
178                         infile = fopen(filename, "r");
179                         if (!infile) {
180                                 fprintf(stderr, "%s: %s: %s\n", progname,
181                                                 filename, strerror(errno));
182                                 return EXIT_FAILURE;
183                         }
184                 }
185
186                 while (getline(&line, &n, infile) >= 0) {
187                         char *c = strchr(line, '\n');
188                         if (c)
189                                 *c = '\0';
190                         if (!test_crossparse(line, mode))
191                                 ret = EXIT_FAILURE;
192                 }
193         } else if (argv[optind]) {
194                 for (int i = optind; i < argc; i++) {
195                         if (!test_crossparse(argv[i], mode))
196                                 ret = EXIT_FAILURE;
197                 }
198         } else {
199                 print_usage(stderr);
200                 return EXIT_FAILURE;
201         }
202
203         return ret;
204 }