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