This repository was archived by the owner on Aug 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter_test.go
More file actions
160 lines (149 loc) · 3.56 KB
/
Copy pathprinter_test.go
File metadata and controls
160 lines (149 loc) · 3.56 KB
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
package diag_test
import (
"bytes"
"strings"
"testing"
"github.com/charmbracelet/colorprofile"
"github.com/charmbracelet/x/exp/golden"
"github.com/nuvrel/diag"
)
func TestPrinter(t *testing.T) {
cases := []struct {
name string
config diag.Config
diags []diag.Diagnostic
}{
{
name: "error",
diags: []diag.Diagnostic{diag.NewError("something went wrong")},
},
{
name: "error with code",
diags: []diag.Diagnostic{diag.NewError("something went wrong").Code("E001")},
},
{
name: "warning",
diags: []diag.Diagnostic{diag.NewWarning("something looks off")},
},
{
name: "help and note",
diags: []diag.Diagnostic{diag.NewError("you can't do that").Help("try this instead").Note("see the docs")},
},
{
name: "detail",
diags: []diag.Diagnostic{
diag.NewError("you can't do that").
Detail(
"This is a detailed explanation of the error.",
"This is a second paragraph with more context.",
),
},
},
{
name: "ordering",
diags: []diag.Diagnostic{
diag.NewError("something went wrong").
Help("try this instead").
Snippet(diag.NewSnippet([]byte("var y = x + z")).
File("main.go").
From(1, 13).
To(1, 14).
Message("z is not defined"),
).
Note("see the docs"),
},
},
{
name: "multiple",
diags: []diag.Diagnostic{
diag.NewError("something went wrong").Code("E001"),
diag.NewWarning("something looks off"),
diag.NewError("another error"),
},
},
{
name: "margin",
config: diag.Config{
Margin: 4,
},
diags: []diag.Diagnostic{
diag.NewError("something went wrong").
Detail("This is a detailed explanation.").
Help("try this instead").
Note("see the docs"),
},
},
{
name: "skip header",
config: diag.Config{
SkipHeader: true,
},
diags: []diag.Diagnostic{
diag.NewError("something went wrong").
Detail("This is a detailed explanation.").
Help("try this instead"),
},
},
{
name: "skip detail",
config: diag.Config{
SkipDetail: true,
},
diags: []diag.Diagnostic{
diag.NewError("something went wrong").
Detail("This is a detailed explanation.").
Help("try this instead"),
},
},
{
name: "header func",
config: diag.Config{
HeaderFunc: func(sev diag.Severity, label, code, message string) string {
header := strings.ToUpper(label)
if code != "" {
header += " " + code
}
return header + " >> " + message
},
},
diags: []diag.Diagnostic{
diag.NewError("something went wrong").Code("E001"),
},
},
{
name: "detail func",
config: diag.Config{
DetailFunc: func(detail []string) string {
return ">>> " + strings.Join(detail, " | ")
},
},
diags: []diag.Diagnostic{
diag.NewError("something went wrong").
Detail("First paragraph.", "Second paragraph."),
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Parallel()
config := diag.Config{
Profile: colorprofile.NoTTY,
Theme: diag.DefaultTheme(),
Characters: diag.DefaultCharacters(),
Prefixes: diag.DefaultPrefixes(),
SeverityLabels: diag.DefaultSeverityLabels(),
Margin: c.config.Margin,
HeaderFunc: c.config.HeaderFunc,
DetailFunc: c.config.DetailFunc,
SkipHeader: c.config.SkipHeader,
SkipDetail: c.config.SkipDetail,
}
var buf bytes.Buffer
p := diag.NewPrinter(&buf, config)
if err := p.Print(c.diags...); err != nil {
t.Fatal(err)
}
golden.RequireEqual(t, buf.Bytes())
})
}
}