From 2ec12f7cb1277d327a9ea6533099025da6e97567 Mon Sep 17 00:00:00 2001 From: Noah Bogart Date: Thu, 21 Nov 2024 10:47:03 -0500 Subject: [PATCH] Fix around macro to be called once per all children --- CHANGELOG.md | 4 ++++ corpus/context_tests/use_fixture.clj | 22 ++++++++++++++++++++++ src/clojure/lazytest/runner.clj | 17 +++++++++-------- test/clojure/lazytest/context_test.clj | 12 +++++++++++- 4 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 corpus/context_tests/use_fixture.clj diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e2137a..7846248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixed + +- `around` context macro is now called once for all children. (See [#12](https://github.com/NoahTheDuke/lazytest/issues/12).) + ## 1.4.0 Released `2024-11-19`. diff --git a/corpus/context_tests/use_fixture.clj b/corpus/context_tests/use_fixture.clj new file mode 100644 index 0000000..cae599a --- /dev/null +++ b/corpus/context_tests/use_fixture.clj @@ -0,0 +1,22 @@ +(ns context-tests.use-fixture + (:require + [lazytest.core :refer [around defdescribe expect it set-ns-context!]])) + +(def use-fixture-state (volatile! [])) + +(defn vconj! [volatile value] + (vswap! volatile conj value)) + +(set-ns-context! + [(around [f] + (vconj! use-fixture-state :around-before) + (f) + (vconj! use-fixture-state :around-after))]) + +(defdescribe first-test + (it "works normally" + (expect (= 1 1)))) + +(defdescribe second-test + (it "also works" + (expect (= 1 1)))) diff --git a/src/clojure/lazytest/runner.clj b/src/clojure/lazytest/runner.clj index a82efe5..46cf3a6 100644 --- a/src/clojure/lazytest/runner.clj +++ b/src/clojure/lazytest/runner.clj @@ -24,14 +24,15 @@ config (-> config (update ::depth #(if id (inc %) %)) (update ::suite-history conj suite)) - f (if-let [around-fn (combine-arounds suite)] - #(let [ret (volatile! nil) - tests (propagate-eachs suite %)] - (around-fn (fn [] (vreset! ret (run-tree tests config)))) - @ret) - #(let [child (propagate-eachs suite %)] - (run-tree child config))) - results (vec (keep f (:children suite))) + around-fn (if-let [around-fn (combine-arounds suite)] + (fn with-around [f] + (let [ret (volatile! nil)] + (around-fn (fn [] (vreset! ret (f)))) + @ret)) + (fn [f] (f))) + f #(let [child (propagate-eachs suite %)] + (run-tree child config)) + results (around-fn #(vec (keep f (:children suite)))) duration (double (- (System/nanoTime) start))] (-> (suite-result suite results) (assoc ::source-type source-type) diff --git a/test/clojure/lazytest/context_test.clj b/test/clojure/lazytest/context_test.clj index 4cf6aed..886e149 100644 --- a/test/clojure/lazytest/context_test.clj +++ b/test/clojure/lazytest/context_test.clj @@ -1,8 +1,11 @@ (ns lazytest.context-test (:require + [context-tests.use-fixture :refer [use-fixture-state]] [lazytest.context :refer [propagate-eachs]] [lazytest.core :refer [after after-each around before before-each - defdescribe describe expect expect-it it]])) + defdescribe describe expect expect-it it]] + [lazytest.main :as main] + [lazytest.runner :as-alias lr])) (defn vconj! [volatile value] (vswap! volatile conj value)) @@ -196,3 +199,10 @@ :after-each-middle-2 :after-each-top :after-each-top-2] @state)))) + +(defdescribe set-ns-context-test + (it "works like clojure.test/use-fixtures" + (vreset! use-fixture-state []) + (main/run ["--output" "quiet" + "--dir" "corpus/context_tests"]) + (expect (= [:around-before :around-after] @use-fixture-state))))