From 312f7156f7711698a0b08a4ed4860be539808370 Mon Sep 17 00:00:00 2001 From: Oleksandr Yakushev Date: Tue, 28 Jan 2025 22:44:36 +0200 Subject: [PATCH] [with-temp] Delay pprint to set testing context in do-with-temp --- src/toucan2/tools/with_temp.clj | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/toucan2/tools/with_temp.clj b/src/toucan2/tools/with_temp.clj index d6c79c9..37c7753 100644 --- a/src/toucan2/tools/with_temp.clj +++ b/src/toucan2/tools/with_temp.clj @@ -46,6 +46,13 @@ :dispatch-value-spec (s/nonconforming ::types/dispatch-value.model)} u/dispatch-on-first-arg) +(defn delayed-string + "Return a CharSequence that only executes `(f)` when toString is called. Caches the result." + [f] + (let [d (delay (f))] + (reify CharSequence + (toString [_] @d)))) + (m/defmethod do-with-temp* :default [model explicit-attributes f] (assert (some? model) (format "%s model cannot be nil." `with-temp)) @@ -61,9 +68,10 @@ (let [temp-object (first (insert/insert-returning-instances! model merged-attributes))] (log/debugf "[with-temp] => %s" temp-object) (try - (t/testing (format "\nwith temporary %s with attributes\n%s\n" - (pr-str model) - (with-out-str (pprint/pprint merged-attributes))) + (t/testing (delayed-string + #(format "\nwith temporary %s with attributes\n%s\n" + (pr-str model) + (with-out-str (pprint/pprint merged-attributes)))) (f temp-object)) (finally (delete/delete! model :toucan/pk ((model/select-pks-fn model) temp-object))))))))