github.com/mshitrit/go-mutesting@v0.0.0-20210528084812-ff81dcaedfea/mutator/expression/comparison.go (about)

     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  		{
    37  			Change: func() {
    38  				n.Op = r
    39  			},
    40  			Reset: func() {
    41  				n.Op = o
    42  			},
    43  		},
    44  	}
    45  }