Skip to content

Commit 59da58c

Browse files
authored
Merge pull request #73 from hashicorp/b-equal-nil-check
Add nil check to Version.Equal
2 parents 2b13044 + a604335 commit 59da58c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

version.go

+4
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ func comparePrereleases(v string, other string) int {
280280

281281
// Equal tests if two versions are equal.
282282
func (v *Version) Equal(o *Version) bool {
283+
if v == nil || o == nil {
284+
return v == o
285+
}
286+
283287
return v.Compare(o) == 0
284288
}
285289

version_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,32 @@ func TestVersionCompare_versionAndSemver(t *testing.T) {
172172
}
173173
}
174174

175+
func TestVersionEqual_nil(t *testing.T) {
176+
mustVersion := func(v string) *Version {
177+
ver, err := NewVersion(v)
178+
if err != nil {
179+
t.Fatal(err)
180+
}
181+
return ver
182+
}
183+
cases := []struct {
184+
leftVersion *Version
185+
rightVersion *Version
186+
expected bool
187+
}{
188+
{mustVersion("1.0.0"), nil, false},
189+
{nil, mustVersion("1.0.0"), false},
190+
{nil, nil, true},
191+
}
192+
193+
for _, tc := range cases {
194+
given := tc.leftVersion.Equal(tc.rightVersion)
195+
if given != tc.expected {
196+
t.Fatalf("expected Equal to nil to be %t", tc.expected)
197+
}
198+
}
199+
}
200+
175201
func TestComparePreReleases(t *testing.T) {
176202
cases := []struct {
177203
v1 string

0 commit comments

Comments
 (0)