github.com/mshitrit/go-mutesting@v0.0.0-20210528084812-ff81dcaedfea/mutator/expression/remove.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/remove", MutatorRemoveTerm)
    13  }
    14  
    15  // MutatorRemoveTerm implements a mutator to remove expression terms.
    16  func MutatorRemoveTerm(pkg *types.Package, info *types.Info, node ast.Node) []mutator.Mutation {
    17  	n, ok := node.(*ast.BinaryExpr)
    18  	if !ok {
    19  		return nil
    20  	}
    21  	if n.Op != token.LAND && n.Op != token.LOR {
    22  		return nil
    23  	}
    24  
    25  	var r *ast.Ident
    26  
    27  	switch n.Op {
    28  	case token.LAND:
    29  		r = ast.NewIdent("true")
    30  	case token.LOR:
    31  		r = ast.NewIdent("false")
    32  	}
    33  
    34  	x := n.X
    35  	y := n.Y
    36  
    37  	return []mutator.Mutation{
    38  		{
    39  			Change: func() {
    40  				n.X = r
    41  			},
    42  			Reset: func() {
    43  				n.X = x
    44  			},
    45  		},
    46  		{
    47  			Change: func() {
    48  				n.Y = r
    49  			},
    50  			Reset: func() {
    51  				n.Y = y
    52  			},
    53  		},
    54  	}
    55  }