forked from zimmski/go-mutesting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zimmski#64 from jackwilsdon/add-comparison-mutator
Add comparison expression mutator
- Loading branch information
Showing
8 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} |