-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineva-test.lisp
152 lines (138 loc) · 5.44 KB
/
lineva-test.lisp
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
(in-package :cl-user)
(defpackage :lineva/test
(:use :common-lisp :fiveam))
(in-package :lineva/test)
(defmacro does-leva-expand-to (result (&rest leva-args))
`(is (equalp (macroexpand '(la:leva ,@leva-args)) ,result)))
(test empty-leva
(does-leva-expand-to '(progn) ()))
(test one-elt-leva
(does-leva-expand-to 1 (1))
(does-leva-expand-to '(- 1) ((- 1))))
(test many-trivial-elt-leva
(does-leva-expand-to '(progn 1 2 3) (1 2 3))
(does-leva-expand-to '(progn (print 1) 1.5 (print 2) 3)
((print 1) 1.5 (print 2) 3)))
(test fake-inst-leva
(does-leva-expand-to ''(:println "Hello world!")
('(:println "Hello world!")))
(does-leva-expand-to '(progn '(:println 1) '(:println 2))
('(:println 1) '(:println 2)))
(does-leva-expand-to '(values :break)
((values :break))))
(test simple-inst-leva
(does-leva-expand-to '(let ((x 10)) (progn))
((:let (x 10))))
(does-leva-expand-to '(let ((x 10) (y 20)) (+ x y))
((:let (x 10) (y 20)) (+ x y)))
(does-leva-expand-to '(progn (print 1) (print 2) (let ((x 10)) x))
((print 1) (print 2) (:let (x 10)) x))
(does-leva-expand-to '(progn (print 1) (print 2) (let ((x 10)) (progn)))
((print 1) (print 2) (:let (x 10))))
(does-leva-expand-to '(let ((x 10)) (progn (print 1) (print x)))
((:let (x 10)) (print 1) (print x))))
(test inst-local-variables
(is (eql 30 (la:leva (:let (x 10) (y 20)) (+ x y))))
(is (eql :error
(handler-case (la:leva
(:let-assert (x 10) (y 20) (z nil)) (+ x y))
(t () :error))))
(is (eql 60 (la:leva (:let-assert (x 10) (y 20) (z 30)) (+ x y z))))
(is (eql 22 (la:leva (:flet (add1 (x) (+ 1 x))
(dot2 (x) (* 2 x)))
(dot2 (add1 10)))))
(is (eql 8 (la:leva
(:labels (fib (n)
(if (< n 2)
1
(+ (fib (- n 1)) (fib (- n 2))))))
(fib 5))))
(is (equal '("Joe" 20 nil)
(la:leva
(:macrolet (record (&rest values) `(list ,@values)))
(record "Joe" 20 nil))))
(let ((output (with-output-to-string (s)
(is (equal '(nil nil nil)
(la:leva (:symbol-macrolet (x (format s "...~%")))
(list x x x)))))))
(is (string= (format nil "...~%...~%...~%") output)))
(is (eql 6 (la:leva
(:defun fac (n)
(if (zerop n)
1
(* n (fac (- n 1)))))
(fac 3))))
(is (eql 10 (la:leva
(:defvar x 10)
x)))
(is (equal '(1 2 (3 4 5))
(la:leva
(:bind (a b &rest c) '(1 2 3 4 5))
(list a b c))))
(is (string= "ALEXANDRIA"
(la:leva
(:defvar name :alexandria)
(:setf name (symbol-name name)
:if (not (stringp name)))
(string-upcase name)))))
(test inst-debug
(does-leva-expand-to '(progn (print 1) (progn (break) (print 2)))
((print 1) :break (print 2)))
(is (eql 10 (la:leva
(:defvar x 10)
(:assert (numberp x) (plusp x) (evenp x))
x)))
(is (eql :error
(handler-case (la:leva
(:defvar x 11)
(:assert (numberp x) (plusp x) (evenp x))
x)
(t () :error))))
(is (equal '("Joe" 20)
(la:leva
(:let (name "Joe") (age 20))
(:check-type (name (array character *) "a string")
(age (integer 0 150)))
(list name age))))
(is (eql :error
(handler-case (la:leva
(:let (name "Joe") (age 200))
(:check-type (name (array character *) "a string")
(age (integer 0 150)))
(list name age))
(t () :error)))))
(test inst-control-flow
(is (eql 10 (la:leva
(:defvar x -10)
(:return (- x) :if (minusp x))
(error "Bye!"))))
(is (string= "cn.bing.com"
(la:leva
(:defvar table
'(:bing "cn.bing.com"))
(:try (getf table :google)
(getf table :duckduckgo)
(getf table :bing))
"No search engine available.")))
(let ((output (with-output-to-string (s)
(is (eql :done
(la:leva
(:defun close-conn () (format s "Bye!~%"))
(format s "Hello!~%")
(:defer (close-conn) (terpri s))
(format s "[...]~%")
(values :done)))))))
(is (string= (format nil "Hello!~%[...]~%Bye!~%~%") output))))
(defmacro does-leva-output (content (&rest leva-args))
(let ((s (gensym)))
`(is (string= ,content
(with-output-to-string (,s)
(let ((*standard-output* ,s))
(la:leva ,@leva-args)))))))
(test inst-display
(does-leva-output (format nil "Hello ~S!~%" :world)
((:printf "Hello ~S!~%" :world)))
(does-leva-output (format nil "Hello world!~%")
((:println "Hello world!")))
(does-leva-output (format nil "~S~%" "Hello world!")
((:pn "Hello world!"))))