Skip to content

Instantly share code, notes, and snippets.


danieroux/transcriptor.clj Secret

Last active Oct 24, 2019
What would you like to do?
(ns kaocha.type.transcriptor
(:require [kaocha.testable]
[clojure.spec.alpha :as s]
[clojure.test :as t]
[cognitect.transcriptor :as xr])
(:import [clojure.lang ExceptionInfo]
[ StringWriter]))
(def ^:dynamic *asserts*)
(defmacro check!
"Delegate to `transcriptor/check!` and on success, mark it as a passing assert.
This only exists for more granular reporting:
You could also just use `transcriptor/check!` directly, in which case one .repl file will
count as one test, with no assertions."
`(check! ~spec *1))
([spec v]
(xr/check! ~spec ~v)
(when (bound? #'*asserts*)
(swap! *asserts* inc))
(t/do-report {:type :pass}))))
(defmethod kaocha.testable/-load :kaocha.type/transcriptor [testable]
(let [test-paths (:kaocha/test-paths testable)
repl-files (mapcat xr/repl-files test-paths)]
(-> testable
(assoc :kaocha.testable/desc "Transcriptor Tests")
(assoc :kaocha.test-plan/tests
(fn [repl-file]
{:kaocha.testable/type :kaocha.type/transcriptor-repl-file
:kaocha.testable/id (keyword repl-file)
:kaocha.testable/desc repl-file
::repl-file repl-file})
(dissoc :kaocha/tests))))
(defmethod kaocha.testable/-run :kaocha.type/transcriptor [testable test-plan]
(t/do-report {:type ::begin-run})
(let [results (kaocha.testable/run-testables (:kaocha.test-plan/tests testable) test-plan)]
(t/do-report {:type ::end-run})
(-> testable
(assoc :kaocha.result/tests results)
(dissoc :kaocha.test-plan/tests))))
(defmethod kaocha.testable/-run :kaocha.type/transcriptor-repl-file [testable _test-plan]
(t/do-report {:type ::begin-repl-file})
(let [repl-file (::repl-file testable)
file-line-m (fn file-line-m [ex]
(zipmap [:file :line] (take-last 2 (:at (first (:via (Throwable->map ex)))))))
sw (StringWriter.)]
(binding [*out* sw
*asserts* (atom 0)]
(xr/run repl-file)
(t/do-report {:type ::end-repl-file})
(assoc testable
:kaocha.result/count 1
:kaocha.result/pass @*asserts*
:kaocha.result/fail 0
:koacha.result/out (str sw)))
(catch ExceptionInfo ex
(binding [kaocha.testable/*test-location* (file-line-m ex)]
(t/do-report {:type ::fail-repl-file}))
(assoc testable
:kaocha.result/count 1
:kaocha.result/pass @*asserts*
:kaocha.result/fail 1
:koacha.result/out (str sw)
:koacha.result/err (ex-message ex))))))
(s/def :kaocha.type/transcriptor (s/keys :req [:kaocha/test-paths]))
(s/def :kaocha.type/transcriptor-repl-file (s/keys :req [::repl-file]))
(kaocha.hierarchy/derive! :kaocha.type/transcriptor :kaocha.testable.type/suite)
(kaocha.hierarchy/derive! :kaocha.type/transcriptor-repl-file :kaocha.testable.type/leaf)
(kaocha.hierarchy/derive! ::begin-run :kaocha/begin-suite)
(kaocha.hierarchy/derive! ::end-run :kaocha/end-suite)
(kaocha.hierarchy/derive! ::begin-repl-file :kaocha/begin-test)
(kaocha.hierarchy/derive! ::fail-repl-file :kaocha/fail-type)
(kaocha.hierarchy/derive! ::end-repl-file :kaocha/end-test)
(require 'kaocha.specs)
(def a-testable {:kaocha.testable/type :kaocha.type/transcriptor
:kaocha.testable/id :transcriptor
:kaocha/source-paths ["src"]
:kaocha/test-paths ["src"]})
(s/valid? :kaocha/testable a-testable)
(use 'kaocha.repl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment