-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwith_source_test.go
More file actions
373 lines (326 loc) · 10.7 KB
/
with_source_test.go
File metadata and controls
373 lines (326 loc) · 10.7 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2026 Aaron Alpar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wile
import (
"context"
"errors"
"strings"
"testing"
qt "github.com/frankban/quicktest"
)
// TestParseWithSource_RuntimeError verifies that ParseWithSource + Eval populates
// RuntimeError.Source with the filename, line, and column when a runtime
// error occurs.
func TestParseWithSource_RuntimeError(t *testing.T) {
c := qt.New(t)
tcs := []struct {
name string
source string
code string
wantSourcePfx string // Source field must start with this prefix
wantCondition string // Condition.SchemeString(), empty to skip check
wantHasSource bool
wantHasTrace bool
}{
{
name: "raise with source",
source: "config.scm",
code: `(raise "boom")`,
wantSourcePfx: "config.scm:",
wantCondition: `"boom"`,
wantHasSource: true,
wantHasTrace: true,
},
{
name: "error with source",
source: "app.scm",
code: `(error "something went wrong" 42)`,
wantSourcePfx: "app.scm:",
wantCondition: "",
wantHasSource: true,
wantHasTrace: true,
},
{
name: "raise without source",
source: "",
code: `(raise "boom")`,
wantSourcePfx: "",
wantCondition: `"boom"`,
wantHasSource: false,
wantHasTrace: false,
},
{
name: "nested call error with source",
source: "nested.scm",
code: `(define (f) (raise "inner")) (f)`,
wantSourcePfx: "nested.scm:",
wantCondition: `"inner"`,
wantHasSource: true,
wantHasTrace: true,
},
{
name: "path-like source",
source: "/etc/config/rules.scm",
code: `(raise "fail")`,
wantSourcePfx: "/etc/config/rules.scm:",
wantCondition: `"fail"`,
wantHasSource: true,
wantHasTrace: true,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
var evalErr error
if tc.source != "" {
_, evalErr = engine.EvalMultipleWithSource(ctx, tc.code, tc.source)
} else {
_, evalErr = engine.EvalMultiple(ctx, tc.code)
}
c.Assert(evalErr, qt.IsNotNil)
var rtErr *RuntimeError
c.Assert(errors.As(evalErr, &rtErr), qt.IsTrue)
if tc.wantHasSource {
c.Assert(rtErr.Source, qt.Not(qt.Equals), "")
c.Assert(strings.HasPrefix(rtErr.Source, tc.wantSourcePfx), qt.IsTrue,
qt.Commentf("Source=%q want prefix=%q", rtErr.Source, tc.wantSourcePfx))
// Source format is "file:line:col" — verify it has at least two colons
parts := strings.Split(rtErr.Source, ":")
c.Assert(len(parts) >= 3, qt.IsTrue,
qt.Commentf("Source=%q doesn't match file:line:col format", rtErr.Source))
} else {
c.Assert(rtErr.Source, qt.Equals, "")
}
if tc.wantHasTrace {
c.Assert(rtErr.StackTrace, qt.Not(qt.Equals), "")
}
if tc.wantCondition != "" {
c.Assert(rtErr.Condition, qt.IsNotNil)
c.Assert(rtErr.Condition.SchemeString(), qt.Equals, tc.wantCondition)
}
// Source should appear in Error() output when present
if tc.wantHasSource {
c.Assert(strings.Contains(rtErr.Error(), tc.wantSourcePfx), qt.IsTrue,
qt.Commentf("Error()=%q should contain %q", rtErr.Error(), tc.wantSourcePfx))
}
})
}
}
// TestParseWithSource_Success verifies that ParseWithSource + Eval returns correct
// results for code that does not error.
func TestParseWithSource_Success(t *testing.T) {
c := qt.New(t)
tcs := []struct {
name string
source string
code string
want string
}{
{
name: "simple expression",
source: "math.scm",
code: "(+ 1 2)",
want: "3",
},
{
name: "string result",
source: "greeting.scm",
code: `(string-append "hello" " " "world")`,
want: `"hello world"`,
},
{
name: "empty source string",
source: "",
code: "(* 6 7)",
want: "42",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
result, err := engine.Eval(ctx, engine.MustParseWithSource(ctx, tc.code, tc.source))
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, tc.want)
})
}
}
// TestEvalMultipleWithSource_RuntimeError verifies that source tracking
// works across multiple expressions.
func TestEvalMultipleWithSource_RuntimeError(t *testing.T) {
c := qt.New(t)
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
// The error occurs in the second expression on line 2.
code := "(define x 10)\n(error \"failure\" x)"
_, err = engine.EvalMultipleWithSource(ctx, code, "multi.scm")
c.Assert(err, qt.IsNotNil)
var rtErr *RuntimeError
c.Assert(errors.As(err, &rtErr), qt.IsTrue)
c.Assert(strings.HasPrefix(rtErr.Source, "multi.scm:"), qt.IsTrue,
qt.Commentf("Source=%q", rtErr.Source))
}
// TestEvalMultipleWithSource_Success verifies that EvalMultipleWithSource
// returns the result of the last expression.
func TestEvalMultipleWithSource_Success(t *testing.T) {
c := qt.New(t)
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
result, err := engine.EvalMultipleWithSource(ctx,
"(define x 10) (define y 20) (+ x y)",
"calc.scm")
c.Assert(err, qt.IsNil)
c.Assert(result.SchemeString(), qt.Equals, "30")
}
// TestCompileParseWithSource_RuntimeError verifies that source information
// survives the compile → run boundary. Code compiled with ParseWithSource + Compile
// and later executed with Run should still carry source info in errors.
func TestCompileParseWithSource_RuntimeError(t *testing.T) {
c := qt.New(t)
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
compiled, err := engine.Compile(ctx, engine.MustParseWithSource(ctx, `(raise "compiled-boom")`, "compiled.scm"))
c.Assert(err, qt.IsNil)
_, err = engine.Run(ctx, compiled)
c.Assert(err, qt.IsNotNil)
var rtErr *RuntimeError
c.Assert(errors.As(err, &rtErr), qt.IsTrue)
c.Assert(strings.HasPrefix(rtErr.Source, "compiled.scm:"), qt.IsTrue,
qt.Commentf("Source=%q", rtErr.Source))
c.Assert(rtErr.Condition.SchemeString(), qt.Equals, `"compiled-boom"`)
}
// TestParseWithSource_ParseError verifies that ParseWithSource returns
// a CompilationError for malformed input.
func TestParseWithSource_ParseError(t *testing.T) {
c := qt.New(t)
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
_, err = engine.ParseWithSource(ctx, "(", "broken.scm")
c.Assert(err, qt.IsNotNil)
var compErr *CompilationError
c.Assert(errors.As(err, &compErr), qt.IsTrue)
}
// TestWithSource_DistinctSources verifies that different source strings
// produce distinct Source fields in errors — the source string is not
// cached or shared between calls.
func TestWithSource_DistinctSources(t *testing.T) {
c := qt.New(t)
tcs := []struct {
name string
source string
}{
{"alpha", "alpha.scm"},
{"beta", "beta.scm"},
{"gamma", "scripts/gamma.scm"},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
_, err = engine.Eval(ctx, engine.MustParseWithSource(ctx, `(raise "x")`, tc.source))
var rtErr *RuntimeError
c.Assert(errors.As(err, &rtErr), qt.IsTrue)
c.Assert(strings.HasPrefix(rtErr.Source, tc.source+":"), qt.IsTrue,
qt.Commentf("Source=%q want prefix=%q:", rtErr.Source, tc.source))
})
}
}
// TestWithSource_SourcelessEvalHasEmptySource is a negative test: errors
// from the sourceless Eval/EvalMultiple/Compile should have empty Source.
func TestWithSource_SourcelessEvalHasEmptySource(t *testing.T) {
c := qt.New(t)
tcs := []struct {
name string
eval func(*Engine, context.Context) error
}{
{
name: "Eval",
eval: func(e *Engine, ctx context.Context) error {
_, err := e.Eval(ctx, e.MustParse(ctx, `(raise "x")`))
return err
},
},
{
name: "EvalMultiple",
eval: func(e *Engine, ctx context.Context) error {
_, err := e.EvalMultiple(ctx, `(raise "x")`)
return err
},
},
{
name: "Compile+Run",
eval: func(e *Engine, ctx context.Context) error {
cc, err := e.Compile(ctx, e.MustParse(ctx, `(raise "x")`))
if err != nil {
return err
}
_, err = e.Run(ctx, cc)
return err
},
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
err = tc.eval(engine, context.Background())
c.Assert(err, qt.IsNotNil)
var rtErr *RuntimeError
c.Assert(errors.As(err, &rtErr), qt.IsTrue)
c.Assert(rtErr.Source, qt.Equals, "")
})
}
}
// TestCompileParseWithSource_ReusedCompiledCode verifies that a single
// ParseWithSource + Compile result can be Run multiple times, each retaining
// the source information in errors.
func TestCompileParseWithSource_ReusedCompiledCode(t *testing.T) {
c := qt.New(t)
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
compiled, err := engine.Compile(ctx, engine.MustParseWithSource(ctx, `(raise "again")`, "reuse.scm"))
c.Assert(err, qt.IsNil)
for i := range 3 {
_, err = engine.Run(ctx, compiled)
c.Assert(err, qt.IsNotNil)
var rtErr *RuntimeError
c.Assert(errors.As(err, &rtErr), qt.IsTrue)
c.Assert(strings.HasPrefix(rtErr.Source, "reuse.scm:"), qt.IsTrue,
qt.Commentf("run %d: Source=%q", i, rtErr.Source))
}
}
// TestParseWithSource_ErrorFormat verifies the full Error() string format
// includes the source prefix.
func TestParseWithSource_ErrorFormat(t *testing.T) {
c := qt.New(t)
engine, err := NewEngine(context.Background())
c.Assert(err, qt.IsNil)
ctx := context.Background()
_, err = engine.Eval(ctx, engine.MustParseWithSource(ctx, `(raise "fmt-test")`, "format.scm"))
var rtErr *RuntimeError
c.Assert(errors.As(err, &rtErr), qt.IsTrue)
// Error() should start with "format.scm:line:col: runtime error: ..."
c.Assert(strings.HasPrefix(rtErr.Error(), "format.scm:"), qt.IsTrue,
qt.Commentf("Error()=%q", rtErr.Error()))
c.Assert(strings.Contains(rtErr.Error(), "runtime error"), qt.IsTrue)
}