All: move some fns to core. Major cleanup.
[jackhill/mal.git] / tests / step8_macros.mal
CommitLineData
d85fc037
JM
1;; Testing cons function
2(cons 1 (list))
3;=>(1)
4(cons 1 (list 2))
5;=>(1 2)
6(cons 1 (list 2 3))
7;=>(1 2 3)
8(cons (list 1) (list 2 3))
9;=>((1) 2 3)
10(cons [1] [2 3])
11;=>([1] 2 3)
12(cons 1 [2 3])
13;=>(1 2 3)
14
15;; Testing concat function
16(concat)
17;=>()
18(concat (list 1 2))
19;=>(1 2)
20(concat (list 1 2) (list 3 4))
21;=>(1 2 3 4)
22(concat (list 1 2) (list 3 4) (list 5 6))
23;=>(1 2 3 4 5 6)
24(concat [1 2] (list 3 4) [5 6])
25;=>(1 2 3 4 5 6)
26(concat (concat))
27;=>()
28
29;; Testing first function
30(first '())
31;=>nil
32(first '(6))
33;=>6
34(first '(7 8 9))
35;=>7
36(first [])
37;=>nil
38(first [10])
39;=>10
40(first [10 11 12])
41;=>10
42
43;; Testing rest function
44(rest '())
45;=>()
46(rest '(6))
47;=>()
48(rest '(7 8 9))
49;=>(8 9)
50(rest [])
51;=>()
52(rest [10])
53;=>()
54(rest [10 11 12])
55;=>(11 12)
56
57
58
31690700
JM
59;; Testing trivial macros
60(defmacro! one (fn* () 1))
61(one)
62;=>1
63(defmacro! two (fn* () 2))
64(two)
65;=>2
66
67;; Testing unless macros
68(defmacro! unless (fn* (pred a b) `(if ~pred ~b ~a)))
69(unless false 7 8)
70;=>7
71(unless true 7 8)
72;=>8
73(defmacro! unless2 (fn* (pred a b) `(if (not ~pred) ~a ~b)))
74(unless2 false 7 8)
75;=>7
76(unless2 true 7 8)
77;=>8
78
79;; Testing macroexpand
80(macroexpand (unless2 2 3 4))
81;=>(if (not 2) 3 4)
82
83;;
84;; Loading core.mal
85(load-file "../core.mal")
86
87;; Testing and macro
88(and)
89;=>true
90(and 1)
91;=>1
92(and 1 2)
93;=>2
94(and 1 2 3)
95;=>3
96(and 1 2 3 4)
97;=>4
98(and 1 2 3 4 false)
99;=>false
100(and 1 2 3 4 false 5)
101;=>false
102
103;; Testing or macro
104(or)
105;=>nil
106(or 1)
107;=>1
108(or 1 2 3 4)
109;=>1
110(or false 2)
111;=>2
112(or false nil 3)
113;=>3
114(or false nil false false nil 4)
115;=>4
116(or false nil 3 false nil 4)
117;=>3
118
119;; Testing -> macro
120
121(-> 7)
122;=>7
123(-> (list 7 8 9) first)
124;=>7
125(-> (list 7 8 9) (first))
126;=>7
127(-> (list 7 8 9) first (+ 7))
128;=>14
129(-> (list 7 8 9) rest (rest) first (+ 7))
130;=>16
131
132;; Testing cond macro
133
134(cond)
135;=>nil
136(cond true 7)
137;=>7
138(cond true 7 true 8)
139;=>7
140(cond false 7 true 8)
141;=>8
142(cond false 7 false 8 "else" 9)
143;=>9
144(cond false 7 (= 2 2) 8 "else" 9)
145;=>8
146(cond false 7 false 8 false 9)
147;=>nil
148
8cb5cda4 149;; Testing all EVAL of non-default locations
31690700
JM
150(let* [x (or nil "yes")] x)
151;=>"yes"
152