Skip to content

liquidz/testdoc

Repository files navigation

testdoc

GitHub Actions for test workflow GitHub Actions for babashka test workflow GitHub Actions for lint workflow Clojars Project

Yet another doctest implementation in Clojure

Usage

testdoc extends clojure.test/is macro.

REPL style

=> (require '[clojure.test :as t]
=>          'testdoc.core)
nil

=> (defn myplus
=>   "Add a and b
=>
=>   => (myplus 1 2)
=>   3
=>   => (myplus 2
=>   =>         3)
=>   5"
=>   [a b]
=>   (+ a b))
var?

=> (t/deftest myplus-test
=>   (t/is (testdoc #'myplus)))
var?

=> (t/test-var *1)
nil

;; Other examples
=> (hash-map :multiple "lines")
{:multiple
 "lines"}

Code-first style

(defn mymulti
  "Multiply a and b

  (mymulti 1 2)
  ;; => 2
  (mymulti 2
           3)
  ;; => 6"
  [a b]
  (* a b))
;; => var?

(t/deftest mymulti-test
  (t/is (testdoc #'mymulti)))
;; => var?

(t/test-var *1)
;; => nil

;; Other examples
(hash-map :multiple "lines")
;; => {:multiple
;; =>  "lines"}

Testing external documents

This document is tested by this library, of course!

(require '[clojure.java.io :as io])
;; => nil

(t/deftest external-document-test
  (t/is (testdoc (slurp (io/file "test/resources/README.adoc")))))
;; => var?

(t/test-var *1)
;; => nil

Test error

testdoc will add a line number information to a error message text.

For example, you have a test code like below:

(t/deftest error-test
  (t/is (testdoc "=> (unresolved-symbol)
                  :failure")))

Then, lein test will show you errors like below:

(= (unresolved-symbol) :failure), [line: 1]

clj-kondo

When you see Unresolved symbol: testdoc error with clj-kondo, you can import a config file for this library by the following command.

clj-kondo --copy-configs --dependencies --lint "$(shell clojure -A:dev -Spath)"
# .clj-kondo/com.github.liquidz/testdoc/config.edn should be created

Other works

License

Copyright © 2018-2022 Masashi Iizuka

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.