-
Notifications
You must be signed in to change notification settings - Fork 3
/
struct_test.go
105 lines (82 loc) · 3.57 KB
/
struct_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package convert_test
import (
"testing"
"github.com/Eun/go-convert"
"github.com/Eun/go-convert/internal/testhelpers"
)
func TestStruct(t *testing.T) {
type Foo struct {
Ok bool
}
type Bool struct {
True string
}
type User struct {
Name string
}
type AnotherUser struct {
Name string
}
type AnotherByteUser struct {
Name []byte
}
type Company struct {
Name string
}
type UserAndCompany struct {
Name string
Company
}
type AnotherUserAndCompany struct {
Name string
Company
}
type AnotherCompany struct {
Name string
}
type AnotherUserAndAnotherCompany struct {
Name string
Company AnotherCompany
}
type AnotherUserAndCompanyString struct {
Name string
Company string
}
type NilField struct {
OptionalField *string
}
tests := []testhelpers.TestCase{
// nil
{nil, struct{}{}, struct{}{}, "", nil},
// string
{"Hello World", struct{}{}, struct{}{}, "unable to convert string to struct {}: no recipe", nil},
// map
{map[string]interface{}{"Ok": true}, Foo{}, Foo{true}, "", nil},
{map[string]string{"Name": "Joe"}, User{}, User{"Joe"}, "", nil},
{map[string]interface{}{"Name": "Joe", "Company": map[string]interface{}{"Name": "Wood Inc"}}, UserAndCompany{}, UserAndCompany{"Joe", Company{"Wood Inc"}}, "", nil},
{map[bool]string{true: "Bar"}, Bool{}, Bool{"Bar"}, "", nil},
{map[string]interface{}{}, NilField{}, NilField{}, "", nil},
{map[string]interface{}{"OptionalField": "Hello World"}, NilField{}, NilField{testhelpers.PtrString("Hello World")}, "", nil},
{map[string]interface{}{"Foo": "Bar"}, User{}, User{}, `unable to convert map[string]interface {} to convert_test.User: unable to find Foo in convert_test.User`, nil},
{map[string]interface{}{"Foo": "Bar"}, User{}, User{}, "", &convert.Options{SkipUnknownFields: true}},
// should be unable to convert key
{map[User]string{{}: ""}, struct{}{}, struct{}{}, `unable to convert map[convert_test.User]string to struct {}: unable to convert convert_test.User to string: convert_test.User has no String() function`, nil},
// should be unable to convert value
{map[string]User{"Foo": {}}, struct{ Foo Foo }{}, struct{ Foo Foo }{}, `unable to convert map[string]convert_test.User to struct { Foo convert_test.Foo }: unable to convert convert_test.User to convert_test.Foo: unable to find Name in convert_test.Foo`, nil},
//
// struct
{User{"Joe"}, User{}, User{"Joe"}, "", nil},
{User{"Joe"}, AnotherUser{}, AnotherUser{"Joe"}, "", nil},
{User{"Joe"}, AnotherByteUser{}, AnotherByteUser{[]byte("Joe")}, "", nil},
{UserAndCompany{"Joe", Company{"Wood Inc"}}, AnotherUserAndCompany{}, AnotherUserAndCompany{"Joe", Company{"Wood Inc"}}, "", nil},
{UserAndCompany{"Joe", Company{"Wood Inc"}}, AnotherUserAndAnotherCompany{}, AnotherUserAndAnotherCompany{"Joe", AnotherCompany{"Wood Inc"}}, "", nil},
{UserAndCompany{"Joe", Company{"Wood Inc"}}, User{}, User{}, "unable to convert convert_test.UserAndCompany to convert_test.User: unable to find Company in convert_test.User", nil},
{UserAndCompany{"Joe", Company{"Wood Inc"}}, User{}, User{"Joe"}, "", &convert.Options{SkipUnknownFields: true}},
{UserAndCompany{"Joe", Company{"Wood Inc"}}, AnotherUserAndCompanyString{}, AnotherUserAndCompanyString{}, "unable to convert convert_test.UserAndCompany to convert_test.AnotherUserAndCompanyString: unable to convert convert_test.Company to string: convert_test.Company has no String() function", nil},
// struct with nil field should stay nil
{NilField{}, NilField{}, NilField{}, "", nil},
}
for i, test := range tests {
testhelpers.RunTest(t, test, i)
}
}