5 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*err1.*
10 (try* 123 (catch* e 456))
13 (try* abc (catch* exc (prn "exc is:" exc)))
14 ;/"exc is:" "'abc' not found"
17 (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
18 ;/"exc is:" "'abc' not found"
21 ;; Make sure error from core can be caught
22 (try* (nth () 1) (catch* exc (prn "exc is:" exc)))
23 ;/"exc is:".*(length|range|[Bb]ounds|beyond).*
26 (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
27 ;/"exc:" "my exception"
30 ;; Test that exception handlers get restored correctly
31 (try* (do (try* "t1" (catch* e "c1")) (throw "e1")) (catch* e "c2"))
33 (try* (try* (throw "e1") (catch* e (throw "e2"))) (catch* e "c2"))
36 ;;; Test that throw is a function:
37 (try* (map throw (list "my err")) (catch* exc exc))
42 ;; Testing builtin functions
66 ;; Testing apply function with core functions
71 (apply prn (list 1 2 "3" (list)))
74 (apply prn 1 2 (list "3" (list)))
79 (apply symbol? (list (quote two)))
82 ;; Testing apply function with user functions
83 (apply (fn* (a b) (+ a b)) (list 2 3))
85 (apply (fn* (a b) (+ a b)) 4 (list 5))
88 ;; Testing map function
89 (def! nums (list 1 2 3))
90 (def! double (fn* (a) (* 2 a)))
95 (map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
100 ;; ------- Deferrable Functionality ----------
101 ;; ------- (Needed for self-hosting) -------
103 ;; Testing throwing a hash-map
104 (throw {:msg "err2"})
105 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*msg.*err2.*
107 ;; Testing symbol and keyword functions
114 (symbol? (symbol "abc"))
124 (keyword? (keyword "abc"))
132 ;; Testing sequential? function
134 (sequential? (list 1 2 3))
138 (sequential? sequential?)
145 ;; Testing apply function with core functions and arguments in vector
148 (apply prn 1 2 ["3" 4])
153 ;; Testing apply function with user functions and arguments in vector
154 (apply (fn* (a b) (+ a b)) [2 3])
156 (apply (fn* (a b) (+ a b)) 4 [5])
160 ;; Testing map function with vectors
161 (map (fn* (a) (* 2 a)) [1 2 3])
164 (map (fn* [& args] (list? args)) [1 2])
167 ;; Testing vector functions
199 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
202 (def! hm1 (hash-map))
221 (def! hm2 (assoc hm1 "a" 1))
237 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
247 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
254 (count (keys (assoc hm2 "b" 2 "c" 3)))
257 ;; Testing keywords as hash-map keys
258 (get {:abc 123} :abc)
260 (contains? {:abc 123} :abc)
262 (contains? {:abcd 123} :abc)
266 (keyword? (nth (keys {:abc 123 :def 456}) 0))
268 (keyword? (nth (vals {"a" :abc "b" :def}) 0))
271 ;; Testing whether assoc updates properly
272 (def! hm4 (assoc {:a 1 :b 2} :a 3 :c 1))
280 ;; Testing nil as hash-map values
281 (contains? {:abc nil} :abc)
287 ;; Additional str and pr-str tests
289 (str "A" {:abc "val"} "Z")
292 (str true "." false "." nil "." :keyw "." 'symb)
293 ;=>"true.false.nil.:keyw.symb"
295 (pr-str "A" {:abc "val"} "Z")
296 ;=>"\"A\" {:abc \"val\"} \"Z\""
298 (pr-str true "." false "." nil "." :keyw "." 'symb)
299 ;=>"true \".\" false \".\" nil \".\" :keyw \".\" symb"
301 (def! s (str {:abc "val1" :def "val2"}))
302 (cond (= s "{:abc val1 :def val2}") true (= s "{:def val2 :abc val1}") true)
305 (def! p (pr-str {:abc "val1" :def "val2"}))
306 (cond (= p "{:abc \"val1\" :def \"val2\"}") true (= p "{:def \"val2\" :abc \"val1\"}") true)
310 ;; Test extra function arguments as Mal List (bypassing TCO with apply)
311 (apply (fn* (& more) (list? more)) [1 2 3])
313 (apply (fn* (& more) (list? more)) [])
315 (apply (fn* (a & more) (list? more)) [1])
321 ;; ------- Optional Functionality --------------
322 ;; ------- (Not needed for self-hosting) -------
325 ;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
326 ;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
327 ;;;; "exc is:" ["data" "foo"] ;;;;=>7
331 ;; Testing try* without catch*
333 ;/.*\'?xyz\'? not found.*
336 ;; Testing throwing non-strings
337 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
343 (def! hm3 (assoc hm2 "b" 2))
352 (dissoc hm3 "a" "b" "c")
357 (dissoc {:cde 345 :fgh 456} :cde)
359 (dissoc {:cde nil :fgh 456} :cde)
363 ;; Testing equality of hash-maps
366 (= {:a 11 :b 22} (hash-map :b 22 :a 11))
368 (= {:a 11 :b [22 33]} (hash-map :b [22 33] :a 11))
370 (= {:a 11 :b {:c 33}} (hash-map :b {:c 33} :a 11))
372 (= {:a 11 :b 22} (hash-map :b 23 :a 11))
374 (= {:a 11 :b 22} (hash-map :a 11))
376 (= {:a [11 22]} {:a (list 11 22)})
378 (= {:a 11 :b 22} (list :a 11 :b 22))
387 (keyword? (first (keys {":abc" 123 ":def" 456})))