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

Add nil check to Version.Equal #73

Merged
merged 1 commit into from
Jun 17, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ func comparePrereleases(v string, other string) int {

// Equal tests if two versions are equal.
func (v *Version) Equal(o *Version) bool {
if v == nil || o == nil {
return v == o
}

return v.Compare(o) == 0
}

Expand Down
26 changes: 26 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,32 @@ func TestVersionCompare_versionAndSemver(t *testing.T) {
}
}

func TestVersionEqual_nil(t *testing.T) {
mustVersion := func(v string) *Version {
ver, err := NewVersion(v)
if err != nil {
t.Fatal(err)
}
return ver
}
cases := []struct {
leftVersion *Version
rightVersion *Version
expected bool
}{
{mustVersion("1.0.0"), nil, false},
{nil, mustVersion("1.0.0"), false},
{nil, nil, true},
}

for _, tc := range cases {
given := tc.leftVersion.Equal(tc.rightVersion)
if given != tc.expected {
t.Fatalf("expected Equal to nil to be %t", tc.expected)
}
}
}

func TestComparePreReleases(t *testing.T) {
cases := []struct {
v1 string
Expand Down