Skip to content

Commit

Permalink
Add tests cases for failing marshal/unmarshal
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Sep 4, 2023
1 parent 54e2719 commit 8063cc8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Collection) MarshalJSON() ([]byte, error) {
func (c *Collection) UnmarshalJSON(data []byte) error {
var strSlice []string
if err := json.Unmarshal(data, &strSlice); err != nil {
return err
return fmt.Errorf("failed to decode JSON input: %w", err)
}
return c.unmarshal(strSlice)
}
Expand All @@ -85,7 +85,7 @@ func (c *Collection) MarshalYAML() (interface{}, error) {
func (c *Collection) UnmarshalYAML(unmarshal func(interface{}) error) error {
var strSlice []string
if err := unmarshal(&strSlice); err != nil {
return err
return fmt.Errorf("failed to decode YAML input: %w", err)
}
return c.unmarshal(strSlice)
}
19 changes: 19 additions & 0 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,22 @@ func TestCollectionUnmarshalling(t *testing.T) {
assert.Equal(t, "v1.0.1+k0s.1", c[1].String())
})
}

func TestFailingCollectionUnmarshalling(t *testing.T) {
t.Run("JSON", func(t *testing.T) {
var c Collection
err := json.Unmarshal([]byte(`invalid_json`), &c)
assert.Error(t, err)
err = json.Unmarshal([]byte(`["invalid_version"]`), &c)
assert.Error(t, err)
})

t.Run("YAML", func(t *testing.T) {
var c Collection
err := c.UnmarshalYAML(func(i interface{}) error {
*(i.(*[]string)) = []string{"invalid\n"}
return nil
})
assert.Error(t, err)
})
}
4 changes: 2 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ func (v *Version) MarshalYAML() (interface{}, error) {
func (v *Version) unmarshal(f func(interface{}) error) error {
var s string
if err := f(&s); err != nil {
return fmt.Errorf("unmarshal failed to decode input: %w", err)
return fmt.Errorf("failed to decode input: %w", err)
}
newV, err := NewVersion(s)
if err != nil {
return fmt.Errorf("failed to unmarshal '%s': %w", s, err)
return fmt.Errorf("failed to unmarshal version: %w", err)
}
*v = *newV
return nil
Expand Down
24 changes: 24 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version

import (
"encoding/json"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -81,3 +82,26 @@ func TestUnmarshalling(t *testing.T) {
assert.Equal(t, "v1.0.0+k0s.1", v.String())
})
}

func TestFailingUnmarshalling(t *testing.T) {
t.Run("JSON", func(t *testing.T) {
var v Version
err := json.Unmarshal([]byte(`invalid_json`), &v)
assert.Error(t, err)
err = json.Unmarshal([]byte(`"invalid_version"`), &v)
assert.Error(t, err)
})

t.Run("YAML", func(t *testing.T) {
var v = &Version{}
err := v.UnmarshalYAML(func(i interface{}) error {
return errors.New("forced error")
})
assert.Error(t, err)
err = v.UnmarshalYAML(func(i interface{}) error {
*(i.(*string)) = "invalid_version"
return nil
})
assert.Error(t, err)
})
}

0 comments on commit 8063cc8

Please sign in to comment.