-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcomparisoninvert.go
51 lines (43 loc) · 1.02 KB
/
comparisoninvert.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
package comparisoninvert
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type ComparisonInvert struct {
mutations map[token.Token]token.Token
}
// New returns a new ComparisonInvert virus.
//
// It replaces `>` with `<=`, `<` with `>=`, `==` with `!=` and vice versa.
func New() *ComparisonInvert {
return &ComparisonInvert{
mutations: map[token.Token]token.Token{
token.GTR: token.LEQ,
token.LSS: token.GEQ,
token.GEQ: token.LSS,
token.LEQ: token.GTR,
token.EQL: token.NEQ,
token.NEQ: token.EQL,
},
}
}
func (v *ComparisonInvert) Incubate(node ast.Node, _ *types.Info) []*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 Invert",
func() { expression.Op = mutatedOperation },
func() { expression.Op = originalOperation },
),
}
}