Skip to content

Commit

Permalink
ansi decorated test reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Nov 24, 2024
1 parent edde72b commit 395adab
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions test/todomvc/test_reporter.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
(ns todomvc.test-reporter
(:require [cljs.test]))

(defn- bold [text]
(str "\033[1m" text "\033[22m"))

(defn- gray [text]
(str "\033[90m" text "\033[39m"))

(defn- green [text]
(str "\033[32m" text "\033[22m"))

(defn- red [text]
(str "\033[31m" text "\033[39m"))

(def ^:private initial-state {:level 0
:seen-context nil})

Expand All @@ -15,17 +27,16 @@
:level (count contexts)
:seen-context contexts)))

(defn- report-test [m bullet]
(defn- report-test [m {:keys[color bullet bullet-color]}]
(let [seen-context (:seen-context @!state)
message (or (:message m) (pr-str (:expected m)))]
(set-state-from-env!)
(let [context (:seen-context @!state)]
(when-not (= seen-context
(:seen-context @!state))
(println (str (indent (:level @!state))
(first context)))))
(when (and context (not= seen-context context))
(print (str (indent (:level @!state))
(bold (first context))))))
(println (str (indent (inc (:level @!state)))
(str bullet " " message)))))
(str (bullet-color bullet) " " (color message))))))

(defmethod cljs.test/report [:cljs.test/default :begin-test-var] [_m]
(reset! !state initial-state))
Expand All @@ -35,15 +46,15 @@

(def ^:private original-pass (get-method cljs.test/report [:cljs.test/default :pass]))
(defmethod cljs.test/report [:cljs.test/default :pass] [m]
(report-test m "")
(report-test m {:color gray :bullet "" :bullet-color green})
(original-pass m))

(def ^:private original-fail (get-method cljs.test/report [:cljs.test/default :fail]))
(defmethod cljs.test/report [:cljs.test/default :fail] [m]
(report-test m "")
(report-test m {:color red :bullet "" :bullet-color red})
(original-fail m))

(def ^:private original-error (get-method cljs.test/report [:cljs.test/default :error]))
(defmethod cljs.test/report [:cljs.test/default :error] [m]
(report-test m "")
(original-error m))
(report-test m {:color red :bullet "" :bullet-color red})
(original-error m))

0 comments on commit 395adab

Please sign in to comment.