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