/
mutateif.go
50 lines (41 loc) · 990 Bytes
/
mutateif.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
47
48
49
50
package branch
import (
"go/ast"
"github.com/zimmski/go-mutesting/astutil"
"github.com/zimmski/go-mutesting/mutator"
)
func init() {
mutator.Register(MutatorIf{}.String(), func() mutator.Mutator {
return NewMutatorIf()
})
}
// NewMutatorIf returns a new instance of a MutatorIf mutator
func NewMutatorIf() *MutatorIf {
return &MutatorIf{}
}
// MutatorIf implements a mutator for if and else if branches
type MutatorIf struct{}
// String implements the String method of the Stringer interface
func (m MutatorIf) String() string {
return "branch/if"
}
// Mutations returns a list of possible mutations for the given node.
func (m *MutatorIf) Mutations(node ast.Node) []mutator.Mutation {
n, ok := node.(*ast.IfStmt)
if !ok {
return nil
}
old := n.Body.List
return []mutator.Mutation{
mutator.Mutation{
Change: func() {
n.Body.List = []ast.Stmt{
astutil.CreateNoopOfStatement(n.Body),
}
},
Reset: func() {
n.Body.List = old
},
},
}
}