-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsearch_filter_test.go
More file actions
254 lines (219 loc) · 6.54 KB
/
search_filter_test.go
File metadata and controls
254 lines (219 loc) · 6.54 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// SPDX-License-Identifier: MIT
package main
import (
"testing"
"github.com/boyter/scc/v3/processor"
)
func TestFilterMatchLocations_OnlyCode_KeepsCodeMatches(t *testing.T) {
bt := make([]byte, 20)
for i := range bt {
bt[i] = processor.ByteTypeCode
}
locs := map[string][][]int{"func": {{0, 4}, {10, 14}}}
cfg := &Config{OnlyCode: true}
filtered, ok := filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected matches to survive")
}
if len(filtered["func"]) != 2 {
t.Errorf("expected 2 locations, got %d", len(filtered["func"]))
}
}
func TestFilterMatchLocations_OnlyCode_RemovesCommentMatches(t *testing.T) {
bt := make([]byte, 20)
for i := range bt {
bt[i] = processor.ByteTypeComment
}
locs := map[string][][]int{"TODO": {{0, 4}}}
cfg := &Config{OnlyCode: true}
_, ok := filterMatchLocations(locs, bt, cfg)
if ok {
t.Fatal("expected no matches to survive")
}
}
func TestFilterMatchLocations_OnlyComments_KeepsCommentMatches(t *testing.T) {
bt := make([]byte, 20)
for i := range bt {
bt[i] = processor.ByteTypeComment
}
locs := map[string][][]int{"TODO": {{0, 4}}}
cfg := &Config{OnlyComments: true}
filtered, ok := filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected matches to survive")
}
if len(filtered["TODO"]) != 1 {
t.Errorf("expected 1 location, got %d", len(filtered["TODO"]))
}
}
func TestFilterMatchLocations_OnlyComments_RemovesCodeMatches(t *testing.T) {
bt := make([]byte, 20)
for i := range bt {
bt[i] = processor.ByteTypeCode
}
locs := map[string][][]int{"func": {{0, 4}}}
cfg := &Config{OnlyComments: true}
_, ok := filterMatchLocations(locs, bt, cfg)
if ok {
t.Fatal("expected no matches to survive")
}
}
func TestFilterMatchLocations_OnlyStrings_KeepsStringMatches(t *testing.T) {
bt := make([]byte, 20)
for i := range bt {
bt[i] = processor.ByteTypeString
}
locs := map[string][][]int{"hello": {{0, 5}}}
cfg := &Config{OnlyStrings: true}
filtered, ok := filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected matches to survive")
}
if len(filtered["hello"]) != 1 {
t.Errorf("expected 1 location, got %d", len(filtered["hello"]))
}
}
func TestFilterMatchLocations_OnlyStrings_RemovesCodeMatches(t *testing.T) {
bt := make([]byte, 20)
for i := range bt {
bt[i] = processor.ByteTypeCode
}
locs := map[string][][]int{"func": {{0, 4}}}
cfg := &Config{OnlyStrings: true}
_, ok := filterMatchLocations(locs, bt, cfg)
if ok {
t.Fatal("expected no matches to survive")
}
}
func TestFilterMatchLocations_MixedContent_FiltersCorrectly(t *testing.T) {
bt := make([]byte, 20)
// Bytes 0-4: code, 5-9: comment, 10-14: string, 15-19: code
for i := 0; i < 5; i++ {
bt[i] = processor.ByteTypeCode
}
for i := 5; i < 10; i++ {
bt[i] = processor.ByteTypeComment
}
for i := 10; i < 15; i++ {
bt[i] = processor.ByteTypeString
}
for i := 15; i < 20; i++ {
bt[i] = processor.ByteTypeCode
}
locs := map[string][][]int{
"match": {{0, 4}, {5, 9}, {10, 14}, {15, 19}},
}
// OnlyCode should keep code matches (at 0 and 15)
cfg := &Config{OnlyCode: true}
filtered, ok := filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected matches to survive")
}
if len(filtered["match"]) != 2 {
t.Errorf("expected 2 code locations, got %d", len(filtered["match"]))
}
// OnlyComments should keep comment match (at 5)
cfg = &Config{OnlyComments: true}
filtered, ok = filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected matches to survive")
}
if len(filtered["match"]) != 1 {
t.Errorf("expected 1 comment location, got %d", len(filtered["match"]))
}
// OnlyStrings should keep string match (at 10)
cfg = &Config{OnlyStrings: true}
filtered, ok = filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected matches to survive")
}
if len(filtered["match"]) != 1 {
t.Errorf("expected 1 string location, got %d", len(filtered["match"]))
}
}
func TestFilterMatchLocations_NilByteType_NoFilter_PassesThrough(t *testing.T) {
locs := map[string][][]int{"func": {{0, 4}}}
cfg := &Config{}
filtered, ok := filterMatchLocations(locs, nil, cfg)
if !ok {
t.Fatal("expected matches to pass through with nil byte type and no filter")
}
if len(filtered["func"]) != 1 {
t.Errorf("expected 1 location, got %d", len(filtered["func"]))
}
}
func TestFilterMatchLocations_NilByteType_OnlyComments_Excluded(t *testing.T) {
locs := map[string][][]int{"func": {{0, 4}}}
cfg := &Config{OnlyComments: true}
_, ok := filterMatchLocations(locs, nil, cfg)
if ok {
t.Fatal("expected nil byte type with OnlyComments to exclude file")
}
}
func TestFilterMatchLocations_NilByteType_OnlyCode_Excluded(t *testing.T) {
locs := map[string][][]int{"func": {{0, 4}}}
cfg := &Config{OnlyCode: true}
_, ok := filterMatchLocations(locs, nil, cfg)
if ok {
t.Fatal("expected nil byte type with OnlyCode to exclude file")
}
}
func TestFilterMatchLocations_NilByteType_OnlyStrings_Excluded(t *testing.T) {
locs := map[string][][]int{"func": {{0, 4}}}
cfg := &Config{OnlyStrings: true}
_, ok := filterMatchLocations(locs, nil, cfg)
if ok {
t.Fatal("expected nil byte type with OnlyStrings to exclude file")
}
}
func TestFilterMatchLocations_AllFilteredOut_ReturnsFalse(t *testing.T) {
bt := make([]byte, 20)
for i := range bt {
bt[i] = processor.ByteTypeString
}
locs := map[string][][]int{"hello": {{0, 5}}}
cfg := &Config{OnlyCode: true}
_, ok := filterMatchLocations(locs, bt, cfg)
if ok {
t.Fatal("expected no matches to survive when all in wrong type")
}
}
func TestFilterMatchLocations_OnlyCode_KeepsBlankAsCode(t *testing.T) {
bt := make([]byte, 10)
for i := range bt {
bt[i] = processor.ByteTypeBlank
}
locs := map[string][][]int{"x": {{0, 1}}}
cfg := &Config{OnlyCode: true}
filtered, ok := filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected blank-region matches to pass through with OnlyCode")
}
if len(filtered["x"]) != 1 {
t.Errorf("expected 1 location, got %d", len(filtered["x"]))
}
}
func TestFilterMatchLocations_MultipleTerms_PartialFilter(t *testing.T) {
bt := make([]byte, 20)
for i := 0; i < 10; i++ {
bt[i] = processor.ByteTypeCode
}
for i := 10; i < 20; i++ {
bt[i] = processor.ByteTypeComment
}
locs := map[string][][]int{
"code_term": {{0, 4}},
"comment_term": {{10, 14}},
}
cfg := &Config{OnlyCode: true}
filtered, ok := filterMatchLocations(locs, bt, cfg)
if !ok {
t.Fatal("expected at least one term to survive")
}
if _, exists := filtered["code_term"]; !exists {
t.Error("expected code_term to survive")
}
if _, exists := filtered["comment_term"]; exists {
t.Error("expected comment_term to be filtered out")
}
}