I’ve previously written some code to play with the idea of executing tests via the docstring, similar to python’s doctest. I wonder if this is maybe something that would be good idea to try to integrate into @arrdem stacks structure or something. https://gist.github.com/pithyless/c7d6954c055ad45b6dfd2fca6ddfcce9
Interesting idea! You could definitely relax the language lines constraint. What has your experience been using this / transcriptor @pithyless?
@pithyless there's probably a few ways to simplify that. clojure.test checks every var in every ns for a :test
key on its metadata, then runs the fn mapped to by that metadata using fixtures defined in the same ns (if any)
so eg. you don't need a macro that calls deftest, it suffices to create a var with ^{:test (fn [] ....)}
metadata on it
and that var could be the same function you are testing
@noisesmith I thought ^:test
was a boolean flag that the tagged var was a test fn, what are you doing there?
double checking...
I mean I can check as well as you can
no need if it was offhand
right, but if I misremembered it's helpful for me to see what the actual situation is
(defmacro deftest
"Defines a test function with no arguments. Test functions may call
other tests, so tests may be composed. If you compose tests, you
should also define a function named test-ns-hook; run-tests will
call test-ns-hook instead of testing all vars.
Note: Actually, the test body goes in the :test metadata on the var,
and the real function (the value of the var) calls test-var on
itself.
When *load-tests* is false, deftest is ignored."
{:added "1.1"}
[name & body]
(when *load-tests*
`(def ~(vary-meta name assoc :test `(fn [] ~@body))
(fn [] (test-var (var ~name))))))
+user=> (clojure.test/deftest foo-test (clojure.test/is true))
#'user/foo-test
+user=> (meta #'foo-test)
{:test #object[user$fn__147 0x2e61d218 "user$fn__147@2e61d218"], :line 2, :column 1, :file "NO_SOURCE_PATH", :name foo-test, :ns #object[clojure.lang.Namespace 0x20b12f8a "user"]}
so the value under the :test metadata contains the actual function to run
as I said
Yeah okay interesting.
TIL
that means you can easily add an inline test to any function you like, if that suits your fancy
well… if you have a thing that either annotates vars after the fact or hacks defn
but maybe that’s worth building
I mean - you can put ^{:test (fn ...)}
in your function definition, as I was suggesting
or a macro that makes a nicer syntax for that of course
just saying there's a more direct route than using deftest is all, especially if part of the root desire here is to define the test in the same code that defines the function
gotcha