unit testing - Clojure equivalent to Python doctest? -
python doctests associate simple tests source code (in python in function documentation). more info , examples here.
is there similar clojure?
i aware of unit tests clojure.test, looking more closely integrated source (typically unit tests in different files; here want have test "inside" defn).
searching around found this, seems un-lispy (the tests in actual text, python - surely macro extends defn preferable?).
or perhaps there other way solve general problem, have tests (generally simple things demonstrate basic properties of function) better included documentation (i using marginalia) in separate unit test files.
update here's example: have function calculates (manhattan) distance (in pixels) rectangle of pixels centre of image. sounds simple, complicated things difference in meaning of "centre" images odd or numbers of pixels on sides, or part of block measure from. had write tests code straight. , @ docs , best if docs included tests because explain better words function does...
and test flag in metadata, http://clojure.org/special_forms
(defn ^{:doc "mymax [xs+] gets maximum value in xs using > " :test (fn [] (assert (= 42 (mymax 2 42 5 4)))) :user/comment "this best fn ever!"} mymax ([x] x) ([x y] (if (> x y) x y)) ([x y & more] (reduce mymax (mymax x y) more))) user=> (test #'mymax) :ok user=> (doc test) ------------------------- clojure.core/test ([v]) test [v] finds fn @ key :test in var metadata , calls it, presuming failure throw exception nil
Comments
Post a Comment