Add GreaterThan method for comparing versions

This commit is contained in:
David Dworken 2023-09-23 11:34:02 -07:00
parent 6cfbec4823
commit 47bdc966b2
No known key found for this signature in database
2 changed files with 19 additions and 0 deletions

View File

@ -642,3 +642,15 @@ func TestVersionLessThan(t *testing.T) {
require.True(t, parsedVersion{1, 200}.lessThan(parsedVersion{1, 205}))
require.True(t, parsedVersion{0, 200}.lessThan(parsedVersion{1, 1}))
}
func TestVersionGreaterThan(t *testing.T) {
require.False(t, parsedVersion{0, 200}.greaterThan(parsedVersion{0, 200}))
require.False(t, parsedVersion{1, 200}.greaterThan(parsedVersion{1, 200}))
require.True(t, parsedVersion{0, 201}.greaterThan(parsedVersion{0, 200}))
require.True(t, parsedVersion{1, 0}.greaterThan(parsedVersion{0, 200}))
require.True(t, parsedVersion{1, 1}.greaterThan(parsedVersion{1, 0}))
require.False(t, parsedVersion{0, 199}.greaterThan(parsedVersion{0, 200}))
require.False(t, parsedVersion{0, 200}.greaterThan(parsedVersion{0, 205}))
require.False(t, parsedVersion{1, 200}.greaterThan(parsedVersion{1, 205}))
require.False(t, parsedVersion{0, 200}.greaterThan(parsedVersion{1, 1}))
}

View File

@ -105,6 +105,13 @@ type parsedVersion struct {
minorVersion int
}
func (pv parsedVersion) greaterThan(other parsedVersion) bool {
if pv.majorVersion == other.majorVersion && pv.minorVersion == other.minorVersion {
return false
}
return !pv.lessThan(other)
}
func (pv parsedVersion) lessThan(other parsedVersion) bool {
if pv.majorVersion != other.majorVersion {
return pv.majorVersion < other.majorVersion