test-scm-c-bind-keyword-arguments.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Copyright (C) 2013, 2014 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. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #undef NDEBUG
  22. #include <libguile.h>
  23. #include <assert.h>
  24. static SCM
  25. test_unrecognized_keyword (void *data)
  26. {
  27. SCM k_foo = scm_from_utf8_keyword ("foo");
  28. SCM k_bar = scm_from_utf8_keyword ("bar");
  29. SCM k_baz = scm_from_utf8_keyword ("baz");
  30. SCM arg_foo, arg_bar;
  31. scm_c_bind_keyword_arguments ("test",
  32. scm_list_n (k_foo, SCM_EOL,
  33. k_baz, SCM_BOOL_T,
  34. SCM_UNDEFINED),
  35. SCM_ALLOW_NON_KEYWORD_ARGUMENTS,
  36. k_foo, &arg_foo,
  37. k_bar, &arg_bar,
  38. SCM_UNDEFINED);
  39. assert (0);
  40. }
  41. static SCM
  42. unrecognized_keyword_error_handler (void *data, SCM key, SCM args)
  43. {
  44. SCM expected_args = scm_list_n
  45. (scm_from_utf8_string ("test"),
  46. scm_from_utf8_string ("Unrecognized keyword"),
  47. SCM_EOL, scm_list_1 (scm_from_utf8_keyword ("baz")),
  48. SCM_UNDEFINED);
  49. assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error")));
  50. assert (scm_is_true (scm_equal_p (args, expected_args)));
  51. return SCM_BOOL_T;
  52. }
  53. static SCM
  54. test_invalid_keyword (void *data)
  55. {
  56. SCM k_foo = scm_from_utf8_keyword ("foo");
  57. SCM k_bar = scm_from_utf8_keyword ("bar");
  58. SCM arg_foo, arg_bar;
  59. scm_c_bind_keyword_arguments ("test",
  60. scm_list_n (k_foo, SCM_EOL,
  61. SCM_INUM0, SCM_INUM1,
  62. SCM_UNDEFINED),
  63. SCM_ALLOW_OTHER_KEYS,
  64. k_foo, &arg_foo,
  65. k_bar, &arg_bar,
  66. SCM_UNDEFINED);
  67. assert (0);
  68. }
  69. static SCM
  70. invalid_keyword_error_handler (void *data, SCM key, SCM args)
  71. {
  72. SCM expected_args = scm_list_n
  73. (scm_from_utf8_string ("test"),
  74. scm_from_utf8_string ("Invalid keyword"),
  75. SCM_EOL, scm_list_1 (SCM_INUM0),
  76. SCM_UNDEFINED);
  77. assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error")));
  78. assert (scm_is_true (scm_equal_p (args, expected_args)));
  79. return SCM_BOOL_T;
  80. }
  81. static SCM
  82. test_missing_value (void *data)
  83. {
  84. SCM k_foo = scm_from_utf8_keyword ("foo");
  85. SCM arg_foo;
  86. scm_c_bind_keyword_arguments ("test",
  87. scm_list_n (k_foo,
  88. SCM_UNDEFINED),
  89. SCM_ALLOW_OTHER_KEYS,
  90. k_foo, &arg_foo,
  91. SCM_UNDEFINED);
  92. assert (0);
  93. }
  94. static SCM
  95. missing_value_error_handler (void *data, SCM key, SCM args)
  96. {
  97. SCM expected_args = scm_list_n
  98. (scm_from_utf8_string ("test"),
  99. scm_from_utf8_string ("Keyword argument has no value"),
  100. SCM_EOL, scm_list_1 (scm_from_utf8_keyword ("foo")),
  101. SCM_UNDEFINED);
  102. assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error")));
  103. assert (scm_is_true (scm_equal_p (args, expected_args)));
  104. return SCM_BOOL_T;
  105. }
  106. static void
  107. test_scm_c_bind_keyword_arguments ()
  108. {
  109. SCM k_foo = scm_from_utf8_keyword ("foo");
  110. SCM k_bar = scm_from_utf8_keyword ("bar");
  111. SCM k_baz = scm_from_utf8_keyword ("baz");
  112. SCM arg_foo, arg_bar;
  113. /* All kwargs provided. */
  114. arg_foo = SCM_INUM0;
  115. arg_bar = SCM_INUM1;
  116. scm_c_bind_keyword_arguments ("test",
  117. scm_list_n (k_bar, SCM_EOL,
  118. k_foo, SCM_BOOL_T,
  119. SCM_UNDEFINED),
  120. 0,
  121. k_foo, &arg_foo,
  122. k_bar, &arg_bar,
  123. SCM_UNDEFINED);
  124. assert (scm_is_eq (arg_foo, SCM_BOOL_T));
  125. assert (scm_is_eq (arg_bar, SCM_EOL));
  126. /* Some kwargs provided. */
  127. arg_foo = SCM_INUM0;
  128. arg_bar = SCM_INUM1;
  129. scm_c_bind_keyword_arguments ("test",
  130. scm_list_n (k_bar, SCM_EOL,
  131. SCM_UNDEFINED),
  132. 0,
  133. k_foo, &arg_foo,
  134. k_bar, &arg_bar,
  135. SCM_UNDEFINED);
  136. assert (scm_is_eq (arg_foo, SCM_INUM0));
  137. assert (scm_is_eq (arg_bar, SCM_EOL));
  138. /* No kwargs provided. */
  139. arg_foo = SCM_INUM0;
  140. arg_bar = SCM_INUM1;
  141. scm_c_bind_keyword_arguments ("test",
  142. SCM_EOL,
  143. 0,
  144. k_foo, &arg_foo,
  145. k_bar, &arg_bar,
  146. SCM_UNDEFINED);
  147. assert (scm_is_eq (arg_foo, SCM_INUM0));
  148. assert (scm_is_eq (arg_bar, SCM_INUM1));
  149. /* Other kwargs provided, when allowed. */
  150. arg_foo = SCM_INUM0;
  151. arg_bar = SCM_INUM1;
  152. scm_c_bind_keyword_arguments ("test",
  153. scm_list_n (k_foo, SCM_EOL,
  154. k_baz, SCM_BOOL_T,
  155. SCM_UNDEFINED),
  156. SCM_ALLOW_OTHER_KEYS,
  157. k_foo, &arg_foo,
  158. k_bar, &arg_bar,
  159. SCM_UNDEFINED);
  160. assert (scm_is_eq (arg_foo, SCM_EOL));
  161. assert (scm_is_eq (arg_bar, SCM_INUM1));
  162. /* Other non-kwargs provided, when allowed. */
  163. arg_foo = SCM_INUM0;
  164. arg_bar = SCM_INUM1;
  165. scm_c_bind_keyword_arguments ("test",
  166. scm_list_n (SCM_BOOL_F,
  167. k_foo, SCM_EOL,
  168. SCM_INUM0,
  169. k_bar, SCM_BOOL_T,
  170. SCM_INUM1,
  171. SCM_UNDEFINED),
  172. SCM_ALLOW_NON_KEYWORD_ARGUMENTS,
  173. k_foo, &arg_foo,
  174. k_bar, &arg_bar,
  175. SCM_UNDEFINED);
  176. assert (scm_is_eq (arg_foo, SCM_EOL));
  177. assert (scm_is_eq (arg_bar, SCM_BOOL_T));
  178. /* Test unrecognized keyword error. */
  179. scm_internal_catch (SCM_BOOL_T,
  180. test_unrecognized_keyword, NULL,
  181. unrecognized_keyword_error_handler, NULL);
  182. /* Test invalid keyword error. */
  183. scm_internal_catch (SCM_BOOL_T,
  184. test_invalid_keyword, NULL,
  185. invalid_keyword_error_handler, NULL);
  186. /* Test missing value error. */
  187. scm_internal_catch (SCM_BOOL_T,
  188. test_missing_value, NULL,
  189. missing_value_error_handler, NULL);
  190. }
  191. static void
  192. tests (void *data, int argc, char **argv)
  193. {
  194. test_scm_c_bind_keyword_arguments ();
  195. }
  196. int
  197. main (int argc, char *argv[])
  198. {
  199. scm_boot_guile (argc, argv, tests, NULL);
  200. return 0;
  201. }