Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created September 14, 2017 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/6ca4c4e5a3396708e7462fcb88105ff9 to your computer and use it in GitHub Desktop.
Save cgrand/6ca4c4e5a3396708e7462fcb88105ff9 to your computer and use it in GitHub Desktop.
Documenting specs inline
(defmacro doc
"Returns a spec which acts in all points like the provided spec,
expect for describe/form where the docstring appears."
[docstring spec]
`(doc-impl ~docstring '~spec (delay (spec/spec ~spec))))
(defn doc-impl [docstring form delayed-spec]
(reify spec/Spec
(conform* [_ x] (spec/conform* @delayed-spec x))
(unform* [_ y] (spec/unform* @delayed-spec y))
(explain* [_ path via in x] (spec/explain* @delayed-spec path via in x))
(gen* [_ overrides path rmap] (spec/gen* @delayed-spec overrides path rmap))
(with-gen* [_ gfn] (spec/with-gen* @delayed-spec gfn))
(describe* [_]
`(doc ~docstring ~(if (keyword? form) form (spec/describe* @delayed-spec))))))
@gphilipp
Copy link

Expect or except ?

@gphilipp
Copy link

Christophe, can you give an example of usage with result ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment