Skip to content
Snippets Groups Projects
Commit 54341518 authored by Elliott Shugerman's avatar Elliott Shugerman
Browse files

goin big with da test lib

parent b5704e78
No related branches found
No related tags found
No related merge requests found
(import spork/path)
(import spork/generators)
(def divider-heavy "================================================================================")
(def divider-light "--------------------------------------------------------------------------------")
(var tests :private @[])
(defn it "Defines a test" [description thunk]
(array/push tests {:description description
:thunk thunk}))
# no reason to use a dynamic binding here since it's global anyway (?)
(def toplevel :private @[])
(defn group [description thunk]
(with-dyns [:children @[]
:before nil
:before-each nil
:after nil
:after-each nil]
(thunk)
(array/push toplevel {:type 'group
:description description
:children (dyn :children)
:before (dyn :before)
:before-each (dyn :before-each)
:after (dyn :after)
:after-each (dyn :after-each)})))
(defn before [thunk]
(setdyn :before thunk))
(defn before-each [thunk]
(setdyn :before-each thunk))
(defn after [thunk]
(setdyn :after thunk))
(defn after-each [thunk]
(setdyn :after-each thunk))
(defn test [description thunk]
(array/push (dyn :children) {:type 'test
:description description
:thunk thunk}))
(defn execute-toplevel []
(map (fn [group]
# TODO: catch errors in hooks?
(when-let [before (group :before)]
(before))
(def child-results
(map (fn [child]
(when-let [before-each (group :before-each)]
(before-each))
(match child
{:type 'test :thunk thunk :description desc}
(try
(do
((test :thunk))
(printf "* %s ✅" (test :description))
{:type 'test :test test :passed true})
([err]
(printf "* %s ❌" (test :description))
{:type 'test :test test :passed false :error err}))
# TODO: :type 'group
)
(when-let [after-each (group :after-each)]
(after-each)))
(group :children)))
(when-let [after (group :after)]
(after))
{:type 'group :group group :children child-results})
toplevel))
(defn run-tests []
(def results (execute-toplevel))
(print divider-heavy)
(report results)
(let
# TODO: do better
[is-repl (and (= "janet" (path/basename (dyn *executable*)))
(all (fn [arg] (not= ".janet" (path/ext arg))) (dyn *args*)))
some-failed (some (fn [res] (not (res :passed))) results)]
(when (and some-failed (not is-repl))
(os/exit 1))))
(defn- report "Default reporter" [results]
(print "FAILURES:")
(loop [result :in results]
(unless (result :passed)
(print divider-light)
(let [{:test {:description test-desc} :error err} result]
(printf "* %s" test-desc)
(printf " %s" err))))
# TODO: recursion
(def failure-results
(reduce
(fn [acc group-result]
(if-let [failed-children (filter (fn [res] (not (res :passed)))
(group-result :children))
failed-group-result (merge group-result
{:children failed-children})]
(array/push acc failed-group-result)
acc))
(array/new)
results))
(defn get-spaces [n]
(string (splice (g/to-array (g/take 10 (g/cycle [" "])))))
(->> [" "]
(generators/cycle)
(generators/take 10)
(generators/to-array)
(splice)
(string)))
(defn print-failure-results [result depth]
(def indent (get-spaces (* 2 depth)))
(match result
{:type 'group :group {:description desc} :children child-results}
(do
(print indent desc)
(loop [child-result :in child-results]
(print-failure-results child-result (+ 1 depth))))
{:type 'test :test {:description desc} :error err}
(do
(print indent desc)
(print err)
(print))))
(loop [result :in failure-results]
(print-failure-result result 0))
(print divider-heavy)
(print "SUMMARY:")
......@@ -27,27 +133,6 @@
(printf "Failed: %i" num-failed))
(print divider-heavy))
# TODO: do better
(defn- is-repl? []
(and (= "janet" (path/basename (dyn *executable*)))
(all (fn [arg] (not= ".janet" (path/ext arg))) (dyn *args*))))
(defn run-tests "Runs all test cases" []
(def results @[])
(loop [test :in tests]
(array/push results (try
(do
(apply (test :thunk))
(printf "* %s ✅" (test :description))
{:test test :passed true})
([err]
(printf "* %s ❌" (test :description))
{:test test :passed false :error err}))))
(print divider-heavy)
(report results)
(when (and (not (is-repl?))
(some (fn [res] (not (res :passed))) results))
(os/exit 1)))
(defn reset "Clear defined tests" []
(set tests @[]))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment