diff --git a/dev/user.clj b/dev/user.clj index 134576f..d05999e 100644 --- a/dev/user.clj +++ b/dev/user.clj @@ -7,17 +7,10 @@ ;; Sets up the expound printer for human-readable spec error messages. kaocha ;; requires this to be configured for generative fdef testing. - -;; Alter the root *explain-out* binding... +;; Alter the root *explain-out* binding. (alter-var-root #'s/*explain-out* (constantly expound/printer)) -(try - ;; ...and attempt to set *explain-out*, assuming that we're inside of - ;; a binding context, which is necessary for some REPL configurations... - (set! s/*explain-out* expound/printer) - - (catch IllegalStateException _ - ;; ...however, we might not be inside a binding context. If not, Clojure - ;; will throw an IllegalStateException, which is why we're catching it and - ;; ignoring it here. -)) +;; Set *explain-out* if it's thread-bound, which is necessary for some REPL +;; configurations. +(when (thread-bound? #'s/*explain-out*) + (set! s/*explain-out* expound/printer)) diff --git a/project.clj b/project.clj index ec1cb67..acddc43 100644 --- a/project.clj +++ b/project.clj @@ -4,7 +4,7 @@ :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :url "https://www.eclipse.org/legal/epl-2.0/"} :dependencies [[org.clojure/clojure "1.10.1"] - [amperity/greenlight "0.5.0"] + [amperity/greenlight "0.6.1"] [lambdaisland/kaocha "1.0.672"]] :aliases {"test" ["run" "-m" "kaocha.runner"]} :profiles {:dev {:dependencies [[nubank/matcher-combinators "0.4.2"] diff --git a/src/caioaao/kaocha_greenlight/report.clj b/src/caioaao/kaocha_greenlight/report.clj deleted file mode 100644 index 2e7f0e7..0000000 --- a/src/caioaao/kaocha_greenlight/report.clj +++ /dev/null @@ -1,114 +0,0 @@ -(ns caioaao.kaocha-greenlight.report - (:require [clojure.test :as ctest] - [greenlight.report] - [clojure.string :as str] - [greenlight.test :as test] - [greenlight.step :as step])) - -(def ^:private sgr-code - "Map of symbols to numeric SGR (select graphic rendition) codes." - {:none 0 - :bold 1 - :underline 3 - :black 30 - :red 31 - :green 32 - :yellow 33 - :blue 34 - :magenta 35 - :cyan 36 - :white 37}) - -(defn- sgr - "Returns an ANSI escope string which will apply the given collection of SGR - codes." - [codes] - (let [codes (map sgr-code codes codes) - codes (str/join \; codes)] - (str \u001b \[ codes \m))) - -(defn- color - [codes string] - (let [codes (if (coll? codes) codes [codes])] - (str (sgr codes) string (sgr [:none])))) - -(defn- state-color - "Return the color keyword for the given outcome state." - [outcome] - (case outcome - :pass :green - :fail :red - :error :red - :timeout :yellow - :magenta)) - -(defmethod ctest/report :test-start - [event] - (let [test-case (:test event)] - (printf "\n %s Testing %s\n" - (color :magenta "*") - (color [:bold :magenta] (::test/title test-case))) - (when (::test/ns test-case) - (printf " %s %s:%s\n" - (color :magenta "|") - (::test/ns test-case) - (::test/line test-case -1))) - (when-let [desc (::test/description test-case)] - (printf " %s %s\n" - (color :magenta "|") - (color :yellow (::test/description test-case)))) - (printf " %s\n" (color :magenta "|")))) - -(defmethod ctest/report :step-start - [event] - (let [step (:step event)] - (printf " %s %s\n" - (color :blue "+->>") - (::step/title step)))) - -(defmethod ctest/report :step-end - [event] - (let [result (:step event)] - (when-let [message (::step/message result)] - (printf " %s %s\n" - (color :blue "|") - (color (state-color (::step/outcome result)) - (::step/message result)))) - (printf " %s [%s] (%s seconds)\n" - (color :blue "|") - (let [outcome (::step/outcome result "???")] - (color - [:bold (state-color outcome)] - (str/upper-case (name outcome)))) - (color :cyan (format "%.3f" (::step/elapsed result)))) - (printf " %s\n" (color :blue "|")))) - -(defmethod ctest/report :cleanup-resource - [event] - (let [{:keys [resource-type parameters]} event] - (printf " %s Cleaning %s resource %s\n" - (color :yellow "+->>") - (color [:bold :yellow] resource-type) - (color :magenta (pr-str parameters))))) - -(defmethod ctest/report :cleanup-error - [event] - (let [{:keys [error resource-type parameters]} event] - (printf " %s Failed to cleanup %s resource %s: %s\n" - (color :yellow "|") - (color [:bold :yellow] resource-type) - (color :magenta (pr-str parameters)) - (color :red error)))) - -(defmethod ctest/report :test-end - [event] - (let [result (:test event) - outcome (::test/outcome result "---") - codes [:bold (state-color outcome)]] - (printf " %s\n" (color :blue "|")) - (printf " %s (%s seconds)\n" - (color codes (str "* " (str/upper-case (name outcome)))) - (color :cyan (format "%.3f" (test/elapsed result)))) - (when-let [message (::test/message result)] - (printf " %s\n" message)) - (newline))) diff --git a/src/caioaao/kaocha_greenlight/test.clj b/src/caioaao/kaocha_greenlight/test.clj index 56dbf80..e799175 100644 --- a/src/caioaao/kaocha_greenlight/test.clj +++ b/src/caioaao/kaocha_greenlight/test.clj @@ -1,22 +1,23 @@ (ns caioaao.kaocha-greenlight.test - (:require [clojure.spec.alpha :as s] - [kaocha.hierarchy :as hierarchy] - [kaocha.load :as load] - [kaocha.test-suite :as test-suite] - [kaocha.testable :as testable] - [kaocha.type.ns :as type.ns] - [caioaao.kaocha-greenlight.report] - [caioaao.kaocha-greenlight.test.ns] - [caioaao.kaocha-greenlight.runner :as runner])) - -(defn- resolve-system + (:require + [caioaao.kaocha-greenlight.runner :as runner] + [caioaao.kaocha-greenlight.test.ns] + [clojure.spec.alpha :as s] + [kaocha.hierarchy :as hierarchy] + [kaocha.load :as load] + [kaocha.test-suite :as test-suite] + [kaocha.testable :as testable] + [kaocha.type.ns :as type.ns])) + +(defn ^:private resolve-system [system-fn-symbol] (let [ns-name (symbol (namespace system-fn-symbol))] (when-not (find-ns ns-name) (require ns-name)) ((resolve system-fn-symbol)))) -(defn- ns->testable [ns] +(defn ^:private ns->testable + [ns] (assoc (type.ns/->testable ns) :kaocha.testable/type ::ns)) (defmethod testable/-load :caioaao.kaocha-greenlight/test diff --git a/src/caioaao/kaocha_greenlight/test/ns.clj b/src/caioaao/kaocha_greenlight/test/ns.clj index e847a58..9a6a8f4 100644 --- a/src/caioaao/kaocha_greenlight/test/ns.clj +++ b/src/caioaao/kaocha_greenlight/test/ns.clj @@ -1,22 +1,26 @@ (ns caioaao.kaocha-greenlight.test.ns - (:require [caioaao.kaocha-greenlight.runner :as runner] - [clojure.spec.alpha :as s] - [clojure.test :as t] - [kaocha.hierarchy :as hierarchy] - [kaocha.output :as output] - [kaocha.testable :as testable])) + (:require + [caioaao.kaocha-greenlight.runner :as runner] + [clojure.spec.alpha :as s] + [clojure.test :as t] + [kaocha.hierarchy :as hierarchy] + [kaocha.output :as output] + [kaocha.testable :as testable])) -(defn- test-var->testable - [v] - {::testable/type :caioaao.kaocha-greenlight.test/var - ::testable/id (keyword (symbol v)) - :caioaao.kaocha-greenlight.test/test-var v}) +(defn ^:private test-var->testable + [test-var] + (let [sym (symbol test-var)] + {::testable/type :caioaao.kaocha-greenlight.test/var + ::testable/id (keyword sym) + :kaocha.var/name sym + :kaocha.var/desc (str sym) + :kaocha.var/var test-var})) -(defn- test-vars +(defn ^:private test-vars [ns] (->> ns ns-interns vals (filter (comp :greenlight.test/test meta)))) -(defn- run-testables +(defn ^:private run-testables [testable test-plan] (let [tests (:kaocha.test-plan/tests testable) results (testable/run-testables tests test-plan)] @@ -50,5 +54,8 @@ :ns (:kaocha.ns/ns testable)}) testable)))) -(s/def :caioaao.kaocha-greenlight.test/ns (s/keys :req [::testable/type ::testable/id :kaocha.ns/name])) -(hierarchy/derive! :caioaao.kaocha-greenlight.test/ns :kaocha.testable.type/group) +(s/def :caioaao.kaocha-greenlight.test/ns + (s/keys :req [::testable/type ::testable/id :kaocha.ns/name])) + +(hierarchy/derive! :caioaao.kaocha-greenlight.test/ns + :kaocha.testable.type/group) diff --git a/src/caioaao/kaocha_greenlight/test/var.clj b/src/caioaao/kaocha_greenlight/test/var.clj index bbc9d3f..b128140 100644 --- a/src/caioaao/kaocha_greenlight/test/var.clj +++ b/src/caioaao/kaocha_greenlight/test/var.clj @@ -1,43 +1,40 @@ (ns caioaao.kaocha-greenlight.test.var - (:require [caioaao.kaocha-greenlight.runner :as runner] - [clojure.spec.alpha :as s] - [clojure.test :as ctest] - [greenlight.step :as step] - [greenlight.test :as test] - [kaocha.hierarchy :as hierarchy] - [kaocha.testable :as testable])) + (:require + [caioaao.kaocha-greenlight.runner :as runner] + [clojure.spec.alpha :as s] + [clojure.test :as ctest] + [clojure.test :as t] + [greenlight.report :as report] + [greenlight.step :as step] + [greenlight.test :as test] + [kaocha.hierarchy :as hierarchy] + [kaocha.testable :as testable] + [kaocha.type.var])) -(defn test-results->kaocha - [rs] - {:kaocha.result/pass (:pass rs 0) - :kaocha.result/error (:error rs 0) - :kaocha.result/fail (:fail rs 0) - :kaocha.result/pending (:pending rs 0) - :kaocha.result/count 1}) - -(defn report - [event] - (when (= :step-end (:type event)) - (->> (:step event) - ::step/reports - (run! ctest/do-report))) - (ctest/do-report event)) +(defn ^:private run-test! + [testable test-plan] + (let [opts {:print-color (:kaocha/color? test-plan)} + report (partial report/handle-test-event opts) + system (:caioaao.kaocha-greenlight.test/system test-plan) + test-var (:kaocha.var/var testable)] + (binding [test/*report* report] + (test/run-test! system (test-var))))) (defmethod testable/-run :caioaao.kaocha-greenlight.test/var [testable test-plan] - (runner/run testable - test-plan - :var - (fn [{:caioaao.kaocha-greenlight.test/keys [test-var] :as testable} - {:caioaao.kaocha-greenlight.test/keys [system]}] - (ctest/do-report {:type :begin-test-var, :var test-var}) - (binding [ctest/*report-counters* (ref ctest/*initial-report-counters*) - test/*report* report] - (test/run-test! system (test-var)) - (ctest/do-report {:type :end-test-var, :var test-var}) - (merge testable (test-results->kaocha @ctest/*report-counters*)))))) + (runner/run + testable + test-plan + :var + (fn [testable test-plan] + (testable/-run + (assoc testable + ::testable/type :kaocha.type/var + :kaocha.var/test (partial run-test! testable test-plan)) + test-plan)))) -(s/def :caioaao.kaocha-greenlight.test/test-var var?) -(s/def :caioaao.kaocha-greenlight.test/var (s/keys :req [::testable/type :caioaao.kaocha-greenlight.test/test-var])) +(s/def :caioaao.kaocha-greenlight.test/var + (s/keys :req [::testable/type :kaocha.var/var])) -(hierarchy/derive! :caioaao.kaocha-greenlight.test/var :kaocha.testable.type/leaf) +(hierarchy/derive! :caioaao.kaocha-greenlight.test/var + :kaocha.testable.type/leaf) diff --git a/test/caioaao/kaocha_greenlight/error_test.clj b/test/caioaao/kaocha_greenlight/error_test.clj index 982873b..be661c1 100644 --- a/test/caioaao/kaocha_greenlight/error_test.clj +++ b/test/caioaao/kaocha_greenlight/error_test.clj @@ -1,11 +1,12 @@ (ns caioaao.kaocha-greenlight.error-test - (:require [clojure.test :refer [deftest testing is]] - [com.stuartsierra.component :as component] - [kaocha.api :as api] - [kaocha.result :as result] - [matcher-combinators.test] - [matcher-combinators.matchers :as matchers] - [matcher-combinators.parser :refer [mimic-matcher]])) + (:require + [clojure.test :refer [deftest testing is]] + [com.stuartsierra.component :as component] + [kaocha.api :as api] + [kaocha.result :as result] + [matcher-combinators.matchers :as matchers] + [matcher-combinators.parser :refer [mimic-matcher]] + [matcher-combinators.test])) (mimic-matcher matchers/equals clojure.lang.Var) diff --git a/test/caioaao/kaocha_greenlight/scope_test.clj b/test/caioaao/kaocha_greenlight/scope_test.clj index 0271fed..3b9b984 100644 --- a/test/caioaao/kaocha_greenlight/scope_test.clj +++ b/test/caioaao/kaocha_greenlight/scope_test.clj @@ -1,9 +1,10 @@ (ns caioaao.kaocha-greenlight.scope-test - (:require [clojure.test :refer [deftest testing is]] - [com.stuartsierra.component :as component] - [kaocha.api :as api] - [matcher-combinators.matchers :as matchers] - [matcher-combinators.parser :refer [mimic-matcher]])) + (:require + [clojure.test :refer [deftest testing is]] + [com.stuartsierra.component :as component] + [kaocha.api :as api] + [matcher-combinators.matchers :as matchers] + [matcher-combinators.parser :refer [mimic-matcher]])) (mimic-matcher matchers/equals clojure.lang.Var) diff --git a/test/caioaao/kaocha_greenlight/test_suite/blue_test.clj b/test/caioaao/kaocha_greenlight/test_suite/blue_test.clj index 5da1ba7..9f013cf 100644 --- a/test/caioaao/kaocha_greenlight/test_suite/blue_test.clj +++ b/test/caioaao/kaocha_greenlight/test_suite/blue_test.clj @@ -1,7 +1,8 @@ (ns caioaao.kaocha-greenlight.test-suite.blue-test - (:require [greenlight.step :as step :refer [defstep]] - [clojure.test :refer [is]] - [greenlight.test :as test :refer [deftest]])) + (:require + [clojure.test :refer [is]] + [greenlight.step :as step :refer [defstep]] + [greenlight.test :as test :refer [deftest]])) (defstep sample-step "A sample greenlight test step in the blue test suite" diff --git a/test/caioaao/kaocha_greenlight/type_test.clj b/test/caioaao/kaocha_greenlight/type_test.clj index aac19ef..983d329 100644 --- a/test/caioaao/kaocha_greenlight/type_test.clj +++ b/test/caioaao/kaocha_greenlight/type_test.clj @@ -1,14 +1,15 @@ (ns caioaao.kaocha-greenlight.type-test - (:require [clojure.test :refer [deftest is testing]] - [com.stuartsierra.component :as component] - [kaocha.testable :as testable] - [kaocha.api :as api] - [kaocha.result :as result] - [matcher-combinators.test] - [matcher-combinators.parser :refer [mimic-matcher]] - [caioaao.kaocha-greenlight.test-suite.blue-test] - [caioaao.kaocha-greenlight.test-suite.red-test] - [matcher-combinators.matchers :as matchers])) + (:require + [caioaao.kaocha-greenlight.test-suite.blue-test] + [caioaao.kaocha-greenlight.test-suite.red-test] + [clojure.test :refer [deftest is testing]] + [com.stuartsierra.component :as component] + [kaocha.api :as api] + [kaocha.result :as result] + [kaocha.testable :as testable] + [matcher-combinators.matchers :as matchers] + [matcher-combinators.parser :refer [mimic-matcher]] + [matcher-combinators.test])) (mimic-matcher matchers/equals clojure.lang.Var) @@ -54,17 +55,17 @@ :kaocha.testable/id :caioaao.kaocha-greenlight.test-suite.blue-test :kaocha.testable/desc "caioaao.kaocha-greenlight.test-suite.blue-test" :kaocha.ns/name 'caioaao.kaocha-greenlight.test-suite.blue-test - :kaocha.test-plan/tests [{:kaocha.testable/type :caioaao.kaocha-greenlight.test/var - :kaocha.testable/id :caioaao.kaocha-greenlight.test-suite.blue-test/sample-test - :caioaao.kaocha-greenlight.test/test-var #'caioaao.kaocha-greenlight.test-suite.blue-test/sample-test}]}]} + :kaocha.test-plan/tests [{:kaocha.testable/type :caioaao.kaocha-greenlight.test/var + :kaocha.testable/id :caioaao.kaocha-greenlight.test-suite.blue-test/sample-test + :kaocha.var/var #'caioaao.kaocha-greenlight.test-suite.blue-test/sample-test}]}]} (testable/load test-suite-blue))) (is (match? {:kaocha.test-plan/tests [{:kaocha.testable/type :caioaao.kaocha-greenlight.test/ns :kaocha.testable/id :caioaao.kaocha-greenlight.test-suite.red-test :kaocha.testable/desc "caioaao.kaocha-greenlight.test-suite.red-test" :kaocha.ns/name 'caioaao.kaocha-greenlight.test-suite.red-test - :kaocha.test-plan/tests [{:kaocha.testable/type :caioaao.kaocha-greenlight.test/var - :kaocha.testable/id :caioaao.kaocha-greenlight.test-suite.red-test/sample-test - :caioaao.kaocha-greenlight.test/test-var #'caioaao.kaocha-greenlight.test-suite.red-test/sample-test}]}]} + :kaocha.test-plan/tests [{:kaocha.testable/type :caioaao.kaocha-greenlight.test/var + :kaocha.testable/id :caioaao.kaocha-greenlight.test-suite.red-test/sample-test + :kaocha.var/var #'caioaao.kaocha-greenlight.test-suite.red-test/sample-test}]}]} (testable/load test-suite-red)))) (testing "description is correct" diff --git a/tests.edn b/tests.edn index 48c35f8..7ff9231 100644 --- a/tests.edn +++ b/tests.edn @@ -1,9 +1,11 @@ #kaocha/v1 - {:plugins [:kaocha.plugin/profiling - :kaocha.plugin/orchestra - :kaocha.plugin.alpha/spec-test-check] +{:plugins [:kaocha.plugin/profiling + :kaocha.plugin/orchestra + :kaocha.plugin.alpha/spec-test-check] - :reporter kaocha.report/documentation + :reporter kaocha.report/documentation - :clojure.spec.test.check/instrument? true - :clojure.spec.test.check/check-asserts? true} + :kaocha.plugin.capture-output/capture-output? false + + :clojure.spec.test.check/instrument? true + :clojure.spec.test.check/check-asserts? true}