Debian Codegraph @

internal/ctxerr/summary.go

raw
1 // Copyright (c) 2019, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package ctxerr
8
9 import (
10 "strings"
11
12 "github.com/maxatome/go-testdeep/internal/color"
13 "github.com/maxatome/go-testdeep/internal/util"
14 )
15
16 // ErrorSummary is the interface used to render error summaries. See
17 // Error.Summary.
18 type ErrorSummary interface {
19 AppendSummary(buf *strings.Builder, prefix string, colorized bool)
20 }
21
22 // ErrorSummaryItem implements the [ErrorSummary] interface and allows
23 // to render a labeled value.
24 //
25 // With explanation set:
26 //
27 // Label: value
28 // Explanation
29 //
30 // With an empty explanation:
31 //
32 // Label: value
33 type ErrorSummaryItem struct {
34 Label string
35 Value string
36 Explanation string
37 }
38
39 var _ ErrorSummary = ErrorSummaryItem{}
40
41 // AppendSummary implements the [ErrorSummary] interface.
42 func (s ErrorSummaryItem) AppendSummary(buf *strings.Builder, prefix string, colorized bool) {
43 buf.WriteString(prefix)
44
45 badOn, badOff := "", ""
46 if colorized {
47 color.Init()
48 badOn, badOff = color.BadOn, color.BadOff
49 buf.WriteString(color.BadOnBold)
50 }
51 buf.WriteString(s.Label)
52 buf.WriteString(": ")
53
54 util.IndentColorizeStringIn(buf, s.Value, prefix+strings.Repeat(" ", len(s.Label)+2), badOn, badOff)
55
56 if s.Explanation != "" {
57 buf.WriteByte('\n')
58 buf.WriteString(prefix)
59 util.IndentColorizeStringIn(buf, s.Explanation, prefix, badOn, badOff)
60 }
61 }
62
63 // ErrorSummaryItems implements the [ErrorSummary] interface and
64 // allows to render summaries with several labeled values. For example:
65 //
66 // Missing 6 items: the 6 items...
67 // Extra 2 items: the 2 items...
68 type ErrorSummaryItems []ErrorSummaryItem
69
70 var _ ErrorSummary = (ErrorSummaryItems)(nil)
71
72 // AppendSummary implements [ErrorSummary] interface.
73 func (s ErrorSummaryItems) AppendSummary(buf *strings.Builder, prefix string, colorized bool) {
74 maxLen := 0
75 for _, item := range s {
76 if len(item.Label) > maxLen {
77 maxLen = len(item.Label)
78 }
79 }
80
81 for idx, item := range s {
82 if idx > 0 {
83 buf.WriteByte('\n')
84 }
85 if len(item.Label) < maxLen {
86 item.Label = strings.Repeat(" ", maxLen-len(item.Label)) + item.Label
87 }
88 item.AppendSummary(buf, prefix, colorized)
89 }
90 }
91
92 type errorSummaryString string
93
94 var _ ErrorSummary = errorSummaryString("")
95
96 func (s errorSummaryString) AppendSummary(buf *strings.Builder, prefix string, colorized bool) {
97 badOn, badOff := "", ""
98 if colorized {
99 color.Init()
100 badOn, badOff = color.BadOn, color.BadOff
101 }
102
103 buf.WriteString(prefix)
104 util.IndentColorizeStringIn(buf, string(s), prefix, badOn, badOff)
105 }
106
107 // NewSummary returns an ErrorSummary composed by the simple string s.
108 func NewSummary(s string) ErrorSummary {
109 return errorSummaryString(s)
110 }
111
112 // NewSummaryReason returns an [ErrorSummary] meaning that the value got
113 // failed for an (optional) reason.
114 //
115 // With a given reason "it is not nil", the generated summary is:
116 //
117 // value: the_got_value
118 // it failed coz: it is not nil
119 //
120 // If reason is empty, the generated summary is:
121 //
122 // value: the_got_value
123 // it failed but didn't say why
124 func NewSummaryReason(got any, reason string) ErrorSummary {
125 if reason == "" {
126 return ErrorSummaryItem{
127 Label: " value", // keep 2 indent spaces
128 Value: util.ToString(got),
129 Explanation: "it failed but didn't say why",
130 }
131 }
132
133 return ErrorSummaryItems{
134 {
135 Label: "value",
136 Value: util.ToString(got),
137 },
138 {
139 Label: "it failed coz",
140 Value: reason,
141 },
142 }
143 }