-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-source-util-test.scm
215 lines (190 loc) · 4.83 KB
/
cj-source-util-test.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;;; Copyright 2010-2019 by Christian Jaeger <[email protected]>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require
;;cj-source-util
(cj-source-quasiquote quasiquote-source)
test
(test-lib-1 %try))
(TEST
> (define c (schemedefinition-arity-checker '(a b c . d)))
> (c 2)
not-enough
> (c 3)
ok
> (c 4)
ok
> (c 500)
ok
> (define c (schemedefinition-arity-checker '(a b c #!rest d)))
> (c 2)
not-enough
> (c 3)
ok
> (c 4)
ok
> (c 500)
ok
> (define c (schemedefinition-arity-checker (quasiquote-source
(a b c #!optional d))))
> (c 2)
not-enough
> (c 3)
ok
> (c 4)
ok
> (c 5)
too-many
> (define c (schemedefinition-arity-checker '(a b c #!key d)))
> (c 2)
not-enough
> (c 3)
ok
> (c 4)
ok ;; uhm, actually invalid!
> (c 5)
ok
> (c 6)
too-many
> (define c (schemedefinition-arity-checker (quasiquote-source
(a b c #!rest d))))
> (c 2)
not-enough
> (c 3)
ok
> (c 4)
ok
> (c 5)
ok
;; Mixing #!rest and #!key
;; (A)
> (def (t a #!rest r #!key b) (vector a rest: r key: b))
> (t 10)
[10 rest: () key: #f]
> (t 10 20)
[10 rest: (20) key: #f]
> (t 10 20 30)
[10 rest: (20 30) key: #f]
> (t 10 20 30 b: 3)
[10 rest: (20 30 b: 3) key: #f]
> (t 10 b: 3 20 30)
[10 rest: (b: 3 20 30) key: 3]
;; (B)
> (def (t a #!key b #!rest r) (vector a rest: r key: b))
> (t 10)
[10 rest: () key: #f]
> (t 10 20)
[10 rest: (20) key: #f]
> (t 10 20 30)
[10 rest: (20 30) key: #f]
> (t 10 20 30 b: 3)
[10 rest: (20 30 b: 3) key: #f]
> (t 10 b: 3 20 30)
[10 rest: (20 30) key: 3] ;; this is the only change to (A)
> (schemedefinition-arity:pattern->template '(#!rest r #!key b))
[at-least 0]
> (schemedefinition-arity:pattern->template '(#!key b #!rest r ))
[at-least 0]
> (schemedefinition-arity:pattern->template '(a #!key b #!rest r ))
[at-least 1]
> (schemedefinition-arity:pattern->template '(a #!rest r #!key b))
[at-least 1]
> (define c (schemedefinition-arity-checker '(a b c #!key d #!rest e)))
> (c 2)
not-enough
> (c 3)
ok
> (c 4)
ok ;; uhm, actually invalid!
> (c 5)
ok
> (c 6)
ok)
(TEST
> (define c (schemedefinition-arity:pattern->template '(a b c #!optional d)))
> (define f (lambda (a b c #!optional d) d))
> (safer-apply c f '(10 20 30 40) error values)
40
> (safer-apply c f '(10 20 30 40 50) vector values)
#("too many arguments")
> (safer-apply c f '(10 20 30) error values)
#f
;; compare with Gambits apply:
;; > (apply inc '(1 2 3))
;; *** ERROR IN (console)@64.1 -- Wrong number of arguments passed to procedure
;; (inc 1 2 3)
;; 1>
;; > (apply inc '())
;; *** ERROR IN (console)@65.1 -- Wrong number of arguments passed to procedure
;; (inc)
;; 1>
;; > (apply inc '(1))
;; 2
)
(TEST
;; combine optional with key
> (schemedefinition-arity:pattern->template '(#!key))
[up-to 0 0]
> (schemedefinition-arity:pattern->template '(#!key a))
[up-to 0 2]
> (schemedefinition-arity:pattern->template '(a #!key b))
[up-to 1 3]
> (schemedefinition-arity:pattern->template '(a #!key b c))
[up-to 1 5]
> (schemedefinition-arity:pattern->template '(#!optional a))
[up-to 0 1]
> (schemedefinition-arity:pattern->template '(#!optional a #!key b))
[up-to 0 3]
;; ^XX Idea: should provide a vector of validities perhaps. a number
;; (bits)? But then, checking really depends on whether keywords are
;; given--or, does it not??*!*
> (schemedefinition-arity:pattern->template '(#!optional a #!key b c))
[up-to 0 5]
> (schemedefinition-arity:pattern->template '(o #!optional a #!key b c))
[up-to 1 6]
> (define (t a b c #!optional d #!key e f)
(vector a b c optional: d key: e f))
> (define c (schemedefinition-arity-checker '(a b c #!optional d #!key e f)))
> (c 2)
not-enough
> (t 1 2 3)
[1 2 3 optional: #f key: #f #f]
> (c 3)
ok
> (t 1 2 3 4)
[1 2 3 optional: 4 key: #f #f]
> (c 4)
ok
> (%try (t 1 2 3 4 5))
(exception
text:
"Wrong number of arguments passed to procedure\n(t 1 2 3 4 5)\n")
> (%try (t 1 2 3 e: 5))
(exception
text:
"Wrong number of arguments passed to procedure\n(t 1 2 3 e: 5)\n")
> (c 5)
ok ;; actually wrong
> (t 1 2 3 4 e: 6)
[1 2 3 optional: 4 key: 6 #f]
> (c 6)
ok
> (c 7)
ok ;; actually wrong
> (t 1 2 3 4 e: 6 f: 8)
[1 2 3 optional: 4 key: 6 8]
> (c 8)
ok
> (c 9)
too-many)
(TEST
;; The other way around isn't actually valid:
;; > (define (t a b c #!key d #!optional e)
;; (vector a b c key: d optional: e))
;; Ill-placed #!optional
> (%try (schemedefinition-arity-checker '(a b c #!key d #!optional e)))
(exception
text:
"#!optional after #!key in argument list: (a b c #!key d #!optional e)\n"))