Skip to content

Commit

Permalink
fix :throws for cljs
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurC committed Sep 26, 2022
1 parent 3e7dc6d commit a3f4ce9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/hyperfiddle/rcf/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,6 @@
form))) (:args ast)))
(ana/emit-invoke ast)))

(defmethod t/assert-expr 'hyperfiddle.rcf/thrown? [msg form]
;; (is (thrown? c expr))
;; Asserts that evaluating expr throws an exception of class c.
;; Returns the exception thrown.
(let [[body klass] (rest form)]
`(try ~body
(do-report {:type :hyperfiddle.rcf/fail, :message ~msg,
:expected '~form, :actual nil})
(catch ~klass e#
(do-report {:type :hyperfiddle.rcf/pass, :message ~msg,
:expected '~form, :actual e#})
e#))))


(defn- stacktrace-file-and-line
[stacktrace]
(if (seq stacktrace)
Expand All @@ -372,7 +358,8 @@
{:file file-name :line (.getLineNumber s)})
{:file nil :line nil}))

(defn do-report [m]

(defn do-report* [m]
(t/report
(case
(:type m)
Expand All @@ -386,6 +373,24 @@
(:error :hyperfiddle.rcf/error) (merge (stacktrace-file-and-line (.getStackTrace ^Throwable (:actual m))) m)
m)))

(defmacro do-report [m]
(if (:js-globals &env)
`(cljs.test/do-report ~m)
`(do-report* ~m)))

(defmethod t/assert-expr 'hyperfiddle.rcf/thrown? [msg form]
;; (is (thrown? c expr))
;; Asserts that evaluating expr throws an exception of class c.
;; Returns the exception thrown.
(let [[body klass] (rest form)]
`(try ~body
(do-report {:type :hyperfiddle.rcf/fail, :message ~msg,
:expected '~form, :actual nil})
(catch ~klass e#
(do-report {:type :hyperfiddle.rcf/pass, :message ~msg,
:expected '~form, :actual e#})
e#))))

(defn test-var
"Like `clojure.test/test-var` but return actual result."
[v]
Expand Down
7 changes: 7 additions & 0 deletions test/hyperfiddle/rcf_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.test :as t :refer [deftest is testing]]
[hyperfiddle.rcf :as rcf :refer [tests]]
#_[hyperfiddle.rcf.analyzer :as ana])
#?(:clj (:import [clojure.lang ExceptionInfo]))
#?(:cljs (:require-macros [hyperfiddle.rcf-test])))

(deftest tap-outside-tests
Expand Down Expand Up @@ -58,6 +59,12 @@
(rcf/! 5)
rcf/% := 5)

(tests
":throws works in clj(s)"
;; inlining `thrower` leads to "unreachable code" warning
(let [thrower #(throw (ex-info "boom" {}))]
(thrower) :throws ExceptionInfo))

;; For an unknown reason, `macroexpand-1` acts as identity when runnning
;; tests without a repl.

Expand Down

0 comments on commit a3f4ce9

Please sign in to comment.