-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval_test.go
95 lines (78 loc) · 1.74 KB
/
eval_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
package goast_test
import (
"go/ast"
"strings"
"testing"
"github.com/m-mizutani/goast"
"github.com/m-mizutani/gt"
"github.com/m-mizutani/opac"
)
const evalExampleCode = `package main
func Add(a, b int) int {
return a + b
}
`
func TestEval(t *testing.T) {
code := strings.NewReader(evalExampleCode)
mock := opac.NewMock(func(in any) (any, error) {
node := gt.Cast[*goast.Node](t, in)
if node.Kind != "File" {
return nil, nil
}
src := gt.Cast[*ast.File](t, node.Node)
gt.A(t, src.Decls).Must().Length(1)
f := gt.Cast[*ast.FuncDecl](t, src.Decls[0])
gt.V(t, f.Name.Name).Equal("Add")
return &goast.EvalOutput{
Fail: []*goast.FailCase{
{
Msg: "eval_test",
Pos: 15,
},
},
}, nil
})
g := goast.New(
goast.WithOpacClient(mock),
)
fails := gt.R1(g.Eval("test.go", code)).NoError(t)
gt.A(t, fails).Length(1)
gt.V(t, fails[0].Line).Equal(3)
gt.V(t, fails[0].Msg).Equal("eval_test")
}
func TestIgnoreAutoGeneratedFile(t *testing.T) {
const code = `// Code generated by yo. DO NOT EDIT.
// Package model contains the types.
package main
func Add(a, b int) int {
return a + b
}
`
mock := opac.NewMock(func(in any) (any, error) {
return &goast.EvalOutput{
Fail: []*goast.FailCase{
{
Msg: "always fail",
Pos: 15,
},
},
}, nil
})
t.Run("with ignore option", func(t *testing.T) {
g := goast.New(
goast.WithOpacClient(mock),
goast.WithIgnoreAutoGen(),
)
fails := gt.R1(
g.Eval("test.go", strings.NewReader(code)),
).NoError(t)
gt.A(t, fails).Length(0)
})
t.Run("without ignore option", func(t *testing.T) {
g := goast.New(
goast.WithOpacClient(mock),
)
fails := gt.R1(g.Eval("test.go", strings.NewReader(code))).NoError(t)
gt.A(t, fails).Longer(0)
})
}