Skip to content

Commit

Permalink
Add future exception test, #884
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Apr 28, 2023
1 parent 27e0678 commit f660c34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ reports
\#*\#

libsci.build_artifacts.txt
src/scratch.clj
33 changes: 23 additions & 10 deletions test/sci/error_test.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns sci.error-test
(:require [clojure.test :as t :refer [deftest testing is]]
[sci.addons.future :as fut]
[sci.core :as sci :refer [eval-string]]))

#?(:cljs (def Exception js/Error))
Expand All @@ -22,8 +23,8 @@
{:ns user, :name foo, :line 3, :column 1}
{:ns user, :name nil, :line 4, :column 1})]
#_#_(println "-- ^ expected --- , actual")
(doseq [st stacktrace]
(prn st))
(doseq [st stacktrace]
(prn st))
(is (= expected
stacktrace)))
(let [stacktrace (try (eval-string "(1 2 3)")
Expand All @@ -32,7 +33,7 @@
(map #(-> %
(select-keys [:ns :name :line :column]))
(sci/stacktrace e))))]
(is (= '({:ns user, :name nil, :line 1, :column 1}) stacktrace )))
(is (= '({:ns user, :name nil, :line 1, :column 1}) stacktrace)))
(testing "unresolved class in import"
(let [stacktrace (try (eval-string "(ns foo (:import [java.io FooBar]))")
(catch #?(:clj Exception
Expand Down Expand Up @@ -120,23 +121,23 @@
:cljs (ex-cause e)))]))))))

(deftest implicit-do-error-test
(let [expected {:type :sci/error
:line 1
:column 1
:message "Assert failed: false"}
(let [expected {:type :sci/error
:line 1
:column 1
:message "Assert failed: false"}
try-string #(try
(eval-string %)
(catch #?(:clj clojure.lang.ExceptionInfo :cljs cljs.core/ExceptionInfo) e
(dissoc (ex-data e) :sci.impl/callstack :file)))]
(testing "top level try with implicit do wraps exception"
(is (= expected
(try-string "(try 1 (assert false))"))))
(try-string "(try 1 (assert false))"))))
(testing "top level let with implicit do wraps exception"
(is (= expected
(try-string "(let [] 1 (assert false))")))
(try-string "(let [] 1 (assert false))")))
; macroexpands to a let
(is (= expected
(try-string "(binding [] (assert false) 1)"))))))
(try-string "(binding [] (assert false) 1)"))))))

(deftest analysis-error-test
(is
Expand All @@ -154,3 +155,15 @@
(let [data (try (sci/eval-string "(defmacro dude [& body] `(do ~@body)) (dude x)") (catch Exception e (ex-data e)))]
(is (:line data))
(is (:column data))))

#?(:clj
(deftest preserve-exception-type-in-threads-test
(is (= java.lang.IllegalArgumentException
(sci/eval-string "
(def f (future (throw (IllegalArgumentException. \"meh\"))))
(try @f
(catch Throwable e
(type (ex-cause e))))"
(-> {:classes {'IllegalArgumentException IllegalArgumentException
'Throwable Throwable}}
(fut/install)))))))

0 comments on commit f660c34

Please sign in to comment.