-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdce_test.go
More file actions
186 lines (133 loc) · 3.67 KB
/
dce_test.go
File metadata and controls
186 lines (133 loc) · 3.67 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package dce
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEliminateDeadCode_RemovesOrphanedComments(t *testing.T) {
src := `package gen
func usedFunc() {
helperFunc()
}
// --- oapi-runtime begin ---
// helperFunc is used by usedFunc.
func helperFunc() {}
// unusedFunc does nothing useful.
// It has a multi-line doc comment.
func unusedFunc() {}
// --- oapi-runtime end ---
`
result, err := EliminateDeadCode(src)
require.NoError(t, err)
assert.Contains(t, result, "helperFunc")
assert.NotContains(t, result, "unusedFunc")
}
func TestEliminateDeadCode_NoMarkers(t *testing.T) {
src := `package gen
func foo() {}
`
result, err := EliminateDeadCode(src)
require.NoError(t, err)
assert.Equal(t, src, result)
}
func TestEliminateDeadCode_AllUsed(t *testing.T) {
src := `package gen
func caller() {
a()
b()
}
// --- oapi-runtime begin ---
// a does something.
func a() {}
// b does something else.
func b() {}
// --- oapi-runtime end ---
`
result, err := EliminateDeadCode(src)
require.NoError(t, err)
assert.Contains(t, result, "func a()")
assert.Contains(t, result, "func b()")
assert.Contains(t, result, "a does something")
assert.Contains(t, result, "b does something else")
}
func TestEliminateDeadCode_NoneUsed(t *testing.T) {
src := `package gen
func caller() {}
// --- oapi-runtime begin ---
// orphanA is unused.
func orphanA() {}
// orphanB is also unused.
func orphanB() {}
// --- oapi-runtime end ---
`
result, err := EliminateDeadCode(src)
require.NoError(t, err)
assert.NotContains(t, result, "orphanA")
assert.NotContains(t, result, "orphanB")
}
func TestEliminateDeadCode_InlineComments(t *testing.T) {
src := `package gen
func caller() {
used()
}
// --- oapi-runtime begin ---
func used() {} // keep this
func unused() {} // drop this
// --- oapi-runtime end ---
`
result, err := EliminateDeadCode(src)
require.NoError(t, err)
assert.Contains(t, result, "keep this")
assert.NotContains(t, result, "drop this")
}
func TestEliminateDeadCode_TypeDecl(t *testing.T) {
src := `package gen
// UsedType is referenced.
type UsedType struct{}
// --- oapi-runtime begin ---
// helperType is used by UsedType (transitively).
type helperType = UsedType
// unusedType is not referenced.
type unusedType struct {
field string
}
// --- oapi-runtime end ---
`
result, err := EliminateDeadCode(src)
require.NoError(t, err)
// helperType is reachable because it references UsedType which is in roots,
// and helperType itself may or may not be reachable depending on the algorithm.
// The key assertion: unusedType and its comment should be gone.
assert.NotContains(t, result, "unusedType")
assert.NotContains(t, result, "unusedType is not referenced")
}
func TestEliminateDeadCode_PreservesNonRuntimeComments(t *testing.T) {
src := `package gen
// Package-level comment that should survive.
// caller calls helper.
func caller() {
helper()
}
// --- oapi-runtime begin ---
// helper is needed.
func helper() {}
// unused is not needed.
func unused() {}
// --- oapi-runtime end ---
`
result, err := EliminateDeadCode(src)
require.NoError(t, err)
assert.Contains(t, result, "Package-level comment")
assert.Contains(t, result, "caller calls helper")
assert.Contains(t, result, "helper is needed")
assert.NotContains(t, result, "unused is not needed")
// The word "unused" should only not appear as a function or its comment
lines := strings.Split(result, "\n")
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.Contains(trimmed, "unused") && !strings.Contains(trimmed, "nolint") {
t.Errorf("unexpected reference to 'unused' in output: %s", trimmed)
}
}
}