diff --git a/sequence_test.go b/sequence_test.go index 446bc42..dd866c6 100644 --- a/sequence_test.go +++ b/sequence_test.go @@ -16,7 +16,7 @@ func TestSetSliceWithoutClearing(t *testing.T) { assert.Equal(t, []int{10, 1, 2, 3}, xs) } -func TestSetSliceWithClear(t *testing.T) { +func TestSetSliceAfterClearing(t *testing.T) { xs := []int{100} entries := []string{"1", "2", "3"} err := setSlice(reflect.ValueOf(&xs).Elem(), entries, true) @@ -24,6 +24,13 @@ func TestSetSliceWithClear(t *testing.T) { assert.Equal(t, []int{1, 2, 3}, xs) } +func TestSetSliceInvalid(t *testing.T) { + xs := []int{100} + entries := []string{"invalid"} + err := setSlice(reflect.ValueOf(&xs).Elem(), entries, true) + assert.Error(t, err) +} + func TestSetSlicePtr(t *testing.T) { var xs []*int entries := []string{"1", "2", "3"} @@ -58,7 +65,7 @@ func TestSetMapWithoutClearing(t *testing.T) { assert.Equal(t, 10, m["foo"]) } -func TestSetMapWithClear(t *testing.T) { +func TestSetMapAfterClearing(t *testing.T) { m := map[string]int{"foo": 10} entries := []string{"a=1", "b=2"} err := setMap(reflect.ValueOf(&m).Elem(), entries, true) @@ -68,6 +75,25 @@ func TestSetMapWithClear(t *testing.T) { assert.Equal(t, 2, m["b"]) } +func TestSetMapWithKeyPointer(t *testing.T) { + // textUnmarshaler is a struct that captures the length of the string passed to it + var m map[*string]int + entries := []string{"abc=123"} + err := setMap(reflect.ValueOf(&m).Elem(), entries, true) + require.NoError(t, err) + require.Len(t, m, 1) +} + +func TestSetMapWithValuePointer(t *testing.T) { + // textUnmarshaler is a struct that captures the length of the string passed to it + var m map[string]*int + entries := []string{"abc=123"} + err := setMap(reflect.ValueOf(&m).Elem(), entries, true) + require.NoError(t, err) + require.Len(t, m, 1) + assert.Equal(t, 123, *m["abc"]) +} + func TestSetMapTextUnmarshaller(t *testing.T) { // textUnmarshaler is a struct that captures the length of the string passed to it var m map[textUnmarshaler]*textUnmarshaler @@ -80,6 +106,20 @@ func TestSetMapTextUnmarshaller(t *testing.T) { assert.Equal(t, &textUnmarshaler{1}, m[textUnmarshaler{3}]) } +func TestSetMapInvalidKey(t *testing.T) { + var m map[int]int + entries := []string{"invalid=123"} + err := setMap(reflect.ValueOf(&m).Elem(), entries, true) + assert.Error(t, err) +} + +func TestSetMapInvalidValue(t *testing.T) { + var m map[int]int + entries := []string{"123=invalid"} + err := setMap(reflect.ValueOf(&m).Elem(), entries, true) + assert.Error(t, err) +} + func TestSetMapMalformed(t *testing.T) { // textUnmarshaler is a struct that captures the length of the string passed to it var m map[string]string