-
Notifications
You must be signed in to change notification settings - Fork 21
/
printer.cljc
76 lines (62 loc) · 2.81 KB
/
printer.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(ns matcher-combinators.printer
(:refer-clojure :exclude [print])
(:require [clojure.pprint :as pprint]
#?(:clj [matcher-combinators.model]
:cljs [matcher-combinators.model :refer [Mismatch
Missing
Unexpected
FailedPredicate
TypeMismatch
InvalidMatcherContext
InvalidMatcherType]])
[matcher-combinators.result :as result]
[matcher-combinators.ansi-color :as ansi-color])
#?(:clj
(:import [matcher_combinators.model Mismatch Missing Unexpected
TypeMismatch FailedPredicate InvalidMatcherContext InvalidMatcherType])))
(defrecord ColorTag [color expression])
(defmulti markup-expression type)
(defmethod markup-expression Mismatch [mismatch]
(list 'mismatch
(->ColorTag :yellow (:expected mismatch))
(->ColorTag :red (:actual mismatch))))
(defmethod markup-expression Missing [missing]
(list 'missing (->ColorTag :red (:expected missing))))
(defmethod markup-expression Unexpected [unexpected]
(list 'unexpected (->ColorTag :red (:actual unexpected))))
(defmethod markup-expression TypeMismatch [mismatch]
(list 'mismatch
(->ColorTag :yellow (type (:expected mismatch)))
(->ColorTag :red (type (:actual mismatch)))))
(defmethod markup-expression FailedPredicate [failed-predicate]
(list 'predicate
(->ColorTag :yellow (:form failed-predicate))
(->ColorTag :red (:actual failed-predicate))))
(defmethod markup-expression InvalidMatcherType [invalid-type]
(list 'invalid-matcher-input
(->ColorTag :yellow (:expected-type-msg invalid-type))
(->ColorTag :red (:provided invalid-type))))
(defmethod markup-expression InvalidMatcherContext [invalid-context]
(list 'invalid-matcher-context
(->ColorTag :red (:message invalid-context))))
(defmethod markup-expression :default [expression]
expression)
(defn colorized-print [{:keys [color expression]}]
(if color
#?(:clj (do (ansi-color/set-color color)
(pprint/write-out expression)
(ansi-color/reset))
:cljs (pprint/write-out (ansi-color/style expression color)))
(pprint/write-out expression)))
(defn print-diff-dispatch [expression]
(let [markup (markup-expression expression)]
(if (instance? ColorTag markup)
(colorized-print markup)
(pprint/simple-dispatch markup))))
(defn pretty-print [expr]
(pprint/with-pprint-dispatch
print-diff-dispatch
(pprint/pprint expr)))
(defn as-string [value]
(with-out-str
(pretty-print value)))