Skip to content

Commit

Permalink
almost finished ...
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonStoeckl committed Feb 27, 2020
1 parent 182585b commit ba6de37
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
85 changes: 85 additions & 0 deletions mutator/structinit/remove.go
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
}
16 changes: 16 additions & 0 deletions mutator/structinit/remove_test.go
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,
)
}
17 changes: 17 additions & 0 deletions testdata/structinit/remove.go
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
}
16 changes: 16 additions & 0 deletions testdata/structinit/remove.go.0.go
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
}
16 changes: 16 additions & 0 deletions testdata/structinit/remove.go.1.go
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
}

0 comments on commit ba6de37

Please sign in to comment.