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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_odd_length (void *data)
  83. {
  84. SCM k_foo = scm_from_utf8_keyword ("foo");
  85. SCM k_bar = scm_from_utf8_keyword ("bar");
  86. SCM arg_foo, arg_bar;
  87. scm_c_bind_keyword_arguments ("test",
  88. scm_list_n (k_foo, SCM_EOL,
  89. SCM_INUM0,
  90. SCM_UNDEFINED),
  91. SCM_ALLOW_OTHER_KEYS,
  92. k_foo, &arg_foo,
  93. k_bar, &arg_bar,
  94. SCM_UNDEFINED);
  95. assert (0);
  96. }
  97. static SCM
  98. odd_length_error_handler (void *data, SCM key, SCM args)
  99. {
  100. SCM expected_args = scm_list_n
  101. (scm_from_utf8_string ("test"),
  102. scm_from_utf8_string ("Odd length of keyword argument list"),
  103. SCM_EOL, SCM_BOOL_F,
  104. SCM_UNDEFINED);
  105. assert (scm_is_eq (key, scm_from_utf8_symbol ("keyword-argument-error")));
  106. assert (scm_is_true (scm_equal_p (args, expected_args)));
  107. return SCM_BOOL_T;
  108. }
  109. static void
  110. test_scm_c_bind_keyword_arguments ()
  111. {
  112. SCM k_foo = scm_from_utf8_keyword ("foo");
  113. SCM k_bar = scm_from_utf8_keyword ("bar");
  114. SCM k_baz = scm_from_utf8_keyword ("baz");
  115. SCM arg_foo, arg_bar;
  116. /* All kwargs provided. */
  117. arg_foo = SCM_INUM0;
  118. arg_bar = SCM_INUM1;
  119. scm_c_bind_keyword_arguments ("test",
  120. scm_list_n (k_bar, SCM_EOL,
  121. k_foo, SCM_BOOL_T,
  122. SCM_UNDEFINED),
  123. 0,
  124. k_foo, &arg_foo,
  125. k_bar, &arg_bar,
  126. SCM_UNDEFINED);
  127. assert (scm_is_eq (arg_foo, SCM_BOOL_T));
  128. assert (scm_is_eq (arg_bar, SCM_EOL));
  129. /* Some kwargs provided. */
  130. arg_foo = SCM_INUM0;
  131. arg_bar = SCM_INUM1;
  132. scm_c_bind_keyword_arguments ("test",
  133. scm_list_n (k_bar, SCM_EOL,
  134. SCM_UNDEFINED),
  135. 0,
  136. k_foo, &arg_foo,
  137. k_bar, &arg_bar,
  138. SCM_UNDEFINED);
  139. assert (scm_is_eq (arg_foo, SCM_INUM0));
  140. assert (scm_is_eq (arg_bar, SCM_EOL));
  141. /* No kwargs provided. */
  142. arg_foo = SCM_INUM0;
  143. arg_bar = SCM_INUM1;
  144. scm_c_bind_keyword_arguments ("test",
  145. SCM_EOL,
  146. 0,
  147. k_foo, &arg_foo,
  148. k_bar, &arg_bar,
  149. SCM_UNDEFINED);
  150. assert (scm_is_eq (arg_foo, SCM_INUM0));
  151. assert (scm_is_eq (arg_bar, SCM_INUM1));
  152. /* Other kwargs provided, when allowed. */
  153. arg_foo = SCM_INUM0;
  154. arg_bar = SCM_INUM1;
  155. scm_c_bind_keyword_arguments ("test",
  156. scm_list_n (k_foo, SCM_EOL,
  157. k_baz, SCM_BOOL_T,
  158. SCM_UNDEFINED),
  159. SCM_ALLOW_OTHER_KEYS,
  160. k_foo, &arg_foo,
  161. k_bar, &arg_bar,
  162. SCM_UNDEFINED);
  163. assert (scm_is_eq (arg_foo, SCM_EOL));
  164. assert (scm_is_eq (arg_bar, SCM_INUM1));
  165. /* Other non-kwargs provided, when allowed. */
  166. arg_foo = SCM_INUM0;
  167. arg_bar = SCM_INUM1;
  168. scm_c_bind_keyword_arguments ("test",
  169. scm_list_n (SCM_BOOL_F,
  170. k_foo, SCM_EOL,
  171. SCM_INUM0,
  172. k_bar, SCM_BOOL_T,
  173. SCM_INUM1,
  174. SCM_UNDEFINED),
  175. SCM_ALLOW_NON_KEYWORD_ARGUMENTS,
  176. k_foo, &arg_foo,
  177. k_bar, &arg_bar,
  178. SCM_UNDEFINED);
  179. assert (scm_is_eq (arg_foo, SCM_EOL));
  180. assert (scm_is_eq (arg_bar, SCM_BOOL_T));
  181. /* Test unrecognized keyword error. */
  182. scm_internal_catch (SCM_BOOL_T,
  183. test_unrecognized_keyword, NULL,
  184. unrecognized_keyword_error_handler, NULL);
  185. /* Test invalid keyword error. */
  186. scm_internal_catch (SCM_BOOL_T,
  187. test_invalid_keyword, NULL,
  188. invalid_keyword_error_handler, NULL);
  189. /* Test odd length error. */
  190. scm_internal_catch (SCM_BOOL_T,
  191. test_odd_length, NULL,
  192. odd_length_error_handler, NULL);
  193. }
  194. static void
  195. tests (void *data, int argc, char **argv)
  196. {
  197. test_scm_c_bind_keyword_arguments ();
  198. }
  199. int
  200. main (int argc, char *argv[])
  201. {
  202. scm_boot_guile (argc, argv, tests, NULL);
  203. return 0;
  204. }