forked from rumd3x/go-mutesting
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
182585b
commit ba6de37
Showing
5 changed files
with
150 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package structinit | ||
|
||
import ( | ||
"go/ast" | ||
"go/types" | ||
|
||
"github.com/AntonStoeckl/go-mutesting/mutator" | ||
) | ||
|
||
func init() { | ||
mutator.Register("structinit/remove", MutatorStructinitRemove) | ||
} | ||
|
||
// MutatorStructinitRemove implements a mutator to remove fields from struct initialisation. | ||
func MutatorStructinitRemove(pkg *types.Package, info *types.Info, node ast.Node) []mutator.Mutation { | ||
var strus []*ast.CompositeLit | ||
_ = strus | ||
|
||
var mutations []mutator.Mutation | ||
|
||
switch n := node.(type) { | ||
case *ast.BlockStmt: | ||
for _, spec := range n.List { | ||
switch a := spec.(type) { | ||
case *ast.AssignStmt: | ||
if cl, ok := a.Rhs[0].(*ast.CompositeLit); ok { | ||
if _, ok := cl.Type.(*ast.Ident).Obj.Decl.(*ast.TypeSpec).Type.(*ast.StructType); ok { | ||
strus = append(strus, cl) | ||
} | ||
} | ||
|
||
} | ||
} | ||
default: | ||
return mutations | ||
} | ||
|
||
for i, stru := range strus { | ||
old := *stru | ||
|
||
//for j, elt := range strus[i].Elts { | ||
// | ||
//} | ||
|
||
mutations = append(mutations, mutator.Mutation{ | ||
Change: func() { | ||
elts := []ast.Expr{stru.Elts[0]} | ||
strus[i].Elts = elts | ||
}, | ||
Reset: func() { | ||
strus[i] = &old | ||
}, | ||
}) | ||
|
||
} | ||
|
||
//for i, _ := range strus { | ||
// currentStruct, ok := strus[i].(*ast.TypeSpec).Type.(*ast.StructType) | ||
// if !ok { | ||
// continue | ||
// } | ||
// | ||
// oldStructFields := *strus[i].(*ast.TypeSpec).Type.(*ast.StructType).Fields | ||
// _ = oldStructFields | ||
// | ||
// for k, field := range oldStructFields.List { | ||
// currentStruct.Fields.List = []*ast.Field{} | ||
// | ||
// mutations = append(mutations, mutator.Mutation{ | ||
// Change: func() { | ||
// for j := 0; j < len(oldStructFields.List); j++ { | ||
// if k != j { | ||
// currentStruct.Fields.List = append(currentStruct.Fields.List, field) | ||
// } | ||
// } | ||
// }, | ||
// Reset: func() { | ||
// strus[i].(*ast.TypeSpec).Type.(*ast.StructType).Fields = &oldStructFields | ||
// }, | ||
// }) | ||
// } | ||
//} | ||
|
||
return mutations | ||
} |
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 structinit | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/AntonStoeckl/go-mutesting/test" | ||
) | ||
|
||
func TestMutatorInitStruct(t *testing.T) { | ||
test.Mutator( | ||
t, | ||
MutatorStructinitRemove, | ||
"../../testdata/structinit/remove.go", | ||
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,17 @@ | ||
// +build test | ||
|
||
package example | ||
|
||
type Some struct { | ||
a string | ||
B int | ||
} | ||
|
||
func removeFieldsFromStructInit() Some { | ||
some := Some{ | ||
a: "a value", | ||
B: 5, | ||
} | ||
|
||
return some | ||
} |
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 @@ | ||
// +build test | ||
|
||
package example | ||
|
||
type Some struct { | ||
a string | ||
B int | ||
} | ||
|
||
func removeFieldsFromStructInit() Some { | ||
some := Some{ | ||
B: 5, | ||
} | ||
|
||
return some | ||
} |
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 @@ | ||
// +build test | ||
|
||
package example | ||
|
||
type Some struct { | ||
a string | ||
B int | ||
} | ||
|
||
func removeFieldsFromStructInit() Some { | ||
some := Some{ | ||
a: "a value", | ||
} | ||
|
||
return some | ||
} |