gapr/gapr_test.go

184 lines
3.2 KiB
Go
Raw Normal View History

2023-01-30 16:40:50 +00:00
package gapr_test
import (
2023-01-30 21:06:18 +00:00
"reflect"
2023-01-30 16:40:50 +00:00
"testing"
"code.icb4dc0.de/prskr/gapr"
)
func TestGapr_Map(t *testing.T) {
2023-01-31 20:14:34 +00:00
t.Parallel()
2023-01-30 16:40:50 +00:00
type args struct {
input any
2023-01-30 21:06:18 +00:00
opts []gapr.Option
2023-01-30 16:40:50 +00:00
}
tests := []struct {
name string
args args
2023-01-30 21:06:18 +00:00
want any
2023-01-30 16:40:50 +00:00
wantErr bool
}{
{
name: "Test simple struct",
args: args{
input: struct {
GivenName string
Surname string
}{
GivenName: "Ted",
Surname: "Tester",
},
},
2023-01-30 21:06:18 +00:00
want: map[string]any{
"GivenName": "Ted",
"Surname": "Tester",
},
2023-01-30 16:40:50 +00:00
wantErr: false,
},
2023-01-30 17:14:53 +00:00
{
name: "Test simple struct pointer",
args: args{
input: &struct {
GivenName string
Surname string
}{
GivenName: "Ted",
Surname: "Tester",
},
},
2023-01-30 21:06:18 +00:00
want: map[string]any{
"GivenName": "Ted",
"Surname": "Tester",
},
2023-01-30 17:14:53 +00:00
wantErr: false,
},
2023-01-30 16:40:50 +00:00
{
name: "Test nested struct",
args: args{
input: struct {
GivenName string
Surname string
Address struct {
Street string
HouseNr string
City string
}
}{
GivenName: "Ted",
Surname: "Tester",
Address: struct {
Street string
HouseNr string
City string
}{
Street: "Some Street",
HouseNr: "3a",
City: "Some City",
},
},
},
2023-01-30 21:06:18 +00:00
want: map[string]any{
"GivenName": "Ted",
"Surname": "Tester",
"Address": map[string]any{
"Street": "Some Street",
"HouseNr": "3a",
"City": "Some City",
},
},
2023-01-30 16:40:50 +00:00
wantErr: false,
},
{
name: "Test simple map",
args: args{
2023-01-30 21:06:18 +00:00
input: map[string]any{
"GivenName": "Ted",
"Surname": "Tester",
2023-01-30 16:40:50 +00:00
},
},
2023-01-30 21:06:18 +00:00
want: map[string]any{
"GivenName": "Ted",
"Surname": "Tester",
},
2023-01-30 16:40:50 +00:00
wantErr: false,
},
{
name: "Test nested slice",
args: args{
input: struct {
GivenName string
Children []struct {
Name string
2023-01-30 21:06:18 +00:00
Age uint `gapr:",age"`
2023-01-30 16:40:50 +00:00
}
}{
GivenName: "Ted",
Children: []struct {
Name string
2023-01-30 21:06:18 +00:00
Age uint `gapr:",age"`
2023-01-30 16:40:50 +00:00
}{
{
Name: "Tim",
Age: 11,
},
},
},
},
2023-01-30 21:06:18 +00:00
want: map[string]any{
"GivenName": "Ted",
"Children": []any{
map[string]any{
"Name": "Tim",
"age": uint(11),
},
},
},
2023-01-30 16:40:50 +00:00
wantErr: false,
},
{
name: "Test map with nested struct",
args: args{
input: map[string]any{
"Hello": struct {
2023-01-30 21:06:18 +00:00
GivenName string `gapr:",givenName"`
2023-01-30 16:40:50 +00:00
Surname string
}{
GivenName: "Ted",
Surname: "Tester",
},
},
},
2023-01-31 20:14:34 +00:00
want: map[string]any{
"Hello": map[string]any{
"givenName": "Ted",
"Surname": "Tester",
},
},
2023-01-30 16:40:50 +00:00
wantErr: false,
},
}
for _, tt := range tests {
2023-01-31 20:14:34 +00:00
tt := tt
2023-01-30 16:40:50 +00:00
t.Run(tt.name, func(t *testing.T) {
2023-01-31 20:14:34 +00:00
t.Parallel()
2023-01-30 21:06:18 +00:00
g := gapr.New(tt.args.opts...)
2023-01-30 16:40:50 +00:00
got, err := g.Map(tt.args.input)
if (got == nil) == (err == nil) {
t.Errorf("cannot get nil value and nil error")
return
}
if (err != nil) != tt.wantErr {
t.Errorf("Map() error = %v, wantErr %v", err, tt.wantErr)
return
}
2023-01-30 21:06:18 +00:00
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("Got %v but want %v", got, tt.want)
}
2023-01-30 16:40:50 +00:00
})
}
}