/
summary.go
143 lines (122 loc) · 3.47 KB
/
summary.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
// Copyright (c) 2019, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package ctxerr
import (
"strings"
"github.com/maxatome/go-testdeep/internal/color"
"github.com/maxatome/go-testdeep/internal/util"
)
// ErrorSummary is the interface used to render error summaries. See
// Error.Summary.
type ErrorSummary interface {
AppendSummary(buf *strings.Builder, prefix string, colorized bool)
}
// ErrorSummaryItem implements the [ErrorSummary] interface and allows
// to render a labeled value.
//
// With explanation set:
//
// Label: value
// Explanation
//
// With an empty explantion:
//
// Label: value
type ErrorSummaryItem struct {
Label string
Value string
Explanation string
}
var _ ErrorSummary = ErrorSummaryItem{}
// AppendSummary implements the [ErrorSummary] interface.
func (s ErrorSummaryItem) AppendSummary(buf *strings.Builder, prefix string, colorized bool) {
buf.WriteString(prefix)
badOn, badOff := "", ""
if colorized {
color.Init()
badOn, badOff = color.BadOn, color.BadOff
buf.WriteString(color.BadOnBold)
}
buf.WriteString(s.Label)
buf.WriteString(": ")
util.IndentColorizeStringIn(buf, s.Value, prefix+strings.Repeat(" ", len(s.Label)+2), badOn, badOff)
if s.Explanation != "" {
buf.WriteByte('\n')
buf.WriteString(prefix)
util.IndentColorizeStringIn(buf, s.Explanation, prefix, badOn, badOff)
}
}
// ErrorSummaryItems implements the [ErrorSummary] interface and
// allows to render summaries with several labeled values. For example:
//
// Missing 6 items: the 6 items...
// Extra 2 items: the 2 items...
type ErrorSummaryItems []ErrorSummaryItem
var _ ErrorSummary = (ErrorSummaryItems)(nil)
// AppendSummary implements [ErrorSummary] interface.
func (s ErrorSummaryItems) AppendSummary(buf *strings.Builder, prefix string, colorized bool) {
maxLen := 0
for _, item := range s {
if len(item.Label) > maxLen {
maxLen = len(item.Label)
}
}
for idx, item := range s {
if idx > 0 {
buf.WriteByte('\n')
}
if len(item.Label) < maxLen {
item.Label = strings.Repeat(" ", maxLen-len(item.Label)) + item.Label
}
item.AppendSummary(buf, prefix, colorized)
}
}
type errorSummaryString string
var _ ErrorSummary = errorSummaryString("")
func (s errorSummaryString) AppendSummary(buf *strings.Builder, prefix string, colorized bool) {
badOn, badOff := "", ""
if colorized {
color.Init()
badOn, badOff = color.BadOn, color.BadOff
}
buf.WriteString(prefix)
util.IndentColorizeStringIn(buf, string(s), prefix, badOn, badOff)
}
// NewSummary returns an ErrorSummary composed by the simple string s.
func NewSummary(s string) ErrorSummary {
return errorSummaryString(s)
}
// NewSummaryReason returns an [ErrorSummary] meaning that the value got
// failed for an (optional) reason.
//
// With a given reason "it is not nil", the generated summary is:
//
// value: the_got_value
// it failed coz: it is not nil
//
// If reason is empty, the generated summary is:
//
// value: the_got_value
// it failed but didn't say why
func NewSummaryReason(got any, reason string) ErrorSummary {
if reason == "" {
return ErrorSummaryItem{
Label: " value", // keep 2 indent spaces
Value: util.ToString(got),
Explanation: "it failed but didn't say why",
}
}
return ErrorSummaryItems{
{
Label: "value",
Value: util.ToString(got),
},
{
Label: "it failed coz",
Value: reason,
},
}
}