Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
youyuanwu committed Aug 1, 2020
1 parent d41bb23 commit 4dcbf27
Showing 4 changed files with 37 additions and 46 deletions.
8 changes: 4 additions & 4 deletions assign.go
Original file line number Diff line number Diff line change
@@ -12,13 +12,13 @@ import (
// Along the path, interface{} is supported and nil ptr is initialized to ptr to zero value
// of the type until the variable to be set is obtained.
// It returns errors when encountering along the path unknown types, uninitialized
// interface{} or interface{} containing struct (not ptr to struct).
// interface{} or interface{} containing struct directly (not ptr to struct).
//
// Slice is resolved similarly in funk.Get(), by traversing each element of the slice,
// Slice is resolved the same way in funk.Get(), by traversing each element of the slice,
// so that each element of the slice's corresponding field are going to be set to the same provided val.
// If Set is called on slice with empty path "", it behaves the same as funk.Fill()
//
// If in is well formed, i.e. do not expect above errors to happen, funk.MustSet()
// If in is well formed, i.e. do not expect above descripted errors to happen, funk.MustSet()
// is a short hand wrapper to discard error return
func Set(in interface{}, val interface{}, path string) error {
if in == nil {
@@ -102,7 +102,7 @@ func set(inValue reflect.Value, setValue reflect.Value, parts []string) error {
// we treat this as a new call to setByParts, and it will do proper check of the types
return setByParts(inValue.Interface(), setValue.Interface(), parts[i:])
default:
return fmt.Errorf("kind %v in path is not supported", kind)
return fmt.Errorf("kind %v in path %v is not supported", kind, parts[i])
}

}
66 changes: 28 additions & 38 deletions assign_test.go
Original file line number Diff line number Diff line change
@@ -273,7 +273,6 @@ func TestSet_SlicePassByPtr(t *testing.T) {
}

func TestSet_SlicePassDirectly(t *testing.T) {
// TODO merge with above
var testCases = []struct {
Original interface{} // slice or array
Path string
@@ -332,88 +331,79 @@ func TestSet_SlicePassDirectly(t *testing.T) {

func TestInterface(t *testing.T) {

type Baz struct {
Name string
Itf interface{}
}

var testCases = []struct {
OriginalBaz Baz
OriginalFoo Foo
Path string
SetVal interface{}
ExpectedBaz Baz
ExpectedFoo Foo
}{
// set string field
{
Baz{Name: "", Itf: nil},
"Name",
Foo{FirstName: ""},
"FirstName",
"hi",
Baz{Name: "hi", Itf: nil},
Foo{FirstName: "hi"},
},
// set interface{} field
{
Baz{Name: "", Itf: nil},
"Itf",
Foo{FirstName: "", GeneralInterface: nil},
"GeneralInterface",
"str",
Baz{Name: "", Itf: "str"},
Foo{FirstName: "", GeneralInterface: "str"},
},
// set field of the interface{} field
// TODO: set uninitialized interface{} should fail
// Note: set uninitialized interface{} should fail
// Note: interface of struct (not ptr to struct) should fail
{
Baz{Name: "", Itf: &Baz{Name: "", Itf: nil}},
"Itf.Name",
"Baz2",
Baz{Name: "", Itf: &Baz{Name: "Baz2", Itf: nil}},
Foo{FirstName: "", GeneralInterface: &Foo{FirstName: ""}}, // if Foo is not ptr this will fail
"GeneralInterface.FirstName",
"foo",
Foo{FirstName: "", GeneralInterface: &Foo{FirstName: "foo"}},
},
// interface two level
{
Baz{Name: "", Itf: &Baz{Name: "", Itf: nil}},
"Itf.Itf",
Foo{FirstName: "", GeneralInterface: &Foo{GeneralInterface: nil}},
"GeneralInterface.GeneralInterface",
"val",
Baz{Name: "", Itf: &Baz{Name: "", Itf: "val"}},
Foo{FirstName: "", GeneralInterface: &Foo{GeneralInterface: "val"}},
},
}

for idx, tc := range testCases {
t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
is := assert.New(t)

err := Set(&tc.OriginalBaz, tc.SetVal, tc.Path)
err := Set(&tc.OriginalFoo, tc.SetVal, tc.Path)
is.NoError(err)
is.Equal(tc.ExpectedBaz, tc.OriginalBaz)
is.Equal(tc.ExpectedFoo, tc.OriginalFoo)
})
}

}

func TestSet_ErrorCaces(t *testing.T) {
type Baz struct {
Name string
Itf interface{}
}

var testCases = []struct {
OriginalBaz Baz
OriginalFoo Foo
Path string
SetVal interface{}
}{
// uninit interface
// Itf is not initialized so Set cannot properly allocate type
{
Baz{Name: "", Itf: nil},
"Itf.Name",
Foo{BarInterface: nil},
"BarInterface.Name",
"val",
},
{
Baz{Name: "", Itf: &Baz{Name: "", Itf: nil}},
"Itf.Itf.Name",
Foo{GeneralInterface: &Foo{BarInterface: nil}},
"GeneralInterface.BarInterface.Name",
"val",
},
// type mismatch
{
Baz{Name: ""},
"Name",
Foo{FirstName: ""},
"FirstName",
20,
},
}
@@ -422,15 +412,15 @@ func TestSet_ErrorCaces(t *testing.T) {
t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
is := assert.New(t)

err := Set(&tc.OriginalBaz, tc.SetVal, tc.Path)
err := Set(&tc.OriginalFoo, tc.SetVal, tc.Path)
is.Error(err)
})
}

t.Run("not pointer", func(t *testing.T) {
is := assert.New(t)
baz := Baz{Name: "dummy"}
err := Set(baz, Baz{Name: "dummy2"}, "Name")
baz := Bar{Name: "dummy"}
err := Set(baz, Bar{Name: "dummy2"}, "Name")
is.Error(err)
})

5 changes: 3 additions & 2 deletions funk_test.go
Original file line number Diff line number Diff line change
@@ -27,8 +27,9 @@ type Foo struct {
Bars []*Bar
EmptyValue sql.NullInt64

BarInterface interface{}
BarPointer interface{}
BarInterface interface{}
BarPointer interface{}
GeneralInterface interface{}
}

func (f Foo) TableName() string {
4 changes: 2 additions & 2 deletions map_test.go
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ func TestKeys(t *testing.T) {

sort.Strings(fields)

is.Equal(fields, []string{"Age", "Bar", "BarInterface", "BarPointer", "Bars", "EmptyValue", "FirstName", "ID", "LastName"})
is.Equal(fields, []string{"Age", "Bar", "BarInterface", "BarPointer", "Bars", "EmptyValue", "FirstName", "GeneralInterface", "ID", "LastName"})
}

func TestValues(t *testing.T) {
@@ -32,5 +32,5 @@ func TestValues(t *testing.T) {

values := Values(foo).([]interface{})

is.Len(values, 9)
is.Len(values, 10)
}

0 comments on commit 4dcbf27

Please sign in to comment.