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

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