Skip to content

Commit

Permalink
add unit tests for library
Browse files Browse the repository at this point in the history
  • Loading branch information
UiP9AV6Y committed Feb 8, 2024
1 parent ba1925a commit ceedbdd
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 13 deletions.
20 changes: 12 additions & 8 deletions buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ func New() *BuildInfo {
}

// Parse unmarshals the given JSON byte data into
// a BuildInfo instance
func Parse(info []byte) (*BuildInfo, error) {
result := New()
if err := json.Unmarshal(info, result); err != nil {
return nil, err
// a BuildInfo instance. An empty input or even nil
// are considered valid values, and return a default
// instance.
func Parse(info []byte) (result *BuildInfo, err error) {
result = New()
if info == nil || len(info) == 0 {
return
}

return result, nil
err = json.Unmarshal(info, result)

return
}

// TryParse calls Parse and returns a
Expand Down Expand Up @@ -81,8 +85,8 @@ func (i *BuildInfo) String() string {

// Equal compares the fields of this instance to the given one
func (i *BuildInfo) Equal(o *BuildInfo) bool {
if o == nil {
return i == nil
if i == nil || o == nil {
return i == nil && o == nil
}

return i.VersionInfo.Equal(o.VersionInfo) && i.EnvironmentInfo.Equal(o.EnvironmentInfo)
Expand Down
222 changes: 222 additions & 0 deletions buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package buildinfo

import (
"testing"
"time"

"gotest.tools/v3/assert"
)

func TestParse(t *testing.T) {
type testCase struct {
have []byte
wantError bool
want *BuildInfo
}

testCases := map[string]testCase{
"nil": {
have: nil,
want: New(),
},
"no data": {
have: []byte{},
want: New(),
},
"empty string": {
have: []byte(""),
want: New(),
},
"empty object": {
have: []byte("{}"),
want: New(),
},
"trailing newline": {
have: []byte("{}\n"),
want: New(),
},
"full": {
have: []byte(`{
"version":"1",
"revision":"2",
"branch":"3",
"user":"4",
"host":"5",
"date":"1970-01-01T00:00:00.00000000Z"
}`),
want: &BuildInfo{
VersionInfo: &VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
},
EnvironmentInfo: &EnvironmentInfo{
User: "4",
Host: "5",
Date: time.Unix(0, 0),
},
},
},
"malformed": {
have: []byte("{"),
wantError: true,
},
}

for ctx, tc := range testCases {
t.Run(ctx, func(t *testing.T) {
got, err := Parse(tc.have)

if tc.wantError {
assert.Assert(t, err != nil)
} else if tc.want == nil {
assert.Assert(t, err)
assert.Assert(t, got == nil)
} else {
assert.Assert(t, err)
assert.Assert(t, tc.want.VersionInfo.Equal(got.VersionInfo), "want=%s; got=%s", tc.want, got)
}
})
}
}

func TestBuildInfoEqual(t *testing.T) {
type testCase struct {
haveLeft, haveRight *BuildInfo
want bool
}

testCases := map[string]testCase{
"nil": {
want: true,
},
// "default": {
// haveLeft: New(),
// haveRight: New(),
// want: true,
// },
"custom": {
haveLeft: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
haveRight: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
want: true,
},
"left nil": {
haveRight: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
},
"right nil": {
haveLeft: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
},
"version nil": {
haveLeft: NewBuildInfo(nil, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
haveRight: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
},
"version mismatch": {
haveLeft: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
haveRight: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "0",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
},
"environment nil": {
haveLeft: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, nil),
haveRight: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
},
"environment mismatch": {
haveLeft: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
}),
haveRight: NewBuildInfo(&VersionInfo{
Version: "1",
Revision: "2",
Branch: "3",
}, &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(1, 0),
}),
},
}

for ctx, tc := range testCases {
t.Run(ctx, func(t *testing.T) {
got := tc.haveLeft.Equal(tc.haveRight)

assert.Equal(t, got, tc.want)
})
}
}
4 changes: 2 additions & 2 deletions environmentinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (i *EnvironmentInfo) String() string {

// Equal compares the fields of this instance to the given one
func (i *EnvironmentInfo) Equal(o *EnvironmentInfo) bool {
if o == nil {
return i == nil
if i == nil || o == nil {
return i == nil && o == nil
}

return i.User == o.User && i.Host == o.Host && i.Date == o.Date
Expand Down
97 changes: 97 additions & 0 deletions environmentinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package buildinfo

import (
"testing"
"time"

"gotest.tools/v3/assert"
)

func TestEnvironmentInfoEqual(t *testing.T) {
type testCase struct {
haveLeft, haveRight *EnvironmentInfo
want bool
}

testCases := map[string]testCase{
"nil": {
want: true,
},
// "default": {
// haveLeft: NewEnvironmentInfo(),
// haveRight: NewEnvironmentInfo(),
// want: true,
// },
"custom": {
haveLeft: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
},
haveRight: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
},
want: true,
},
"left nil": {
haveRight: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
},
},
"right nil": {
haveLeft: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
},
},
"user mismatch": {
haveLeft: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
},
haveRight: &EnvironmentInfo{
User: "0",
Host: "2",
Date: time.Unix(0, 0),
},
},
"host mismatch": {
haveLeft: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
},
haveRight: &EnvironmentInfo{
User: "1",
Host: "0",
Date: time.Unix(0, 0),
},
},
"date mismatch": {
haveLeft: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(0, 0),
},
haveRight: &EnvironmentInfo{
User: "1",
Host: "2",
Date: time.Unix(1, 0),
},
},
}

for ctx, tc := range testCases {
t.Run(ctx, func(t *testing.T) {
got := tc.haveLeft.Equal(tc.haveRight)

assert.Equal(t, got, tc.want)
})
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/UiP9AV6Y/buildinfo

go 1.19

require (
github.com/google/go-cmp v0.5.9 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
Loading

0 comments on commit ceedbdd

Please sign in to comment.