| 1 |
;;;; optargs.test --- test suite for optional arg processing -*- scheme -*- |
| 2 |
;;;; Matthias Koeppe <[email protected]> --- 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 |
|
| 20 |
(define-module (test-suite test-optargs) |
| 21 |
#:use-module (test-suite lib) |
| 22 |
#:use-module (system base compile) |
| 23 |
#:use-module (ice-9 optargs)) |
| 24 |
|
| 25 |
(define exception:invalid-keyword |
| 26 |
'(keyword-argument-error . "Invalid keyword")) |
| 27 |
|
| 28 |
(define exception:unrecognized-keyword |
| 29 |
'(keyword-argument-error . "Unrecognized keyword")) |
| 30 |
|
| 31 |
(define exception:extraneous-arguments |
| 32 |
;; Message depends on whether we use the interpreter or VM, and on the |
| 33 |
;; evenness of the number of extra arguments (!). |
| 34 |
;'(keyword-argument-error . ".*") |
| 35 |
'(#t . ".*")) |
| 36 |
|
| 37 |
(with-test-prefix/c&e "optional argument processing" |
| 38 |
(pass-if "local defines work with optional arguments" |
| 39 |
(eval '(begin |
| 40 |
(define* (test-1 #:optional (x 0)) |
| 41 |
(define d 1) ; local define |
| 42 |
#t) |
| 43 |
(false-if-exception (test-1))) |
| 44 |
(interaction-environment)))) |
| 45 |
|
| 46 |
;;; |
| 47 |
;;; let-keywords |
| 48 |
;;; |
| 49 |
|
| 50 |
(define-syntax-rule (without-compiler-warnings exp ...) |
| 51 |
(parameterize ((default-warning-level 0)) exp ...)) |
| 52 |
|
| 53 |
(without-compiler-warnings |
| 54 |
(with-test-prefix/c&e "let-keywords" |
| 55 |
;; in guile 1.6.4 and earlier, an empty binding list only used `begin', |
| 56 |
;; which caused apparently internal defines to "leak" out into the |
| 57 |
;; encompasing environment |
| 58 |
(pass-if-exception "empty bindings internal defines leaking out" |
| 59 |
exception:unbound-var |
| 60 |
(let ((rest '())) |
| 61 |
(let-keywords rest #f () |
| 62 |
(define localvar #f) |
| 63 |
#f) |
| 64 |
localvar)) |
| 65 |
|
| 66 |
(pass-if "one key" |
| 67 |
(let-keywords '(#:foo 123) #f (foo) |
| 68 |
(= foo 123)))) |
| 69 |
|
| 70 |
|
| 71 |
(with-test-prefix/c&e "let-keywords*" |
| 72 |
;; in guile 1.6.4 and earlier, an empty binding list only used `begin', |
| 73 |
;; which caused apparently internal defines to "leak" out into the |
| 74 |
;; encompasing environment |
| 75 |
(pass-if-exception "empty bindings internal defines leaking out" |
| 76 |
exception:unbound-var |
| 77 |
(let ((rest '())) |
| 78 |
(let-keywords* rest #f () |
| 79 |
(define localvar #f) |
| 80 |
#f) |
| 81 |
localvar)) |
| 82 |
|
| 83 |
(pass-if "one key" |
| 84 |
(let-keywords* '(#:foo 123) #f (foo) |
| 85 |
(= foo 123)))) |
| 86 |
|
| 87 |
(with-test-prefix/c&e "let-optional" |
| 88 |
;; in guile 1.6.4 and earlier, an empty binding list only used `begin', |
| 89 |
;; which caused apparently internal defines to "leak" out into the |
| 90 |
;; encompasing environment |
| 91 |
(pass-if-exception "empty bindings internal defines leaking out" |
| 92 |
exception:unbound-var |
| 93 |
(let ((rest '())) |
| 94 |
(let-optional rest () |
| 95 |
(define localvar #f) |
| 96 |
#f) |
| 97 |
localvar)) |
| 98 |
|
| 99 |
(pass-if "one var" |
| 100 |
(let ((rest '(123))) |
| 101 |
(let-optional rest ((foo 999)) |
| 102 |
(= foo 123))))) |
| 103 |
|
| 104 |
(with-test-prefix/c&e "let-optional*" |
| 105 |
;; in guile 1.6.4 and earlier, an empty binding list only used `begin', |
| 106 |
;; which caused apparently internal defines to "leak" out into the |
| 107 |
;; encompasing environment |
| 108 |
(pass-if-exception "empty bindings internal defines leaking out" |
| 109 |
exception:unbound-var |
| 110 |
(let ((rest '())) |
| 111 |
(let-optional* rest () |
| 112 |
(define localvar #f) |
| 113 |
#f) |
| 114 |
localvar)) |
| 115 |
|
| 116 |
(pass-if "one var" |
| 117 |
(let ((rest '(123))) |
| 118 |
(let-optional* rest ((foo 999)) |
| 119 |
(= foo 123)))))) |
| 120 |
|
| 121 |
(define* (foo a b #:optional c (d 1) (e c) f #:key g (h a) (i r) #:rest r) |
| 122 |
(list a b c d e f g h i r)) |
| 123 |
|
| 124 |
;; So we could use lots more tests here, but the fact that lambda* is in |
| 125 |
;; the compiler, and the compiler compiles itself, using the evaluator |
| 126 |
;; (when bootstrapping) and compiled code (when doing a partial rebuild) |
| 127 |
;; makes me a bit complacent. |
| 128 |
(without-compiler-warnings |
| 129 |
(with-test-prefix/c&e "define*" |
| 130 |
(pass-if "the whole enchilada" |
| 131 |
(equal? (foo 1 2) |
| 132 |
'(1 2 #f 1 #f #f #f 1 () ()))) |
| 133 |
|
| 134 |
(pass-if-exception "extraneous arguments" |
| 135 |
exception:extraneous-arguments |
| 136 |
(let ((f (lambda* (#:key x) x))) |
| 137 |
(f 1 2 #:x 'x))) |
| 138 |
|
| 139 |
(pass-if-equal "unrecognized keyword" '(#:y) |
| 140 |
(catch 'keyword-argument-error |
| 141 |
(lambda () |
| 142 |
(let ((f (lambda* (#:key x) x))) |
| 143 |
(f #:y 'not-recognized))) |
| 144 |
(lambda (key proc fmt args data) |
| 145 |
data))) |
| 146 |
|
| 147 |
(pass-if-equal "missing argument" '("Keyword argument has no value" #:x) |
| 148 |
(catch 'keyword-argument-error |
| 149 |
(lambda () |
| 150 |
(let ((f (lambda* (#:key x) x))) |
| 151 |
(f #:x))) |
| 152 |
(lambda (key proc fmt args data) |
| 153 |
(cons fmt data)))) |
| 154 |
|
| 155 |
(pass-if-equal "invalid keyword" '(not-a-keyword) |
| 156 |
(catch 'keyword-argument-error |
| 157 |
(lambda () |
| 158 |
(let ((f (lambda* (#:key x) x))) |
| 159 |
(f 'not-a-keyword 'something))) |
| 160 |
(lambda (key proc fmt args data) |
| 161 |
data))) |
| 162 |
|
| 163 |
(pass-if "rest given before keywords" |
| 164 |
;; Passing the rest argument before the keyword arguments should not |
| 165 |
;; prevent keyword argument binding. |
| 166 |
(let ((f (lambda* (#:key x y z #:rest r) (list x y z r)))) |
| 167 |
(equal? (f 1 2 3 #:x 'x #:z 'z) |
| 168 |
'(x #f z (1 2 3 #:x x #:z z))))))) |
| 169 |
|
| 170 |
(with-test-prefix "scm_c_bind_keyword_arguments" |
| 171 |
|
| 172 |
(pass-if-equal "unrecognized keyword" '(#:y) |
| 173 |
(catch 'keyword-argument-error |
| 174 |
(lambda () |
| 175 |
(open-file "/dev/null" "r" #:y 'not-recognized)) |
| 176 |
(lambda (key proc fmt args data) |
| 177 |
data))) |
| 178 |
|
| 179 |
(pass-if-equal "missing argument" |
| 180 |
'("Keyword argument has no value" #:encoding) |
| 181 |
(catch 'keyword-argument-error |
| 182 |
(lambda () |
| 183 |
(open-file "/dev/null" "r" #:encoding)) |
| 184 |
(lambda (key proc fmt args data) |
| 185 |
(cons fmt data)))) |
| 186 |
|
| 187 |
(pass-if-equal "invalid keyword" '(not-a-keyword) |
| 188 |
(catch 'keyword-argument-error |
| 189 |
(lambda () |
| 190 |
(open-file "/dev/null" "r" 'not-a-keyword 'something)) |
| 191 |
(lambda (key proc fmt args data) |
| 192 |
data)))) |
| 193 |
|
| 194 |
(with-test-prefix/c&e "lambda* inits" |
| 195 |
(pass-if "can bind lexicals within inits" |
| 196 |
(begin |
| 197 |
(define qux |
| 198 |
(lambda* (#:optional a #:key (b (or a 13) #:a)) |
| 199 |
b)) |
| 200 |
#t)) |
| 201 |
(pass-if "testing qux" |
| 202 |
(and (equal? (qux) 13) |
| 203 |
(equal? (qux 1) 1) |
| 204 |
(equal? (qux #:a 2) 2))) |
| 205 |
(pass-if "nested lambda* with optional" |
| 206 |
(begin |
| 207 |
(define (foo x) |
| 208 |
(define baz x) |
| 209 |
(define* (bar #:optional (y baz)) |
| 210 |
(or (zero? y) (bar (1- y)))) |
| 211 |
(bar)) |
| 212 |
(foo 10))) |
| 213 |
(pass-if "nested lambda* with key" |
| 214 |
(begin |
| 215 |
(define (foo x) |
| 216 |
(define baz x) |
| 217 |
(define* (bar #:key (y baz)) |
| 218 |
(or (zero? y) (bar #:y (1- y)))) |
| 219 |
(bar)) |
| 220 |
(foo 10)))) |
| 221 |
|
| 222 |
|
| 223 |
(with-test-prefix/c&e "defmacro*" |
| 224 |
(pass-if "definition" |
| 225 |
(begin |
| 226 |
(defmacro* transmogrify (a #:optional (b 10)) |
| 227 |
`(,a ,b)) |
| 228 |
#t)) |
| 229 |
|
| 230 |
(pass-if "explicit arg" |
| 231 |
(equal? (transmogrify quote 5) |
| 232 |
5)) |
| 233 |
|
| 234 |
(pass-if "default arg" |
| 235 |
(equal? (transmogrify quote) |
| 236 |
10))) |
| 237 |
|
| 238 |
(without-compiler-warnings |
| 239 |
(with-test-prefix/c&e "case-lambda" |
| 240 |
(pass-if-exception "no clauses, no args" exception:wrong-num-args |
| 241 |
((case-lambda))) |
| 242 |
|
| 243 |
(pass-if-exception "no clauses, args" exception:wrong-num-args |
| 244 |
((case-lambda) 1)) |
| 245 |
|
| 246 |
(pass-if "docstring" |
| 247 |
(equal? "docstring test" |
| 248 |
(procedure-documentation |
| 249 |
(case-lambda |
| 250 |
"docstring test" |
| 251 |
(() 0) |
| 252 |
((x) 1))))))) |
| 253 |
|
| 254 |
(without-compiler-warnings |
| 255 |
(with-test-prefix/c&e "case-lambda*" |
| 256 |
(pass-if-exception "no clauses, no args" exception:wrong-num-args |
| 257 |
((case-lambda*))) |
| 258 |
|
| 259 |
(pass-if-exception "no clauses, args" exception:wrong-num-args |
| 260 |
((case-lambda*) 1)) |
| 261 |
|
| 262 |
(pass-if "docstring" |
| 263 |
(equal? "docstring test" |
| 264 |
(procedure-documentation |
| 265 |
(case-lambda* |
| 266 |
"docstring test" |
| 267 |
(() 0) |
| 268 |
((x) 1))))) |
| 269 |
|
| 270 |
(pass-if "unambiguous" |
| 271 |
((case-lambda* |
| 272 |
((a b) #t) |
| 273 |
((a) #f)) |
| 274 |
1 2)) |
| 275 |
|
| 276 |
(pass-if "unambiguous (reversed)" |
| 277 |
((case-lambda* |
| 278 |
((a) #f) |
| 279 |
((a b) #t)) |
| 280 |
1 2)) |
| 281 |
|
| 282 |
(pass-if "optionals (order disambiguates)" |
| 283 |
((case-lambda* |
| 284 |
((a #:optional b) #t) |
| 285 |
((a b) #f)) |
| 286 |
1 2)) |
| 287 |
|
| 288 |
(pass-if "optionals (order disambiguates (2))" |
| 289 |
((case-lambda* |
| 290 |
((a b) #t) |
| 291 |
((a #:optional b) #f)) |
| 292 |
1 2)) |
| 293 |
|
| 294 |
(pass-if "optionals (one arg)" |
| 295 |
((case-lambda* |
| 296 |
((a b) #f) |
| 297 |
((a #:optional b) #t)) |
| 298 |
1)) |
| 299 |
|
| 300 |
(pass-if "optionals (one arg (2))" |
| 301 |
((case-lambda* |
| 302 |
((a #:optional b) #t) |
| 303 |
((a b) #f)) |
| 304 |
1)) |
| 305 |
|
| 306 |
(pass-if "keywords without keyword" |
| 307 |
((case-lambda* |
| 308 |
((a #:key c) #t) |
| 309 |
((a b) #f)) |
| 310 |
1)) |
| 311 |
|
| 312 |
(pass-if "keywords with keyword" |
| 313 |
((case-lambda* |
| 314 |
((a #:key c) #t) |
| 315 |
((a b) #f)) |
| 316 |
1 #:c 2)) |
| 317 |
|
| 318 |
(pass-if "keywords (too many positionals)" |
| 319 |
((case-lambda* |
| 320 |
((a #:key c) #f) |
| 321 |
((a b) #t)) |
| 322 |
1 2)) |
| 323 |
|
| 324 |
(pass-if "keywords (order disambiguates)" |
| 325 |
((case-lambda* |
| 326 |
((a #:key c) #t) |
| 327 |
((a b c) #f)) |
| 328 |
1 #:c 2)) |
| 329 |
|
| 330 |
(pass-if "keywords (order disambiguates (2))" |
| 331 |
((case-lambda* |
| 332 |
((a b c) #t) |
| 333 |
((a #:key c) #f)) |
| 334 |
1 #:c 2)))) |