From 16f643c4dc14891d4ee9ef4b2119c1ff41b00212 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 13 Nov 2019 18:00:57 +0100 Subject: [PATCH] [#100] add clojure.core/assert --- src/babashka/impl/clojure/core.clj | 21 +++++++++++++++++---- src/babashka/impl/exceptions.clj | 4 +++- src/babashka/main.clj | 11 ++++++++--- test/babashka/main_test.clj | 4 ++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/babashka/impl/clojure/core.clj b/src/babashka/impl/clojure/core.clj index 3a2f90ca..e5ec3e6e 100644 --- a/src/babashka/impl/clojure/core.clj +++ b/src/babashka/impl/clojure/core.clj @@ -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})}) diff --git a/src/babashka/impl/exceptions.clj b/src/babashka/impl/exceptions.clj index 69224f2d..2d0dc9fd 100644 --- a/src/babashka/impl/exceptions.clj +++ b/src/babashka/impl/exceptions.clj @@ -2,4 +2,6 @@ (def exception-bindings {'ArithmeticException ArithmeticException - 'java.lang.ArithmeticException ArithmeticException}) + 'java.lang.ArithmeticException ArithmeticException + 'java.lang.AssertionError AssertionError + 'AssertionError AssertionError}) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index ca8e14e1..1e17ab67 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -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) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 93edcee3..952fe12e 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -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)"))))