-
Notifications
You must be signed in to change notification settings - Fork 9
/
musttag_test.go
71 lines (57 loc) · 1.93 KB
/
musttag_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
package musttag
import (
"io"
"os"
"os/exec"
"path/filepath"
"testing"
"go-simpler.org/assert"
. "go-simpler.org/assert/EF"
"golang.org/x/tools/go/analysis/analysistest"
)
func TestAnalyzer(t *testing.T) {
testdata := analysistest.TestData()
setupModules(t, testdata)
t.Run("tests", func(t *testing.T) {
analyzer := New(
Func{Name: "example.com/custom.Marshal", Tag: "custom", ArgPos: 0},
Func{Name: "example.com/custom.Unmarshal", Tag: "custom", ArgPos: 1},
)
analysistest.Run(t, testdata, analyzer, "tests")
})
t.Run("bad Func.ArgPos", func(t *testing.T) {
analyzer := New(
Func{Name: "encoding/json.Marshal", Tag: "json", ArgPos: 10},
)
err := analysistest.Run(nopT{}, testdata, analyzer, "tests")[0].Err
assert.Equal[E](t, err.Error(), "musttag: Func.ArgPos cannot be 10: encoding/json.Marshal accepts only 1 argument(s)")
})
}
func TestFlags(t *testing.T) {
analyzer := New()
analyzer.Flags.Usage = func() {}
analyzer.Flags.SetOutput(io.Discard)
t.Run("ok", func(t *testing.T) {
err := analyzer.Flags.Parse([]string{"-fn=test.Test:test:0"})
assert.NoErr[E](t, err)
})
t.Run("invalid format", func(t *testing.T) {
err := analyzer.Flags.Parse([]string{"-fn=test.Test"})
assert.Equal[E](t, err.Error(), `invalid value "test.Test" for flag -fn: invalid syntax`)
})
t.Run("non-number argument position", func(t *testing.T) {
err := analyzer.Flags.Parse([]string{"-fn=test.Test:test:-"})
assert.Equal[E](t, err.Error(), `invalid value "test.Test:test:-" for flag -fn: strconv.Atoi: parsing "-": invalid syntax`)
})
}
type nopT struct{}
func (nopT) Errorf(string, ...any) {}
// NOTE: analysistest does not yet support modules;
// see https://github.com/golang/go/issues/37054 for details.
func setupModules(t *testing.T, testdata string) {
t.Helper()
err := os.Chdir(filepath.Join(testdata, "src"))
assert.NoErr[F](t, err)
err = exec.Command("go", "work", "vendor").Run()
assert.NoErr[F](t, err)
}