4 (try* 123 (catch* e 456))
7 (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
8 ; "exc is:" "'abc' not found"
11 (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
12 ; "exc:" "my exception"
15 ;;; Test that throw is a function:
16 (try* (map throw (list "my err")) (catch* exc exc))
21 ;; Testing builtin functions
45 ;; Testing apply function with core functions
50 (apply prn (list 1 2 "3" (list)))
53 (apply prn 1 2 (list "3" (list)))
58 (apply symbol? (list (quote two)))
61 ;; Testing apply function with user functions
62 (apply (fn* (a b) (+ a b)) (list 2 3))
64 (apply (fn* (a b) (+ a b)) 4 (list 5))
67 ;; Testing map function
68 (def! nums (list 1 2 3))
69 (def! double (fn* (a) (* 2 a)))
74 (map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
79 ;; ------- Deferrable Functionality ----------
80 ;; ------- (Needed for self-hosting) -------
82 ;; Testing symbol and keyword functions
89 (symbol? (symbol "abc"))
99 (keyword? (keyword "abc"))
104 ;;;TODO: all implementations should suppport this too
110 ;; Testing sequential? function
112 (sequential? (list 1 2 3))
116 (sequential? sequential?)
123 ;; Testing apply function with core functions and arguments in vector
126 (apply prn 1 2 ["3" 4])
131 ;; Testing apply function with user functions and arguments in vector
132 (apply (fn* (a b) (+ a b)) [2 3])
134 (apply (fn* (a b) (+ a b)) 4 [5])
138 ;; Testing map function with vectors
139 (map (fn* (a) (* 2 a)) [1 2 3])
142 (map (fn* [& args] (list? args)) [1 2])
145 ;; Testing vector functions
176 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
179 (def! hm1 (hash-map))
198 (def! hm2 (assoc hm1 "a" 1))
214 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
221 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
228 (count (keys (assoc hm2 "b" 2 "c" 3)))
231 ;; Testing keywords as hash-map keys
232 (get {:abc 123} :abc)
234 (contains? {:abc 123} :abc)
236 (contains? {:abcd 123} :abc)
240 (keyword? (nth (keys {:abc 123 :def 456}) 0))
242 ;;; TODO: support : in strings in make impl
243 ;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
245 (keyword? (nth (vals {"a" :abc "b" :def}) 0))
248 ;; Testing whether assoc updates properly
249 (def! hm4 (assoc {:a 1 :b 2} :a 3 :c 1))
257 ;; Testing nil as hash-map values
258 (contains? {:abc nil} :abc)
264 ;; Additional str and pr-str tests
266 (str "A" {:abc "val"} "Z")
269 (str true "." false "." nil "." :keyw "." 'symb)
270 ;=>"true.false.nil.:keyw.symb"
272 (pr-str "A" {:abc "val"} "Z")
273 ;=>"\"A\" {:abc \"val\"} \"Z\""
275 (pr-str true "." false "." nil "." :keyw "." 'symb)
276 ;=>"true \".\" false \".\" nil \".\" :keyw \".\" symb"
278 (def! s (str {:abc "val1" :def "val2"}))
279 (or (= s "{:abc val1 :def val2}") (= s "{:def val2 :abc val1}"))
282 (def! p (pr-str {:abc "val1" :def "val2"}))
283 (or (= p "{:abc \"val1\" :def \"val2\"}") (= p "{:def \"val2\" :abc \"val1\"}"))
287 ;; Test extra function arguments as Mal List (bypassing TCO with apply)
288 (apply (fn* (& more) (list? more)) [1 2 3])
290 (apply (fn* (& more) (list? more)) [])
292 (apply (fn* (a & more) (list? more)) [1])
298 ;; ------- Optional Functionality --------------
299 ;; ------- (Not needed for self-hosting) -------
302 ;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
303 ;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
304 ;;;; "exc is:" ["data" "foo"] ;;;;=>7
308 ;; Testing throwing non-strings
309 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
315 (def! hm3 (assoc hm2 "b" 2))
324 (dissoc hm3 "a" "b" "c")
329 (dissoc {:cde 345 :fgh 456} :cde)
331 (dissoc {:cde nil :fgh 456} :cde)
335 ;; Testing equality of hash-maps
338 (= {:a 11 :b 22} (hash-map :b 22 :a 11))
340 (= {:a 11 :b [22 33]} (hash-map :b [22 33] :a 11))
342 (= {:a 11 :b {:c 33}} (hash-map :b {:c 33} :a 11))
344 (= {:a 11 :b 22} (hash-map :b 23 :a 11))
346 (= {:a 11 :b 22} (hash-map :a 11))
348 (= {:a [11 22]} {:a (list 11 22)})
350 (= {:a 11 :b 22} (list :a 11 :b 22))