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)))
59 ;; Testing apply function with user functions
60 (apply (fn* (a b) (+ a b)) (list 2 3))
62 (apply (fn* (a b) (+ a b)) 4 (list 5))
65 ;; Testing map function
66 (def! nums (list 1 2 3))
67 (def! double (fn* (a) (* 2 a)))
72 (map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
77 ;; ------- Deferrable Functionality ----------
78 ;; ------- (Needed for self-hosting) -------
80 ;; Testing symbol and keyword functions
87 (symbol? (symbol "abc"))
97 (keyword? (keyword "abc"))
102 ;;;TODO: all implementations should suppport this too
108 ;; Testing sequential? function
110 (sequential? (list 1 2 3))
114 (sequential? sequential?)
121 ;; Testing apply function with core functions and arguments in vector
124 (apply prn 1 2 ["3" 4])
129 ;; Testing apply function with user functions and arguments in vector
130 (apply (fn* (a b) (+ a b)) [2 3])
132 (apply (fn* (a b) (+ a b)) 4 [5])
136 ;; Testing map function with vectors
137 (map (fn* (a) (* 2 a)) [1 2 3])
140 ;; Testing vector functions
171 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
174 (def! hm1 (hash-map))
193 (def! hm2 (assoc hm1 "a" 1))
209 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
216 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
223 (count (keys (assoc hm2 "b" 2 "c" 3)))
226 (def! hm3 (assoc hm2 "b" 2))
238 (dissoc hm3 "a" "b" "c")
244 ;; Testing keywords as hash-map keys
245 (get {:abc 123} :abc)
247 (contains? {:abc 123} :abc)
249 (contains? {:abcd 123} :abc)
253 (dissoc {:cde 345 :fgh 456} :cde)
255 (keyword? (nth (keys {:abc 123 :def 456}) 0))
257 ;;; TODO: support : in strings in make impl
258 ;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
260 (keyword? (nth (vals {"a" :abc "b" :def}) 0))
263 ;; Testing whether assoc updates properly
264 (def! hm4 (assoc {:a 1 :b 2} :a 3 :c 1))
272 ;; Testing nil as hash-map values
273 (contains? {:abc nil} :abc)
277 (dissoc {:cde nil :fgh 456} :cde)
280 ;; Testing equality of hash-maps
283 (= {:a 11 :b 22} (hash-map :b 22 :a 11))
285 (= {:a 11 :b [22 33]} (hash-map :b [22 33] :a 11))
287 (= {:a 11 :b {:c 33}} (hash-map :b {:c 33} :a 11))
289 (= {:a 11 :b 22} (hash-map :b 23 :a 11))
291 (= {:a 11 :b 22} (hash-map :a 11))
293 (= {:a [11 22]} {:a (list 11 22)})
295 (= {:a 11 :b 22} (list :a 11 :b 22))
303 ;; Additional str and pr-str tests
305 (str "A" {:abc "val"} "Z")
308 (str true "." false "." nil "." :keyw "." 'symb)
309 ;=>"true.false.nil.:keyw.symb"
311 (pr-str "A" {:abc "val"} "Z")
312 ;=>"\"A\" {:abc \"val\"} \"Z\""
314 (pr-str true "." false "." nil "." :keyw "." 'symb)
315 ;=>"true \".\" false \".\" nil \".\" :keyw \".\" symb"
317 (def! s (str {:abc "val1" :def "val2"}))
318 (or (= s "{:abc val1 :def val2}") (= s "{:def val2 :abc val1}"))
321 (def! p (pr-str {:abc "val1" :def "val2"}))
322 (or (= p "{:abc \"val1\" :def \"val2\"}") (= p "{:def \"val2\" :abc \"val1\"}"))
326 ;; Test extra function arguments as Mal List (bypassing TCO with apply)
327 (apply (fn* (& more) (list? more)) [1 2 3])
329 (apply (fn* (& more) (list? more)) [])
331 (apply (fn* (a & more) (list? more)) [1])
337 ;; ------- Optional Functionality --------------
338 ;; ------- (Not needed for self-hosting) -------
341 ;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
342 ;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
343 ;;;; "exc is:" ["data" "foo"] ;;;;=>7
346 ;; Testing throwing non-strings
347 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))