Merge branch 'stable-2.0'
[bpt/guile.git] / test-suite / standalone / test-scm-c-bind-keyword-arguments.c
1 /* Copyright (C) 2013 Free Software Foundation, Inc.
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
23 #include <libguile.h>
24
25 #include <assert.h>
26
27 static SCM
28 test_unrecognized_keyword (void *data)
29 {
30 SCM k_foo = scm_from_utf8_keyword ("foo");
31 SCM k_bar = scm_from_utf8_keyword ("bar");
32 SCM k_baz = scm_from_utf8_keyword ("baz");
33 SCM arg_foo, arg_bar;
34
35 scm_c_bind_keyword_arguments ("test",
36 scm_list_n (k_foo, SCM_EOL,
37 k_baz, SCM_BOOL_T,
38 SCM_UNDEFINED),
39 SCM_ALLOW_NON_KEYWORD_ARGUMENTS,
40 k_foo, &arg_foo,
41 k_bar, &arg_bar,
42 SCM_UNDEFINED);
43 assert (0);
44 }
45
46 static SCM
47 unrecognized_keyword_error_handler (void *data, SCM key, SCM args)
48 {
49 SCM expected_args = scm_list_n
50 (scm_from_utf8_string ("test"),
51 scm_from_utf8_string ("Unrecognized keyword"),
52 SCM_EOL, scm_list_1 (scm_from_utf8_keyword ("baz")),
53 SCM_UNDEFINED);
54
55 assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error")));
56 assert (scm_is_true (scm_equal_p (args, expected_args)));
57
58 return SCM_BOOL_T;
59 }
60
61 static SCM
62 test_invalid_keyword (void *data)
63 {
64 SCM k_foo = scm_from_utf8_keyword ("foo");
65 SCM k_bar = scm_from_utf8_keyword ("bar");
66 SCM arg_foo, arg_bar;
67
68 scm_c_bind_keyword_arguments ("test",
69 scm_list_n (k_foo, SCM_EOL,
70 SCM_INUM0, SCM_INUM1,
71 SCM_UNDEFINED),
72 SCM_ALLOW_OTHER_KEYS,
73 k_foo, &arg_foo,
74 k_bar, &arg_bar,
75 SCM_UNDEFINED);
76 assert (0);
77 }
78
79 static SCM
80 invalid_keyword_error_handler (void *data, SCM key, SCM args)
81 {
82 SCM expected_args = scm_list_n
83 (scm_from_utf8_string ("test"),
84 scm_from_utf8_string ("Invalid keyword"),
85 SCM_EOL, scm_list_1 (SCM_INUM0),
86 SCM_UNDEFINED);
87
88 assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error")));
89 assert (scm_is_true (scm_equal_p (args, expected_args)));
90
91 return SCM_BOOL_T;
92 }
93
94 static SCM
95 test_odd_length (void *data)
96 {
97 SCM k_foo = scm_from_utf8_keyword ("foo");
98 SCM k_bar = scm_from_utf8_keyword ("bar");
99 SCM arg_foo, arg_bar;
100
101 scm_c_bind_keyword_arguments ("test",
102 scm_list_n (k_foo, SCM_EOL,
103 SCM_INUM0,
104 SCM_UNDEFINED),
105 SCM_ALLOW_OTHER_KEYS,
106 k_foo, &arg_foo,
107 k_bar, &arg_bar,
108 SCM_UNDEFINED);
109 assert (0);
110 }
111
112 static SCM
113 odd_length_error_handler (void *data, SCM key, SCM args)
114 {
115 SCM expected_args = scm_list_n
116 (scm_from_utf8_string ("test"),
117 scm_from_utf8_string ("Odd length of keyword argument list"),
118 SCM_EOL, SCM_BOOL_F,
119 SCM_UNDEFINED);
120
121 assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error")));
122 assert (scm_is_true (scm_equal_p (args, expected_args)));
123
124 return SCM_BOOL_T;
125 }
126
127 static void
128 test_scm_c_bind_keyword_arguments ()
129 {
130 SCM k_foo = scm_from_utf8_keyword ("foo");
131 SCM k_bar = scm_from_utf8_keyword ("bar");
132 SCM k_baz = scm_from_utf8_keyword ("baz");
133 SCM arg_foo, arg_bar;
134
135 /* All kwargs provided. */
136 arg_foo = SCM_INUM0;
137 arg_bar = SCM_INUM1;
138 scm_c_bind_keyword_arguments ("test",
139 scm_list_n (k_bar, SCM_EOL,
140 k_foo, SCM_BOOL_T,
141 SCM_UNDEFINED),
142 0,
143 k_foo, &arg_foo,
144 k_bar, &arg_bar,
145 SCM_UNDEFINED);
146 assert (scm_is_eq (arg_foo, SCM_BOOL_T));
147 assert (scm_is_eq (arg_bar, SCM_EOL));
148
149 /* Some kwargs provided. */
150 arg_foo = SCM_INUM0;
151 arg_bar = SCM_INUM1;
152 scm_c_bind_keyword_arguments ("test",
153 scm_list_n (k_bar, SCM_EOL,
154 SCM_UNDEFINED),
155 0,
156 k_foo, &arg_foo,
157 k_bar, &arg_bar,
158 SCM_UNDEFINED);
159 assert (scm_is_eq (arg_foo, SCM_INUM0));
160 assert (scm_is_eq (arg_bar, SCM_EOL));
161
162 /* No kwargs provided. */
163 arg_foo = SCM_INUM0;
164 arg_bar = SCM_INUM1;
165 scm_c_bind_keyword_arguments ("test",
166 SCM_EOL,
167 0,
168 k_foo, &arg_foo,
169 k_bar, &arg_bar,
170 SCM_UNDEFINED);
171 assert (scm_is_eq (arg_foo, SCM_INUM0));
172 assert (scm_is_eq (arg_bar, SCM_INUM1));
173
174 /* Other kwargs provided, when allowed. */
175 arg_foo = SCM_INUM0;
176 arg_bar = SCM_INUM1;
177 scm_c_bind_keyword_arguments ("test",
178 scm_list_n (k_foo, SCM_EOL,
179 k_baz, SCM_BOOL_T,
180 SCM_UNDEFINED),
181 SCM_ALLOW_OTHER_KEYS,
182 k_foo, &arg_foo,
183 k_bar, &arg_bar,
184 SCM_UNDEFINED);
185 assert (scm_is_eq (arg_foo, SCM_EOL));
186 assert (scm_is_eq (arg_bar, SCM_INUM1));
187
188 /* Other non-kwargs provided, when allowed. */
189 arg_foo = SCM_INUM0;
190 arg_bar = SCM_INUM1;
191 scm_c_bind_keyword_arguments ("test",
192 scm_list_n (SCM_BOOL_F,
193 k_foo, SCM_EOL,
194 SCM_INUM0,
195 k_bar, SCM_BOOL_T,
196 SCM_INUM1,
197 SCM_UNDEFINED),
198 SCM_ALLOW_NON_KEYWORD_ARGUMENTS,
199 k_foo, &arg_foo,
200 k_bar, &arg_bar,
201 SCM_UNDEFINED);
202 assert (scm_is_eq (arg_foo, SCM_EOL));
203 assert (scm_is_eq (arg_bar, SCM_BOOL_T));
204
205 /* Test unrecognized keyword error. */
206 scm_internal_catch (SCM_BOOL_T,
207 test_unrecognized_keyword, NULL,
208 unrecognized_keyword_error_handler, NULL);
209
210 /* Test invalid keyword error. */
211 scm_internal_catch (SCM_BOOL_T,
212 test_invalid_keyword, NULL,
213 invalid_keyword_error_handler, NULL);
214
215 /* Test odd length error. */
216 scm_internal_catch (SCM_BOOL_T,
217 test_odd_length, NULL,
218 odd_length_error_handler, NULL);
219 }
220
221 static void
222 tests (void *data, int argc, char **argv)
223 {
224 test_scm_c_bind_keyword_arguments ();
225 }
226
227 int
228 main (int argc, char *argv[])
229 {
230 scm_boot_guile (argc, argv, tests, NULL);
231 return 0;
232 }