optargs.test 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ;;;; optargs.test --- test suite for optional arg processing -*- scheme -*-
  2. ;;;; Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> --- June 2001
  3. ;;;;
  4. ;;;; Copyright (C) 2001, 2006, 2009, 2010, 2013 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-optargs)
  20. #:use-module (test-suite lib)
  21. #:use-module (system base compile)
  22. #:use-module (ice-9 optargs))
  23. (define exception:invalid-keyword
  24. '(keyword-argument-error . "Invalid keyword"))
  25. (define exception:unrecognized-keyword
  26. '(keyword-argument-error . "Unrecognized keyword"))
  27. (define exception:extraneous-arguments
  28. ;; Message depends on whether we use the interpreter or VM, and on the
  29. ;; evenness of the number of extra arguments (!).
  30. ;'(keyword-argument-error . ".*")
  31. '(#t . ".*"))
  32. (with-test-prefix/c&e "optional argument processing"
  33. (pass-if "local defines work with optional arguments"
  34. (eval '(begin
  35. (define* (test-1 #:optional (x 0))
  36. (define d 1) ; local define
  37. #t)
  38. (false-if-exception (test-1)))
  39. (interaction-environment))))
  40. ;;;
  41. ;;; let-keywords
  42. ;;;
  43. (define-syntax-rule (without-compiler-warnings exp ...)
  44. (parameterize ((default-warning-level 0)) exp ...))
  45. (without-compiler-warnings
  46. (with-test-prefix/c&e "let-keywords"
  47. ;; in guile 1.6.4 and earlier, an empty binding list only used `begin',
  48. ;; which caused apparently internal defines to "leak" out into the
  49. ;; encompasing environment
  50. (pass-if-exception "empty bindings internal defines leaking out"
  51. exception:unbound-var
  52. (let ((rest '()))
  53. (let-keywords rest #f ()
  54. (define localvar #f)
  55. #f)
  56. localvar))
  57. (pass-if "one key"
  58. (let-keywords '(#:foo 123) #f (foo)
  59. (= foo 123))))
  60. (with-test-prefix/c&e "let-keywords*"
  61. ;; in guile 1.6.4 and earlier, an empty binding list only used `begin',
  62. ;; which caused apparently internal defines to "leak" out into the
  63. ;; encompasing environment
  64. (pass-if-exception "empty bindings internal defines leaking out"
  65. exception:unbound-var
  66. (let ((rest '()))
  67. (let-keywords* rest #f ()
  68. (define localvar #f)
  69. #f)
  70. localvar))
  71. (pass-if "one key"
  72. (let-keywords* '(#:foo 123) #f (foo)
  73. (= foo 123))))
  74. (with-test-prefix/c&e "let-optional"
  75. ;; in guile 1.6.4 and earlier, an empty binding list only used `begin',
  76. ;; which caused apparently internal defines to "leak" out into the
  77. ;; encompasing environment
  78. (pass-if-exception "empty bindings internal defines leaking out"
  79. exception:unbound-var
  80. (let ((rest '()))
  81. (let-optional rest ()
  82. (define localvar #f)
  83. #f)
  84. localvar))
  85. (pass-if "one var"
  86. (let ((rest '(123)))
  87. (let-optional rest ((foo 999))
  88. (= foo 123)))))
  89. (with-test-prefix/c&e "let-optional*"
  90. ;; in guile 1.6.4 and earlier, an empty binding list only used `begin',
  91. ;; which caused apparently internal defines to "leak" out into the
  92. ;; encompasing environment
  93. (pass-if-exception "empty bindings internal defines leaking out"
  94. exception:unbound-var
  95. (let ((rest '()))
  96. (let-optional* rest ()
  97. (define localvar #f)
  98. #f)
  99. localvar))
  100. (pass-if "one var"
  101. (let ((rest '(123)))
  102. (let-optional* rest ((foo 999))
  103. (= foo 123))))))
  104. (define* (foo a b #:optional c (d 1) (e c) f #:key g (h a) (i r) #:rest r)
  105. (list a b c d e f g h i r))
  106. ;; So we could use lots more tests here, but the fact that lambda* is in
  107. ;; the compiler, and the compiler compiles itself, using the evaluator
  108. ;; (when bootstrapping) and compiled code (when doing a partial rebuild)
  109. ;; makes me a bit complacent.
  110. (without-compiler-warnings
  111. (with-test-prefix/c&e "define*"
  112. (pass-if "the whole enchilada"
  113. (equal? (foo 1 2)
  114. '(1 2 #f 1 #f #f #f 1 () ())))
  115. (pass-if-exception "extraneous arguments"
  116. exception:extraneous-arguments
  117. (let ((f (lambda* (#:key x) x)))
  118. (f 1 2 #:x 'x)))
  119. (pass-if-equal "unrecognized keyword" '(#:y)
  120. (catch 'keyword-argument-error
  121. (lambda ()
  122. (let ((f (lambda* (#:key x) x)))
  123. (f #:y 'not-recognized)))
  124. (lambda (key proc fmt args data)
  125. data)))
  126. (pass-if-equal "missing argument" '("Keyword argument has no value" #:x)
  127. (catch 'keyword-argument-error
  128. (lambda ()
  129. (let ((f (lambda* (#:key x) x)))
  130. (f #:x)))
  131. (lambda (key proc fmt args data)
  132. (cons fmt data))))
  133. (pass-if-equal "invalid keyword" '(not-a-keyword)
  134. (catch 'keyword-argument-error
  135. (lambda ()
  136. (let ((f (lambda* (#:key x) x)))
  137. (f 'not-a-keyword 'something)))
  138. (lambda (key proc fmt args data)
  139. data)))
  140. (pass-if "rest given before keywords"
  141. ;; Passing the rest argument before the keyword arguments should not
  142. ;; prevent keyword argument binding.
  143. (let ((f (lambda* (#:key x y z #:rest r) (list x y z r))))
  144. (equal? (f 1 2 3 #:x 'x #:z 'z)
  145. '(x #f z (1 2 3 #:x x #:z z)))))))
  146. (with-test-prefix "scm_c_bind_keyword_arguments"
  147. (pass-if-equal "unrecognized keyword" '(#:y)
  148. (catch 'keyword-argument-error
  149. (lambda ()
  150. (open-file "/dev/null" "r" #:y 'not-recognized))
  151. (lambda (key proc fmt args data)
  152. data)))
  153. (pass-if-equal "missing argument"
  154. '("Keyword argument has no value" #:encoding)
  155. (catch 'keyword-argument-error
  156. (lambda ()
  157. (open-file "/dev/null" "r" #:encoding))
  158. (lambda (key proc fmt args data)
  159. (cons fmt data))))
  160. (pass-if-equal "invalid keyword" '(not-a-keyword)
  161. (catch 'keyword-argument-error
  162. (lambda ()
  163. (open-file "/dev/null" "r" 'not-a-keyword 'something))
  164. (lambda (key proc fmt args data)
  165. data))))
  166. (with-test-prefix/c&e "lambda* inits"
  167. (pass-if "can bind lexicals within inits"
  168. (begin
  169. (define qux
  170. (lambda* (#:optional a #:key (b (or a 13) #:a))
  171. b))
  172. #t))
  173. (pass-if "testing qux"
  174. (and (equal? (qux) 13)
  175. (equal? (qux 1) 1)
  176. (equal? (qux #:a 2) 2)))
  177. (pass-if "nested lambda* with optional"
  178. (begin
  179. (define (foo x)
  180. (define baz x)
  181. (define* (bar #:optional (y baz))
  182. (or (zero? y) (bar (1- y))))
  183. (bar))
  184. (foo 10)))
  185. (pass-if "nested lambda* with key"
  186. (begin
  187. (define (foo x)
  188. (define baz x)
  189. (define* (bar #:key (y baz))
  190. (or (zero? y) (bar #:y (1- y))))
  191. (bar))
  192. (foo 10))))
  193. (with-test-prefix/c&e "defmacro*"
  194. (pass-if "definition"
  195. (begin
  196. (defmacro* transmogrify (a #:optional (b 10))
  197. `(,a ,b))
  198. #t))
  199. (pass-if "explicit arg"
  200. (equal? (transmogrify quote 5)
  201. 5))
  202. (pass-if "default arg"
  203. (equal? (transmogrify quote)
  204. 10)))
  205. (without-compiler-warnings
  206. (with-test-prefix/c&e "case-lambda"
  207. (pass-if-exception "no clauses, no args" exception:wrong-num-args
  208. ((case-lambda)))
  209. (pass-if-exception "no clauses, args" exception:wrong-num-args
  210. ((case-lambda) 1))
  211. (pass-if "docstring"
  212. (equal? "docstring test"
  213. (procedure-documentation
  214. (case-lambda
  215. "docstring test"
  216. (() 0)
  217. ((x) 1)))))))
  218. (without-compiler-warnings
  219. (with-test-prefix/c&e "case-lambda*"
  220. (pass-if-exception "no clauses, no args" exception:wrong-num-args
  221. ((case-lambda*)))
  222. (pass-if-exception "no clauses, args" exception:wrong-num-args
  223. ((case-lambda*) 1))
  224. (pass-if "docstring"
  225. (equal? "docstring test"
  226. (procedure-documentation
  227. (case-lambda*
  228. "docstring test"
  229. (() 0)
  230. ((x) 1)))))
  231. (pass-if "unambiguous"
  232. ((case-lambda*
  233. ((a b) #t)
  234. ((a) #f))
  235. 1 2))
  236. (pass-if "unambiguous (reversed)"
  237. ((case-lambda*
  238. ((a) #f)
  239. ((a b) #t))
  240. 1 2))
  241. (pass-if "optionals (order disambiguates)"
  242. ((case-lambda*
  243. ((a #:optional b) #t)
  244. ((a b) #f))
  245. 1 2))
  246. (pass-if "optionals (order disambiguates (2))"
  247. ((case-lambda*
  248. ((a b) #t)
  249. ((a #:optional b) #f))
  250. 1 2))
  251. (pass-if "optionals (one arg)"
  252. ((case-lambda*
  253. ((a b) #f)
  254. ((a #:optional b) #t))
  255. 1))
  256. (pass-if "optionals (one arg (2))"
  257. ((case-lambda*
  258. ((a #:optional b) #t)
  259. ((a b) #f))
  260. 1))
  261. (pass-if "keywords without keyword"
  262. ((case-lambda*
  263. ((a #:key c) #t)
  264. ((a b) #f))
  265. 1))
  266. (pass-if "keywords with keyword"
  267. ((case-lambda*
  268. ((a #:key c) #t)
  269. ((a b) #f))
  270. 1 #:c 2))
  271. (pass-if "keywords (too many positionals)"
  272. ((case-lambda*
  273. ((a #:key c) #f)
  274. ((a b) #t))
  275. 1 2))
  276. (pass-if "keywords (order disambiguates)"
  277. ((case-lambda*
  278. ((a #:key c) #t)
  279. ((a b c) #f))
  280. 1 #:c 2))
  281. (pass-if "keywords (order disambiguates (2))"
  282. ((case-lambda*
  283. ((a b c) #t)
  284. ((a #:key c) #f))
  285. 1 #:c 2))))