Skip to content

Commit

Permalink
Merge pull request #22 from zimmski/noop-all-the-way
Browse files Browse the repository at this point in the history
Noop all the way
  • Loading branch information
zimmski committed Jun 23, 2016
2 parents 54a00d2 + 1a3ed25 commit fdfaaf4
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 59 deletions.
36 changes: 36 additions & 0 deletions astutil/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package astutil

import (
"go/ast"
"go/token"
)

// CreateNoopOfStatement creates a syntactically safe noop statement out of a given statement.
func CreateNoopOfStatement(stmt ast.Stmt) ast.Stmt {
return CreateNoopOfStatements([]ast.Stmt{stmt})
}

// CreateNoopOfStatement creates a syntactically safe noop statement out of a given statement.
func CreateNoopOfStatements(stmts []ast.Stmt) ast.Stmt {
var ids []ast.Expr
for _, stmt := range stmts {
ids = append(ids, IdentifiersInStatement(stmt)...)
}

if len(ids) == 0 {
return &ast.EmptyStmt{
Semicolon: token.NoPos,
}
}

lhs := make([]ast.Expr, len(ids))
for i := range ids {
lhs[i] = ast.NewIdent("_")
}

return &ast.AssignStmt{
Lhs: lhs,
Rhs: ids,
Tok: token.ASSIGN,
}
}
33 changes: 33 additions & 0 deletions astutil/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package astutil

import (
"go/ast"
)

// IdentifiersInStatement returns all identifiers with their found in a statement.
func IdentifiersInStatement(stmt ast.Stmt) []ast.Expr {
w := &identifierWalker{}

ast.Walk(w, stmt)

return w.identifiers
}

type identifierWalker struct {
identifiers []ast.Expr
}

func (w *identifierWalker) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.Ident:
w.identifiers = append(w.identifiers, n)

return nil
case *ast.SelectorExpr:
w.identifiers = append(w.identifiers, n)

return nil
}

return w
}
2 changes: 1 addition & 1 deletion cmd/go-mutesting/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func TestMain(t *testing.T) {
out := <-bufChannel

assert.Equal(t, returnOk, exitCode)
assert.Contains(t, out, "The mutation score is 0.578947 (11 passed, 8 failed, 2 skipped, total is 21)")
assert.Contains(t, out, "The mutation score is 0.583333 (7 passed, 5 failed, 1 skipped, total is 13)")
}
5 changes: 4 additions & 1 deletion mutator/branch/mutatecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package branch
import (
"go/ast"

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

Expand Down Expand Up @@ -47,7 +48,9 @@ func (m *MutatorCase) Mutate(node ast.Node, changed chan bool) {
}

old := n.Body
n.Body = make([]ast.Stmt, 0)
n.Body = []ast.Stmt{
astutil.CreateNoopOfStatements(n.Body),
}

changed <- true
<-changed
Expand Down
5 changes: 2 additions & 3 deletions mutator/branch/mutateelse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package branch
import (
"go/ast"

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

Expand Down Expand Up @@ -56,9 +57,7 @@ func (m *MutatorElse) Mutate(node ast.Node, changed chan bool) {
}

old := n.Else
n.Else = &ast.EmptyStmt{
Semicolon: n.Else.Pos(),
}
n.Else = astutil.CreateNoopOfStatement(old)

changed <- true
<-changed
Expand Down
5 changes: 4 additions & 1 deletion mutator/branch/mutateif.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package branch
import (
"go/ast"

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

Expand Down Expand Up @@ -47,7 +48,9 @@ func (m *MutatorIf) Mutate(node ast.Node, changed chan bool) {
}

old := n.Body.List
n.Body.List = make([]ast.Stmt, 0)
n.Body.List = []ast.Stmt{
astutil.CreateNoopOfStatement(n.Body),
}

changed <- true
<-changed
Expand Down
45 changes: 2 additions & 43 deletions mutator/statement/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"go/ast"
"go/token"

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

Expand Down Expand Up @@ -85,7 +86,7 @@ func (m *MutatorRemoveStatement) Mutate(node ast.Node, changed chan bool) {
for i, ni := range l {
if m.checkStatement(ni) {
old := l[i]
l[i] = createNoop(old)
l[i] = astutil.CreateNoopOfStatement(old)

changed <- true
<-changed
Expand All @@ -98,48 +99,6 @@ func (m *MutatorRemoveStatement) Mutate(node ast.Node, changed chan bool) {
}
}

func createNoop(old ast.Stmt) ast.Stmt {
v := &idCollector{}
ast.Walk(v, old)
return v.generateStatement(old)
}

type idCollector struct {
Ids []ast.Expr
}

func (i *idCollector) Visit(node ast.Node) ast.Visitor {
switch v := node.(type) {
case *ast.Ident:
i.Ids = append(i.Ids, v)
return nil
case *ast.SelectorExpr:
i.Ids = append(i.Ids, v)
return nil
}
return i
}

func (i *idCollector) generateStatement(old ast.Stmt) ast.Stmt {
if len(i.Ids) == 0 {
return &ast.EmptyStmt{
Semicolon: old.Pos(),
}
}

lhs := make([]ast.Expr, len(i.Ids))
for i := range i.Ids {
lhs[i] = ast.NewIdent("_")
}

return &ast.AssignStmt{
Lhs: lhs,
Rhs: i.Ids,
Tok: token.ASSIGN,
TokPos: old.Pos(),
}
}

// String implements the String method of the Stringer interface
func (m MutatorRemoveStatement) String() string {
return "statement/remove"
Expand Down
8 changes: 4 additions & 4 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ if [ -z ${ROOT_DIR+x} ]; then echo "ROOT_DIR is not set"; exit 1; fi

echo "gofmt:"
OUT=$(gofmt -l $ROOT_DIR)
if [ $($OUT | wc -l) -ne 0 ]; then echo $OUT; PROBLEM=1; fi
if [ $(echo -n "$OUT" | wc -l) -ne 0 ]; then echo -n "$OUT"; PROBLEM=1; fi

echo "errcheck:"
OUT=$(errcheck $PKG/...)
if [ $($OUT | wc -l) -ne 0 ]; then echo $OUT; PROBLEM=1; fi
if [ $(echo -n "$OUT" | wc -l) -ne 0 ]; then echo -n "$OUT"; PROBLEM=1; fi

echo "go vet:"
OUT=$(go tool vet -all=true -v=true $ROOT_DIR 2>&1 | grep --invert-match -P "(Checking file|\%p of wrong type|can't check non-constant format|not compatible with reflect.StructTag.Get)")
if [ $($OUT | wc -l) -ne 0 ]; then echo $OUT; PROBLEM=1; fi
if [ $(echo -n "$OUT" | wc -l) -ne 0 ]; then echo -n "$OUT"; PROBLEM=1; fi

echo "golint:"
OUT=$(golint $PKG/... | grep --invert-match -P "(_string.go:)")
if [ $($OUT | wc -l) -ne 0 ]; then echo $OUT; PROBLEM=1; fi
if [ $(echo -n "$OUT" | wc -l) -ne 0 ]; then echo -n "$OUT"; PROBLEM=1; fi

if [ -n "$PROBLEM" ]; then exit 1; fi
2 changes: 1 addition & 1 deletion testdata/branch/mutatecase.go.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
for i != 4 {
switch {
case i == 1:

_, _ = fmt.Println, i
case i == 2:
fmt.Println(i * 2)
default:
Expand Down
2 changes: 1 addition & 1 deletion testdata/branch/mutatecase.go.1.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
case i == 1:
fmt.Println(i)
case i == 2:

_, _ = fmt.Println, i
default:
fmt.Println(i * 3)
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/branch/mutatecase.go.2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
case i == 2:
fmt.Println(i * 2)
default:

_, _ = fmt.Println, i
}

i++
Expand Down
2 changes: 1 addition & 1 deletion testdata/branch/mutateelse.go.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
} else if i == 2 {
fmt.Println(i * 2)
} else {

_, _ = fmt.Println, i
}

i++
Expand Down
2 changes: 1 addition & 1 deletion testdata/branch/mutateif.go.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {

for i != 4 {
if i == 1 {

_, _ = fmt.Println, i
} else if i == 2 {
fmt.Println(i * 2)
} else {
Expand Down
2 changes: 1 addition & 1 deletion testdata/branch/mutateif.go.1.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
if i == 1 {
fmt.Println(i)
} else if i == 2 {

_, _ = fmt.Println, i
} else {
fmt.Println(i * 3)
}
Expand Down

0 comments on commit fdfaaf4

Please sign in to comment.