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