Skip to content

Commit

Permalink
test: structs - fix action test error and add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 28, 2022
1 parent 972be5c commit 5daaf93
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
26 changes: 26 additions & 0 deletions structs/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ func TestToMap_useTag(t *testing.T) {
assert.NotContains(t, mp, "city")
}

func TestToMap_nestStruct(t *testing.T) {
type Extra struct {
City string `json:"city"`
Github string `json:"github"`
}
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Extra Extra `json:"extra"`
}

u := &User{
Name: "inhere",
Age: 30,
Extra: Extra{
City: "chengdu",
Github: "https://github.com/inhere",
},
}

mp := structs.MustToMap(u)
dump.P(mp)
assert.ContainsKeys(t, mp, []string{"name", "age", "extra"})
assert.ContainsKeys(t, mp["extra"], []string{"city", "github"})
}

func TestTryToMap_customTag(t *testing.T) {
type User struct {
Name string `export:"name"`
Expand Down
40 changes: 40 additions & 0 deletions structs/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,46 @@ import (
"github.com/gookit/goutil/structs"
)

func ExampleToMap() {
type Extra struct {
City string `json:"city"`
Github string `json:"github"`
}
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Extra Extra `json:"extra"`
}

u := &User{
Name: "inhere",
Age: 30,
Extra: Extra{
City: "chengdu",
Github: "https://github.com/inhere",
},
}

mp := structs.ToMap(u)
dump.P(mp)
/*dump:
map[string]interface {} { #len=3
"name": string("inhere"), #len=6
"age": int(30),
"extra": map[string]interface {} { #len=2
"city": string("chengdu"), #len=7
"github": string("https://github.com/inhere"), #len=25
},
},
*/

fmt.Println("mp.ame:", mp["name"])
fmt.Println("mp.age:", mp["age"])
// Output:
// mp.ame: inhere
// mp.age: 30
}

func ExampleInitDefaults() {
type Extra struct {
City string `default:"chengdu"`
Expand Down
2 changes: 1 addition & 1 deletion structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type InitOptions struct {
// }
//
// u1 := &User1{}
// err = structs.InitDefaults(u1, nil)
// err = structs.InitDefaults(u1)
// fmt.Printf("%+v\n", u1)
// // Output: {Name:inhere Age:30}
func InitDefaults(ptr interface{}, optFns ...InitOptFunc) error {
Expand Down
10 changes: 5 additions & 5 deletions structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestInitDefaults(t *testing.T) {
}

u := &User{}
err := structs.InitDefaults(u, nil)
err := structs.InitDefaults(u)
assert.NoErr(t, err)
assert.Eq(t, "inhere", u.Name)
assert.Eq(t, 0, u.Age)
Expand All @@ -29,19 +29,19 @@ func TestInitDefaults(t *testing.T) {
}

u1 := &User1{}
err = structs.InitDefaults(u1, nil)
err = structs.InitDefaults(u1)
assert.NoErr(t, err)
assert.Eq(t, "inhere", u1.Name)
assert.Eq(t, int32(30), u1.Age)
assert.Eq(t, "", u1.city)
// dump.P(u1)
// fmt.Printf("%+v\n", u1)

err = structs.InitDefaults([]string{"invalid"}, nil)
err = structs.InitDefaults([]string{"invalid"})
assert.ErrMsg(t, err, "must be provider an pointer value")

arr := []string{"invalid"}
err = structs.InitDefaults(&arr, nil)
err = structs.InitDefaults(&arr)
assert.ErrMsg(t, err, "must be provider an struct value")
}

Expand All @@ -52,7 +52,7 @@ func TestInitDefaults_convTypeError(t *testing.T) {
}

u := &User{}
err := structs.InitDefaults(u, nil)
err := structs.InitDefaults(u)
assert.ErrMsg(t, err, "convert value type is failure")
assert.Eq(t, "inhere", u.Name)
assert.Eq(t, 0, u.Age)
Expand Down

0 comments on commit 5daaf93

Please sign in to comment.