-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategorize_test.go
More file actions
165 lines (143 loc) · 6.68 KB
/
Copy pathcategorize_test.go
File metadata and controls
165 lines (143 loc) · 6.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
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
package repomap
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRenderKindWeight(t *testing.T) {
t.Parallel()
tests := []struct {
name string
kind string
exported bool
want int
}{
// Exported weight tiers.
{"exported struct", "struct", true, 10},
{"exported interface", "interface", true, 10},
{"exported type alias", "type", true, 8},
{"exported function", "function", true, 6},
{"exported fn alias", "fn", true, 6},
{"exported method", "method", true, 5},
{"exported constant", "constant", true, 3},
{"exported const", "const", true, 3},
{"exported variable", "variable", true, 3},
{"exported var", "var", true, 3},
{"exported static", "static", true, 3},
{"exported unknown kind", "macro", true, 2},
{"exported class", "class", true, 10},
// Unexported always returns 1, regardless of kind.
{"unexported struct", "struct", false, 1},
{"unexported function", "function", false, 1},
{"unexported method", "method", false, 1},
{"unexported unknown", "macro", false, 1},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := renderKindWeight(tc.kind, tc.exported)
assert.Equal(t, tc.want, got, "renderKindWeight(%q, %v)", tc.kind, tc.exported)
})
}
}
func TestRenderKindWeight_OrderingProperty(t *testing.T) {
t.Parallel()
// Verify the ordering invariants hold for the weight tiers.
assert.Greater(t, renderKindWeight("struct", true), renderKindWeight("type", true), "struct > type alias")
assert.Greater(t, renderKindWeight("interface", true), renderKindWeight("type", true), "interface > type alias")
assert.Greater(t, renderKindWeight("type", true), renderKindWeight("function", true), "type alias > function")
assert.Greater(t, renderKindWeight("function", true), renderKindWeight("method", true), "function > method")
assert.Greater(t, renderKindWeight("method", true), renderKindWeight("const", true), "method > const")
assert.Greater(t, renderKindWeight("const", true), renderKindWeight("macro", true), "const > unknown exported")
assert.Greater(t, renderKindWeight("macro", true), renderKindWeight("struct", false), "any exported > unexported")
}
// TestRenderKindWeightPHP verifies PHP-specific kind weights. Go weights must
// remain unchanged; PHP weights must follow the spec table.
func TestRenderKindWeightPHP(t *testing.T) {
t.Parallel()
tests := []struct {
name string
kind string
exported bool
want int
}{
// PHP exported weights per spec.
{"php class", "class", true, 10},
{"php interface", "interface", true, 10},
{"php trait", "trait", true, 9},
{"php enum", "enum", true, 8},
{"php function", "function", true, 6},
{"php method", "method", true, 5},
{"php case", "case", true, 4},
{"php property", "property", true, 3},
{"php const", "const", true, 3},
{"php namespace", "namespace", true, 0},
// Unexported PHP symbols always return 1.
{"unexported php class", "class", false, 1},
{"unexported php trait", "trait", false, 1},
{"unexported php enum", "enum", false, 1},
{"unexported php case", "case", false, 1},
{"unexported php property", "property", false, 1},
{"unexported php namespace", "namespace", false, 1},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := renderKindWeight(tc.kind, tc.exported)
assert.Equal(t, tc.want, got, "renderKindWeight(%q, %v)", tc.kind, tc.exported)
})
}
// Verify PHP ordering invariants match spec rationale.
assert.Greater(t, renderKindWeight("class", true), renderKindWeight("trait", true), "class > trait")
assert.Greater(t, renderKindWeight("trait", true), renderKindWeight("enum", true), "trait > enum")
assert.Greater(t, renderKindWeight("enum", true), renderKindWeight("function", true), "enum > function")
assert.Greater(t, renderKindWeight("function", true), renderKindWeight("method", true), "function > method")
assert.Greater(t, renderKindWeight("method", true), renderKindWeight("case", true), "method > case")
assert.Greater(t, renderKindWeight("case", true), renderKindWeight("property", true), "case > property")
assert.Equal(t, renderKindWeight("property", true), renderKindWeight("const", true), "property == const (both 3)")
// Go weights must be unchanged.
assert.Equal(t, 10, renderKindWeight("struct", true), "Go struct unchanged at 10")
assert.Equal(t, 10, renderKindWeight("interface", true), "Go interface unchanged at 10")
assert.Equal(t, 8, renderKindWeight("type", true), "Go type unchanged at 8")
assert.Equal(t, 6, renderKindWeight("fn", true), "Go fn unchanged at 6")
}
// TestKindWeightSymbolOrdering is an integration test that builds a FileSymbols
// with symbols in deliberately reverse order and verifies that formatFileBlockDefault
// emits them in weight-descending order: struct → var → (unexported func omitted).
func TestKindWeightSymbolOrdering(t *testing.T) {
t.Parallel()
syms := []Symbol{
{Name: "helper", Kind: "function", Exported: false, Line: 1}, // unexported — should be omitted
{Name: "MaxSize", Kind: "variable", Exported: true, Line: 2}, // weight 3
{Name: "Agent", Kind: "struct", Exported: true, Line: 3}, // weight 10
}
f := makeRankedFile("core/agent.go", 2, syms)
out := formatFileBlockDefault(f, false)
// Unexported symbol must not appear.
assert.NotContains(t, out, "helper", "unexported func must be omitted")
// Both exported symbols must appear.
assert.Contains(t, out, "Agent", "exported struct must appear")
assert.Contains(t, out, "MaxSize", "exported var must appear")
// Agent (struct, weight 10) must appear before MaxSize (var, weight 3).
agentPos := strings.Index(out, "Agent")
maxSizePos := strings.Index(out, "MaxSize")
assert.Greater(t, maxSizePos, agentPos, "struct (Agent) must appear before var (MaxSize)")
}
// TestKindWeightSymbolOrdering_WithFunction verifies that exported struct → exported
// function → exported const ordering is preserved across all three weight tiers.
func TestKindWeightSymbolOrdering_WithFunction(t *testing.T) {
t.Parallel()
// Intentionally listed in reverse weight order.
syms := []Symbol{
{Name: "DefaultTimeout", Kind: "const", Exported: true, Line: 1}, // weight 3
{Name: "New", Kind: "function", Exported: true, Line: 2}, // weight 6
{Name: "Config", Kind: "struct", Exported: true, Line: 3}, // weight 10
}
f := makeRankedFile("pkg/config.go", 2, syms)
out := formatFileBlockDefault(f, false)
configPos := strings.Index(out, "Config")
newPos := strings.Index(out, "New")
timeoutPos := strings.Index(out, "DefaultTimeout")
assert.Greater(t, newPos, configPos, "struct (Config) must appear before function (New)")
assert.Greater(t, timeoutPos, newPos, "function (New) must appear before const (DefaultTimeout)")
}