Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify test var execution, cleanups #13

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions dev/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
114 changes: 0 additions & 114 deletions src/caioaao/kaocha_greenlight/report.clj

This file was deleted.

25 changes: 13 additions & 12 deletions src/caioaao/kaocha_greenlight/test.clj
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 22 additions & 15 deletions src/caioaao/kaocha_greenlight/test/ns.clj
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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)
69 changes: 33 additions & 36 deletions src/caioaao/kaocha_greenlight/test/var.clj
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 8 additions & 7 deletions test/caioaao/kaocha_greenlight/error_test.clj
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
11 changes: 6 additions & 5 deletions test/caioaao/kaocha_greenlight/scope_test.clj
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
7 changes: 4 additions & 3 deletions test/caioaao/kaocha_greenlight/test_suite/blue_test.clj
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Loading