docs

About docs of Clojure & libs
pithyless 2018-01-04T13:39:07.000381Z

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

arrdem 2018-01-04T16:59:33.000030Z

Interesting idea! You could definitely relax the language lines constraint. What has your experience been using this / transcriptor @pithyless?

2018-01-04T17:19:43.000249Z

@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)

2018-01-04T17:20:26.000065Z

so eg. you don't need a macro that calls deftest, it suffices to create a var with ^{:test (fn [] ....)} metadata on it

2018-01-04T17:20:42.000507Z

and that var could be the same function you are testing

arrdem 2018-01-04T19:05:50.000371Z

@noisesmith I thought ^:test was a boolean flag that the tagged var was a test fn, what are you doing there?

2018-01-04T19:06:56.000236Z

double checking...

arrdem 2018-01-04T19:07:49.000071Z

I mean I can check as well as you can

arrdem 2018-01-04T19:07:54.000705Z

no need if it was offhand

2018-01-04T19:08:09.000275Z

right, but if I misremembered it's helpful for me to see what the actual situation is

arrdem 2018-01-04T19:08:44.000259Z

(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))))))

2018-01-04T19:09:02.000486Z

+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"]}

2018-01-04T19:09:15.000150Z

so the value under the :test metadata contains the actual function to run

2018-01-04T19:09:20.000163Z

as I said

arrdem 2018-01-04T19:09:35.000371Z

Yeah okay interesting.

arrdem 2018-01-04T19:09:38.000043Z

TIL

2018-01-04T19:09:52.000487Z

that means you can easily add an inline test to any function you like, if that suits your fancy

arrdem 2018-01-04T19:10:17.000811Z

well… if you have a thing that either annotates vars after the fact or hacks defn

arrdem 2018-01-04T19:10:33.000292Z

but maybe that’s worth building

2018-01-04T19:10:45.000213Z

I mean - you can put ^{:test (fn ...)} in your function definition, as I was suggesting

2018-01-04T19:10:58.000411Z

or a macro that makes a nicer syntax for that of course

2018-01-04T19:11:32.000171Z

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

arrdem 2018-01-04T19:11:55.000049Z

gotcha