-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
test_report.cljs
196 lines (169 loc) · 6 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
(ns portal.ui.viewer.test-report
(:require [clojure.spec.alpha :as sp]
[portal.colors :as c]
[portal.ui.icons :as icons]
[portal.ui.inspector :as ins]
[portal.ui.select :as select]
[portal.ui.styled :as s]
[portal.ui.theme :as theme]))
(sp/def :test-ns/type #{:begin-test-ns :end-test-ns})
(sp/def ::test-ns
(sp/keys :req-un [:test-ns/type ::ns]))
(sp/def :test-var/type #{:begin-test-var :end-test-var})
(sp/def ::test-var
(sp/keys :req-un [:test-var/type ::var]))
(sp/def :assert/type #{:pass :fail :error})
(sp/def ::assert
(sp/keys :req-un [:assert/type]
:opt-un [::file ::line ::actual ::expected ::message]))
(sp/def :summary/type #{:summary})
(sp/def ::pass number?)
(sp/def ::fail number?)
(sp/def ::error number?)
(sp/def ::test number?)
(sp/def ::summary
(sp/keys :req-un [:summary/type ::pass ::fail ::error ::test]))
(sp/def ::output
(sp/or :ns ::test-ns
:var ::test-var
:assert ::assert
:summary ::summary))
(sp/def ::report
(sp/coll-of ::output :min-count 1))
(defn- label [value]
[ins/with-key
:label
[select/with-position
{:row -1 :column 0}
[ins/inspector
(or (:message value)
(when-let [expected (:expected value)]
(with-meta
expected
{:portal.viewer/default
:portal.viewer/pr-str}))
(:var value)
(:ns value))]]])
(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))]
[s/div
[s/div
{:style
{:display :flex
:flex-direction :row
:align-items :stretch
:background background}}
[s/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
:summary icons/info-circle
:<>)
{:style {:color background}}]]
[s/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))}}
[label value]
(when-let [file (:file value)]
[s/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 (sp/valid? ::report value)
(begin? (:type (first value))))
inspect-test-report
(and (sp/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})