[#100] add clojure.core/assert

This commit is contained in:
Michiel Borkent 2019-11-13 18:00:57 +01:00
parent e8f923d68f
commit 16f643c4dc
4 changed files with 32 additions and 8 deletions

View file

@ -6,7 +6,7 @@
[_ _ & body]
`(~'future-call (fn [] ~@body)))
(defn close! [^java.io.Closeable x]
(defn __close!__ [^java.io.Closeable x]
(.close x))
(defn with-open*
@ -17,10 +17,21 @@
(try
(with-open ~(subvec bindings 2) ~@body)
(finally
(~'close! ~(bindings 0)))))
(~'__close!__ ~(bindings 0)))))
:else (throw (IllegalArgumentException.
"with-open only allows Symbols in bindings"))))
(defn __assertion-error__ [^String m]
(AssertionError. m))
(defn assert*
([_ _ x]
`(when-not ~x
(throw (~'__assertion-error__ (str "Assert failed: " (pr-str '~x))))))
([_ _ x message]
`(when-not ~x
(throw (~'__assertion-error__ (str "Assert failed: " ~message "\n" (pr-str '~x)))))))
(def core-extras
{'file-seq file-seq
'future-call future-call
@ -45,5 +56,7 @@
'println-str println-str
'flush flush
'read-line read-line
'close! close!
'with-open (with-meta with-open* {:sci/macro true})})
'__close!__ __close!__
'with-open (with-meta with-open* {:sci/macro true})
'__assertion-error__ __assertion-error__
'assert (with-meta assert* {:sci/macro true})})

View file

@ -2,4 +2,6 @@
(def exception-bindings
{'ArithmeticException ArithmeticException
'java.lang.ArithmeticException ArithmeticException})
'java.lang.ArithmeticException ArithmeticException
'java.lang.AssertionError AssertionError
'AssertionError AssertionError})

View file

@ -37,6 +37,9 @@
(case opt
("--version") {:version true}
("--help" "-h" "-?") {:help? true}
("--verbose")(recur (rest options)
(assoc opts-map
:verbose? true))
("--stream") (recur (rest options)
(assoc opts-map
:stream? true))
@ -171,11 +174,11 @@ Everything after that is bound to *command-line-args*."))
[& args]
(handle-pipe!)
#_(binding [*out* *err*]
(prn "M" (meta (get bindings 'future))))
(prn "M" (meta (get bindings 'future))))
(let [t0 (System/currentTimeMillis)
{:keys [:version :shell-in :edn-in :shell-out :edn-out
:help? :file :command-line-args
:expression :stream? :time? :socket-repl] :as _opts}
:expression :stream? :time? :socket-repl :verbose?] :as _opts}
(parse-opts args)
read-next (fn [*in*]
(if (pipe-signal-received?)
@ -254,7 +257,9 @@ Everything after that is bound to *command-line-args*."))
(let [d (ex-data e)
exit-code (:bb/exit-code d)]
(if exit-code [nil exit-code]
(do (print-stack-trace e)
(do (if verbose?
(print-stack-trace e)
(println (.getMessage e)))
(flush)
[nil 1]))))))))
1)

View file

@ -232,3 +232,7 @@
(is (= '(["Adult" "87727"] ["Elderly" "43914"] ["Child" "33411"] ["Adolescent" "29849"]
["Infant" "15238"] ["Newborn" "10050"] ["In Utero" "1198"])
(bb nil (.getPath (io/file "test" "babashka" "scripts" "csv.bb"))))))
(deftest assert-test
(is (thrown-with-msg? Exception #"should-be-true"
(bb nil "(def should-be-true false) (assert should-be-true)"))))