Commit | Line | Data |
---|---|---|
0c1f2b0e | 1 | /* Copyright (C) 2013, 2014 Free Software Foundation, Inc. |
a16d4e82 MW |
2 | * |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public License | |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301 USA | |
17 | */ | |
18 | ||
19 | #if HAVE_CONFIG_H | |
20 | # include <config.h> | |
21 | #endif | |
22 | ||
0c1f2b0e LC |
23 | #undef NDEBUG |
24 | ||
a16d4e82 MW |
25 | #include <libguile.h> |
26 | ||
27 | #include <assert.h> | |
28 | ||
a16d4e82 MW |
29 | static SCM |
30 | test_unrecognized_keyword (void *data) | |
31 | { | |
32 | SCM k_foo = scm_from_utf8_keyword ("foo"); | |
33 | SCM k_bar = scm_from_utf8_keyword ("bar"); | |
34 | SCM k_baz = scm_from_utf8_keyword ("baz"); | |
35 | SCM arg_foo, arg_bar; | |
36 | ||
37 | scm_c_bind_keyword_arguments ("test", | |
38 | scm_list_n (k_foo, SCM_EOL, | |
39 | k_baz, SCM_BOOL_T, | |
40 | SCM_UNDEFINED), | |
41 | SCM_ALLOW_NON_KEYWORD_ARGUMENTS, | |
42 | k_foo, &arg_foo, | |
43 | k_bar, &arg_bar, | |
44 | SCM_UNDEFINED); | |
45 | assert (0); | |
46 | } | |
47 | ||
8b12a34c MW |
48 | static SCM |
49 | unrecognized_keyword_error_handler (void *data, SCM key, SCM args) | |
50 | { | |
51 | SCM expected_args = scm_list_n | |
52 | (scm_from_utf8_string ("test"), | |
53 | scm_from_utf8_string ("Unrecognized keyword"), | |
54 | SCM_EOL, scm_list_1 (scm_from_utf8_keyword ("baz")), | |
55 | SCM_UNDEFINED); | |
56 | ||
57 | assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error"))); | |
58 | assert (scm_is_true (scm_equal_p (args, expected_args))); | |
59 | ||
60 | return SCM_BOOL_T; | |
61 | } | |
62 | ||
a16d4e82 MW |
63 | static SCM |
64 | test_invalid_keyword (void *data) | |
65 | { | |
66 | SCM k_foo = scm_from_utf8_keyword ("foo"); | |
67 | SCM k_bar = scm_from_utf8_keyword ("bar"); | |
68 | SCM arg_foo, arg_bar; | |
69 | ||
70 | scm_c_bind_keyword_arguments ("test", | |
3e5a164a MW |
71 | scm_list_n (k_foo, SCM_EOL, |
72 | SCM_INUM0, SCM_INUM1, | |
73 | SCM_UNDEFINED), | |
74 | SCM_ALLOW_OTHER_KEYS, | |
75 | k_foo, &arg_foo, | |
76 | k_bar, &arg_bar, | |
77 | SCM_UNDEFINED); | |
a16d4e82 MW |
78 | assert (0); |
79 | } | |
80 | ||
8b12a34c MW |
81 | static SCM |
82 | invalid_keyword_error_handler (void *data, SCM key, SCM args) | |
83 | { | |
84 | SCM expected_args = scm_list_n | |
85 | (scm_from_utf8_string ("test"), | |
86 | scm_from_utf8_string ("Invalid keyword"), | |
87 | SCM_EOL, scm_list_1 (SCM_INUM0), | |
88 | SCM_UNDEFINED); | |
89 | ||
90 | assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error"))); | |
91 | assert (scm_is_true (scm_equal_p (args, expected_args))); | |
92 | ||
93 | return SCM_BOOL_T; | |
94 | } | |
95 | ||
a16d4e82 MW |
96 | static SCM |
97 | test_odd_length (void *data) | |
98 | { | |
99 | SCM k_foo = scm_from_utf8_keyword ("foo"); | |
100 | SCM k_bar = scm_from_utf8_keyword ("bar"); | |
101 | SCM arg_foo, arg_bar; | |
102 | ||
103 | scm_c_bind_keyword_arguments ("test", | |
104 | scm_list_n (k_foo, SCM_EOL, | |
105 | SCM_INUM0, | |
106 | SCM_UNDEFINED), | |
107 | SCM_ALLOW_OTHER_KEYS, | |
108 | k_foo, &arg_foo, | |
109 | k_bar, &arg_bar, | |
110 | SCM_UNDEFINED); | |
111 | assert (0); | |
112 | } | |
113 | ||
8b12a34c MW |
114 | static SCM |
115 | odd_length_error_handler (void *data, SCM key, SCM args) | |
116 | { | |
117 | SCM expected_args = scm_list_n | |
118 | (scm_from_utf8_string ("test"), | |
119 | scm_from_utf8_string ("Odd length of keyword argument list"), | |
120 | SCM_EOL, SCM_BOOL_F, | |
121 | SCM_UNDEFINED); | |
122 | ||
123 | assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error"))); | |
124 | assert (scm_is_true (scm_equal_p (args, expected_args))); | |
125 | ||
126 | return SCM_BOOL_T; | |
127 | } | |
128 | ||
a16d4e82 MW |
129 | static void |
130 | test_scm_c_bind_keyword_arguments () | |
131 | { | |
132 | SCM k_foo = scm_from_utf8_keyword ("foo"); | |
133 | SCM k_bar = scm_from_utf8_keyword ("bar"); | |
134 | SCM k_baz = scm_from_utf8_keyword ("baz"); | |
135 | SCM arg_foo, arg_bar; | |
136 | ||
137 | /* All kwargs provided. */ | |
138 | arg_foo = SCM_INUM0; | |
139 | arg_bar = SCM_INUM1; | |
140 | scm_c_bind_keyword_arguments ("test", | |
141 | scm_list_n (k_bar, SCM_EOL, | |
142 | k_foo, SCM_BOOL_T, | |
143 | SCM_UNDEFINED), | |
144 | 0, | |
145 | k_foo, &arg_foo, | |
146 | k_bar, &arg_bar, | |
147 | SCM_UNDEFINED); | |
148 | assert (scm_is_eq (arg_foo, SCM_BOOL_T)); | |
149 | assert (scm_is_eq (arg_bar, SCM_EOL)); | |
150 | ||
151 | /* Some kwargs provided. */ | |
152 | arg_foo = SCM_INUM0; | |
153 | arg_bar = SCM_INUM1; | |
154 | scm_c_bind_keyword_arguments ("test", | |
155 | scm_list_n (k_bar, SCM_EOL, | |
156 | SCM_UNDEFINED), | |
157 | 0, | |
158 | k_foo, &arg_foo, | |
159 | k_bar, &arg_bar, | |
160 | SCM_UNDEFINED); | |
161 | assert (scm_is_eq (arg_foo, SCM_INUM0)); | |
162 | assert (scm_is_eq (arg_bar, SCM_EOL)); | |
163 | ||
164 | /* No kwargs provided. */ | |
165 | arg_foo = SCM_INUM0; | |
166 | arg_bar = SCM_INUM1; | |
167 | scm_c_bind_keyword_arguments ("test", | |
168 | SCM_EOL, | |
169 | 0, | |
170 | k_foo, &arg_foo, | |
171 | k_bar, &arg_bar, | |
172 | SCM_UNDEFINED); | |
173 | assert (scm_is_eq (arg_foo, SCM_INUM0)); | |
174 | assert (scm_is_eq (arg_bar, SCM_INUM1)); | |
175 | ||
176 | /* Other kwargs provided, when allowed. */ | |
177 | arg_foo = SCM_INUM0; | |
178 | arg_bar = SCM_INUM1; | |
179 | scm_c_bind_keyword_arguments ("test", | |
180 | scm_list_n (k_foo, SCM_EOL, | |
181 | k_baz, SCM_BOOL_T, | |
182 | SCM_UNDEFINED), | |
183 | SCM_ALLOW_OTHER_KEYS, | |
184 | k_foo, &arg_foo, | |
185 | k_bar, &arg_bar, | |
186 | SCM_UNDEFINED); | |
187 | assert (scm_is_eq (arg_foo, SCM_EOL)); | |
188 | assert (scm_is_eq (arg_bar, SCM_INUM1)); | |
189 | ||
190 | /* Other non-kwargs provided, when allowed. */ | |
191 | arg_foo = SCM_INUM0; | |
192 | arg_bar = SCM_INUM1; | |
193 | scm_c_bind_keyword_arguments ("test", | |
194 | scm_list_n (SCM_BOOL_F, | |
195 | k_foo, SCM_EOL, | |
196 | SCM_INUM0, | |
197 | k_bar, SCM_BOOL_T, | |
198 | SCM_INUM1, | |
199 | SCM_UNDEFINED), | |
200 | SCM_ALLOW_NON_KEYWORD_ARGUMENTS, | |
201 | k_foo, &arg_foo, | |
202 | k_bar, &arg_bar, | |
203 | SCM_UNDEFINED); | |
204 | assert (scm_is_eq (arg_foo, SCM_EOL)); | |
205 | assert (scm_is_eq (arg_bar, SCM_BOOL_T)); | |
206 | ||
207 | /* Test unrecognized keyword error. */ | |
208 | scm_internal_catch (SCM_BOOL_T, | |
209 | test_unrecognized_keyword, NULL, | |
8b12a34c | 210 | unrecognized_keyword_error_handler, NULL); |
a16d4e82 MW |
211 | |
212 | /* Test invalid keyword error. */ | |
213 | scm_internal_catch (SCM_BOOL_T, | |
214 | test_invalid_keyword, NULL, | |
8b12a34c | 215 | invalid_keyword_error_handler, NULL); |
a16d4e82 MW |
216 | |
217 | /* Test odd length error. */ | |
218 | scm_internal_catch (SCM_BOOL_T, | |
219 | test_odd_length, NULL, | |
8b12a34c | 220 | odd_length_error_handler, NULL); |
a16d4e82 MW |
221 | } |
222 | ||
223 | static void | |
224 | tests (void *data, int argc, char **argv) | |
225 | { | |
226 | test_scm_c_bind_keyword_arguments (); | |
227 | } | |
228 | ||
229 | int | |
230 | main (int argc, char *argv[]) | |
231 | { | |
232 | scm_boot_guile (argc, argv, tests, NULL); | |
233 | return 0; | |
234 | } |