Skip to content

Commit

Permalink
test: improving test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Mar 28, 2024
1 parent 8cf444c commit 2a85fad
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
6 changes: 6 additions & 0 deletions pkg/types/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ func (p Properties) SetFromStruct(data interface{}) Properties { //nolint:funlen
case reflect.Map:
for _, key := range value.MapKeys() {
val := value.MapIndex(key)
if key.Kind() == reflect.Ptr {
key = key.Elem()
}
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
name = key.String()
p.SetTagWithPrefix(prefix, &name, val.Interface())
}
Expand Down
72 changes: 49 additions & 23 deletions pkg/types/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ func TestPropertiesSetFromStruct(t *testing.T) {
Name byte
}

type testStruct5 struct {
Name string
Tags map[string]*string
}

type testStruct6 struct {
Name string
Tags map[*string]*string
}

cases := []struct {
name string
s interface{}
Expand All @@ -384,24 +394,25 @@ func TestPropertiesSetFromStruct(t *testing.T) {
want: types.NewProperties(),
},
{
name: "nonempty",
s: testStruct{Name: "Alice", Age: 42},
want: types.NewProperties().Set("Age", 42).Set("Name", "Alice"),
name: "unsupported-type-panic",
s: testStruct4{
Name: 'a',
},
want: types.NewProperties(),
error: true,
},
{
name: "nonempty-struct2",
s: testStruct2{
Name: "Alice",
Region: ptr.String("us-west-2"),
Tags: &map[string]string{"key": "value"},
},
want: types.NewProperties().
Set("Name", "Alice").
Set("Region", "us-west-2").
SetTagWithPrefix("awesome", &[]string{"key"}[0], "value"),
name: "from-struct",
s: testStruct3{Name: "testing"},
want: types.NewPropertiesFromStruct(testStruct3{Name: "testing"}),
},
{
name: "nonempty-struct3",
name: "simple",
s: testStruct{Name: "Alice", Age: 42},
want: types.NewProperties().Set("Age", 42).Set("Name", "Alice"),
},
{
name: "complex",
s: testStruct3{
Name: "Alice",
Age: &[]int{42}[0],
Expand All @@ -418,7 +429,19 @@ func TestPropertiesSetFromStruct(t *testing.T) {
SetTag(ptr.String("key1"), "value1"),
},
{
name: "nonempty-struct3-is-set",
name: "tags-map",
s: testStruct2{
Name: "Alice",
Region: ptr.String("us-west-2"),
Tags: &map[string]string{"key": "value"},
},
want: types.NewProperties().
Set("Name", "Alice").
Set("Region", "us-west-2").
SetTagWithPrefix("awesome", &[]string{"key"}[0], "value"),
},
{
name: "tags-struct",
s: testStruct3{
Name: "Alice",
Age: &[]int{42}[0],
Expand All @@ -436,17 +459,20 @@ func TestPropertiesSetFromStruct(t *testing.T) {
SetTag(ptr.String("key1"), "value1"),
},
{
name: "unsupported-type-panic",
s: testStruct4{
Name: 'a',
name: "tags-string-pointer",
s: testStruct5{
Name: "Alice",
Tags: map[string]*string{"key": ptr.String("value")},
},
want: types.NewProperties(),
error: true,
want: types.NewProperties().Set("Name", "Alice").SetTag(ptr.String("key"), "value"),
},
{
name: "new-properties-from-struct",
s: testStruct3{Name: "testing"},
want: types.NewPropertiesFromStruct(testStruct3{Name: "testing"}),
name: "tags-pointer-pointer",
s: testStruct6{
Name: "Alice",
Tags: map[*string]*string{ptr.String("key"): ptr.String("value")},
},
want: types.NewProperties().Set("Name", "Alice").SetTag(ptr.String("key"), "value"),
},
}

Expand Down

0 comments on commit 2a85fad

Please sign in to comment.