Fork me on GitHub
#testing
<
2021-02-23
>
Noah Bogart15:02:44

to follow-up on my question above, i was able to get this to work for me:

👍 3
Noah Bogart15:02:52

(defmacro before-each
  [let-bindings & testing-blocks]
  (assert (every? #(= 'testing (first %)) testing-blocks))
  (let [bundles (for [block testing-blocks] `(let [~@let-bindings] ~block))]
    `(do ~@bundles)))

Noah Bogart15:02:25

the assert isn't needed if you don't want to wrap every "block" in a testing

Noah Bogart15:02:17

usage looks like this:

(deftest add-subtype-test
  (before-each [sg {:subtypes []}]
    (testing "accepts strings and keywords"
      (is (add-subtype sg "type"))
      (is (add-subtype sg :type)))
    (testing "performs no change on keyword input"
      (is (= "type" (first (:subtypes (add-subtype sg :type))))))
    (testing "throws if given subtype isn't a keyword or string"
      (is (thrown? java.lang.AssertionError (add-subtype sg nil))))))