1 /* Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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
28 test_unrecognized_keyword (void *data
)
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");
35 scm_c_bind_keyword_arguments ("test",
36 scm_list_n (k_foo
, SCM_EOL
,
39 SCM_ALLOW_NON_KEYWORD_ARGUMENTS
,
47 unrecognized_keyword_error_handler (void *data
, SCM key
, SCM args
)
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")),
55 assert (scm_is_eq (key
, scm_from_utf8_symbol ("keyword-argument-error")));
56 assert (scm_is_true (scm_equal_p (args
, expected_args
)));
62 test_invalid_keyword (void *data
)
64 SCM k_foo
= scm_from_utf8_keyword ("foo");
65 SCM k_bar
= scm_from_utf8_keyword ("bar");
68 scm_c_bind_keyword_arguments ("test",
69 scm_list_n (k_foo
, SCM_EOL
,
80 invalid_keyword_error_handler (void *data
, SCM key
, SCM args
)
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
),
88 assert (scm_is_eq (key
, scm_from_utf8_symbol ("keyword-argument-error")));
89 assert (scm_is_true (scm_equal_p (args
, expected_args
)));
95 test_odd_length (void *data
)
97 SCM k_foo
= scm_from_utf8_keyword ("foo");
98 SCM k_bar
= scm_from_utf8_keyword ("bar");
101 scm_c_bind_keyword_arguments ("test",
102 scm_list_n (k_foo
, SCM_EOL
,
105 SCM_ALLOW_OTHER_KEYS
,
113 odd_length_error_handler (void *data
, SCM key
, SCM args
)
115 SCM expected_args
= scm_list_n
116 (scm_from_utf8_string ("test"),
117 scm_from_utf8_string ("Odd length of keyword argument list"),
121 assert (scm_is_eq (key
, scm_from_utf8_symbol ("keyword-argument-error")));
122 assert (scm_is_true (scm_equal_p (args
, expected_args
)));
128 test_scm_c_bind_keyword_arguments ()
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
;
135 /* All kwargs provided. */
138 scm_c_bind_keyword_arguments ("test",
139 scm_list_n (k_bar
, SCM_EOL
,
146 assert (scm_is_eq (arg_foo
, SCM_BOOL_T
));
147 assert (scm_is_eq (arg_bar
, SCM_EOL
));
149 /* Some kwargs provided. */
152 scm_c_bind_keyword_arguments ("test",
153 scm_list_n (k_bar
, SCM_EOL
,
159 assert (scm_is_eq (arg_foo
, SCM_INUM0
));
160 assert (scm_is_eq (arg_bar
, SCM_EOL
));
162 /* No kwargs provided. */
165 scm_c_bind_keyword_arguments ("test",
171 assert (scm_is_eq (arg_foo
, SCM_INUM0
));
172 assert (scm_is_eq (arg_bar
, SCM_INUM1
));
174 /* Other kwargs provided, when allowed. */
177 scm_c_bind_keyword_arguments ("test",
178 scm_list_n (k_foo
, SCM_EOL
,
181 SCM_ALLOW_OTHER_KEYS
,
185 assert (scm_is_eq (arg_foo
, SCM_EOL
));
186 assert (scm_is_eq (arg_bar
, SCM_INUM1
));
188 /* Other non-kwargs provided, when allowed. */
191 scm_c_bind_keyword_arguments ("test",
192 scm_list_n (SCM_BOOL_F
,
198 SCM_ALLOW_NON_KEYWORD_ARGUMENTS
,
202 assert (scm_is_eq (arg_foo
, SCM_EOL
));
203 assert (scm_is_eq (arg_bar
, SCM_BOOL_T
));
205 /* Test unrecognized keyword error. */
206 scm_internal_catch (SCM_BOOL_T
,
207 test_unrecognized_keyword
, NULL
,
208 unrecognized_keyword_error_handler
, NULL
);
210 /* Test invalid keyword error. */
211 scm_internal_catch (SCM_BOOL_T
,
212 test_invalid_keyword
, NULL
,
213 invalid_keyword_error_handler
, NULL
);
215 /* Test odd length error. */
216 scm_internal_catch (SCM_BOOL_T
,
217 test_odd_length
, NULL
,
218 odd_length_error_handler
, NULL
);
222 tests (void *data
, int argc
, char **argv
)
224 test_scm_c_bind_keyword_arguments ();
228 main (int argc
, char *argv
[])
230 scm_boot_guile (argc
, argv
, tests
, NULL
);