Skip to content

Commit

Permalink
Merge pull request #213 from KnutZuidema/feat/nil_equal
Browse files Browse the repository at this point in the history
feat: nil version equality
  • Loading branch information
mattfarina committed May 14, 2024
2 parents 3d240d7 + 441ff8e commit 347f541
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
6 changes: 6 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ func (v *Version) GreaterThan(o *Version) bool {
// Note, versions can be equal with different metadata since metadata
// is not considered part of the comparable version.
func (v *Version) Equal(o *Version) bool {
if v == o {
return true
}
if v == nil || o == nil {
return false
}
return v.Compare(o) == 0
}

Expand Down
27 changes: 10 additions & 17 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,28 +343,21 @@ func TestGreaterThan(t *testing.T) {

func TestEqual(t *testing.T) {
tests := []struct {
v1 string
v2 string
v1 *Version
v2 *Version
expected bool
}{
{"1.2.3", "1.5.1", false},
{"2.2.3", "1.5.1", false},
{"3.2-beta", "3.2-beta", true},
{"3.2-beta+foo", "3.2-beta+bar", true},
{MustParse("1.2.3"), MustParse("1.5.1"), false},
{MustParse("2.2.3"), MustParse("1.5.1"), false},
{MustParse("3.2-beta"), MustParse("3.2-beta"), true},
{MustParse("3.2-beta+foo"), MustParse("3.2-beta+bar"), true},
{nil, nil, true},
{nil, MustParse("1.2.3"), false},
{MustParse("1.2.3"), nil, false},
}

for _, tc := range tests {
v1, err := NewVersion(tc.v1)
if err != nil {
t.Errorf("Error parsing version: %s", err)
}

v2, err := NewVersion(tc.v2)
if err != nil {
t.Errorf("Error parsing version: %s", err)
}

a := v1.Equal(v2)
a := tc.v1.Equal(tc.v2)
e := tc.expected
if a != e {
t.Errorf(
Expand Down

0 comments on commit 347f541

Please sign in to comment.