testing

Testing tools, testing philosophy & methodology...
NoahTheDuke 2021-02-23T15:05:44.005100Z

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

👍 1
NoahTheDuke 2021-02-23T15:05:52.005300Z

(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)))

NoahTheDuke 2021-02-23T15:06:25.005700Z

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

NoahTheDuke 2021-02-23T15:12:17.006100Z

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))))))