Skip to content

Commit

Permalink
Fix makeValue for struct ptr. Added strings.Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelS11 committed Jun 28, 2019
1 parent ea4ba68 commit 6d9ab88
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ func init() {
"TrimSpace": strings.TrimSpace,
"TrimSuffix": strings.TrimSuffix,
}
stringsGo110()
}
13 changes: 13 additions & 0 deletions packages/stringsGo110.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build go1.10

package packages

import (
"strings"
)

func stringsGo110() {
PackageTypes["strings"] = map[string]interface{}{
"Builder": strings.Builder{},
}
}
16 changes: 16 additions & 0 deletions packages/stringsGo110_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package packages

import (
"os"
"testing"

"github.com/mattn/anko/internal/testlib"
)

func TestStringsGo110(t *testing.T) {
os.Setenv("ANKO_DEBUG", "1")
tests := []testlib.Test{
{Script: `strings = import("strings"); a = make(strings.Builder); _, err = a.WriteString("a"); if err != nil { return err.Error() }; _, err = a.WriteString("b"); if err != nil { return err.Error() }; _, err = a.WriteString("c"); if err != nil { return err.Error() }; a.String()`, RunOutput: "abc"},
}
testlib.Run(t, tests, &testlib.Options{EnvSetupFunc: &testPackagesEnvSetupFunc})
}
5 changes: 5 additions & 0 deletions packages/stringsNotGo110.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !go1.10

package packages

func stringsGo110() {}
3 changes: 3 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ func makeValue(t reflect.Type) (reflect.Value, error) {
case reflect.Struct:
structV := reflect.New(t).Elem()
for i := 0; i < structV.NumField(); i++ {
if structV.Field(i).Kind() == reflect.Ptr {
continue
}
v, err := makeValue(structV.Field(i).Type())
if err != nil {
return nilValue, err
Expand Down
80 changes: 80 additions & 0 deletions vm/vmContainers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,84 @@ import (
"github.com/mattn/anko/parser"
)

type (
testStruct1 struct {
aInterface interface{}
aBool bool
aInt32 int32
aInt64 int64
aFloat32 float32
aFloat64 float32
aString string
aFunc func()

aPtrInterface *interface{}
aPtrBool *bool
aPtrInt32 *int32
aPtrInt64 *int64
aPtrFloat32 *float32
aPtrFloat64 *float32
aPtrString *string
aPtrSliceInterface *[]interface{}
aPtrSliceBool *[]bool
aPtrSliceInt32 *[]int32
aPtrSliceInt64 *[]int64
aPtrSliceFloat32 *[]float32
aPtrSliceFloat64 *[]float32
aPtrSliceString *[]string

aSliceInterface []interface{}
aSliceBool []bool
aSliceInt32 []int32
aSliceInt64 []int64
aSliceFloat32 []float32
aSliceFloat64 []float32
aSliceString []string
aSlicePtrInterface []*interface{}
aSlicePtrBool []*bool
aSlicePtrInt32 []*int32
aSlicePtrInt64 []*int64
aSlicePtrFloat32 []*float32
aSlicePtrFloat64 []*float32
aSlicePtrString []*string

aMapInterface map[string]interface{}
aMapBool map[string]bool
aMapInt32 map[string]int32
aMapInt64 map[string]int64
aMapFloat32 map[string]float32
aMapFloat64 map[string]float32
aMapString map[string]string
aMapPtrInterface map[string]*interface{}
aMapPtrBool map[string]*bool
aMapPtrInt32 map[string]*int32
aMapPtrInt64 map[string]*int64
aMapPtrFloat32 map[string]*float32
aMapPtrFloat64 map[string]*float32
aMapPtrString map[string]*string

aChanInterface chan interface{}
aChanBool chan bool
aChanInt32 chan int32
aChanInt64 chan int64
aChanFloat32 chan float32
aChanFloat64 chan float32
aChanString chan string
aChanPtrInterface chan *interface{}
aChanPtrBool chan *bool
aChanPtrInt32 chan *int32
aChanPtrInt64 chan *int64
aChanPtrFloat32 chan *float32
aChanPtrFloat64 chan *float32
aChanPtrString chan *string

aPtrStruct *testStruct1
}
testStruct2 struct {
aStruct testStruct1
}
)

var (
testSliceEmpty []interface{}
testSlice = []interface{}{nil, true, int64(1), float64(1.1), "a"}
Expand Down Expand Up @@ -1769,6 +1847,8 @@ func TestStructs(t *testing.T) {
func TestMakeStructs(t *testing.T) {
os.Setenv("ANKO_DEBUG", "1")
tests := []testlib.Test{
{Script: `a = make(struct1)`, Types: map[string]interface{}{"struct1": &testStruct1{}}, RunOutput: &testStruct1{}, Output: map[string]interface{}{"a": &testStruct1{}}},
{Script: `a = make(struct2)`, Types: map[string]interface{}{"struct2": &testStruct2{}}, RunOutput: &testStruct2{}, Output: map[string]interface{}{"a": &testStruct2{}}},
{Script: `make(struct)`, Types: map[string]interface{}{"struct": &struct {
A interface{}
B interface{}
Expand Down

0 comments on commit 6d9ab88

Please sign in to comment.