1(define-library (chibi app-test)
2  (import (scheme base) (chibi app) (chibi config) (chibi test))
3  (export run-tests)
4  (begin
5    (define (feed cfg spec . args)
6      (let ((animals (conf-get-list cfg 'animals '())))
7        (cons (if (conf-get cfg 'lions) (cons 'lions animals) animals) args)))
8    (define (wash cfg spec . args)
9      (let ((animals (conf-get-list cfg 'animals '())))
10        (cons (cons 'soap (conf-get cfg '(command wash soap))) animals)))
11    (define zoo-app-spec
12      `(zoo
13        "Zookeeper Application"
14        (@
15         (animals (list symbol) "list of animals to act on (default all)")
16         (lions boolean (#\l) "also apply the action to lions"))
17        (or
18         (feed "feed the animals" (,feed animals ...))
19         (wash "wash the animals" (@ (soap boolean)) (,wash animals ...))
20         (help "print help" (,app-help-command)))
21        ))
22    (define (run-tests)
23      (test-begin "app")
24      (test '((camel elephant) "today")
25          (run-application
26           zoo-app-spec
27           '("zoo" "--animals" "camel,elephant" "feed" "today")))
28      (test '((lions camel elephant) "tomorrow")
29          (run-application
30           zoo-app-spec
31           '("zoo" "--animals" "camel,elephant" "--lions" "feed" "tomorrow")))
32      (test '((soap . #f) rhino)
33          (run-application zoo-app-spec '("zoo" "--animals" "rhino" "wash")))
34      (test '((soap . #t) rhino)
35          (run-application zoo-app-spec
36                           '("zoo" "--animals" "rhino" "wash" "--soap")))
37      (test '((soap . #t) rhino)
38          (run-application zoo-app-spec
39                           '("zoo" "wash" "--soap" "--animals" "rhino")))
40      (test 'error
41          (guard (exn (else 'error))
42            (run-application zoo-app-spec
43                           '("zoo" "--soap" "wash" "--animals" "rhino"))))
44      (test-end))))
45