optargs.test 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. (with-test-prefix/c&e "let-keywords"
  44. ;; in guile 1.6.4 and earlier, an empty binding list only used `begin',
  45. ;; which caused apparently internal defines to "leak" out into the
  46. ;; encompasing environment
  47. (pass-if-exception "empty bindings internal defines leaking out"
  48. exception:unbound-var
  49. (let ((rest '()))
  50. (let-keywords rest #f ()
  51. (define localvar #f)
  52. #f)
  53. localvar))
  54. (pass-if "one key"
  55. (let-keywords '(#:foo 123) #f (foo)
  56. (= foo 123))))
  57. ;;;
  58. ;;; let-keywords*
  59. ;;;
  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. ;;;
  75. ;;; let-optional
  76. ;;;
  77. (with-test-prefix/c&e "let-optional"
  78. ;; in guile 1.6.4 and earlier, an empty binding list only used `begin',
  79. ;; which caused apparently internal defines to "leak" out into the
  80. ;; encompasing environment
  81. (pass-if-exception "empty bindings internal defines leaking out"
  82. exception:unbound-var
  83. (let ((rest '()))
  84. (let-optional rest ()
  85. (define localvar #f)
  86. #f)
  87. localvar))
  88. (pass-if "one var"
  89. (let ((rest '(123)))
  90. (let-optional rest ((foo 999))
  91. (= foo 123)))))
  92. ;;;
  93. ;;; let-optional*
  94. ;;;
  95. (with-test-prefix/c&e "let-optional*"
  96. ;; in guile 1.6.4 and earlier, an empty binding list only used `begin',
  97. ;; which caused apparently internal defines to "leak" out into the
  98. ;; encompasing environment
  99. (pass-if-exception "empty bindings internal defines leaking out"
  100. exception:unbound-var
  101. (let ((rest '()))
  102. (let-optional* rest ()
  103. (define localvar #f)
  104. #f)
  105. localvar))
  106. (pass-if "one var"
  107. (let ((rest '(123)))
  108. (let-optional* rest ((foo 999))
  109. (= foo 123)))))
  110. (define* (foo a b #:optional c (d 1) (e c) f #:key g (h a) (i r) #:rest r)
  111. (list a b c d e f g h i r))
  112. ;; So we could use lots more tests here, but the fact that lambda* is in
  113. ;; the compiler, and the compiler compiles itself, using the evaluator
  114. ;; (when bootstrapping) and compiled code (when doing a partial rebuild)
  115. ;; makes me a bit complacent.
  116. (with-test-prefix/c&e "define*"
  117. (pass-if "the whole enchilada"
  118. (equal? (foo 1 2)
  119. '(1 2 #f 1 #f #f #f 1 () ())))
  120. (pass-if-exception "extraneous arguments"
  121. exception:extraneous-arguments
  122. (let ((f (lambda* (#:key x) x)))
  123. (f 1 2 #:x 'x)))
  124. (pass-if-equal "unrecognized keyword" '(#:y)
  125. (catch 'keyword-argument-error
  126. (lambda ()
  127. (let ((f (lambda* (#:key x) x)))
  128. (f #:y 'not-recognized)))
  129. (lambda (key proc fmt args data)
  130. data)))
  131. (pass-if-equal "missing argument" '("Keyword argument has no value" #:x)
  132. (catch 'keyword-argument-error
  133. (lambda ()
  134. (let ((f (lambda* (#:key x) x)))
  135. (f #:x)))
  136. (lambda (key proc fmt args data)
  137. (cons fmt data))))
  138. (pass-if-equal "invalid keyword" '(not-a-keyword)
  139. (catch 'keyword-argument-error
  140. (lambda ()
  141. (let ((f (lambda* (#:key x) x)))
  142. (f 'not-a-keyword 'something)))
  143. (lambda (key proc fmt args data)
  144. data)))
  145. (pass-if "rest given before keywords"
  146. ;; Passing the rest argument before the keyword arguments should not
  147. ;; prevent keyword argument binding.
  148. (let ((f (lambda* (#:key x y z #:rest r) (list x y z r))))
  149. (equal? (f 1 2 3 #:x 'x #:z 'z)
  150. '(x #f z (1 2 3 #:x x #:z z))))))
  151. (with-test-prefix "scm_c_bind_keyword_arguments"
  152. (pass-if-equal "unrecognized keyword" '(#:y)
  153. (catch 'keyword-argument-error
  154. (lambda ()
  155. (open-file "/dev/null" "r" #:y 'not-recognized))
  156. (lambda (key proc fmt args data)
  157. data)))
  158. (pass-if-equal "missing argument"
  159. '("Keyword argument has no value" #:encoding)
  160. (catch 'keyword-argument-error
  161. (lambda ()
  162. (open-file "/dev/null" "r" #:encoding))
  163. (lambda (key proc fmt args data)
  164. (cons fmt data))))
  165. (pass-if-equal "invalid keyword" '(not-a-keyword)
  166. (catch 'keyword-argument-error
  167. (lambda ()
  168. (open-file "/dev/null" "r" 'not-a-keyword 'something))
  169. (lambda (key proc fmt args data)
  170. data))))
  171. (with-test-prefix/c&e "lambda* inits"
  172. (pass-if "can bind lexicals within inits"
  173. (begin
  174. (define qux
  175. (lambda* (#:optional a #:key (b (or a 13) #:a))
  176. b))
  177. #t))
  178. (pass-if "testing qux"
  179. (and (equal? (qux) 13)
  180. (equal? (qux 1) 1)
  181. (equal? (qux #:a 2) 2)))
  182. (pass-if "nested lambda* with optional"
  183. (begin
  184. (define (foo x)
  185. (define baz x)
  186. (define* (bar #:optional (y baz))
  187. (or (zero? y) (bar (1- y))))
  188. (bar))
  189. (foo 10)))
  190. (pass-if "nested lambda* with key"
  191. (begin
  192. (define (foo x)
  193. (define baz x)
  194. (define* (bar #:key (y baz))
  195. (or (zero? y) (bar #:y (1- y))))
  196. (bar))
  197. (foo 10))))
  198. (with-test-prefix/c&e "defmacro*"
  199. (pass-if "definition"
  200. (begin
  201. (defmacro* transmogrify (a #:optional (b 10))
  202. `(,a ,b))
  203. #t))
  204. (pass-if "explicit arg"
  205. (equal? (transmogrify quote 5)
  206. 5))
  207. (pass-if "default arg"
  208. (equal? (transmogrify quote)
  209. 10)))
  210. (with-test-prefix/c&e "case-lambda"
  211. (pass-if-exception "no clauses, no args" exception:wrong-num-args
  212. ((case-lambda)))
  213. (pass-if-exception "no clauses, args" exception:wrong-num-args
  214. ((case-lambda) 1))
  215. (pass-if "docstring"
  216. (equal? "docstring test"
  217. (procedure-documentation
  218. (case-lambda
  219. "docstring test"
  220. (() 0)
  221. ((x) 1))))))
  222. (with-test-prefix/c&e "case-lambda*"
  223. (pass-if-exception "no clauses, no args" exception:wrong-num-args
  224. ((case-lambda*)))
  225. (pass-if-exception "no clauses, args" exception:wrong-num-args
  226. ((case-lambda*) 1))
  227. (pass-if "docstring"
  228. (equal? "docstring test"
  229. (procedure-documentation
  230. (case-lambda*
  231. "docstring test"
  232. (() 0)
  233. ((x) 1)))))
  234. (pass-if "unambiguous"
  235. ((case-lambda*
  236. ((a b) #t)
  237. ((a) #f))
  238. 1 2))
  239. (pass-if "unambiguous (reversed)"
  240. ((case-lambda*
  241. ((a) #f)
  242. ((a b) #t))
  243. 1 2))
  244. (pass-if "optionals (order disambiguates)"
  245. ((case-lambda*
  246. ((a #:optional b) #t)
  247. ((a b) #f))
  248. 1 2))
  249. (pass-if "optionals (order disambiguates (2))"
  250. ((case-lambda*
  251. ((a b) #t)
  252. ((a #:optional b) #f))
  253. 1 2))
  254. (pass-if "optionals (one arg)"
  255. ((case-lambda*
  256. ((a b) #f)
  257. ((a #:optional b) #t))
  258. 1))
  259. (pass-if "optionals (one arg (2))"
  260. ((case-lambda*
  261. ((a #:optional b) #t)
  262. ((a b) #f))
  263. 1))
  264. (pass-if "keywords without keyword"
  265. ((case-lambda*
  266. ((a #:key c) #t)
  267. ((a b) #f))
  268. 1))
  269. (pass-if "keywords with keyword"
  270. ((case-lambda*
  271. ((a #:key c) #t)
  272. ((a b) #f))
  273. 1 #:c 2))
  274. (pass-if "keywords (too many positionals)"
  275. ((case-lambda*
  276. ((a #:key c) #f)
  277. ((a b) #t))
  278. 1 2))
  279. (pass-if "keywords (order disambiguates)"
  280. ((case-lambda*
  281. ((a #:key c) #t)
  282. ((a b c) #f))
  283. 1 #:c 2))
  284. (pass-if "keywords (order disambiguates (2))"
  285. ((case-lambda*
  286. ((a b c) #t)
  287. ((a #:key c) #f))
  288. 1 #:c 2)))