Skip to content

Instantly share code, notes, and snippets.

@kazk
Created March 24, 2023 21:15
Show Gist options
  • Save kazk/70e6dbcf1e74383f984fcce1c1d122ec to your computer and use it in GitHub Desktop.
Save kazk/70e6dbcf1e74383f984fcce1c1d122ec to your computer and use it in GitHub Desktop.
; TODO improve error output
(ns qualified.codewars-reporter
(:require [clojure.test :as t]
[clojure.string :as s]
[clojure.stacktrace :as stack]))
(defn- escape-lf [x] (s/replace x "\n" "<:LF:>"))
(defn- with-message [status {:keys [:message]}]
(if (string? message) (escape-lf message) status))
(defn- expr-str [expression]
(if (instance? Throwable expression)
(with-out-str
(stack/print-cause-trace expression t/*stack-trace-depth*))
(pr-str expression)))
(defn- expectations-str [{:keys [:expected :actual]}]
(escape-lf (str "expected: " (pr-str expected) "\n actual: " (expr-str actual))))
(defn- error-str [{:keys [:actual]}]
(escape-lf (expr-str actual)))
(defmulti ^:dynamic codewars-report :type)
; (defmethod codewars-report :default [data]
; (t/with-test-out
; (println (pr-str data))))
(defmethod codewars-report :pass [m]
(t/with-test-out
(println (str "\n<IT::>" (t/testing-contexts-str)))
; (println (t/testing-vars-str m))
; (println (pr-str m))
(t/inc-report-counter :pass)
; (print-context)
(println "\n<PASSED::>Test Passed")
(println "\n<COMPLETEDIN::>")))
(defmethod codewars-report :fail [m]
(t/with-test-out
; (println (pr-str m))
(println (str "\n<IT::>" (t/testing-contexts-str)))
(t/inc-report-counter :fail)
(println (str "\n<FAILED::>" (with-message "Test Failed" m) "<:LF:>" (expectations-str m)))
(println "\n<COMPLETEDIN::>")))
(defmethod codewars-report :error [m]
(t/with-test-out
(println (str "\n<IT::>" (t/testing-contexts-str)))
; (println (pr-str m))
(t/inc-report-counter :error)
; (print-context)
(println (str "\n<ERROR::>" (error-str m)))
(println "\n<COMPLETEDIN::>")))
; deftest
(defmethod codewars-report :begin-test-var [m]
(t/with-test-out
; (println (pr-str m))
(println (str "\n<DESCRIBE::>" (pr-str (-> m :var (. sym)))))))
(defmethod codewars-report :end-test-var [_]
(t/with-test-out
; (println (pr-str m))
(println "\n<COMPLETEDIN::>")))
; maybe useful when supporting multiple test files
(defmethod codewars-report :begin-test-ns [_])
(defmethod codewars-report :end-test-ns [_])
; don't print the summary at the end
(defmethod codewars-report :summary [_])
(defn run-tests [& namespaces]
(binding [t/report codewars-report]
(apply t/run-tests namespaces)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment