Add seq and string? tests.
[jackhill/mal.git] / tests / stepA_mal.mal
1 ;;;
2 ;;; See IMPL/tests/stepA_mal.mal for implementation specific
3 ;;; interop tests.
4 ;;;
5
6
7 ;;
8 ;; Testing readline
9 (readline "mal-user> ")
10 "hello"
11 ;=>"\"hello\""
12
13 ;;
14 ;; Testing *host-language*
15 ;;; each impl is different, but this should return false
16 ;;; rather than throwing an exception
17 (= "something bogus" *host-language*)
18 ;=>false
19
20
21 ;;
22 ;; ------- Optional Functionality ----------
23 ;; ------- (Needed for self-hosting) -------
24
25 ;;
26 ;; Testing metadata on functions
27
28 ;;
29 ;; Testing metadata on mal functions
30
31 (meta (fn* (a) a))
32 ;=>nil
33
34 (meta (with-meta (fn* (a) a) {"b" 1}))
35 ;=>{"b" 1}
36
37 (meta (with-meta (fn* (a) a) "abc"))
38 ;=>"abc"
39
40 (def! l-wm (with-meta (fn* (a) a) {"b" 2}))
41 (meta l-wm)
42 ;=>{"b" 2}
43
44 (meta (with-meta l-wm {"new_meta" 123}))
45 ;=>{"new_meta" 123}
46 (meta l-wm)
47 ;=>{"b" 2}
48
49 (def! f-wm (with-meta (fn* [a] (+ 1 a)) {"abc" 1}))
50 (meta f-wm)
51 ;=>{"abc" 1}
52
53 (meta (with-meta f-wm {"new_meta" 123}))
54 ;=>{"new_meta" 123}
55 (meta f-wm)
56 ;=>{"abc" 1}
57
58 (def! f-wm2 ^{"abc" 1} (fn* [a] (+ 1 a)))
59 (meta f-wm2)
60 ;=>{"abc" 1}
61
62 ;; Meta of native functions should return nil (not fail)
63 (meta +)
64 ;=>nil
65
66
67 ;;
68 ;; Make sure closures and metadata co-exist
69 (def! gen-plusX (fn* (x) (with-meta (fn* (b) (+ x b)) {"meta" 1})))
70 (def! plus7 (gen-plusX 7))
71 (def! plus8 (gen-plusX 8))
72 (plus7 8)
73 ;=>15
74 (meta plus7)
75 ;=>{"meta" 1}
76 (meta plus8)
77 ;=>{"meta" 1}
78 (meta (with-meta plus7 {"meta" 2}))
79 ;=>{"meta" 2}
80 (meta plus8)
81 ;=>{"meta" 1}
82
83 ;;
84 ;; Testing hash-map evaluation and atoms (i.e. an env)
85 (def! e (atom {"+" +}))
86 (swap! e assoc "-" -)
87 ( (get @e "+") 7 8)
88 ;=>15
89 ( (get @e "-") 11 8)
90 ;=>3
91 (swap! e assoc "foo" (list))
92 (get @e "foo")
93 ;=>()
94 (swap! e assoc "bar" '(1 2 3))
95 (get @e "bar")
96 ;=>(1 2 3)
97
98
99 ;;
100 ;; ------- Optional Functionality --------------
101 ;; ------- (Not needed for self-hosting) -------
102 ;>>> soft=True
103
104 ;;
105 ;; Testing string? function
106 (string? "")
107 ;=>true
108 (string? 'abc)
109 ;=>false
110 (string? "abc")
111 ;=>true
112 (string? :abc)
113 ;=>false
114 (string? (keyword "abc"))
115 ;=>false
116 (string? 234)
117 ;=>false
118 (string? nil)
119 ;=>false
120
121
122 ;;
123 ;; Testing conj function
124 (conj (list) 1)
125 ;=>(1)
126 (conj (list 1) 2)
127 ;=>(2 1)
128 (conj (list 2 3) 4)
129 ;=>(4 2 3)
130 (conj (list 2 3) 4 5 6)
131 ;=>(6 5 4 2 3)
132 (conj (list 1) (list 2 3))
133 ;=>((2 3) 1)
134
135 (conj [] 1)
136 ;=>[1]
137 (conj [1] 2)
138 ;=>[1 2]
139 (conj [2 3] 4)
140 ;=>[2 3 4]
141 (conj [2 3] 4 5 6)
142 ;=>[2 3 4 5 6]
143 (conj [1] [2 3])
144 ;=>[1 [2 3]]
145
146 ;;
147 ;; Testing seq function
148 (seq "abc")
149 ;=>("a" "b" "c")
150 (apply str (seq "this is a test"))
151 ;=>"this is a test"
152 (seq '(2 3 4))
153 ;=>(2 3 4)
154 (seq [2 3 4])
155 ;=>(2 3 4)
156
157 (seq "")
158 ;=>nil
159 (seq '())
160 ;=>nil
161 (seq [])
162 ;=>nil
163 (seq nil)
164 ;=>nil
165
166 ;;
167 ;; Testing metadata on collections
168
169 (meta [1 2 3])
170 ;=>nil
171
172 (with-meta [1 2 3] {"a" 1})
173 ;=>[1 2 3]
174
175 (meta (with-meta [1 2 3] {"a" 1}))
176 ;=>{"a" 1}
177
178 (vector? (with-meta [1 2 3] {"a" 1}))
179 ;=>true
180
181 (meta (with-meta [1 2 3] "abc"))
182 ;=>"abc"
183
184 (meta (with-meta (list 1 2 3) {"a" 1}))
185 ;=>{"a" 1}
186
187 (list? (with-meta (list 1 2 3) {"a" 1}))
188 ;=>true
189
190 (meta (with-meta {"abc" 123} {"a" 1}))
191 ;=>{"a" 1}
192
193 (map? (with-meta {"abc" 123} {"a" 1}))
194 ;=>true
195
196 ;;; Not actually supported by Clojure
197 ;;;(meta (with-meta (atom 7) {"a" 1}))
198 ;;;;=>{"a" 1}
199
200 (def! l-wm (with-meta [4 5 6] {"b" 2}))
201 ;=>[4 5 6]
202 (meta l-wm)
203 ;=>{"b" 2}
204
205 (meta (with-meta l-wm {"new_meta" 123}))
206 ;=>{"new_meta" 123}
207 (meta l-wm)
208 ;=>{"b" 2}
209
210 ;;
211 ;; Testing metadata on builtin functions
212 (meta +)
213 ;=>nil
214 (def! f-wm3 ^{"def" 2} +)
215 (meta f-wm3)
216 ;=>{"def" 2}
217 (meta +)
218 ;=>nil
219
220 ;;
221 ;; Testing gensym and clean or macro
222 (= (gensym) (gensym))
223 ;=>false
224 (let* [or_FIXME 23] (or false (+ or_FIXME 100)))
225 ;=>123