]> git.draconx.ca Git - cdecl99.git/blob - tests/decl-good.at
tests: Ensure that English keywords can be parsed.
[cdecl99.git] / tests / decl-good.at
1 # Copyright © 2020-2021, 2023 Nick Bowler
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 AT_BANNER([C declarations])
17
18 dnl _SIMPLE_DECL_ARG([n1 n2], [arg1], ..., [argn])
19 dnl
20 dnl Similar to m4_argn, except the first argument has (up to) two argument
21 dnl numbers, and the first nonempty matching argument is selected.
22 dnl
23 dnl The expansion is followed by a newline.
24 m4_define([_SIMPLE_DECL_ARG],
25 [m4_default(m4_map_args_w([$1], [m4_argn(], [, $2)], [,]))
26 ])
27
28 dnl SIMPLE_DECLS_EXPLAIN([arg], [arg...])
29 dnl
30 dnl Generate tests to validate correct parsing and output of each specification
31 dnl argument.  The arguments are quoted list of quoted items, with the first
32 dnl item of each list being the C declaration or type name to be explained.
33 dnl
34 dnl The expected output is the fourth item of the list, or, if that is empty,
35 dnl the second item in the list (this allows the same lists to be used for
36 dnl both SIMPLE_DECLS_DECLARE and SIMPLE_DECLS_EXPLAIN even in case of
37 dnl various equivalent syntactic forms)
38 dnl
39 dnl The items should be related to each other as the user-visible test group
40 dnl name is determined only by the first argument.
41 m4_define([SIMPLE_DECLS_EXPLAIN],
42 [AT_SETUP([Explain "m4_car($1)"m4_ifnblank([$2], [ etc.])])
43 AT_DATA([test.dat],
44 [m4_map_args([explain m4_curry([_SIMPLE_DECL_ARG], [1])], $@)])
45 AT_DATA([expout],
46 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [4 2])], $@)])
47 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
48 AT_CLEANUP])
49
50 dnl SIMPLE_DECLS_DECLARE([arg], [arg ...])
51 dnl
52 dnl Similar to SIMPLE_DECLS_EXPLAIN, except in the opposite direction.  The
53 dnl arguments are quoted lists of quoted items, with the second item of each
54 dnl list being the input query.
55 dnl
56 dnl The expected output is the third item of the list, or, if that is empty,
57 dnl the first item in the list (this allows the same lists to be used for
58 dnl both SIMPLE_DECLS_DECLARE and SIMPLE_DECLS_EXPLAIN even in the case
59 dnl of various equivalent syntactic forsm)
60 dnl
61 dnl The items should be related to each other as the user-visible test group
62 dnl name is determined only by the first argument.
63 m4_define([SIMPLE_DECLS_DECLARE],
64 [AT_SETUP([Declare "m4_car($1)"m4_ifnblank([$2], [ etc.])])
65 AT_DATA([test.dat],
66 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [2])], $@)])
67 AT_DATA([expout],
68 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [3 1])], $@)])
69 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
70 AT_CLEANUP])
71
72 dnl SIMPLE_DECLS(arg, [arg ...])
73 m4_define([SIMPLE_DECLS],
74 [SIMPLE_DECLS_EXPLAIN($@)
75 SIMPLE_DECLS_DECLARE($@)])
76
77 SIMPLE_DECLS(
78   [[int **],  [type pointer to pointer to int]],
79   [[int **x], [declare x as pointer to pointer to int]])
80
81 SIMPLE_DECLS(
82   [[int (*x)], [declare x as pointer to int], [int *x]],
83   [[int (*)],  [type pointer to int], [int *]])
84
85 SIMPLE_DECLS(
86   [[int (x*)], [type function (pointer to x) returning int], [int (x *)]])
87
88 SIMPLE_DECLS(
89   [[int (x)()], [declare x as function returning int], [int x()]])
90
91 dnl Check that function reduction does not occur in english parses
92 SIMPLE_DECLS(
93   [[int (x)], [type function (x) returning int], [], [declare x as int]])
94
95 SIMPLE_DECLS(
96   [[int x@<:@@:>@], [declare x as array of int]],
97   [[int @<:@@:>@],  [type array of int]])
98
99 SIMPLE_DECLS_EXPLAIN(
100   [[int ((int))], [type function (int) returning int], [int (int)]],
101   [[int (x(int))], [declare x as function (int) returning int], [int x(int)]],
102   [[int (())], [type function returning int], [int ()]])
103
104 SIMPLE_DECLS_EXPLAIN(
105   [[int (x())], [declare x as function returning int], [int x()]],
106   [[int ((x)())], [declare x as function returning int], [int x()]])
107
108 dnl Test that english-only keywords are not rejected as idenfitiers in C mode.
109 SIMPLE_DECLS_EXPLAIN(
110   [[of array@<:@@:>@], [declare array as array of of]],
111   [[returning as(function)],
112     [declare as as function (function) returning returning]],
113   [[pointer *declare], [declare declare as pointer to pointer]],
114   [[type to], [declare to as type]])