-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorphans_test.go
More file actions
137 lines (119 loc) · 3.68 KB
/
Copy pathorphans_test.go
File metadata and controls
137 lines (119 loc) · 3.68 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
package repomap
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeRefs struct{ byName map[string][]Location }
func (f fakeRefs) Refs(_ context.Context, _ string, _ int, symbol string) ([]Location, error) {
return f.byName[symbol], nil
}
type errRefs struct{}
func (errRefs) Refs(context.Context, string, int, string) ([]Location, error) {
return nil, assert.AnError
}
func orphanNames(c []OrphanCandidate) []string {
names := make([]string, 0, len(c))
for _, cand := range c {
names = append(names, cand.Name)
}
return names
}
func TestOrphanCandidatesBuckets(t *testing.T) {
t.Parallel()
m := New("/repo", DefaultConfig())
m.ranked = []RankedFile{
{
FileSymbols: &FileSymbols{
Path: "service.go",
Language: "go",
Symbols: []Symbol{
{Name: "ZeroRef", Kind: "function", Exported: true, Line: 10},
{Name: "TestOnly", Kind: "function", Exported: true, Line: 20},
{Name: "LiveRef", Kind: "function", Exported: true, Line: 30},
{Name: "WithReceiver", Kind: "method", Receiver: "*Svc", Exported: true, Line: 40},
{Name: "lowercase", Kind: "function", Exported: false, Line: 50},
},
},
},
{
Tag: "entry",
FileSymbols: &FileSymbols{
Path: "main.go",
Language: "go",
Symbols: []Symbol{
{Name: "Run", Kind: "function", Exported: true, Line: 5},
},
},
},
{
FileSymbols: &FileSymbols{
Path: "service_test.go",
Language: "go",
Symbols: []Symbol{
{Name: "TestSomething", Kind: "function", Exported: true, Line: 5},
},
},
},
}
q := fakeRefs{byName: map[string][]Location{
// Only its own def line → ZeroRefs.
"ZeroRef": {{File: "/repo/service.go", Line: 10}},
// Def site + a *_test.go ref → TestOnlyRefs.
"TestOnly": {
{File: "/repo/service.go", Line: 20},
{File: "/repo/service_test.go", Line: 7},
},
// Def site + a non-test ref → neither bucket.
"LiveRef": {
{File: "/repo/service.go", Line: 30},
{File: "/repo/other.go", Line: 99},
},
// Zero inbound (only its own def line) → ZeroRefs, Receiver preserved.
"WithReceiver": {{File: "/repo/service.go", Line: 40}},
// Defined in a *_test.go file → excluded from both buckets.
"TestSomething": {{File: "/repo/service_test.go", Line: 5}},
}}
report, err := m.OrphanCandidates(context.Background(), q)
require.NoError(t, err)
assert.Equal(t, orphanCaveat, report.Caveat)
zeroNames := orphanNames(report.ZeroRefs)
assert.Contains(t, zeroNames, "ZeroRef")
assert.Contains(t, zeroNames, "WithReceiver")
assert.NotContains(t, zeroNames, "LiveRef")
assert.NotContains(t, zeroNames, "lowercase")
assert.NotContains(t, zeroNames, "Run")
// Defined in a *_test.go file → excluded from both buckets.
assert.NotContains(t, zeroNames, "TestSomething")
testOnlyNames := orphanNames(report.TestOnlyRefs)
assert.Equal(t, []string{"TestOnly"}, testOnlyNames)
assert.NotContains(t, testOnlyNames, "TestSomething")
var withReceiver *OrphanCandidate
for i := range report.ZeroRefs {
if report.ZeroRefs[i].Name == "WithReceiver" {
withReceiver = &report.ZeroRefs[i]
}
}
require.NotNil(t, withReceiver)
assert.Equal(t, "*Svc", withReceiver.Receiver)
}
func TestOrphanCandidatesSkipsRefsErrors(t *testing.T) {
t.Parallel()
m := New("/repo", DefaultConfig())
m.ranked = []RankedFile{
{
FileSymbols: &FileSymbols{
Path: "service.go",
Language: "go",
Symbols: []Symbol{
{Name: "Exported", Kind: "function", Exported: true, Line: 10},
},
},
},
}
report, err := m.OrphanCandidates(context.Background(), errRefs{})
require.NoError(t, err)
assert.Empty(t, report.ZeroRefs)
assert.Empty(t, report.TestOnlyRefs)
}