forked from avito-tech/go-mutesting
/
base.go
46 lines (39 loc) · 808 Bytes
/
base.go
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
package arithmetic
import (
"go/ast"
"go/token"
"go/types"
"github.com/JekaMas/go-mutesting/mutator"
)
func init() {
mutator.Register("arithmetic/base", MutatorArithmeticBase)
}
var arithmeticMutations = map[token.Token]token.Token{
token.ADD: token.SUB,
token.SUB: token.ADD,
token.MUL: token.QUO,
token.QUO: token.MUL,
token.REM: token.MUL,
}
// MutatorArithmeticBase implements a mutator to change base arithmetic.
func MutatorArithmeticBase(_ *types.Package, _ *types.Info, node ast.Node) []mutator.Mutation {
n, ok := node.(*ast.BinaryExpr)
if !ok {
return nil
}
original := n.Op
mutated, ok := arithmeticMutations[n.Op]
if !ok {
return nil
}
return []mutator.Mutation{
{
Change: func() {
n.Op = mutated
},
Reset: func() {
n.Op = original
},
},
}
}