Skip to content

Commit

Permalink
Merge pull request #73 from hashicorp/b-equal-nil-check
Browse files Browse the repository at this point in the history
Add nil check to Version.Equal
  • Loading branch information
radeksimko committed Jun 17, 2020
2 parents 2b13044 + a604335 commit 59da58c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions version.go
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
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

0 comments on commit 59da58c

Please sign in to comment.