-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_display_test.go
More file actions
240 lines (211 loc) · 5.71 KB
/
error_display_test.go
File metadata and controls
240 lines (211 loc) · 5.71 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
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
package main
import (
"errors"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
func captureStderr(t *testing.T, fn func()) string {
t.Helper()
original := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create stderr pipe: %v", err)
}
os.Stderr = w
defer func() {
os.Stderr = original
}()
fn()
if err := w.Close(); err != nil {
t.Fatalf("failed to close write pipe: %v", err)
}
data, err := io.ReadAll(r)
if err != nil {
t.Fatalf("failed to read captured stderr: %v", err)
}
return string(data)
}
func TestDefaultErrorConfig(t *testing.T) {
config := DefaultErrorConfig()
if !config.UseColor {
t.Fatalf("expected UseColor to default to true")
}
if !config.ShowContext {
t.Fatalf("expected ShowContext to default to true")
}
if config.ContextLines != 2 {
t.Fatalf("expected ContextLines to be 2, got %d", config.ContextLines)
}
if !config.ShowStackTrace {
t.Fatalf("expected ShowStackTrace to default to true")
}
}
func TestFormatError(t *testing.T) {
if got := FormatError(nil); got != "" {
t.Fatalf("expected empty string for nil error, got %q", got)
}
pe := parseError{
reason: "unexpected token",
pos: pos{
fileName: "sample.oak",
line: 3,
col: 7,
},
}
if got := FormatError(pe); got != "Parse Error in sample.oak at [3:7]: unexpected token" {
t.Fatalf("unexpected parse error format: %q", got)
}
re := &runtimeError{
reason: "bad call",
pos: pos{
line: 9,
col: 2,
},
stackTrace: []stackEntry{
{name: "outer", pos: pos{line: 1, col: 1}},
{name: "", pos: pos{line: 2, col: 5}},
},
}
formatted := FormatError(re)
if !strings.Contains(formatted, "Runtime Error at [9:2]: bad call") {
t.Fatalf("unexpected runtime error header: %q", formatted)
}
if !strings.Contains(formatted, "in fn outer [1:1]") {
t.Fatalf("missing named stack entry: %q", formatted)
}
if !strings.Contains(formatted, "in anonymous fn [2:5]") {
t.Fatalf("missing anonymous stack entry: %q", formatted)
}
generic := errors.New("plain failure")
if got := FormatError(generic); got != "plain failure" {
t.Fatalf("unexpected generic error format: %q", got)
}
}
func TestDisplayError_Generic(t *testing.T) {
output := captureStderr(t, func() {
DisplayError(errors.New("generic boom"), ErrorDisplayConfig{UseColor: false})
})
if !strings.Contains(output, "generic boom") {
t.Fatalf("expected generic error output, got: %q", output)
}
}
func TestDisplayError_Nil(t *testing.T) {
output := captureStderr(t, func() {
DisplayError(nil, ErrorDisplayConfig{UseColor: false})
})
if output != "" {
t.Fatalf("expected no output for nil error, got: %q", output)
}
}
func TestDisplayError_ParseWithContext(t *testing.T) {
tmpDir := t.TempDir()
fileName := filepath.Join(tmpDir, "program.oak")
content := "line 1\nline two\nline 3\n"
if err := os.WriteFile(fileName, []byte(content), 0o644); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
parseErr := parseError{
reason: "expected expression",
pos: pos{
fileName: fileName,
line: 2,
col: 5,
},
}
output := captureStderr(t, func() {
DisplayError(parseErr, ErrorDisplayConfig{
UseColor: false,
ShowContext: true,
ContextLines: 1,
})
})
if !strings.Contains(output, "Parse Error") {
t.Fatalf("missing parse header: %q", output)
}
if !strings.Contains(output, "expected expression") {
t.Fatalf("missing parse reason: %q", output)
}
if !strings.Contains(output, "Context:") {
t.Fatalf("missing source context block: %q", output)
}
if !strings.Contains(output, "line two") {
t.Fatalf("missing highlighted source line: %q", output)
}
if !strings.Contains(output, "^") {
t.Fatalf("missing error pointer: %q", output)
}
}
func TestDisplayError_RuntimeWithStackTrace(t *testing.T) {
err := &runtimeError{
reason: "division by zero",
pos: pos{
line: 4,
col: 11,
},
stackTrace: []stackEntry{{name: "compute", pos: pos{line: 1, col: 1}}},
}
output := captureStderr(t, func() {
DisplayError(err, ErrorDisplayConfig{
UseColor: false,
ShowContext: false,
ShowStackTrace: true,
})
})
if !strings.Contains(output, "Runtime Error") {
t.Fatalf("missing runtime header: %q", output)
}
if !strings.Contains(output, "division by zero") {
t.Fatalf("missing runtime reason: %q", output)
}
if !strings.Contains(output, "Stack Trace") {
t.Fatalf("missing stack trace header: %q", output)
}
if !strings.Contains(output, "in fn compute [1:1]") {
t.Fatalf("missing stack trace entry: %q", output)
}
}
func TestDisplayError_RuntimeWithoutStackTrace(t *testing.T) {
err := &runtimeError{
reason: "overflow",
pos: pos{
line: 1,
col: 1,
},
stackTrace: []stackEntry{{name: "f", pos: pos{line: 1, col: 1}}},
}
output := captureStderr(t, func() {
DisplayError(err, ErrorDisplayConfig{
UseColor: false,
ShowContext: false,
ShowStackTrace: false,
})
})
if strings.Contains(output, "Stack Trace") {
t.Fatalf("did not expect stack trace block when disabled: %q", output)
}
}
func TestDisplayError_ParseWithoutFileContext(t *testing.T) {
parseErr := parseError{
reason: "missing close brace",
pos: pos{
line: 5,
col: 3,
},
}
output := captureStderr(t, func() {
DisplayError(parseErr, ErrorDisplayConfig{UseColor: false, ShowContext: true})
})
if strings.Contains(output, "Context:") {
t.Fatalf("did not expect context when file name is empty: %q", output)
}
}
func TestDisplaySourceContext_FileMissing(t *testing.T) {
var buf strings.Builder
displaySourceContext(&buf, "definitely-missing-file.oak", 1, 1, ErrorDisplayConfig{UseColor: false, ShowContext: true, ContextLines: 1})
if buf.Len() != 0 {
t.Fatalf("expected no output for missing context file, got: %q", buf.String())
}
}