about summary refs log tree commit diff
path: root/tests.lisp
blob: 6c8ab08cc1affb6bee35b0c15a18cfd020c2664d (plain) (blame)
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
(in-package :cl-user)

(defpackage :quasiquote-2.0-tests
  (:use :cl :quasiquote-2.0 :fiveam)
  (:export #:run-tests))

(in-package :quasiquote-2.0-tests)

(def-suite quasiquote-2.0)
(in-suite quasiquote-2.0)

(defun run-tests ()
  (let ((results (run 'quasiquote-2.0)))
    (fiveam:explain! results)
    (unless (fiveam:results-status results)
      (error "Tests failed."))))

(test basic
  (is (equal '(nil :just-quote-it!) (multiple-value-list (%codewalk-dig-form '(dig nil)))))
  (is (equal '(nil :just-form-it!) (multiple-value-list (%codewalk-dig-form '(dig (inject a))))))
  (is (equal '(nil :just-form-it!) (multiple-value-list (%codewalk-dig-form '(dig 2 (inject 2 a))))))
  (is (equal '(((((inject b) c (inject d)) car cdr car) (((inject d)) car cdr cdr cdr car)) nil)
	     (multiple-value-list (%codewalk-dig-form '(dig (a (inject b) c (inject d)))))))
  (is (equal '(nil nil)
	     (multiple-value-list (%codewalk-dig-form '(dig (dig (a (inject b) c (inject d))))))))
  (is (equal '(((((inject 2 d)) car cdr cdr cdr car cdr car)) nil)
	     (multiple-value-list (%codewalk-dig-form '(dig (dig (a (inject b) c (inject 2 d)))))))))
  
(test transform
  (is (equal '(quote a) (transform-dig-form '(dig a))))
  (is (equal '(quote a) (transform-dig-form '(dig 2 a))))
  (is (equal 'a (transform-dig-form '(dig (inject a)))))
  (is (equal 'a (transform-dig-form '(dig 2 (inject 2 a))))))

(defun foo (b d)
  (dig (a (inject b) c (inject d))))

(defun foo1-transparent (x)
  (declare (ignorable x))
  (dig (dig (a (inject (b (inject x) c))))))

(defun foo1-opaque (x)
  (declare (ignorable x))
  (dig (dig (a (oinject (b (inject x) c))))))

(defun foo-recursive (x y)
  (dig (a (inject (list x (dig (c (inject y))))))))
  

(test foos
  (is (equal '(a 1 c 2) (foo 1 2)))
  (is (equal '(a 100 c 200) (foo 100 200))))

(test opaque-vs-transparent
  (is (equal '(quote a) (transform-dig-form '(odig a))))
  (is (equal '(quote a) (transform-dig-form '(odig 2 a))))
  (is (equal 'a (transform-dig-form '(odig (inject a)))))
  (is (equal 'a (transform-dig-form '(odig 2 (inject 2 a)))))
  (is (equal '(odig (inject 2 a)) (eval (transform-dig-form '(dig (odig (inject 2 a)))))))
  (is (equal '(dig (a (inject (b 3 c)))) (foo1-transparent 3)))
  (is (equal '(dig (a (oinject (b (inject x) c)))) (foo1-opaque 3))))

(test recursive-compile-time
  (is (equal '(a (1 (c 2))) (foo-recursive 1 2))))
	     

(test splicing
  (is (equal '(a b c d) (eval (transform-dig-form '(dig (a (splice '(b c)) d))))))
  (is (equal '(b c d) (eval (transform-dig-form '(dig ((splice '(b c)) d))))))
  (is (equal '(a b c) (eval (transform-dig-form '(dig (a (splice '(b c))))))))
  (is (equal '(a b) (eval (transform-dig-form '(dig (a (splice nil) b))))))
  (is (equal '(b) (eval (transform-dig-form '(dig ((splice nil) b))))))
  (is (equal '(a) (eval (transform-dig-form '(dig (a (splice nil)))))))
  (is (equal '() (eval (transform-dig-form '(dig ((splice nil)))))))
  (is (equal '(a b) (eval (transform-dig-form '(dig ((splice '(a b)))))))))


(test are-they-macro
  (is (not (equal '(dig (a b)) (macroexpand-1 '(dig (a b))))))
  (is (not (equal '(odig (a b)) (macroexpand-1 '(odig (a b)))))))


(defmacro triple-var (x)
  `((inject ,x) (inject ,x) (inject ,x)))

(test correct-order-of-effects
  (is (equal '(a 1 2 3) (let ((x 0))
			  (dig (a (inject (incf x)) (inject (incf x)) (inject (incf x)))))))
  (is (equal '(a (((1))) 2)
	     (let ((x 0))
	       (dig (a ((((inject (incf x))))) (inject (incf x))))))))

(test macro-injects
  (is (equal '(a (3 3 3)) (let ((x 3))
			    (dig (a (macro-inject (triple-var x)))))))
  (is (equal '(a (1 2 3)) (let ((x 0))
			    (dig (a (macro-inject (triple-var (incf x))))))))
  (macrolet ((frob (form n)
	       (mapcar (lambda (x)
			 `(inject ,x))
		       (make-list n :initial-element form)))
	     (frob1 (form)
	       `(frob ,form 4)))
    (is (equal '(a (1 2 3 4 5))
	       (let ((x 0))
		 (dig (a (macro-inject (frob (incf x) 5)))))))
    (is (equal '(a 1 2 3 4 5)
	       (let ((x 0))
		 (dig (a (macro-splice (frob (incf x) 5)))))))
    (is (equal '(a)
	       (let ((x 0))
		 (declare (ignorable x))
		 (dig (a (macro-splice (frob (incf x) 0)))))))
    (is (equal '(a frob (incf x) 4)
	       (let ((x 0))
		 (declare (ignorable x))
		 (dig (a (macro-splice (frob1 (incf x))))))))
    (is (equal '(a 1 2 3 4)
	       (let ((x 0))
		 (dig (a (macro-splice-all (frob1 (incf x))))))))))
    
	       
(quasiquote-2.0:enable-quasiquote-2.0)

(test reader
  (is (equal '(inject x) ',x))
  (is (equal '(inject 3 x) ',,,x))
  (is (equal '(splice x) ',@x))
  (is (equal '(splice 3 x) ',,,@x))
  (is (equal '(omacro-splice-all 4 x) ',,,,!oma@x))
  (is (equal '(inject 4 oma@x) ',,,,oma@x)))

(test macro-splices
  (macrolet ((splicer (x)
	       ``(splice ,x)))
    (is (equal '(a 1 2 3) (let ((x '(1 2 3)))
			    `(a ,!m(splicer x)))))))

(test repeated-splices
  (is (equal '(a) `(a ,@nil ,@nil ,@nil ,@nil)))
  (is (equal '(a b c d e f g) `(a ,@(list 'b 'c) ,@(list 'd 'e) ,@nil ,@(list 'f 'g)))))