package gapr_test import ( "reflect" "testing" "code.icb4dc0.de/prskr/gapr" ) func TestGapr_Map(t *testing.T) { t.Parallel() type args struct { input any opts []gapr.Option } tests := []struct { name string args args want any wantErr bool }{ { name: "Test simple struct", args: args{ input: struct { GivenName string Surname string }{ GivenName: "Ted", Surname: "Tester", }, }, want: map[string]any{ "GivenName": "Ted", "Surname": "Tester", }, wantErr: false, }, { name: "Test simple struct pointer", args: args{ input: &struct { GivenName string Surname string }{ GivenName: "Ted", Surname: "Tester", }, }, want: map[string]any{ "GivenName": "Ted", "Surname": "Tester", }, wantErr: false, }, { 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", }, }, }, want: map[string]any{ "GivenName": "Ted", "Surname": "Tester", "Address": map[string]any{ "Street": "Some Street", "HouseNr": "3a", "City": "Some City", }, }, wantErr: false, }, { name: "Test simple map", args: args{ input: map[string]any{ "GivenName": "Ted", "Surname": "Tester", }, }, want: map[string]any{ "GivenName": "Ted", "Surname": "Tester", }, wantErr: false, }, { name: "Test nested slice", args: args{ input: struct { GivenName string Children []struct { Name string Age uint `gapr:",age"` } }{ GivenName: "Ted", Children: []struct { Name string Age uint `gapr:",age"` }{ { Name: "Tim", Age: 11, }, }, }, }, want: map[string]any{ "GivenName": "Ted", "Children": []any{ map[string]any{ "Name": "Tim", "age": uint(11), }, }, }, wantErr: false, }, { name: "Test map with nested struct", args: args{ input: map[string]any{ "Hello": struct { GivenName string `gapr:",givenName"` Surname string }{ GivenName: "Ted", Surname: "Tester", }, }, }, want: map[string]any{ "Hello": map[string]any{ "givenName": "Ted", "Surname": "Tester", }, }, wantErr: false, }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() g := gapr.New(tt.args.opts...) 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 } if tt.want != nil && !reflect.DeepEqual(got, tt.want) { t.Errorf("Got %v but want %v", got, tt.want) } }) } }