/
comparison.go
48 lines (40 loc) 路 913 Bytes
/
comparison.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
package comparison
import (
"go/ast"
"go/token"
"github.com/gtramontina/ooze/viruses"
)
type Comparison struct {
mutations map[token.Token]token.Token
}
// New returns a new Comparison virus.
//
// It replaces `<` with `<=`, `>` with `>=` and vice versa.
func New() *Comparison {
return &Comparison{
mutations: map[token.Token]token.Token{
token.LSS: token.LEQ,
token.LEQ: token.LSS,
token.GTR: token.GEQ,
token.GEQ: token.GTR,
},
}
}
func (v *Comparison) Incubate(node ast.Node) []*viruses.Infection {
expression, matches := node.(*ast.BinaryExpr)
if !matches {
return nil
}
originalOperation := expression.Op
mutatedOperation, matches := v.mutations[expression.Op]
if !matches {
return nil
}
return []*viruses.Infection{
viruses.NewInfection(
"Comparison",
func() { expression.Op = mutatedOperation },
func() { expression.Op = originalOperation },
),
}
}