summaryrefslogtreecommitdiff
path: root/src/geiser/test.ss
blob: 24074483fb3fb66861320c8e46b5b728105c1116 (plain)
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
(import (geiser)
        (chezscheme))

(define-syntax assert-equal
  (syntax-rules ()
    ((_ a b)
     (if (equal? a b)
         #t
         (begin
           (display (format "failed assertion `~a' == `~a'" a b))
           (assert (equal? a b)))))))

(define-syntax get-result
  (syntax-rules ()
    ((_ form)
     (with-output-to-string
       (lambda ()
         (geiser:eval #f form))))))

(define-syntax do-test
  (syntax-rules ()
    ((_ form result)
     (assert
      (equal?
       (get-result form)
       result)))))

(define-syntax do-test-macroexpand
  (syntax-rules ()
    ((_ form result)
     (assert
      (equal? (geiser:macroexpand form)
              result)))))

(define-syntax test-or
  (syntax-rules ()
    ((_ x) x)
    ((_ x xs ...)
     (if x
         x
         (test-or xs ...)))))

(do-test-macroexpand
 '(test-or 1)
 '1)

(do-test-macroexpand
 '(test-or 1 2)
 '(if 1 1 2))

;; (something-doesnot-exist)
;;=> Error: Exception: variable something-doesnot-exist is not bound
(do-test
 '(something-doesnot-exist)
 "((result \"\") (output . \"\") (error (key . \"Exception: variable something-doesnot-exist is not bound\")))\n"
 )

;; (make-violation)
;;=> #<condition &violation>
(do-test
 '(make-violation)
 "((result \"#<condition &violation>\\n\") (output . \"\"))\n")

;; (values 1 2 3)
;;==> (1 2 3)
(do-test
 '(values 1 2 3)
 "((result \"(1 2 3)\\n\") (output . \"\"))\n")

;; 1
;;=> 1
(do-test '1 "((result \"1\\n\") (output . \"\"))\n")


;; '(case-lambda
;;    [(x1 x2) (+ x1 x2)]
;;    [(x1 x2 x3) (+ (+ x1 x2) x3)]
;;    [(x1 x2 . rest)
;;     ((letrec ([loop (lambda (x1 x2 rest)
;;                    (let ([x (+ x1 x2)])
;;                      (if (null? rest)
;;                          x
;;                          (loop x (car rest) (cdr rest)))))])
;;        loop)
;;      x1
;;      x2
;;      rest)]
;;    [(x1) (+ x1)]
;;    [() (+)])
#|=> (case-lambda
  [(x1 x2) (+ x1 x2)]
  [(x1 x2 x3) (+ (+ x1 x2) x3)]
  [(x1 x2 . rest)
   ((letrec ([loop (lambda (x1 x2 rest)
                     (let ([x (+ x1 x2)])
                       (if (null? rest)
                           x
                           (loop x (car rest) (cdr rest)))))])
      loop)
     x1
     x2
     rest)]
  [(x1) (+ x1)]
  [() (+)])
  |#
(do-test (quote '(case-lambda
  [(x1 x2) (+ x1 x2)]
  [(x1 x2 x3) (+ (+ x1 x2) x3)]
  [(x1 x2 . rest)
   ((letrec ([loop (lambda (x1 x2 rest)
                     (let ([x (+ x1 x2)])
                       (if (null? rest)
                           x
                           (loop x (car rest) (cdr rest)))))])
      loop)
     x1
     x2
     rest)]
  [(x1) (+ x1)]
  [() (+)])) "((result \"(case-lambda\\n  [(x1 x2) (+ x1 x2)]\\n  [(x1 x2 x3) (+ (+ x1 x2) x3)]\\n  [(x1 x2 . rest)\\n   ((letrec ([loop (lambda (x1 x2 rest)\\n                     (let ([x (+ x1 x2)])\\n                       (if (null? rest)\\n                           x\\n                           (loop x (car rest) (cdr rest)))))])\\n      loop)\\n     x1\\n     x2\\n     rest)]\\n  [(x1) (+ x1)]\\n  [() (+)])\\n\") (output . \"\"))\n")