Skip to content

Commit 1462e78

Browse files
committed
refactor: consolidate stringToToken into a single shared function
Each mutator had its own stringToToken method with overlapping operator mappings. Consolidate into a single package-level function in token.go that handles all operator types, reducing boilerplate when adding new mutators.
1 parent 1473567 commit 1462e78

File tree

10 files changed

+160
-209
lines changed

10 files changed

+160
-209
lines changed

internal/mutation/arithmetic.go

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (m *ArithmeticMutator) applyBinary(node ast.Node, mutant Mutant) bool {
195195
return false
196196
}
197197

198-
newOp := m.stringToToken(mutant.Mutated)
198+
newOp := stringToToken(mutant.Mutated)
199199
if newOp != token.ILLEGAL {
200200
expr.Op = newOp
201201

@@ -214,7 +214,7 @@ func (m *ArithmeticMutator) applyAssign(node ast.Node, mutant Mutant) bool {
214214
return false
215215
}
216216

217-
newOp := m.stringToToken(mutant.Mutated)
217+
newOp := stringToToken(mutant.Mutated)
218218
if newOp != token.ILLEGAL {
219219
stmt.Tok = newOp
220220

@@ -233,7 +233,7 @@ func (m *ArithmeticMutator) applyIncDec(node ast.Node, mutant Mutant) bool {
233233
return false
234234
}
235235

236-
newOp := m.stringToToken(mutant.Mutated)
236+
newOp := stringToToken(mutant.Mutated)
237237
if newOp != token.ILLEGAL {
238238
stmt.Tok = newOp
239239

@@ -243,33 +243,3 @@ func (m *ArithmeticMutator) applyIncDec(node ast.Node, mutant Mutant) bool {
243243

244244
return false
245245
}
246-
247-
// stringToToken converts string representation to token.Token for arithmetic operations.
248-
func (m *ArithmeticMutator) stringToToken(s string) token.Token {
249-
switch s {
250-
case "+":
251-
return token.ADD
252-
case "-":
253-
return token.SUB
254-
case "*":
255-
return token.MUL
256-
case "/":
257-
return token.QUO
258-
case "%":
259-
return token.REM
260-
case "++":
261-
return token.INC
262-
case "--":
263-
return token.DEC
264-
case "+=":
265-
return token.ADD_ASSIGN
266-
case "-=":
267-
return token.SUB_ASSIGN
268-
case "*=":
269-
return token.MUL_ASSIGN
270-
case "/=":
271-
return token.QUO_ASSIGN
272-
default:
273-
return token.ILLEGAL
274-
}
275-
}

internal/mutation/arithmetic_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -475,34 +475,3 @@ func TestArithmeticMutator_Apply(t *testing.T) {
475475
})
476476
}
477477
}
478-
479-
func TestArithmeticMutator_stringToToken(t *testing.T) {
480-
mutator := &ArithmeticMutator{}
481-
482-
tests := []struct {
483-
input string
484-
expected token.Token
485-
}{
486-
{"+", token.ADD},
487-
{"-", token.SUB},
488-
{"*", token.MUL},
489-
{"/", token.QUO},
490-
{"%", token.REM},
491-
{"+=", token.ADD_ASSIGN},
492-
{"-=", token.SUB_ASSIGN},
493-
{"*=", token.MUL_ASSIGN},
494-
{"/=", token.QUO_ASSIGN},
495-
{"++", token.INC},
496-
{"--", token.DEC},
497-
{"invalid", token.ILLEGAL},
498-
}
499-
500-
for _, tt := range tests {
501-
t.Run(tt.input, func(t *testing.T) {
502-
result := mutator.stringToToken(tt.input)
503-
if result != tt.expected {
504-
t.Errorf("stringToToken(%q) = %v, expected %v", tt.input, result, tt.expected)
505-
}
506-
})
507-
}
508-
}

internal/mutation/bitwise.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (m *BitwiseMutator) applyBinary(node ast.Node, mutant Mutant) bool {
162162
return false
163163
}
164164

165-
newOp := m.stringToToken(mutant.Mutated)
165+
newOp := stringToToken(mutant.Mutated)
166166
if newOp != token.ILLEGAL {
167167
expr.Op = newOp
168168

@@ -181,7 +181,7 @@ func (m *BitwiseMutator) applyAssign(node ast.Node, mutant Mutant) bool {
181181
return false
182182
}
183183

184-
newOp := m.stringToToken(mutant.Mutated)
184+
newOp := stringToToken(mutant.Mutated)
185185
if newOp != token.ILLEGAL {
186186
stmt.Tok = newOp
187187

@@ -191,33 +191,3 @@ func (m *BitwiseMutator) applyAssign(node ast.Node, mutant Mutant) bool {
191191

192192
return false
193193
}
194-
195-
// stringToToken converts string representation to token.Token for bitwise operations.
196-
func (m *BitwiseMutator) stringToToken(s string) token.Token {
197-
switch s {
198-
case "&":
199-
return token.AND
200-
case "|":
201-
return token.OR
202-
case "^":
203-
return token.XOR
204-
case "&^":
205-
return token.AND_NOT
206-
case "<<":
207-
return token.SHL
208-
case ">>":
209-
return token.SHR
210-
case "&=":
211-
return token.AND_ASSIGN
212-
case "|=":
213-
return token.OR_ASSIGN
214-
case "^=":
215-
return token.XOR_ASSIGN
216-
case "<<=":
217-
return token.SHL_ASSIGN
218-
case ">>=":
219-
return token.SHR_ASSIGN
220-
default:
221-
return token.ILLEGAL
222-
}
223-
}

internal/mutation/bitwise_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -293,37 +293,6 @@ func TestBitwiseMutator_Apply(t *testing.T) {
293293
}
294294
}
295295

296-
func TestBitwiseMutator_stringToToken(t *testing.T) {
297-
mutator := &BitwiseMutator{}
298-
299-
tests := []struct {
300-
input string
301-
expected token.Token
302-
}{
303-
{"&", token.AND},
304-
{"|", token.OR},
305-
{"^", token.XOR},
306-
{"&^", token.AND_NOT},
307-
{"<<", token.SHL},
308-
{">>", token.SHR},
309-
{"&=", token.AND_ASSIGN},
310-
{"|=", token.OR_ASSIGN},
311-
{"^=", token.XOR_ASSIGN},
312-
{"<<=", token.SHL_ASSIGN},
313-
{">>=", token.SHR_ASSIGN},
314-
{"invalid", token.ILLEGAL},
315-
}
316-
317-
for _, tt := range tests {
318-
t.Run(tt.input, func(t *testing.T) {
319-
result := mutator.stringToToken(tt.input)
320-
if result != tt.expected {
321-
t.Errorf("stringToToken(%q) = %v, expected %v", tt.input, result, tt.expected)
322-
}
323-
})
324-
}
325-
}
326-
327296
func TestBitwiseMutator_getBitwiseMutations(t *testing.T) {
328297
mutator := &BitwiseMutator{}
329298

internal/mutation/conditional.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (m *ConditionalMutator) applyBinary(node ast.Node, mutant Mutant) bool {
108108
return false
109109
}
110110

111-
newOp := m.stringToToken(mutant.Mutated)
111+
newOp := stringToToken(mutant.Mutated)
112112
if newOp != token.ILLEGAL {
113113
expr.Op = newOp
114114

@@ -118,23 +118,3 @@ func (m *ConditionalMutator) applyBinary(node ast.Node, mutant Mutant) bool {
118118

119119
return false
120120
}
121-
122-
// stringToToken converts string representation to token.Token for conditional operations.
123-
func (m *ConditionalMutator) stringToToken(s string) token.Token {
124-
switch s {
125-
case "==":
126-
return token.EQL
127-
case "!=":
128-
return token.NEQ
129-
case "<":
130-
return token.LSS
131-
case "<=":
132-
return token.LEQ
133-
case ">":
134-
return token.GTR
135-
case ">=":
136-
return token.GEQ
137-
default:
138-
return token.ILLEGAL
139-
}
140-
}

internal/mutation/conditional_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -451,29 +451,3 @@ func TestConditionalMutator_Apply(t *testing.T) {
451451
})
452452
}
453453
}
454-
455-
func TestConditionalMutator_stringToToken(t *testing.T) {
456-
mutator := &ConditionalMutator{}
457-
458-
tests := []struct {
459-
input string
460-
expected token.Token
461-
}{
462-
{"==", token.EQL},
463-
{"!=", token.NEQ},
464-
{"<", token.LSS},
465-
{"<=", token.LEQ},
466-
{">", token.GTR},
467-
{">=", token.GEQ},
468-
{"invalid", token.ILLEGAL},
469-
}
470-
471-
for _, tt := range tests {
472-
t.Run(tt.input, func(t *testing.T) {
473-
result := mutator.stringToToken(tt.input)
474-
if result != tt.expected {
475-
t.Errorf("stringToToken(%q) = %v, expected %v", tt.input, result, tt.expected)
476-
}
477-
})
478-
}
479-
}

internal/mutation/logical.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (m *LogicalMutator) applyBinary(node ast.Node, mutant Mutant) bool {
9595
return false
9696
}
9797

98-
newOp := m.stringToToken(mutant.Mutated)
98+
newOp := stringToToken(mutant.Mutated)
9999
if newOp != token.ILLEGAL {
100100
expr.Op = newOp
101101

@@ -105,15 +105,3 @@ func (m *LogicalMutator) applyBinary(node ast.Node, mutant Mutant) bool {
105105

106106
return false
107107
}
108-
109-
// stringToToken converts string representation to token.Token for logical operations.
110-
func (m *LogicalMutator) stringToToken(s string) token.Token {
111-
switch s {
112-
case "&&":
113-
return token.LAND
114-
case "||":
115-
return token.LOR
116-
default:
117-
return token.ILLEGAL
118-
}
119-
}

internal/mutation/logical_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -272,25 +272,3 @@ func TestLogicalMutator_Apply(t *testing.T) {
272272
})
273273
}
274274
}
275-
276-
func TestLogicalMutator_stringToToken(t *testing.T) {
277-
mutator := &LogicalMutator{}
278-
279-
tests := []struct {
280-
input string
281-
expected token.Token
282-
}{
283-
{"&&", token.LAND},
284-
{"||", token.LOR},
285-
{"invalid", token.ILLEGAL},
286-
}
287-
288-
for _, tt := range tests {
289-
t.Run(tt.input, func(t *testing.T) {
290-
result := mutator.stringToToken(tt.input)
291-
if result != tt.expected {
292-
t.Errorf("stringToToken(%q) = %v, expected %v", tt.input, result, tt.expected)
293-
}
294-
})
295-
}
296-
}

internal/mutation/token.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package mutation
2+
3+
import "go/token"
4+
5+
// stringToToken converts a string representation of an operator to its token.Token.
6+
//
7+
//nolint:gocyclo,cyclop
8+
func stringToToken(s string) token.Token {
9+
switch s {
10+
// Arithmetic
11+
case "+":
12+
return token.ADD
13+
case "-":
14+
return token.SUB
15+
case "*":
16+
return token.MUL
17+
case "/":
18+
return token.QUO
19+
case "%":
20+
return token.REM
21+
case "++":
22+
return token.INC
23+
case "--":
24+
return token.DEC
25+
26+
// Arithmetic assignment
27+
case "+=":
28+
return token.ADD_ASSIGN
29+
case "-=":
30+
return token.SUB_ASSIGN
31+
case "*=":
32+
return token.MUL_ASSIGN
33+
case "/=":
34+
return token.QUO_ASSIGN
35+
36+
// Conditional
37+
case "==":
38+
return token.EQL
39+
case "!=":
40+
return token.NEQ
41+
case "<":
42+
return token.LSS
43+
case "<=":
44+
return token.LEQ
45+
case ">":
46+
return token.GTR
47+
case ">=":
48+
return token.GEQ
49+
50+
// Logical
51+
case "&&":
52+
return token.LAND
53+
case "||":
54+
return token.LOR
55+
56+
// Bitwise
57+
case "&":
58+
return token.AND
59+
case "|":
60+
return token.OR
61+
case "^":
62+
return token.XOR
63+
case "&^":
64+
return token.AND_NOT
65+
case "<<":
66+
return token.SHL
67+
case ">>":
68+
return token.SHR
69+
70+
// Bitwise assignment
71+
case "&=":
72+
return token.AND_ASSIGN
73+
case "|=":
74+
return token.OR_ASSIGN
75+
case "^=":
76+
return token.XOR_ASSIGN
77+
case "<<=":
78+
return token.SHL_ASSIGN
79+
case ">>=":
80+
return token.SHR_ASSIGN
81+
82+
default:
83+
return token.ILLEGAL
84+
}
85+
}

0 commit comments

Comments
 (0)