Skip to content

Commit

Permalink
convert Check method to return the mutation count
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmski committed Dec 29, 2014
1 parent 8e7cc38 commit 8fe3825
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
9 changes: 6 additions & 3 deletions mutator/branch/mutateelse.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ func (m *MutatorElse) check(node ast.Node) (*ast.IfStmt, bool) {
return n, true
}

// Check validates if a given node can be mutated by the mutator
func (m *MutatorElse) Check(node ast.Node) bool {
// Check validates how often a node can be mutated by a mutator
func (m *MutatorElse) Check(node ast.Node) uint {
_, ok := m.check(node)
if !ok {
return 0
}

return ok
return 1
}

// Mutate mutates a given node if it can be mutated by the mutator.
Expand Down
9 changes: 6 additions & 3 deletions mutator/branch/mutateif.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ func (m *MutatorIf) check(node ast.Node) (*ast.IfStmt, bool) {
return n, ok
}

// Check validates if a given node can be mutated by the mutator
func (m *MutatorIf) Check(node ast.Node) bool {
// Check validates how often a node can be mutated by a mutator
func (m *MutatorIf) Check(node ast.Node) uint {
_, ok := m.check(node)
if !ok {
return 0
}

return ok
return 1
}

// Mutate mutates a given node if it can be mutated by the mutator.
Expand Down
4 changes: 2 additions & 2 deletions mutator/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

// Mutator defines a mutator for mutation testing
type Mutator interface {
// Check validates if a given node can be mutated by the mutator
Check(node ast.Node) bool
// Check validates how often a node can be mutated by a mutator
Check(node ast.Node) uint
// Mutate mutates a given node if it can be mutated by the mutator.
// It first checks if the given node can be mutated by the mutator. If the node cannot be mutated, false is send into the given control channel and the method returns. If the node can be mutated, the current state of the node is saved. Afterwards the node is mutated, true is send into the given control channel and the method waits on the channel to continue the process. After receiving a value from the channel the original state of the node is restored, true is send into the given control channel and the method waits on the channel to continue the process. After receiving a value from the channel the method returns which finishes the mutation process.
Mutate(node ast.Node, changed chan bool)
Expand Down
19 changes: 14 additions & 5 deletions walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package mutesting
import (
"fmt"
"go/ast"
"strings"

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

// CountWalk returns the number of corresponding nodes for a given mutator.
// It traverses the AST of the given node and calls the method Check of the given mutator for every node. If Check returns true an internal counter is increment. After completion of the traversal the final counter is returned.
// CountWalk returns the number of corresponding mutations for a given mutator.
// It traverses the AST of the given node and calls the method Check of the given mutator for every node and sums up the returned counts. After completion of the traversal the final counter is returned.
func CountWalk(node ast.Node, m mutator.Mutator) uint {
w := &countWalk{
count: 0,
Expand All @@ -27,8 +28,12 @@ type countWalk struct {

// Visit implements the Visit method of the ast.Visitor interface
func (w *countWalk) Visit(node ast.Node) ast.Visitor {
if w.mutator.Check(node) {
w.count++
if node == nil {
return w
}

if n := w.mutator.Check(node); n > 0 {
w.count += n
}

return w
Expand Down Expand Up @@ -58,7 +63,11 @@ type mutateWalk struct {

// Visit implements the Visit method of the ast.Visitor interface
func (w *mutateWalk) Visit(node ast.Node) ast.Visitor {
if w.mutator.Check(node) {
if node == nil {
return w
}

if w.mutator.Check(node) > 0 {
w.mutator.Mutate(node, w.changed)
}

Expand Down

0 comments on commit 8fe3825

Please sign in to comment.