Skip to content

Commit 88a05ba

Browse files
committed
Add comparison expression mutator
1 parent 4e8457f commit 88a05ba

File tree

8 files changed

+199
-3
lines changed

8 files changed

+199
-3
lines changed

cmd/go-mutesting/main_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestMain(t *testing.T) {
1515
"../../example",
1616
[]string{"--debug", "--exec-timeout", "1"},
1717
returnOk,
18-
"The mutation score is 0.500000 (8 passed, 8 failed, 8 duplicated, 0 skipped, total is 16)",
18+
"The mutation score is 0.450000 (9 passed, 11 failed, 8 duplicated, 0 skipped, total is 20)",
1919
)
2020
}
2121

@@ -25,7 +25,7 @@ func TestMainRecursive(t *testing.T) {
2525
"../../example",
2626
[]string{"--debug", "--exec-timeout", "1", "./..."},
2727
returnOk,
28-
"The mutation score is 0.529412 (9 passed, 8 failed, 8 duplicated, 0 skipped, total is 17)",
28+
"The mutation score is 0.476190 (10 passed, 11 failed, 8 duplicated, 0 skipped, total is 21)",
2929
)
3030
}
3131

@@ -35,7 +35,7 @@ func TestMainFromOtherDirectory(t *testing.T) {
3535
"../..",
3636
[]string{"--debug", "--exec-timeout", "1", "github.com/zimmski/go-mutesting/example"},
3737
returnOk,
38-
"The mutation score is 0.500000 (8 passed, 8 failed, 8 duplicated, 0 skipped, total is 16)",
38+
"The mutation score is 0.450000 (9 passed, 11 failed, 8 duplicated, 0 skipped, total is 20)",
3939
)
4040
}
4141

mutator/expression/comparison.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package expression
2+
3+
import (
4+
"go/ast"
5+
"go/token"
6+
"go/types"
7+
8+
"github.com/zimmski/go-mutesting/mutator"
9+
)
10+
11+
func init() {
12+
mutator.Register("expression/comparison", MutatorComparison)
13+
}
14+
15+
var comparisonMutations = map[token.Token]token.Token{
16+
token.LSS: token.LEQ,
17+
token.LEQ: token.LSS,
18+
token.GTR: token.GEQ,
19+
token.GEQ: token.GTR,
20+
}
21+
22+
// MutatorComparison implements a mutator to change comparisons.
23+
func MutatorComparison(pkg *types.Package, info *types.Info, node ast.Node) []mutator.Mutation {
24+
n, ok := node.(*ast.BinaryExpr)
25+
if !ok {
26+
return nil
27+
}
28+
29+
o := n.Op
30+
r, ok := comparisonMutations[n.Op]
31+
if !ok {
32+
return nil
33+
}
34+
35+
return []mutator.Mutation{
36+
mutator.Mutation{
37+
Change: func() {
38+
n.Op = r
39+
},
40+
Reset: func() {
41+
n.Op = o
42+
},
43+
},
44+
}
45+
}

mutator/expression/comparison_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package expression
2+
3+
import (
4+
"testing"
5+
6+
"github.com/zimmski/go-mutesting/test"
7+
)
8+
9+
func TestMutatorComparison(t *testing.T) {
10+
test.Mutator(
11+
t,
12+
MutatorComparison,
13+
"../../testdata/expression/comparison.go",
14+
4,
15+
)
16+
}

testdata/expression/comparison.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build example-main
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func main() {
8+
if 1 > 2 {
9+
fmt.Printf("1 is greater than 2!")
10+
}
11+
12+
if 1 < 2 {
13+
fmt.Printf("1 is less than 2!")
14+
}
15+
16+
if 1 <= 2 {
17+
fmt.Printf("1 is less than or equal to 2!")
18+
}
19+
20+
if 1 >= 2 {
21+
fmt.Printf("1 is greater than or equal to 2!")
22+
}
23+
24+
if 1 == 2 {
25+
fmt.Print("1 is equal to 2!")
26+
}
27+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build example-main
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func main() {
8+
if 1 >= 2 {
9+
fmt.Printf("1 is greater than 2!")
10+
}
11+
12+
if 1 < 2 {
13+
fmt.Printf("1 is less than 2!")
14+
}
15+
16+
if 1 <= 2 {
17+
fmt.Printf("1 is less than or equal to 2!")
18+
}
19+
20+
if 1 >= 2 {
21+
fmt.Printf("1 is greater than or equal to 2!")
22+
}
23+
24+
if 1 == 2 {
25+
fmt.Print("1 is equal to 2!")
26+
}
27+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build example-main
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func main() {
8+
if 1 > 2 {
9+
fmt.Printf("1 is greater than 2!")
10+
}
11+
12+
if 1 <= 2 {
13+
fmt.Printf("1 is less than 2!")
14+
}
15+
16+
if 1 <= 2 {
17+
fmt.Printf("1 is less than or equal to 2!")
18+
}
19+
20+
if 1 >= 2 {
21+
fmt.Printf("1 is greater than or equal to 2!")
22+
}
23+
24+
if 1 == 2 {
25+
fmt.Print("1 is equal to 2!")
26+
}
27+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build example-main
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func main() {
8+
if 1 > 2 {
9+
fmt.Printf("1 is greater than 2!")
10+
}
11+
12+
if 1 < 2 {
13+
fmt.Printf("1 is less than 2!")
14+
}
15+
16+
if 1 < 2 {
17+
fmt.Printf("1 is less than or equal to 2!")
18+
}
19+
20+
if 1 >= 2 {
21+
fmt.Printf("1 is greater than or equal to 2!")
22+
}
23+
24+
if 1 == 2 {
25+
fmt.Print("1 is equal to 2!")
26+
}
27+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build example-main
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func main() {
8+
if 1 > 2 {
9+
fmt.Printf("1 is greater than 2!")
10+
}
11+
12+
if 1 < 2 {
13+
fmt.Printf("1 is less than 2!")
14+
}
15+
16+
if 1 <= 2 {
17+
fmt.Printf("1 is less than or equal to 2!")
18+
}
19+
20+
if 1 > 2 {
21+
fmt.Printf("1 is greater than or equal to 2!")
22+
}
23+
24+
if 1 == 2 {
25+
fmt.Print("1 is equal to 2!")
26+
}
27+
}

0 commit comments

Comments
 (0)