5 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*err1.*
7 ;/.*([Ee][Rr][Rr][Oo][Rr]|[Ee]xception).*msg.*err2.*
10 ;; Testing try*/catch*
12 (try* 123 (catch* e 456))
15 (try* (abc 1 2) (catch* exc (prn "exc is:" exc)))
16 ;/"exc is:" "'abc' not found"
19 ;; Make sure error from core can be caught
20 (try* (nth [] 1) (catch* exc (prn "exc is:" exc)))
21 ;/"exc is:".*(length|range|[Bb]ounds|beyond).*
24 (try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
25 ;/"exc:" "my exception"
28 ;;; Test that throw is a function:
29 (try* (map throw (list "my err")) (catch* exc exc))
34 ;; Testing builtin functions
58 ;; Testing apply function with core functions
63 (apply prn (list 1 2 "3" (list)))
66 (apply prn 1 2 (list "3" (list)))
71 (apply symbol? (list (quote two)))
74 ;; Testing apply function with user functions
75 (apply (fn* (a b) (+ a b)) (list 2 3))
77 (apply (fn* (a b) (+ a b)) 4 (list 5))
80 ;; Testing map function
81 (def! nums (list 1 2 3))
82 (def! double (fn* (a) (* 2 a)))
87 (map (fn* (x) (symbol? x)) (list 1 (quote two) "three"))
92 ;; ------- Deferrable Functionality ----------
93 ;; ------- (Needed for self-hosting) -------
95 ;; Testing symbol and keyword functions
102 (symbol? (symbol "abc"))
112 (keyword? (keyword "abc"))
117 ;;;TODO: all implementations should suppport this too
123 ;; Testing sequential? function
125 (sequential? (list 1 2 3))
129 (sequential? sequential?)
136 ;; Testing apply function with core functions and arguments in vector
139 (apply prn 1 2 ["3" 4])
144 ;; Testing apply function with user functions and arguments in vector
145 (apply (fn* (a b) (+ a b)) [2 3])
147 (apply (fn* (a b) (+ a b)) 4 [5])
151 ;; Testing map function with vectors
152 (map (fn* (a) (* 2 a)) [1 2 3])
155 (map (fn* [& args] (list? args)) [1 2])
158 ;; Testing vector functions
189 (get (assoc (assoc {"a" 1 } "b" 2) "c" 3) "a")
192 (def! hm1 (hash-map))
211 (def! hm2 (assoc hm1 "a" 1))
227 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
234 ;;; TODO: fix. Clojure returns nil but this breaks mal impl
241 (count (keys (assoc hm2 "b" 2 "c" 3)))
244 ;; Testing keywords as hash-map keys
245 (get {:abc 123} :abc)
247 (contains? {:abc 123} :abc)
249 (contains? {:abcd 123} :abc)
253 (keyword? (nth (keys {:abc 123 :def 456}) 0))
255 ;;; TODO: support : in strings in make impl
256 ;;;(keyword? (nth (keys {":abc" 123 ":def" 456}) 0))
258 (keyword? (nth (vals {"a" :abc "b" :def}) 0))
261 ;; Testing whether assoc updates properly
262 (def! hm4 (assoc {:a 1 :b 2} :a 3 :c 1))
270 ;; Testing nil as hash-map values
271 (contains? {:abc nil} :abc)
277 ;; Additional str and pr-str tests
279 (str "A" {:abc "val"} "Z")
282 (str true "." false "." nil "." :keyw "." 'symb)
283 ;=>"true.false.nil.:keyw.symb"
285 (pr-str "A" {:abc "val"} "Z")
286 ;=>"\"A\" {:abc \"val\"} \"Z\""
288 (pr-str true "." false "." nil "." :keyw "." 'symb)
289 ;=>"true \".\" false \".\" nil \".\" :keyw \".\" symb"
291 (def! s (str {:abc "val1" :def "val2"}))
292 (or (= s "{:abc val1 :def val2}") (= s "{:def val2 :abc val1}"))
295 (def! p (pr-str {:abc "val1" :def "val2"}))
296 (or (= p "{:abc \"val1\" :def \"val2\"}") (= p "{:def \"val2\" :abc \"val1\"}"))
300 ;; Test extra function arguments as Mal List (bypassing TCO with apply)
301 (apply (fn* (& more) (list? more)) [1 2 3])
303 (apply (fn* (& more) (list? more)) [])
305 (apply (fn* (a & more) (list? more)) [1])
311 ;; ------- Optional Functionality --------------
312 ;; ------- (Not needed for self-hosting) -------
315 ;;;TODO: fix so long lines don't trigger ANSI escape codes ;;;(try*
316 ;;;(try* (throw ["data" "foo"]) (catch* exc (do (prn "exc is:" exc) 7))) ;;;;
317 ;;;; "exc is:" ["data" "foo"] ;;;;=>7
321 ;; Testing try* without catch*
323 ;/.*\'?xyz\'? not found.*
326 ;; Testing throwing non-strings
327 (try* (throw (list 1 2 3)) (catch* exc (do (prn "err:" exc) 7)))
333 (def! hm3 (assoc hm2 "b" 2))
342 (dissoc hm3 "a" "b" "c")
347 (dissoc {:cde 345 :fgh 456} :cde)
349 (dissoc {:cde nil :fgh 456} :cde)
353 ;; Testing equality of hash-maps
356 (= {:a 11 :b 22} (hash-map :b 22 :a 11))
358 (= {:a 11 :b [22 33]} (hash-map :b [22 33] :a 11))
360 (= {:a 11 :b {:c 33}} (hash-map :b {:c 33} :a 11))
362 (= {:a 11 :b 22} (hash-map :b 23 :a 11))
364 (= {:a 11 :b 22} (hash-map :a 11))
366 (= {:a [11 22]} {:a (list 11 22)})
368 (= {:a 11 :b 22} (list :a 11 :b 22))