From 2e3fa98e5be9799892f2340ca166f36ef913cc4b Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Tue, 13 Dec 2022 10:36:10 +0100 Subject: [PATCH] [fix] [#12] Fix issue with duplicate side effects when using within another macro --- src/taoensso/truss/impl.cljc | 8 ++++++-- test/taoensso/truss_tests.cljc | 26 +++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/taoensso/truss/impl.cljc b/src/taoensso/truss/impl.cljc index b79bf7b..dfe4842 100644 --- a/src/taoensso/truss/impl.cljc +++ b/src/taoensso/truss/impl.cljc @@ -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) diff --git a/test/taoensso/truss_tests.cljc b/test/taoensso/truss_tests.cljc index b0e42db..a81959d 100644 --- a/test/taoensso/truss_tests.cljc +++ b/test/taoensso/truss_tests.cljc @@ -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) @@ -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))