Skip to content

Commit

Permalink
Merge pull request zimmski#64 from jackwilsdon/add-comparison-mutator
Browse files Browse the repository at this point in the history
Add comparison expression mutator
  • Loading branch information
zimmski authored Nov 1, 2018
2 parents da0dc4f + 88a05ba commit f90d14f
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/go-mutesting/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMain(t *testing.T) {
"../../example",
[]string{"--debug", "--exec-timeout", "1"},
returnOk,
"The mutation score is 0.500000 (8 passed, 8 failed, 8 duplicated, 0 skipped, total is 16)",
"The mutation score is 0.450000 (9 passed, 11 failed, 8 duplicated, 0 skipped, total is 20)",
)
}

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

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

Expand Down
45 changes: 45 additions & 0 deletions mutator/expression/comparison.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package expression

import (
"go/ast"
"go/token"
"go/types"

"github.com/zimmski/go-mutesting/mutator"
)

func init() {
mutator.Register("expression/comparison", MutatorComparison)
}

var comparisonMutations = map[token.Token]token.Token{
token.LSS: token.LEQ,
token.LEQ: token.LSS,
token.GTR: token.GEQ,
token.GEQ: token.GTR,
}

// MutatorComparison implements a mutator to change comparisons.
func MutatorComparison(pkg *types.Package, info *types.Info, node ast.Node) []mutator.Mutation {
n, ok := node.(*ast.BinaryExpr)
if !ok {
return nil
}

o := n.Op
r, ok := comparisonMutations[n.Op]
if !ok {
return nil
}

return []mutator.Mutation{
mutator.Mutation{
Change: func() {
n.Op = r
},
Reset: func() {
n.Op = o
},
},
}
}
16 changes: 16 additions & 0 deletions mutator/expression/comparison_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package expression

import (
"testing"

"github.com/zimmski/go-mutesting/test"
)

func TestMutatorComparison(t *testing.T) {
test.Mutator(
t,
MutatorComparison,
"../../testdata/expression/comparison.go",
4,
)
}
27 changes: 27 additions & 0 deletions testdata/expression/comparison.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build example-main

package main

import "fmt"

func main() {
if 1 > 2 {
fmt.Printf("1 is greater than 2!")
}

if 1 < 2 {
fmt.Printf("1 is less than 2!")
}

if 1 <= 2 {
fmt.Printf("1 is less than or equal to 2!")
}

if 1 >= 2 {
fmt.Printf("1 is greater than or equal to 2!")
}

if 1 == 2 {
fmt.Print("1 is equal to 2!")
}
}
27 changes: 27 additions & 0 deletions testdata/expression/comparison.go.0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build example-main

package main

import "fmt"

func main() {
if 1 >= 2 {
fmt.Printf("1 is greater than 2!")
}

if 1 < 2 {
fmt.Printf("1 is less than 2!")
}

if 1 <= 2 {
fmt.Printf("1 is less than or equal to 2!")
}

if 1 >= 2 {
fmt.Printf("1 is greater than or equal to 2!")
}

if 1 == 2 {
fmt.Print("1 is equal to 2!")
}
}
27 changes: 27 additions & 0 deletions testdata/expression/comparison.go.1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build example-main

package main

import "fmt"

func main() {
if 1 > 2 {
fmt.Printf("1 is greater than 2!")
}

if 1 <= 2 {
fmt.Printf("1 is less than 2!")
}

if 1 <= 2 {
fmt.Printf("1 is less than or equal to 2!")
}

if 1 >= 2 {
fmt.Printf("1 is greater than or equal to 2!")
}

if 1 == 2 {
fmt.Print("1 is equal to 2!")
}
}
27 changes: 27 additions & 0 deletions testdata/expression/comparison.go.2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build example-main

package main

import "fmt"

func main() {
if 1 > 2 {
fmt.Printf("1 is greater than 2!")
}

if 1 < 2 {
fmt.Printf("1 is less than 2!")
}

if 1 < 2 {
fmt.Printf("1 is less than or equal to 2!")
}

if 1 >= 2 {
fmt.Printf("1 is greater than or equal to 2!")
}

if 1 == 2 {
fmt.Print("1 is equal to 2!")
}
}
27 changes: 27 additions & 0 deletions testdata/expression/comparison.go.3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build example-main

package main

import "fmt"

func main() {
if 1 > 2 {
fmt.Printf("1 is greater than 2!")
}

if 1 < 2 {
fmt.Printf("1 is less than 2!")
}

if 1 <= 2 {
fmt.Printf("1 is less than or equal to 2!")
}

if 1 > 2 {
fmt.Printf("1 is greater than or equal to 2!")
}

if 1 == 2 {
fmt.Print("1 is equal to 2!")
}
}

0 comments on commit f90d14f

Please sign in to comment.