-
Notifications
You must be signed in to change notification settings - Fork 8
/
comparisonreplace.go
63 lines (52 loc) · 1.45 KB
/
comparisonreplace.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
51
52
53
54
55
56
57
58
59
60
61
62
63
package comparisonreplace
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type ComparisonReplace struct {
mutations map[token.Token]*ast.Ident
}
// New returns a new ComparisonReplace virus.
//
// It replaces the left and right sides of an `&&` comparison with `true` and
// the left and right sides of an `||` with false. E.g. `1 == 1 && 2 == 2` gets
// two mutations: `true && 2 == 2` and `1 == 1 && true`.
func New() *ComparisonReplace {
return &ComparisonReplace{
mutations: map[token.Token]*ast.Ident{
token.LAND: ast.NewIdent("true"),
token.LOR: ast.NewIdent("false"),
},
}
}
func (v *ComparisonReplace) Incubate(node ast.Node, _ *types.Info) []*viruses.Infection {
expression, matches := node.(*ast.BinaryExpr)
if !matches {
return nil
}
mutatedBoolean, matches := v.mutations[expression.Op]
if !matches {
return nil
}
originalX := expression.X
originalY := expression.Y
infections := []*viruses.Infection{}
if fmt.Sprint(originalX) != fmt.Sprint(mutatedBoolean) {
infections = append(infections, viruses.NewInfection(
"Comparison Replace",
func() { expression.X = mutatedBoolean },
func() { expression.X = originalX },
))
}
if fmt.Sprint(originalY) != fmt.Sprint(mutatedBoolean) {
infections = append(infections, viruses.NewInfection(
"Comparison Replace",
func() { expression.Y = mutatedBoolean },
func() { expression.Y = originalY },
))
}
return infections
}