Debian Codegraph @

test/parseopt.scm

raw
1 ;;
2 ;; test for parseopt
3 ;;
4
5 (use gauche.test)
6
7 (test-start "parseopt")
8 (use gauche.parseopt)
9 (test-module 'gauche.parseopt)
10
11 (define (help) (display "Help message"))
12
13 (define parser
14 (make-option-parser
15 (("help" => help)
16 ("a" () (format #t "a,"))
17 ("bb=s" (arg) (format #t "bb=~a," arg))
18 ("b=s" (arg) (format #t "b=~a," arg))
19 ("cc=ss" (arg1 arg2) (format #t "cc=(~a ~a)," arg1 arg2))
20 ("ddd=sss" args (format #t "ddd=~a," args))
21 ("eee=i" (arg) (format #t "eee=~s," arg))
22 ("ffff=si" (arg1 arg2) (format #t "ffff=(~a ~s)," arg1 arg2))
23 ("ggggg=fff" args (format #t "ggggg=~s," args))
24 ("h|hh|hhh" () (format #t "h*,"))
25 ("j:s" (arg) (format #t "j=~a," arg))
26 ("k:sss" args (format #t "k=~a," args))
27 (else (option args looper)
28 (format #t "?=~a," option)
29 (looper args))
30 )))
31
32 (define (tester . cmdargs)
33 (lambda ()
34 (let* ((restarg '())
35 (output (with-output-to-string
36 (lambda ()
37 (set! restarg (parser cmdargs))))))
38 (cons output restarg))))
39
40 (test "help" '("Help message")
41 (tester "-help"))
42
43 (test "help" '("Help message")
44 (tester "--help"))
45
46 (test "help" '("Help message" "x" "y")
47 (tester "-help" "x" "y"))
48
49 (test "-a" '("a," "x" "y") (tester "-a" "x" "y"))
50 (test "--a" '("" "x" "-a" "y") (tester "x" "-a" "y"))
51 (test "-bb" '("bb=x," "y") (tester "-bb" "x" "y"))
52 (test "--bb" '("bb=x," "y") (tester "--bb" "x" "y"))
53 (test "-bb" '("bb=x," "y") (tester "-bb=x" "y"))
54 (test "-bb" '("bb=x," "y") (tester "-bb=x" "y"))
55 (test "-a -bb" '("a,bb=x," "y") (tester "-a" "-bb" "x" "y"))
56 (test "-bb -a" '("bb=x,a," "y") (tester "-bb" "x" "-a" "y"))
57 (test "-bb -a" '("bb=-a," "x" "y") (tester "-bb" "-a" "x" "y"))
58
59 (test "-cc" '("bb=x,cc=(y z),") (tester "-bb" "x" "-cc" "y" "z"))
60 (test "-cc" '("cc=(y -bb)," "x" "z") (tester "-cc" "y" "-bb" "x" "z"))
61 (test "-cc" '("cc=(y -bb)," "x" "z") (tester "-cc=y" "-bb" "x" "z"))
62
63 (test "-ddd" '("ddd=(x y z),") (tester "-ddd" "x" "y" "z"))
64 (test "-ddd" '("ddd=(x y z),") (tester "--ddd=x" "y" "z"))
65
66 (test "-eee" '("eee=23," "x") (tester "-eee" "23" "x"))
67 (test "-eee" '("eee=23," "x") (tester "-eee" "023" "x"))
68 (test "-eee" '("eee=-23," "x") (tester "-eee" "-23" "x"))
69 (test "-eee" '("eee=23," "x") (tester "-eee" "#x17" "x"))
70 (test "-eee" '("eee=23," "x") (tester "-eee" "#o27" "x"))
71 (test "-eee" '("eee=23," "x") (tester "-eee" "#b10111" "x"))
72
73 (test "-ffff" '("ffff=(-a -3),") (tester "--ffff" "-a" "-3"))
74 (test "-ffff" '("ffff=(-a -3),") (tester "-ffff" "-a" "-03"))
75 (test "-ffff" '("ffff=(-03 -3),") (tester "--ffff" "-03" "-03"))
76
77 (test "-ggggg" '("ggggg=(1.0 2.0 3.0),") (tester "-ggggg" "1.0" "2.0" "3.0"))
78
79 (test "-h" '("h*,") (tester "-h"))
80 (test "-hh" '("h*,") (tester "-hh"))
81 (test "-hhh" '("h*,") (tester "-hhh"))
82
83 (test "-j" '("j=jj,a,") (tester "-j" "jj" "-a"))
84 (test "-j" '("j=#f,a,") (tester "-j" "-a"))
85 (test "-j" '("j=#f,") (tester "-j"))
86 (test "-k" '("k=(1 2 3),a,") (tester "-k" "1" "2" "3" "-a"))
87 (test "-k" '("k=(#f #f #f),a,") (tester "-k" "-a"))
88
89 (test "-b 1" '("b=abc,") (tester "-b" "abc"))
90 (test "-b 2" '("b=abc,") (tester "-b=abc"))
91 (test "-b 3" '("b=abc,") (tester "-babc"))
92 (test "-b (long option match)" '("bb=abc,") (tester "-bb" "abc"))
93
94 (test "--" '("bb=x," "-a" "-cc") (tester "-bb" "x" "--" "-a" "-cc"))
95 (test "--" '("" "-bb" "x" "--" "-a" "-cc") (tester "--" "-bb" "x" "--" "-a" "-cc"))
96
97 (test "else" '("bb=x,?=what," "x" "y") (tester "-bb" "x" "-what" "x" "y"))
98 (test "else" '("bb=x,?=what,eee=3," "x" "y")
99 (tester "-bb" "x" "-what" "-eee" "3" "x" "y"))
100 (test "else" '("bb=x,?=what," "q" "-eee" "3" "x" "y")
101 (tester "-bb" "x" "-what=q" "-eee" "3" "x" "y"))
102
103 (test* "let-args (foo)" 9
104 (let-args '() ((foo "foo=n" 9)) foo))
105
106 (test* "let-args (foo)" #f
107 (let-args '() ((foo "foo=n")) foo))
108
109 (test* "let-args (foo)" 3
110 (let-args '("--foo" "3") ((foo "foo=n" 9)) foo))
111
112 (test* "let-args (foo)" (test-error <parseopt-error>)
113 (let-args '("--foof" "3") ((foo "foo=n" 9)) foo))
114
115 (test* "let-args (foo)" 3
116 (let-args '("--foo" "3") ((bar "bar") (foo "foo=n" 9)) foo))
117
118 (test* "let-args (bar)" #t
119 (let-args '("--bar") ((bar "bar") (foo "foo=n" 9)) bar))
120
121 (test* "let-args (bar)" #t
122 (let-args '("--bar") ((foo "foo=n" 9) (bar "bar")) bar))
123
124 (test* "let-args (bar)" #f
125 (let-args '("--foo" "3") ((foo "foo=n" 9) (bar "bar")) bar))
126
127 (test* "let-args (baz)" '("4" 2)
128 (let-args '("--foo" "3" "--baz" "4" "2")
129 ((foo "foo=n" 9)
130 (bar "bar")
131 (baz "baz=si"))
132 baz))
133
134 (test* "let-args (baz)" (test-error <parseopt-error>)
135 (let-args '("--foo" "3" "--baz" "4")
136 ((foo "foo=n" 9)
137 (bar "bar")
138 (baz "baz=si"))
139 baz))
140
141 (test* "let-args (baz)" #f
142 (let-args '("--foo" "3" "--bar")
143 ((foo "foo=n" 9)
144 (bar "bar")
145 (baz "baz=si"))
146 baz))
147
148 (test* "let-args (rest)" '("bunga" "bonga")
149 (let-args '("--foo" "3" "--bar" "bunga" "bonga")
150 ((foo "foo=n" 9)
151 (bar "bar")
152 (baz "baz=si")
153 . rest)
154 rest))
155
156 (test* "let-args (rest)" '("bunga" "bonga")
157 (let-args '("bunga" "bonga")
158 ((foo "foo=n" 9)
159 (bar "bar")
160 (baz "baz=si")
161 . rest)
162 rest))
163
164 (test* "let-args (rest)" '()
165 (let-args '()
166 ((foo "foo=n" 9)
167 (bar "bar")
168 (baz "baz=si")
169 . rest)
170 rest))
171
172 (test* "let-args (else)" '("foo" ("5"))
173 (call/cc
174 (lambda (ret)
175 (let-args '("-foo" "5")
176 ((else (opt args cont) (ret (list opt args))))
177 #f))))
178
179 (test* "let-args (else)" 5
180 (call/cc
181 (lambda (ret)
182 (let-args '("-foo" "5")
183 ((bar "bar=i")
184 (else (opt args cont) (cont (cons "-bar" args))))
185 bar))))
186
187 (test* "let-args (callback)" 25
188 (let-args '("-foo" "5")
189 ((foo "foo=n" => (^x (* x x)))
190 (bar "bar"))
191 foo))
192
193 (test* "let-args (callback)" #f
194 (let-args '()
195 ((foo "foo=n" => (^x (* x x)))
196 (bar "bar"))
197 foo))
198
199 (test* "let-args (callback)" 8
200 (let-args '()
201 ((foo "foo=n" 8 => (^x (* x x)))
202 (bar "bar"))
203 foo))
204
205 (test* "let-args (side-effect)" 5
206 (let ((boo 0))
207 (let-args '("-foo" "5")
208 ((#f "foo=n" => (^x (set! boo x)))
209 (bar "bar"))
210 boo)))
211
212 (test* "let-args (side-effect)" 0
213 (let ((boo 0))
214 (let-args '("-bar")
215 ((#f "foo=n" => (^x (set! boo x)))
216 (#f "bar"))
217 boo)))
218
219 (test* "let-args (scope)" 7
220 (let ((foo 7))
221 (let-args '("-foo" "6")
222 ((foo "foo=n")
223 (bar "bar=n" foo))
224 bar)))
225
226 (test* "let-args (scope)" 8
227 (let ((x 8))
228 (let-args '("-foo")
229 ((x "x")
230 (foo "foo" => (lambda () x)))
231 foo)))
232
233 (test* "let-args (scope)" 9
234 (let ((x 9))
235 (call/cc
236 (lambda (ret)
237 (let-args '("-foo")
238 ((x "x")
239 (else _ (ret x)))
240 11)))))
241
242 (let ()
243 (define (run args)
244 (let-args args
245 ((a "a*")
246 (b "b*=s"))
247 (list a b)))
248
249 (test* "plural args" '(() ())
250 (run '()))
251 (test* "plural args" '((#t) ("foo"))
252 (run '("-a" "-b" "foo")))
253 (test* "plural args" '((#t #t) ("foo" "bar"))
254 (run '("-a" "-b" "foo" "-a" "-b" "bar")))
255 )
256
257 (let ()
258 (define (run args help-string-args)
259 (let/cc ret
260 (let-args args
261 ((a "a" ? "An option with no arg")
262 (b "b=s{PARAM_B}" ? "Taking string argument {PARAM_B}.")
263 (c "c=s{PARAM_C}" "wow" ? "Taking string argument and default. \
264 And this is a long help string that will span \
265 more than one line.")
266 (d "d" => (^[] 'yo) ? "Callback, no default")
267 (e "e" 'boo => (^[] 'yay) ? "Callback, default")
268 (f "f|floccinaucinihilipilification=s" ? "Long option name")
269 (g "g=i{PARAM_G1}i{PARAM_G2}" => (^[g1 g2] (list g1 g2))
270 ? "Taking two integer arguments, {PARAM_G1} and {PARAM_G2}.")
271 (h "h") ;no help string
272 (else => (^[badopt rest loop]
273 (ret (string-append "Unknown option: " badopt "\n"
274 "Options:\n"
275 (apply option-parser-help-string
276 help-string-args))))))
277 (list a b c d e f g))))
278
279 (define (t args expect)
280 (test* #"help string ~|args|" expect (run args '())))
281
282 ;; Just to make sure help string doesn't affect normal operations.
283 (t '() '(#f #f "wow" #f boo #f #f))
284 (t '("-a") '(#t #f "wow" #f boo #f #f))
285 (t '("-b" "bee") '(#f "bee" "wow" #f boo #f #f))
286 (t '("-c" "sea") '(#f #f "sea" #f boo #f #f))
287 (t '("-d") '(#f #f "wow" yo boo #f #f))
288 (t '("-e") '(#f #f "wow" #f yay #f #f))
289 (t '("-f" "foo") '(#f #f "wow" #f boo "foo" #f))
290
291 ;; And now, help string generation
292 ($ test*/diff "help string generation 1"
293 '("Unknown option: zzz"
294 "Options:"
295 " -a An option with no arg"
296 " -b PARAM_B Taking string argument PARAM_B."
297 " -c PARAM_C Taking string argument and default. And this is a long help"
298 " string that will span more than one line."
299 " -d Callback, no default"
300 " -e Callback, default"
301 " -f, --floccinaucinihilipilification s"
302 " Long option name"
303 " -g PARAM_G1 PARAM_G2"
304 " Taking two integer arguments, PARAM_G1 and PARAM_G2."
305 " -h (No help available)")
306 (run '("--zzz") '()))
307
308 ($ test*/diff "help string generation 2"
309 '("Unknown option: zzz"
310 "Options:"
311 " -a An option with no arg"
312 " -b PARAM_B Taking string argument PARAM_B."
313 " -c PARAM_C Taking string argument and default."
314 " And this is a long help string that"
315 " will span more than one line."
316 " -d Callback, no default"
317 " -e Callback, default"
318 " -f, --floccinaucinihilipilification s"
319 " Long option name"
320 " -g PARAM_G1 PARAM_G2 Taking two integer arguments,"
321 " PARAM_G1 and PARAM_G2.")
322 (run '("--zzz") '(:omit-options-without-help #t
323 :option-indent 4
324 :description-indent 25
325 :width 60)))
326 )
327
328 (test-end)