-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_test.go
94 lines (89 loc) · 1.8 KB
/
core_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
package main
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
)
func TestToggle(t *testing.T) {
inputsPaths, err := filepath.Glob(filepath.Join("testdata", "*.input"))
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
if err != nil {
t.Fatal(err)
}
for _, p := range inputsPaths {
p := p
_, filename := filepath.Split(p)
testName := filename[:len(filename)-len(filepath.Ext(p))]
t.Run(testName, func(t *testing.T) {
t.Parallel()
input, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
got, err := toggle(ctx, input)
if err != nil {
t.Fatal(err)
}
want, err := os.ReadFile(
filepath.Join("testdata", testName+".golden"),
)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Errorf(filesCmpErr, got, want)
}
})
}
}
const getSymbolsInfoFromBuildErrorsInput = `
package p
func main() {
var (
notUsed0 = false
used0 bool
)
notUsed1, used1 := "", "", "" // more values than variables
_, _ = used0, used1 // no closing brace
`
func TestGetSymbolsInfoFromBuildErrors(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
t.Run("ignore other errors", func(t *testing.T) {
t.Parallel()
input := []byte(
getSymbolsInfoFromBuildErrorsInput,
)
want := []symbolInfo{
{"notUsed0", 5},
{"notUsed1", 8},
}
got, err := getSymbolsInfoFromBuildErrors(
ctx, input, notUsedErrorRegexpSuffix,
)
if err != nil {
t.Fatal(err)
}
for i, info := range got {
if info.name != want[i].name {
t.Errorf(
"got: %s, want: %s",
info.name,
want[i].name,
)
}
if info.lineNum != want[i].lineNum {
t.Errorf(
"got: %d, want: %d",
info.lineNum,
want[i].lineNum,
)
}
}
})
}