|
| 1 | +package dce |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestEliminateDeadCode_RemovesOrphanedComments(t *testing.T) { |
| 12 | + src := `package gen |
| 13 | +
|
| 14 | +func usedFunc() { |
| 15 | + helperFunc() |
| 16 | +} |
| 17 | +
|
| 18 | +// --- oapi-runtime begin --- |
| 19 | +
|
| 20 | +// helperFunc is used by usedFunc. |
| 21 | +func helperFunc() {} |
| 22 | +
|
| 23 | +// unusedFunc does nothing useful. |
| 24 | +// It has a multi-line doc comment. |
| 25 | +func unusedFunc() {} |
| 26 | +
|
| 27 | +// --- oapi-runtime end --- |
| 28 | +` |
| 29 | + |
| 30 | + result, err := EliminateDeadCode(src) |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + assert.Contains(t, result, "helperFunc") |
| 34 | + assert.NotContains(t, result, "unusedFunc") |
| 35 | +} |
| 36 | + |
| 37 | +func TestEliminateDeadCode_NoMarkers(t *testing.T) { |
| 38 | + src := `package gen |
| 39 | +
|
| 40 | +func foo() {} |
| 41 | +` |
| 42 | + result, err := EliminateDeadCode(src) |
| 43 | + require.NoError(t, err) |
| 44 | + assert.Equal(t, src, result) |
| 45 | +} |
| 46 | + |
| 47 | +func TestEliminateDeadCode_AllUsed(t *testing.T) { |
| 48 | + src := `package gen |
| 49 | +
|
| 50 | +func caller() { |
| 51 | + a() |
| 52 | + b() |
| 53 | +} |
| 54 | +
|
| 55 | +// --- oapi-runtime begin --- |
| 56 | +
|
| 57 | +// a does something. |
| 58 | +func a() {} |
| 59 | +
|
| 60 | +// b does something else. |
| 61 | +func b() {} |
| 62 | +
|
| 63 | +// --- oapi-runtime end --- |
| 64 | +` |
| 65 | + |
| 66 | + result, err := EliminateDeadCode(src) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + assert.Contains(t, result, "func a()") |
| 70 | + assert.Contains(t, result, "func b()") |
| 71 | + assert.Contains(t, result, "a does something") |
| 72 | + assert.Contains(t, result, "b does something else") |
| 73 | +} |
| 74 | + |
| 75 | +func TestEliminateDeadCode_NoneUsed(t *testing.T) { |
| 76 | + src := `package gen |
| 77 | +
|
| 78 | +func caller() {} |
| 79 | +
|
| 80 | +// --- oapi-runtime begin --- |
| 81 | +
|
| 82 | +// orphanA is unused. |
| 83 | +func orphanA() {} |
| 84 | +
|
| 85 | +// orphanB is also unused. |
| 86 | +func orphanB() {} |
| 87 | +
|
| 88 | +// --- oapi-runtime end --- |
| 89 | +` |
| 90 | + |
| 91 | + result, err := EliminateDeadCode(src) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + assert.NotContains(t, result, "orphanA") |
| 95 | + assert.NotContains(t, result, "orphanB") |
| 96 | +} |
| 97 | + |
| 98 | +func TestEliminateDeadCode_InlineComments(t *testing.T) { |
| 99 | + src := `package gen |
| 100 | +
|
| 101 | +func caller() { |
| 102 | + used() |
| 103 | +} |
| 104 | +
|
| 105 | +// --- oapi-runtime begin --- |
| 106 | +
|
| 107 | +func used() {} // keep this |
| 108 | +
|
| 109 | +func unused() {} // drop this |
| 110 | +
|
| 111 | +// --- oapi-runtime end --- |
| 112 | +` |
| 113 | + |
| 114 | + result, err := EliminateDeadCode(src) |
| 115 | + require.NoError(t, err) |
| 116 | + |
| 117 | + assert.Contains(t, result, "keep this") |
| 118 | + assert.NotContains(t, result, "drop this") |
| 119 | +} |
| 120 | + |
| 121 | +func TestEliminateDeadCode_TypeDecl(t *testing.T) { |
| 122 | + src := `package gen |
| 123 | +
|
| 124 | +// UsedType is referenced. |
| 125 | +type UsedType struct{} |
| 126 | +
|
| 127 | +// --- oapi-runtime begin --- |
| 128 | +
|
| 129 | +// helperType is used by UsedType (transitively). |
| 130 | +type helperType = UsedType |
| 131 | +
|
| 132 | +// unusedType is not referenced. |
| 133 | +type unusedType struct { |
| 134 | + field string |
| 135 | +} |
| 136 | +
|
| 137 | +// --- oapi-runtime end --- |
| 138 | +` |
| 139 | + |
| 140 | + result, err := EliminateDeadCode(src) |
| 141 | + require.NoError(t, err) |
| 142 | + |
| 143 | + // helperType is reachable because it references UsedType which is in roots, |
| 144 | + // and helperType itself may or may not be reachable depending on the algorithm. |
| 145 | + // The key assertion: unusedType and its comment should be gone. |
| 146 | + assert.NotContains(t, result, "unusedType") |
| 147 | + assert.NotContains(t, result, "unusedType is not referenced") |
| 148 | +} |
| 149 | + |
| 150 | +func TestEliminateDeadCode_PreservesNonRuntimeComments(t *testing.T) { |
| 151 | + src := `package gen |
| 152 | +
|
| 153 | +// Package-level comment that should survive. |
| 154 | +
|
| 155 | +// caller calls helper. |
| 156 | +func caller() { |
| 157 | + helper() |
| 158 | +} |
| 159 | +
|
| 160 | +// --- oapi-runtime begin --- |
| 161 | +
|
| 162 | +// helper is needed. |
| 163 | +func helper() {} |
| 164 | +
|
| 165 | +// unused is not needed. |
| 166 | +func unused() {} |
| 167 | +
|
| 168 | +// --- oapi-runtime end --- |
| 169 | +` |
| 170 | + |
| 171 | + result, err := EliminateDeadCode(src) |
| 172 | + require.NoError(t, err) |
| 173 | + |
| 174 | + assert.Contains(t, result, "Package-level comment") |
| 175 | + assert.Contains(t, result, "caller calls helper") |
| 176 | + assert.Contains(t, result, "helper is needed") |
| 177 | + assert.NotContains(t, result, "unused is not needed") |
| 178 | + // The word "unused" should only not appear as a function or its comment |
| 179 | + lines := strings.Split(result, "\n") |
| 180 | + for _, line := range lines { |
| 181 | + trimmed := strings.TrimSpace(line) |
| 182 | + if strings.Contains(trimmed, "unused") && !strings.Contains(trimmed, "nolint") { |
| 183 | + t.Errorf("unexpected reference to 'unused' in output: %s", trimmed) |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments