Ok, sorry for my rent above haha,
Now I have a question/idea.
I like to sometimes as part of my doc-string show some example usage. So I wondered, and I’m not sure it’ll work, but could there somehow be a reader-macro something like #rcf/doc ""
that would somehow return a doc-string so it works as one inside defn
, but also runs the tests when it is evaled?
(defn double
#rcf/doc
"Multiply the given number by two.
tests:
positive:
(double 2) := 4
negatives:
(double -2) := -4
edge-cases:
(double 0) := 0
(double 1) := 1
(double -1) := -1"
[num]
(* 2 num))
It would need to parse the string, but also I’m not sure how it could set itself up to run the tests, especially if like the function isn’t evaled yet at the time the reader macro expands… But if there was a way, I would find this awesome.
Edit:
I guess it could take a tuple instead, that might make it work with less trouble.
(defn double
#rcf/doc
("Multiply the given number by two."
(tests
"positive"
(double 2) := 4
"negatives"
(double -2) := -4
"edge-cases"
(double 0) := 0
(double 1) := 1
(double -1) := -1))
[num]
(* 2 num))
So #rcf/doc
would transform the tuple into a string and return that, so the doc-string would include the tests in it so you see them when you look up the doc.
But now at the REPL I can also just eval the (tests ...)
part which would run as a normal rcf tests macro.
And then it would need to somehow generate deftests at CI.
The only thing this wouldn’t do is run the tests in #rcf/doc when you load/eval the file or namespace. But I’d be okay with that compromise, unless there’s a way to get that working as well.
And I’m saying a reader-macro, because having a special defnrcf
and the like is quite intrusive and doesn’t combine well with other libs that have their own defn
like macro.