5 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*err1.*
10 (try* 123 (catch* e 456))
13 (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
14 ;/"exc is:" "'abc' not found"
17 ;; Make sure error from core can be caught
18 (try* (nth () 1) (catch* exc (prn "exc is:" exc)))
19 ;/"exc is:".*(length|range|[Bb]ounds|beyond).*
22 (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
23 ;/"exc:" "my exception"
26 ;;; Test that throw is a function:
27 (try* (map throw (list "my err")) (catch* exc exc))
32 ;; Testing builtin functions
56 ;; Testing apply function with core functions
61 (apply prn (list 1 2 "3" (list)))
64 (apply prn 1 2 (list "3" (list)))
69 (apply symbol? (list (quote two)))
72 ;; Testing apply function with user functions
73 (apply (fn* (a b) (+ a b)) (list 2 3))
75 (apply (fn* (a b) (+ a b)) 4 (list 5))
78 ;; Testing map function
79 (def! nums (list 1 2 3))
80 (def! double (fn* (a) (* 2 a)))
85 (map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
90 ;; ------- Deferrable Functionality ----------
91 ;; ------- (Needed for self-hosting) -------
93 ;; Testing throwing a hash-map
95 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*msg.*err2.*
97 ;; Testing symbol and keyword functions
104 (symbol? (symbol "abc"))
114 (keyword? (keyword "abc"))
119 ;;;TODO: all implementations should suppport this too
125 ;; Testing sequential? function
127 (sequential? (list 1 2 3))
131 (sequential? sequential?)
138 ;; Testing apply function with core functions and arguments in vector
141 (apply prn 1 2 ["3" 4])
146 ;; Testing apply function with user functions and arguments in vector
147 (apply (fn* (a b) (+ a b)) [2 3])
149 (apply (fn* (a b) (+ a b)) 4 [5])
153 ;; Testing map function with vectors
154 (map (fn* (a) (* 2 a)) [1 2 3])
157 (map (fn* [& args] (list? args)) [1 2])
160 ;; Testing vector functions
191 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
194 (def! hm1 (hash-map))
213 (def! hm2 (assoc hm1 "a" 1))
229 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
236 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
243 (count (keys (assoc hm2 "b" 2 "c" 3)))
246 ;; Testing keywords as hash-map keys
247 (get {:abc 123} :abc)
249 (contains? {:abc 123} :abc)
251 (contains? {:abcd 123} :abc)
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)
279 ;; Additional str and pr-str tests
281 (str "A" {:abc "val"} "Z")
284 (str true "." false "." nil "." :keyw "." 'symb)
285 ;=>"true.false.nil.:keyw.symb"
287 (pr-str "A" {:abc "val"} "Z")
288 ;=>"\"A\" {:abc \"val\"} \"Z\""
290 (pr-str true "." false "." nil "." :keyw "." 'symb)
291 ;=>"true \".\" false \".\" nil \".\" :keyw \".\" symb"
293 (def! s (str {:abc "val1" :def "val2"}))
294 (or (= s "{:abc val1 :def val2}") (= s "{:def val2 :abc val1}"))
297 (def! p (pr-str {:abc "val1" :def "val2"}))
298 (or (= p "{:abc \"val1\" :def \"val2\"}") (= p "{:def \"val2\" :abc \"val1\"}"))
302 ;; Test extra function arguments as Mal List (bypassing TCO with apply)
303 (apply (fn* (& more) (list? more)) [1 2 3])
305 (apply (fn* (& more) (list? more)) [])
307 (apply (fn* (a & more) (list? more)) [1])
313 ;; ------- Optional Functionality --------------
314 ;; ------- (Not needed for self-hosting) -------
317 ;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
318 ;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
319 ;;;; "exc is:" ["data" "foo"] ;;;;=>7
323 ;; Testing try* without catch*
325 ;/.*\'?xyz\'? not found.*
328 ;; Testing throwing non-strings
329 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
335 (def! hm3 (assoc hm2 "b" 2))
344 (dissoc hm3 "a" "b" "c")
349 (dissoc {:cde 345 :fgh 456} :cde)
351 (dissoc {:cde nil :fgh 456} :cde)
355 ;; Testing equality of hash-maps
358 (= {:a 11 :b 22} (hash-map :b 22 :a 11))
360 (= {:a 11 :b [22 33]} (hash-map :b [22 33] :a 11))
362 (= {:a 11 :b {:c 33}} (hash-map :b {:c 33} :a 11))
364 (= {:a 11 :b 22} (hash-map :b 23 :a 11))
366 (= {:a 11 :b 22} (hash-map :a 11))
368 (= {:a [11 22]} {:a (list 11 22)})
370 (= {:a 11 :b 22} (list :a 11 :b 22))