Skip to content

Commit

Permalink
[fix] [#12] Fix issue with duplicate side effects when using within a…
Browse files Browse the repository at this point in the history
…nother macro
  • Loading branch information
ptaoussanis committed Dec 13, 2022
1 parent c94cb5f commit 2e3fa98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/taoensso/truss/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,18 @@

(defn- ns-sym [] (symbol (str *ns*)))

#?(:clj
(defn const-form? "See issue #12" [x]
(not (or (list? x) (instance? clojure.lang.Cons x)))))

#?(:clj
(defmacro -invar
"Written to maximize performance + minimize post Closure+gzip Cljs code size."
[elidable? truthy? line pred x ?data-fn]
(let [safe-x? (not (list? x)) ; Pre-evaluated (common case)
(let [const-x? (const-form? x) ; Common case
[pred* safe-pred?] (xpred #?(:clj nil :cljs &env) pred)]

(if safe-x? ; Common case
(if const-x? ; Common case
(if safe-pred? ; Common case
`(if (~pred* ~x)
~(if truthy? true x)
Expand Down
26 changes: 25 additions & 1 deletion test/taoensso/truss_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
[clojure.string :as str]
[taoensso.encore :as enc :refer [throws throws?]]
[taoensso.truss :as truss :refer [have have? have! have!?]]
[taoensso.truss.impl :as impl]))
[taoensso.truss.impl :as impl])

#?(:cljs
(:require-macros [taoensso.truss-tests :refer [my-macro1]])))

(comment
(remove-ns 'taoensso.truss-tests)
Expand Down Expand Up @@ -125,4 +128,25 @@

;;;;

#?(:clj (defmacro my-macro1 "See issue #12" [n x] `(have string? (do (swap! ~n inc) ~x))))

(deftest _side-effects
(testing "Side effects"

(let [n (atom 0)]
[(is (= (have string? (do (swap! n inc) "str")) "str"))
(is (= @n 1))])

(let [n (atom 0)]
[(is (throws? (have string? (do (swap! n inc) :kw))))
(is (= @n 1))])

(let [n (atom 0)]
[(is (= (my-macro1 n "str") "str"))
(is (= @n 1))])

(let [n (atom 0)]
[(is (throws? (my-macro1 n :kw)))
(is (= @n 1))])))

#?(:cljs (test/run-tests))

0 comments on commit 2e3fa98

Please # to comment.