-
Notifications
You must be signed in to change notification settings - Fork 0
/
goal_test.go
379 lines (363 loc) · 9.37 KB
/
goal_test.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
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
374
375
376
377
378
379
package ari_test
import (
"fmt"
"io/fs"
"log"
"os"
"strings"
"testing"
"codeberg.org/anaseto/goal"
"github.com/jarcoal/httpmock"
_ "github.com/marcboeker/go-duckdb"
"github.com/semperos/ari"
)
func TestGoalOk(t *testing.T) {
// t.Parallel() // go test reports data race
tests := map[string]struct {
input string
result string
}{
"simple goal expression": {
input: "+/!10",
result: "45",
},
"multiline goal expression": {
input: `(1 2
3 4)`,
result: strings.TrimSpace(`
(1 2
3 4)`),
},
"goal json, marshal": {
input: `""json -0w`,
result: `"false"`,
},
"goal json, unmarshal": {
input: `json "false"`,
result: `-0w`,
},
"goal os.env": {
input: `k:"ARI_TEST";k env $1234;env k`,
result: `"1234"`,
},
"ari keyword is defined": {
input: "http.post",
result: "http.post",
},
}
for name, test := range tests {
ctx, err := ari.NewContext("")
goalCtx := ctx.GoalContext
if err != nil {
t.Fatalf("error creating ari Context: %v", err)
}
t.Run(name, func(t *testing.T) {
// t.Parallel() // go test reports data race
goalV, err := goalCtx.Eval(test.input)
if err != nil {
t.Fatalf("Context.GoalContext.Eval(%q) returned an error: %v", test.input, err)
}
goalVString := goalV.Sprint(goalCtx, false)
if got, expected := goalVString, test.result; got != expected {
t.Fatalf("Context.GoalContext.Eval(%q) returned %q; expected %q", test.input, got, expected)
}
})
}
}
func TestUniversalGoalOk(t *testing.T) {
tests := map[string]struct {
input string
result string
}{
"simple goal expression": {
input: "+/!10",
result: "45",
},
"multiline goal expression": {
input: `(1 2
3 4)`,
result: strings.TrimSpace(`
(1 2
3 4)`),
},
"goal json, marshal": {
input: `""json -0w`,
result: `"false"`,
},
"goal json, unmarshal": {
input: `json "false"`,
result: `-0w`,
},
"goal os.env": {
input: `k:"ARI_TEST";k env $1234;env k`,
result: `"1234"`,
},
"ari keyword is defined": {
input: "time.Hour",
result: "3600000000000",
},
}
for name, test := range tests {
ctx, err := ari.NewUniversalContext()
goalCtx := ctx.GoalContext
if err != nil {
t.Fatalf("error creating ari Context: %v", err)
}
t.Run(name, func(t *testing.T) {
goalV, err := goalCtx.Eval(test.input)
if err != nil {
t.Fatalf("Context.GoalContext.Eval(%q) returned an error: %v", test.input, err)
}
goalVString := goalV.Sprint(goalCtx, false)
if got, expected := goalVString, test.result; got != expected {
t.Fatalf("Context.GoalContext.Eval(%q) returned %q; expected %q", test.input, got, expected)
}
})
}
}
func TestGoalError(t *testing.T) {
// t.Parallel() // go test reports data race
tests := map[string]struct {
input string
errMsg string
}{
"undefined is an error": {
input: "http.wacky",
errMsg: "undefined global: http.wacky",
},
}
for name, test := range tests {
ctx, err := ari.NewContext("")
goalCtx := ctx.GoalContext
if err != nil {
t.Fatalf("error creating ari Context: %v", err)
}
t.Run(name, func(t *testing.T) {
// t.Parallel() // go test reports data race
goalV, err := goalCtx.Eval(test.input)
if err == nil {
t.Fatalf("Context.GoalContext.Eval(%q) should return an error, but instead returned: %v",
test.input,
goalV.Sprint(goalCtx, false))
}
// goalVString := goalV.Sprint(goalCtx, false)
if got, expected := err.Error(), test.errMsg; got != expected {
t.Fatalf("Context.GoalContext.Eval(%q) should return an error like %q, but instead returned one like %q",
test.input,
expected,
got)
}
})
}
}
// Adapted from Goal implementation.
type matchTest struct {
Fname string
Line int
Left string
Right string
IsScript bool
}
// Adapted from Goal implementation.
func getMatchTests(glob string) ([]matchTest, error) {
d := os.DirFS("testing/via-go/")
fnames, err := fs.Glob(d, glob)
if err != nil {
return nil, err
}
mts := []matchTest{}
for _, fname := range fnames {
bs, err := fs.ReadFile(d, fname)
if err != nil {
return nil, err
}
text := string(bs)
lines := strings.Split(text, "\n")
for i, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 || line[0] == '/' {
continue
}
left, right, found := strings.Cut(line, " /")
if !found {
log.Printf("%s:%d: bad line", fname, i+1)
continue
}
mts = append(mts, matchTest{
Fname: fname,
Line: i + 1,
Left: strings.TrimSpace(left),
Right: strings.TrimSpace(right),
IsScript: false,
})
}
}
return mts, nil
}
// Adapted from Goal implementation.
func getScriptMatchTests(glob string) ([]matchTest, error) {
d := os.DirFS("testing/scripts")
fnames, err := fs.Glob(d, glob)
if err != nil {
return nil, err
}
mts := []matchTest{}
for _, fname := range fnames {
bs, err := fs.ReadFile(d, fname)
if err != nil {
return nil, err
}
text := string(bs)
body := strings.SplitN(text, "\n/RESULT:\n", 2)
if len(body) != 2 {
log.Printf("%s: bad script", fname)
continue
}
left := body[0]
right := body[1]
mts = append(mts, matchTest{
Fname: fname,
Left: strings.TrimSpace(left),
Right: strings.TrimSpace(right),
IsScript: true,
})
}
return mts, nil
}
// Adapted from Goal implementation.
//
//nolint:gocognit // upstream
func TestEval(t *testing.T) {
mts, err := getMatchTests("*.goal")
if err != nil {
t.Fatalf("getMatchTests: %v", err)
}
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get current working directory: %v", err)
}
for _, mt := range mts {
if mt.Fname == "errors.goal" {
continue
}
name := fmt.Sprintf("%s:%d", mt.Fname, mt.Line)
matchString := fmt.Sprintf("(%s) ~ (%s)", mt.Left, mt.Right)
t.Run(name, func(t *testing.T) {
ariContextLeft, err := ari.NewContext("")
if err != nil {
t.Fatalf("ari context error: %v", err)
}
ariContextRight, err := ari.NewContext("")
if err != nil {
t.Fatalf("ari context error: %v", err)
}
// HTTP mocking
httpClient, err := ari.NewHTTPClient(goalNewDictEmpty())
if err != nil {
t.Fatalf("failed to create HTTP client for testing: %v", err)
}
ariContextLeft.HTTPClient = httpClient
ariContextRight.HTTPClient = httpClient
httpmock.ActivateNonDefault(ariContextLeft.HTTPClient.Client.GetClient())
httpmock.ActivateNonDefault(ariContextRight.HTTPClient.Client.GetClient())
defer httpmock.DeactivateAndReset()
registerHTTPMocks()
err = os.Chdir(cwd + "/testing/via-go/")
if err != nil {
t.Fatalf("failed to chdir to 'testing/via-go': %v", err)
}
err = ariContextLeft.GoalContext.Compile(mt.Left, "", "")
ps := ariContextLeft.GoalContext.String()
if err != nil {
t.Log(ps)
t.Log(matchString)
t.Fatalf("compile error: %v", err)
}
vLeft, errLeft := ariContextLeft.GoalContext.Run()
vRight, errRight := ariContextRight.GoalContext.Eval(mt.Right)
if errLeft != nil || errRight != nil {
t.Log(ps)
t.Log(matchString)
t.Fatalf("return error: `%v` vs `%v`", errLeft, errRight)
}
if !vLeft.Matches(vRight) {
t.Log(ps)
t.Log(matchString)
if vLeft != (goal.NewGap()) {
//nolint:lll // upstream
t.Logf("results:\n %s\nvs %s\n", vLeft.Sprint(ariContextLeft.GoalContext, true), vRight.Sprint(ariContextRight.GoalContext, true))
} else {
t.Logf("results:\n %v\nvs %s\n", vLeft, vRight.Sprint(ariContextRight.GoalContext, true))
}
t.FailNow()
}
})
}
}
func registerHTTPMocks() {
httpmock.RegisterResponder("GET", "https://example.com/api/sprockets",
httpmock.NewStringResponder(200, `[{"id": 1, "name": "Test Sprocket 1"},{"id": 2, "name": "Test Sprocket 2"}]`))
httpmock.RegisterResponder("GET", "https://example.com/api/sprockets/1",
httpmock.NewStringResponder(200, `{"id": 1, "name": "Test Sprocket 1"}`))
}
func goalNewDictEmpty() *goal.D {
dv := goal.NewD(goal.NewAV(nil), goal.NewAV(nil))
d, ok := dv.BV().(*goal.D)
if !ok {
panic("Developer error: Empty Goal dictionary expected.")
}
return d
}
// Adapted from Goal implementation.
//
//nolint:gocognit // upstream
func TestErrors(t *testing.T) {
mts, err := getMatchTests("errors.goal")
if err != nil {
t.Fatalf("getMatchTests: %v", err)
}
smts, err := getScriptMatchTests("errors.goal")
if err != nil {
t.Fatalf("getScriptMatchTests: %v", err)
}
for _, mt := range append(mts, smts...) {
name := fmt.Sprintf("%s:%d", mt.Fname, mt.Line)
matchString := mt.Left
t.Run(name, func(t *testing.T) {
ariContext, err := ari.NewContext("")
if err != nil {
t.Fatalf("ari context error: %v", err)
}
err = ariContext.GoalContext.Compile(mt.Left, "", "")
ps := ariContext.GoalContext.String()
if err == nil {
var v goal.V
v, err = ariContext.GoalContext.Run()
if err == nil {
t.Log(ps)
t.Log(matchString)
t.Fatalf("no error left: result: %s\nexpected: %v", v.Sprint(ariContext.GoalContext, true), mt.Right)
}
}
//nolint:errorlint // upstream
e, ok := err.(*goal.Panic)
if !ok {
// should never happen
t.Log(ps)
t.Log(matchString)
t.Fatalf("bad error: `%v`\nexpected:`%v`", err, mt.Right)
}
msg := e.Error()
if strings.Contains(mt.Left, "\n") {
msg = e.ErrorStack()
}
if !strings.Contains(e.Error(), mt.Right) {
t.Log(ps)
t.Log(matchString)
t.Logf("\n error: %q\nexpected: %q", msg, mt.Right)
t.Fail()
return
}
})
}
}