diff --git a/version.go b/version.go index 30454e1..09703e8 100644 --- a/version.go +++ b/version.go @@ -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 } diff --git a/version_test.go b/version_test.go index bd3534a..cbbeac8 100644 --- a/version_test.go +++ b/version_test.go @@ -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