Skip to content

Instantly share code, notes, and snippets.

@andrewchambers
Created December 3, 2019 01:29
Show Gist options
  • Save andrewchambers/a868325587f058ce0a969d02d45d3cf5 to your computer and use it in GitHub Desktop.
Save andrewchambers/a868325587f058ce0a969d02d45d3cf5 to your computer and use it in GitHub Desktop.
self-test
running suite tester-self-test-suite
✔✔
passed 2/2
(import "../src/tester" :prefix "")
# A meta test of the test suite
(var setup-ran false)
(var teardown-ran false)
(test-suite
:name "tester-self-test-suite"
:setup
(fn []
(set setup-ran true)
@{:t-setup-count 0 :t-teardown-count 0})
:test-setup
(fn [suite-state]
(set teardown-ran true)
(update suite-state :t-setup-count inc)
:ok)
:test-teardown
(fn [suite-state test-state]
(update suite-state :t-teardown-count inc)
(assert (= test-state :ok)))
:teardown
(fn [state]
(assert (= (state :t-setup-count) 2))
(assert (= (state :t-teardown-count) 2)))
:tests [
(test-case sanity1
(assert (not false)))
(test-case sanity2
nil)])
(assert setup-ran)
(assert teardown-ran)
(var num-tests-run 0)
(var num-tests-failed 0)
(var num-tests-passed 0)
(var num-ticks 0)
(var test-name nil)
(defn test-suite
[&keys opts]
(set num-tests-run 0)
(set num-tests-failed 0)
(set num-tests-passed 0)
(def suite-name (get opts :name))
(def suite-setup (get opts :setup (fn [&] nil)))
(def suite-teardown (get opts :teardown (fn [&] nil)))
(def test-setup (get opts :test-setup (fn [&] nil)))
(def test-teardown (get opts :test-teardown (fn [&] nil)))
(def tests (get opts :tests []))
(print "running suite" (if suite-name (string " " suite-name) ""))
(with [suite-state (suite-setup) suite-teardown]
(each t tests
(set test-name nil)
(try
(do
(++ num-tests-run)
(with [test-state (test-setup suite-state) (partial test-teardown suite-state)]
(t suite-state test-state)
(when (= num-ticks 25)
(set num-ticks 0)
(print))
(prin "✔")
(++ num-ticks)
(++ num-tests-passed)))
([err fiber]
(print "\ntest failed...")
(debug/stacktrace fiber err)
(set num-ticks 0)
(++ num-tests-failed)))))
(print)
(print "passed " num-tests-passed "/" num-tests-run))
(defmacro test-case
[name body]
(def suite-state (gensym))
(def test-state (gensym))
~(fn ,(symbol "test-" name)
[,suite-state ,test-state]
(setdyn :suite-state ,suite-state)
(setdyn :test-state ,test-state)
,body))
(defmacro assert
[assertion]
(with-syms [v]
~(let [,v ,assertion]
(when (not ,v) (error "assertion failed!")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment