Skip to content

Commit

Permalink
Fixed JSON/YAML/Binary parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Nov 21, 2023
1 parent ccd5943 commit 89efb15
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (v *Version) UnmarshalBinary(data []byte) error {

buff, data = decodeArray(data)
v.raw = string(buff)
v.bytes = []byte(v.raw)
v.major, data = decodeInt(data)
v.minor, data = decodeInt(data)
v.patch, data = decodeInt(data)
Expand Down
12 changes: 12 additions & 0 deletions binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func TestGOBEncoderVersion(t *testing.T) {

require.Equal(t, dumpV, dumpU)
require.Equal(t, testVersion, u.String())

{
dataV := new(bytes.Buffer)
dataU := new(bytes.Buffer)
require.NoError(t, gob.NewEncoder(dataV).Encode(MustParse("1.6.2")))
require.NoError(t, gob.NewEncoder(dataU).Encode(MustParse("1.6.3")))

var v, u *Version
require.NoError(t, gob.NewDecoder(dataV).Decode(&v))
require.NoError(t, gob.NewDecoder(dataU).Decode(&u))
require.True(t, u.GreaterThan(v))
}
}

func TestGOBEncoderRelaxedVersion(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (v *Version) UnmarshalJSON(data []byte) error {
}

v.raw = parsed.raw
v.bytes = []byte(v.raw)
v.major = parsed.major
v.minor = parsed.minor
v.patch = parsed.patch
Expand Down
4 changes: 4 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func TestJSONParseVersion(t *testing.T) {

err = json.Unmarshal([]byte(`123`), &u)
require.Error(t, err)

require.NoError(t, json.Unmarshal([]byte(`"1.6.2"`), &v))
require.NoError(t, json.Unmarshal([]byte(`"1.6.3"`), &u))
require.True(t, u.GreaterThan(v))
}

func TestJSONParseRelaxedVersion(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (v *Version) UnmarshalYAML(node *yaml.Node) error {
}

v.raw = parsed.raw
v.bytes = []byte(v.raw)
v.major = parsed.major
v.minor = parsed.minor
v.patch = parsed.patch
Expand Down
4 changes: 4 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func TestYAMLParseVersion(t *testing.T) {

err = yaml.Unmarshal([]byte(`invalid:`), &u)
require.Error(t, err)

require.NoError(t, yaml.Unmarshal([]byte(`"1.6.2"`), &v))
require.NoError(t, yaml.Unmarshal([]byte(`"1.6.3"`), &u))
require.True(t, u.GreaterThan(v))
}

func TestYAMLParseRelaxedVersion(t *testing.T) {
Expand Down

0 comments on commit 89efb15

Please sign in to comment.