-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
test_report.cljs
234 lines (200 loc) · 6.85 KB
/
test_report.cljs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(ns portal.ui.viewer.test-report
(:require [clojure.spec.alpha :as s]
[portal.colors :as c]
[portal.ui.icons :as icons]
[portal.ui.inspector :as ins]
[portal.ui.select :as select]
[portal.ui.styled :as d]
[portal.ui.theme :as theme]))
;;; :spec
(s/def :test-run/type #{:end-run-tests})
(s/def ::test-run
(s/keys :req-un [:test-run/type]))
(s/def :test-ns/type #{:begin-test-ns :end-test-ns})
(s/def ::test-ns
(s/keys :req-un [:test-ns/type ::ns]))
(s/def :test-var/type #{:begin-test-var :end-test-var})
(s/def ::test-var
(s/keys :req-un [:test-var/type ::var]))
(s/def :assert/type #{:pass :fail :error})
(s/def ::assert
(s/keys :req-un [:assert/type]
:opt-un [::file ::line ::actual ::expected ::message]))
(s/def :summary/type #{:summary})
(s/def ::pass number?)
(s/def ::fail number?)
(s/def ::error number?)
(s/def ::test number?)
(s/def ::summary
(s/keys :req-un [:summary/type ::pass ::fail ::error ::test]))
(s/def ::output
(s/or :run ::test-run
:ns ::test-ns
:var ::test-var
:assert ::assert
:summary ::summary))
(s/def ::report
(s/coll-of ::output :min-count 1))
;;;
(defn- label [{:keys [message expected var ns type] :as value}]
[d/div
{:style {:flex "1"}}
[ins/with-collection
value
[select/with-position
{:row -1 :column 0}
(cond
(coll? message)
[ins/with-key
:message
[ins/inspector
(cond-> message
(coll? message)
(with-meta
{:portal.viewer/default
:portal.viewer/pr-str}))]]
message
[ins/with-key :message [ins/inspector message]]
expected
[ins/with-key
:expected
[ins/inspector
(cond-> expected
(coll? expected)
(with-meta
{:portal.viewer/default
:portal.viewer/pr-str}))]]
var
[ins/with-key :var [ins/inspector var]]
ns
[ins/with-default-viewer
:portal.viewer/pr-str
[ins/with-key :ns [ins/inspector ns]]]
type
[ins/with-default-viewer
:portal.viewer/pr-str
[ins/with-key :type [ins/inspector type]]])]]])
(defn- inspect-assertion [value]
(let [theme (theme/use-theme)
options (ins/use-options)
expanded? (:expanded? options)
background (ins/get-background)
color (case (:type value)
:pass (::c/diff-add theme)
(:error :fail) (::c/diff-remove theme)
(::c/border theme))]
[d/div
[d/div
{:style
{:display :flex
:flex-direction :row
:align-items :stretch
:background background}}
[d/div
{:style
{:padding (:padding theme)
:background color
:border-top [1 :solid color]
:border-bottom [1 :solid color]
:border-right [1 :solid color]
:border-top-left-radius (:border-radius theme)
:border-bottom-left-radius (when-not expanded? (:border-radius theme))}}
[(case (:type value)
:pass icons/check-circle
:fail icons/times-circle
:error icons/times-circle
:begin-test-ns icons/play-circle
:end-test-ns icons/stop-circle
:begin-test-var icons/play-circle
:end-test-var icons/stop-circle
icons/info-circle)
{:style {:color background}}]]
[d/div
{:style
{:flex "1"
:display :flex
:gap (:padding theme)
:align-items :center
:padding (:padding theme)
:border-top [1 :solid (::c/border theme)]
:border-bottom (when-not expanded? [1 :solid (::c/border theme)])
:border-right [1 :solid (::c/border theme)]
:border-top-right-radius (:border-radius theme)
:border-bottom-right-radius (when-not expanded? (:border-radius theme))}}
[ins/toggle-expand {:padding-left (:padding theme)}]
[label value]
(when-let [file (:file value)]
[d/div
{:style {:color (::c/uri theme)}}
file
(when-let [line (:line value)]
[:<> ":" line])])]]
(when (:expanded? options)
[ins/inspect-map-k-v (dissoc value :type :message :file :line :ns :var)])]))
(def ^:private begin? #{:begin-test-ns :begin-test-var})
(defn- summary [rows]
(let [counts (frequencies (map :type rows))]
(assoc counts :type (if (or (:error counts) (:fail counts))
:fail
:pass))))
(def ^:private empty-vec ^{:portal.viewer/default :portal.viewer/inspector} [])
(defn- get-results [value]
(:results
(reduce
(fn [{:keys [test-ns test-var] :as state} row]
(case (:type row)
:summary state
:begin-test-ns (assoc state :test-ns (:ns row))
:end-test-ns (-> state
(dissoc :test-ns :vars)
(update :results
(fnil conj empty-vec)
(merge
(summary (:vars state))
{:ns test-ns
:vars (:vars state)})))
:begin-test-var (assoc state :test-var (:var row))
:end-test-var (-> state
(dissoc :test-var :asserts)
(update :vars
(fnil conj empty-vec)
(merge
(summary (:asserts state))
{:ns test-ns
:var test-var
:asserts (:asserts state)})))
(update
state
:asserts
(fnil conj empty-vec)
(cond-> row
test-ns (assoc :ns test-ns)
test-var (assoc :var test-var)))))
{:results empty-vec}
value)))
(defn- group-assertions [value]
(let [results (get-results value)]
(if (= 1 (count results))
(first results)
(merge
(summary results)
{:message :test-report
:results results}))))
(defn- inspect-test-report [value]
[inspect-assertion (group-assertions value)])
(defn get-component [value]
(cond
(and (s/valid? ::report value)
(begin? (:type (first value))))
inspect-test-report
(and (s/valid? ::output value)
(not= (:type value) :summary))
inspect-assertion))
(defn inspect-test [value]
(let [component (get-component value)]
[component value]))
(def viewer
{:predicate get-component
:component inspect-test
:name :portal.viewer/test-report
:doc "View clojure.test report output."})