Skip to content

Instantly share code, notes, and snippets.

@defclass
Last active September 15, 2022 00:02
Show Gist options
  • Save defclass/d0becf23224eb67f66928d2cf99d1df4 to your computer and use it in GitHub Desktop.
Save defclass/d0becf23224eb67f66928d2cf99d1df4 to your computer and use it in GitHub Desktop.
(ns defclass.test-framework)
(def all-test (atom {}))
(def ^:dynamic *test* true)
(defmacro is [expr]
`(let [result# ~expr]
(when-not result#
(set! *test* false)
(printf "FAILED: %s%n" '~expr))))
(defmacro deftest [name & body]
`(let [f# (fn []
(binding [*test* true]
(do ~@body)
*test*))]
(swap! all-test assoc '~name f#)
(def ~name f#)))
(defn run-all-tests []
(let [failed (atom 0) pass (atom 0)]
(doseq [[key val] @all-test]
(let [result (val)]
(if result
(swap! pass inc)
(swap! failed inc))
(printf "%s: test %s\n\n" key (if result "PASS" "FAILED"))))
(printf "All tests: %d, Passed tests: %d, Failed tests: %s\n"
(+ @failed @pass) @pass @failed)))
(comment
(deftest test-a
(is (= (+ 1 2) 3))
(is (= (+ 1 2 3) 6))
(is (= (+ -1 -3) -4)))
;; => #'test.test-framework/test-a
(test-a)
;; => true
(deftest test-b
(is (= (+ 1 2) 2))
(is (= (+ 1 2 3) 3))
(is (= (+ -1 -3) -1)))
;; => #'test.test-framework/test-b
(test-b)
;; FAILED: (= (+ 1 2) 2)
;; FAILED: (= (+ 1 2 3) 3)
;; FAILED: (= (+ -1 -3) -1)
;; => false
(run-all-tests)
;; test-a: test PASS
;
;; FAILED: (= (+ 1 2) 2)
;; FAILED: (= (+ 1 2 3) 3)
;; FAILED: (= (+ -1 -3) -1)
;; test-b: test FAILED
;
;; All tests: 2, Passed tests: 1, Failed tests: 1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment