/
result.go
360 lines (306 loc) · 6.63 KB
/
result.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package detest
import (
"fmt"
"os"
"reflect"
"strconv"
"github.com/houseabsolute/detest/pkg/detest/internal/ansi"
"github.com/houseabsolute/detest/pkg/detest/internal/term"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/mattn/go-runewidth"
)
// We wrap values in a struct so that we can use a nil *value to indicate that
// the value wasn't present, as opposed to a nil *value.value, which is a nil
// _value_.
type value struct {
value interface{}
desc string
}
type result struct {
actual *value
expect *value
op string
pass bool
path []Path
where failure
description string
}
func newValue(val interface{}) *value {
return &value{value: val}
}
func (r result) hasPath() bool {
return len(r.path) != 0
}
func (r result) showActual() bool {
return r.actual != nil
}
func (r result) showExpect() bool {
return r.expect != nil
}
type describer struct {
r result
tw table.Writer
s ansi.Scheme
}
func (r result) describe(name string, s ansi.Scheme) string {
tw := tableWithTitle(fmt.Sprintf("Assertion not ok: %s", name), s)
return describer{r, tw, s}.table()
}
func (d describer) table() string {
d.addHeaders()
footer, widths := d.footer()
rowLen := len(footer)
body := []table.Row{}
for _, p := range d.r.path {
row := table.Row{p.data}
w := displayWidth(p.data)
if w > widths["PATH"] {
widths["PATH"] = w
}
for i := 0; i < rowLen-2; i++ {
row = append(row, "")
}
called := p.CalledAt()
row = append(row, called)
w = displayWidth(called)
if w > widths["CALLER"] {
widths["CALLER"] = w
}
body = append(body, row)
}
for _, b := range body {
d.tw.AppendRow(b, table.RowConfig{AutoMerge: true})
}
d.tw.AppendFooter(footer)
cc := columnConfigs(widths)
if cc != nil {
d.tw.SetColumnConfigs(cc)
}
var post string
if d.r.description != "" {
post = d.s.Strong(d.s.Incorrect(d.r.description)) + "\n"
}
return d.tw.Render() + "\n" + post
}
func (d describer) addHeaders() {
header := table.Row{}
if d.r.hasPath() {
header = append(header, "PATH")
}
if d.r.showActual() {
header = append(header, "GOT")
}
if d.r.op != "" {
header = append(header, "OP")
}
if d.r.showExpect() {
header = append(header, "EXPECT")
}
if d.r.hasPath() {
header = append(header, "CALLER")
}
d.tw.AppendHeader(header, table.RowConfig{AutoMerge: true})
}
func (d describer) footer() ([]interface{}, map[string]int) {
widths := map[string]int{"PATH": 0, "CALLER": 0}
var actual, expect, op string
if d.r.showActual() {
actual = fmt.Sprintf("%v", d.r.actual.value)
widths["GOT"] = displayWidth(actual)
}
if d.r.showExpect() {
expect = fmt.Sprintf("%v", d.r.expect.value)
widths["ACTUAL"] = displayWidth(actual)
}
op = d.r.op
var aType, eType string
if d.r.showActual() {
aType = d.r.actual.description()
w := displayWidth(aType)
if w > widths["GOT"] {
widths["GOT"] = w
}
}
if d.r.showExpect() {
eType = d.r.expect.description()
w := displayWidth(eType)
if w > widths["ACTUAL"] {
widths["ACTUAL"] = w
}
}
switch d.r.where {
case inType:
aType = d.s.Incorrect(aType)
eType = d.s.Correct(eType)
case inValue:
actual = d.s.Incorrect(actual)
expect = d.s.Correct(expect)
case inDataStructure:
op = d.s.Incorrect(op)
}
footer := table.Row{}
if d.r.hasPath() {
footer = append(footer, "")
}
if d.r.showActual() {
footer = append(footer, d.s.Em(aType)+"\n"+actual)
}
if op != "" {
// The extra space is required to make go-pretty render the right
// border for the first line of this cell.
footer = append(footer, " \n"+op)
widths["OP"] = displayWidth(op)
}
if d.r.showExpect() {
footer = append(footer, d.s.Em(eType)+"\n"+expect)
}
if d.r.hasPath() {
footer = append(footer, "")
}
return footer, widths
}
func (v *value) description() string {
if v.desc != "" {
return v.desc
}
v.desc = describeTypeOfActualValue(v.value)
if v.value == nil {
v.desc += " <nil>"
}
return v.desc
}
func describeTypeOfActualValue(val interface{}) string {
return describeType(reflect.TypeOf(val))
}
func describeTypeOfReflectValue(v reflect.Value) string {
if !v.IsValid() {
return "nil"
}
return describeType(v.Type())
}
func describeType(ty reflect.Type) string {
if ty == nil {
return "nil"
}
// This is only true for built-in types like string, float64, etc. If it's
// not composite or it's not a built-in then the name doesn't match the
// kind.
if ty.Kind().String() == ty.Name() {
return ty.Name()
}
if ty.Name() != "" {
return ty.Name()
}
switch ty.Kind() {
case reflect.Array:
return fmt.Sprintf("[%d]", ty.Len()) + describeType(ty.Elem())
case reflect.Chan:
return fmt.Sprintf("chan(%s)", describeType(ty.Elem()))
case reflect.Func:
return describeFunc(ty)
case reflect.Interface:
// Can this happen?
case reflect.Map:
return fmt.Sprintf("map[%s]%s", describeType(ty.Key()), describeType(ty.Elem()))
case reflect.Ptr:
return "*" + describeType(ty.Elem())
case reflect.Slice:
return "[]" + describeType(ty.Elem())
case reflect.Struct:
return describeStruct(ty)
case reflect.UnsafePointer:
return "*<unsafe>"
}
// wtf - should not get here
return ""
}
func describeFunc(ty reflect.Type) string {
desc := "func "
if name := ty.Name(); name != "" {
desc = desc + name + " "
}
desc += "("
for i := 0; i < ty.NumIn(); i++ {
desc += describeType(ty.In(i))
}
if ty.IsVariadic() {
desc += "..."
}
desc += ") "
if ty.NumOut() > 1 {
desc += "("
}
for i := 0; i < ty.NumOut(); i++ {
desc += describeType(ty.Out(i))
}
if ty.NumOut() > 1 {
desc += ")"
}
return desc
}
func describeStruct(ty reflect.Type) string {
if ty.Name() != "" {
return ty.Name()
}
return "<anon struct>"
}
func displayWidth(content string) int {
return runewidth.StringWidth(ansi.Strip(content))
}
func columnConfigs(widths map[string]int) []table.ColumnConfig {
var total int
for _, w := range widths {
total += w
// 2 for padding, 1 for separator
total += 3
}
// Left most border
total++
w := termWidth()
if total <= w {
return nil
}
for total > w {
diff := total - w
if diff < 10 {
widths["CALLER"] -= diff
break
}
widths["CALLER"] -= 10
total -= 10
if total < w {
break
}
widths["PATH"] -= 5
total -= 5
if total < w {
break
}
}
var configs []table.ColumnConfig
for k, v := range widths {
configs = append(
configs,
table.ColumnConfig{
Name: k,
WidthMax: v,
},
)
}
return configs
}
const defaultWidth = 100
func termWidth() int {
w := term.Width()
if w != 0 {
return w
}
col := os.Getenv("COLUMNS")
if col != "" {
w, err := strconv.Atoi(col)
if err != nil && w > 0 {
return w
}
}
return defaultWidth
}