Skip to content

Commit

Permalink
Fix around macro to be called once per all children
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahTheDuke committed Nov 21, 2024
1 parent 9c66a26 commit 2ec12f7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
22 changes: 22 additions & 0 deletions corpus/context_tests/use_fixture.clj
Original file line number Diff line number Diff line change
@@ -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))))
17 changes: 9 additions & 8 deletions src/clojure/lazytest/runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion test/clojure/lazytest/context_test.clj
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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))))

0 comments on commit 2ec12f7

Please sign in to comment.