Fork me on GitHub
#hyperfiddle
<
2022-03-23
>
Daniel Jomphe11:03:00

How to expect an exception cleanly with RCF?

(defn πŸ’£ [] (/ 1 0))

(rcf/tests
 (ex-message (clojure.test/is (thrown? ArithmeticException (πŸ’£))))
 := "Divide by zero")
Done this way, my expectation leaks on both sides of :=.

πŸ‘€ 1
Daniel Jomphe13:03:21

Some experiments. But still, parts of the expectation appears on LHS.

(catch-a :message (πŸ’£)) := "Divide by zero"
(ns test
  (:require [clojure.test :as test]
            [hyperfiddle.rcf :refer [tests]]))

(defn πŸ’£ [] (/ 1 0))

(defmacro catch-a [k & body]
  `(try ~@body
        (ex-info "Expected something thrown!" {}) ; don't throw it!
        (catch Exception e#
          (~k (first (:via (Throwable->map e#)))))))

(tests

 (+ 1 2) := 3

 (ex-message (test/is (thrown? ArithmeticException (πŸ’£))))
 := "Divide by zero"

 ;;(catch-a :message 1) := 1

 (catch-a :message (πŸ’£)) := (catch-a :message (πŸ’£))
 (catch-a :message (πŸ’£)) := "Divide by zero"

 (catch-a :type (πŸ’£)) := (catch-a :type (πŸ’£))
;;(catch-a :type (πŸ’£)) := java.lang.ArithmeticException  ; <>!?

 ;;
 )

Geoffrey Gaillard16:03:43

The current version of RCF doesn’t provide a way to expect exceptions. We are working on a version where we could leverage clojure.test/assert-expr. It already defines thrown? so we’ll get exception handling for free. Here is the feature request: https://github.com/hyperfiddle/rcf/issues/47

Dustin Getz17:03:41

The next version of RCF has much better facilities for adding features like this, no public release ETA but we are using it internally

Daniel Jomphe17:03:23

Thanks to both of you. In the meantime we don't mind doing the kind of things above. We're slowly ramping up on RCF usage. πŸ™‚

πŸ™‚ 2
Daniel Jomphe13:03:21

Some experiments. But still, parts of the expectation appears on LHS.

(catch-a :message (πŸ’£)) := "Divide by zero"
(ns test
  (:require [clojure.test :as test]
            [hyperfiddle.rcf :refer [tests]]))

(defn πŸ’£ [] (/ 1 0))

(defmacro catch-a [k & body]
  `(try ~@body
        (ex-info "Expected something thrown!" {}) ; don't throw it!
        (catch Exception e#
          (~k (first (:via (Throwable->map e#)))))))

(tests

 (+ 1 2) := 3

 (ex-message (test/is (thrown? ArithmeticException (πŸ’£))))
 := "Divide by zero"

 ;;(catch-a :message 1) := 1

 (catch-a :message (πŸ’£)) := (catch-a :message (πŸ’£))
 (catch-a :message (πŸ’£)) := "Divide by zero"

 (catch-a :type (πŸ’£)) := (catch-a :type (πŸ’£))
;;(catch-a :type (πŸ’£)) := java.lang.ArithmeticException  ; <>!?

 ;;
 )

Geoffrey Gaillard16:03:43

The current version of RCF doesn’t provide a way to expect exceptions. We are working on a version where we could leverage clojure.test/assert-expr. It already defines thrown? so we’ll get exception handling for free. Here is the feature request: https://github.com/hyperfiddle/rcf/issues/47