Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nil version equality #213

Merged
merged 1 commit into from May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions version.go
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
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