methods for comparison and getting info added
This commit is contained in:
parent
69be6e3bee
commit
50c2c27060
|
@ -23,18 +23,46 @@ type Version struct {
|
|||
code uint32
|
||||
}
|
||||
|
||||
func (v Version) String() string {
|
||||
return fmt.Sprintf("%d.%d.%d", v.major, v.minor, v.patch)
|
||||
func (v Version) Major() uint16 {
|
||||
return v.major
|
||||
}
|
||||
|
||||
func (v Version) Minor() uint16 {
|
||||
return v.minor
|
||||
}
|
||||
|
||||
func (v Version) Patch() uint8 {
|
||||
return v.patch
|
||||
}
|
||||
|
||||
func (v Version) Code() uint32 {
|
||||
return v.code
|
||||
}
|
||||
|
||||
func (a Version) Equal(b Version) bool {
|
||||
return a.Code() == b.Code()
|
||||
}
|
||||
|
||||
func (a Version) Less(b Version) bool {
|
||||
return a.Code() < b.Code()
|
||||
}
|
||||
|
||||
func (a Version) Lte(b Version) bool {
|
||||
return a.Code() <= b.Code()
|
||||
}
|
||||
|
||||
func (a Version) Greater(b Version) bool {
|
||||
return a.Code() > b.Code()
|
||||
}
|
||||
|
||||
func (a Version) Gte(b Version) bool {
|
||||
return a.Code() >= b.Code()
|
||||
}
|
||||
|
||||
func (v Version) String() string {
|
||||
return fmt.Sprintf("%d.%d.%d", v.major, v.minor, v.patch)
|
||||
}
|
||||
|
||||
func ParseVersion(str string) (Version, error) {
|
||||
parts := strings.Split(str, ".")
|
||||
partsLen := len(parts)
|
||||
|
|
|
@ -8,24 +8,194 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
cases := []struct {
|
||||
versionStr string
|
||||
valid bool
|
||||
expectedStr string
|
||||
expectedCode uint32
|
||||
}{
|
||||
{versionStr: "0.0.0", valid: true, expectedStr: "0.0.0", expectedCode: 0},
|
||||
{versionStr: "0.0.1", valid: true, expectedStr: "0.0.1", expectedCode: 1},
|
||||
{versionStr: "0.1.0", valid: true, expectedStr: "0.1.0", expectedCode: 100},
|
||||
{versionStr: "0.1.1", valid: true, expectedStr: "0.1.1", expectedCode: 101},
|
||||
{versionStr: "1.0.0", valid: true, expectedStr: "1.0.0", expectedCode: 100000},
|
||||
{versionStr: "1.0.1", valid: true, expectedStr: "1.0.1", expectedCode: 100001},
|
||||
{versionStr: "1.1.0", valid: true, expectedStr: "1.1.0", expectedCode: 100100},
|
||||
{versionStr: "1.1.1", valid: true, expectedStr: "1.1.1", expectedCode: 100101},
|
||||
func TestVersion(t *testing.T) {
|
||||
t.Run("Equal", func(t *testing.T) {
|
||||
cases := []struct {
|
||||
a versioning.Version
|
||||
b versioning.Version
|
||||
expectedEqual bool
|
||||
}{
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedEqual: true,
|
||||
},
|
||||
|
||||
{versionStr: "12345.123.12", valid: true, expectedStr: "12345.123.12", expectedCode: 1234512312},
|
||||
{versionStr: "42948.999.99", valid: true, expectedStr: "42948.999.99", expectedCode: 4294899999},
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0.1"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedEqual: false,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("0.999.99"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedEqual: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
caseNum := i + 1
|
||||
|
||||
actualEqual := c.a.Equal(c.b)
|
||||
require.EqualValues(t, c.expectedEqual, actualEqual, "case#%d", caseNum)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Less", func(t *testing.T) {
|
||||
cases := []struct {
|
||||
a versioning.Version
|
||||
b versioning.Version
|
||||
expectedLess bool
|
||||
}{
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedLess: false,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0.1"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedLess: false,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("0.999.99"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedLess: true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
caseNum := i + 1
|
||||
|
||||
actualLess := c.a.Less(c.b)
|
||||
require.EqualValues(t, c.expectedLess, actualLess, "case#%d", caseNum)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Lte", func(t *testing.T) {
|
||||
cases := []struct {
|
||||
a versioning.Version
|
||||
b versioning.Version
|
||||
expectedLte bool
|
||||
}{
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedLte: true,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0.1"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedLte: false,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("0.999.99"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedLte: true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
caseNum := i + 1
|
||||
|
||||
actualLte := c.a.Lte(c.b)
|
||||
require.EqualValues(t, c.expectedLte, actualLte, "case#%d", caseNum)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Greater", func(t *testing.T) {
|
||||
cases := []struct {
|
||||
a versioning.Version
|
||||
b versioning.Version
|
||||
expectedGreater bool
|
||||
}{
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedGreater: false,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0.1"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedGreater: true,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("0.999.99"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedGreater: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
caseNum := i + 1
|
||||
|
||||
actualGreater := c.a.Greater(c.b)
|
||||
require.EqualValues(t, c.expectedGreater, actualGreater, "case#%d", caseNum)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Gte", func(t *testing.T) {
|
||||
cases := []struct {
|
||||
a versioning.Version
|
||||
b versioning.Version
|
||||
expectedGte bool
|
||||
}{
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedGte: true,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("1.0.1"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedGte: true,
|
||||
},
|
||||
|
||||
{
|
||||
a: versioning.MustParseVersion("0.999.99"),
|
||||
b: versioning.MustParseVersion("1.0.0"),
|
||||
expectedGte: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
caseNum := i + 1
|
||||
|
||||
actualGte := c.a.Gte(c.b)
|
||||
require.EqualValues(t, c.expectedGte, actualGte, "case#%d", caseNum)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseVersion(t *testing.T) {
|
||||
cases := []struct {
|
||||
versionStr string
|
||||
valid bool
|
||||
expectedMajor uint16
|
||||
expectedMinor uint16
|
||||
expectedPatch uint8
|
||||
expectedStr string
|
||||
expectedCode uint32
|
||||
}{
|
||||
{versionStr: "0.0.0", valid: true, expectedMajor: 0, expectedMinor: 0, expectedPatch: 0, expectedStr: "0.0.0", expectedCode: 0},
|
||||
{versionStr: "0.0.1", valid: true, expectedMajor: 0, expectedMinor: 0, expectedPatch: 1, expectedStr: "0.0.1", expectedCode: 1},
|
||||
{versionStr: "0.1.0", valid: true, expectedMajor: 0, expectedMinor: 1, expectedPatch: 0, expectedStr: "0.1.0", expectedCode: 100},
|
||||
{versionStr: "0.1.1", valid: true, expectedMajor: 0, expectedMinor: 1, expectedPatch: 1, expectedStr: "0.1.1", expectedCode: 101},
|
||||
{versionStr: "1.0.0", valid: true, expectedMajor: 1, expectedMinor: 0, expectedPatch: 0, expectedStr: "1.0.0", expectedCode: 100000},
|
||||
{versionStr: "1.0.1", valid: true, expectedMajor: 1, expectedMinor: 0, expectedPatch: 1, expectedStr: "1.0.1", expectedCode: 100001},
|
||||
{versionStr: "1.1.0", valid: true, expectedMajor: 1, expectedMinor: 1, expectedPatch: 0, expectedStr: "1.1.0", expectedCode: 100100},
|
||||
{versionStr: "1.1.1", valid: true, expectedMajor: 1, expectedMinor: 1, expectedPatch: 1, expectedStr: "1.1.1", expectedCode: 100101},
|
||||
|
||||
{versionStr: "12345.123.12", valid: true, expectedMajor: 12345, expectedMinor: 123, expectedPatch: 12, expectedStr: "12345.123.12", expectedCode: 1234512312},
|
||||
{versionStr: "42948.999.99", valid: true, expectedMajor: 42948, expectedMinor: 999, expectedPatch: 99, expectedStr: "42948.999.99", expectedCode: 4294899999},
|
||||
{versionStr: "42948.999.100", valid: false},
|
||||
{versionStr: "42948.1000.99", valid: false},
|
||||
{versionStr: "42948.1000.100", valid: false},
|
||||
|
@ -34,22 +204,22 @@ func TestParse(t *testing.T) {
|
|||
{versionStr: "42949.1000.99", valid: false},
|
||||
{versionStr: "42949.1000.100", valid: false},
|
||||
|
||||
{versionStr: "0.0", valid: true, expectedStr: "0.0.0", expectedCode: 0},
|
||||
{versionStr: "0.1", valid: true, expectedStr: "0.1.0", expectedCode: 100},
|
||||
{versionStr: "1.0", valid: true, expectedStr: "1.0.0", expectedCode: 100000},
|
||||
{versionStr: "1.1", valid: true, expectedStr: "1.1.0", expectedCode: 100100},
|
||||
{versionStr: "0.0", valid: true, expectedMajor: 0, expectedMinor: 0, expectedPatch: 0, expectedStr: "0.0.0", expectedCode: 0},
|
||||
{versionStr: "0.1", valid: true, expectedMajor: 0, expectedMinor: 1, expectedPatch: 0, expectedStr: "0.1.0", expectedCode: 100},
|
||||
{versionStr: "1.0", valid: true, expectedMajor: 1, expectedMinor: 0, expectedPatch: 0, expectedStr: "1.0.0", expectedCode: 100000},
|
||||
{versionStr: "1.1", valid: true, expectedMajor: 1, expectedMinor: 1, expectedPatch: 0, expectedStr: "1.1.0", expectedCode: 100100},
|
||||
|
||||
{versionStr: "12345.123", valid: true, expectedStr: "12345.123.0", expectedCode: 1234512300},
|
||||
{versionStr: "42948.999", valid: true, expectedStr: "42948.999.0", expectedCode: 4294899900},
|
||||
{versionStr: "12345.123", valid: true, expectedMajor: 12345, expectedMinor: 123, expectedPatch: 0, expectedStr: "12345.123.0", expectedCode: 1234512300},
|
||||
{versionStr: "42948.999", valid: true, expectedMajor: 42948, expectedMinor: 999, expectedPatch: 0, expectedStr: "42948.999.0", expectedCode: 4294899900},
|
||||
{versionStr: "42948.1000", valid: false},
|
||||
{versionStr: "42949.999", valid: false},
|
||||
{versionStr: "42949.1000", valid: false},
|
||||
|
||||
{versionStr: "0", valid: true, expectedStr: "0.0.0", expectedCode: 0},
|
||||
{versionStr: "1", valid: true, expectedStr: "1.0.0", expectedCode: 100000},
|
||||
{versionStr: "0", valid: true, expectedMajor: 0, expectedMinor: 0, expectedPatch: 0, expectedStr: "0.0.0", expectedCode: 0},
|
||||
{versionStr: "1", valid: true, expectedMajor: 1, expectedMinor: 0, expectedPatch: 0, expectedStr: "1.0.0", expectedCode: 100000},
|
||||
|
||||
{versionStr: "12345", valid: true, expectedStr: "12345.0.0", expectedCode: 1234500000},
|
||||
{versionStr: "42948", valid: true, expectedStr: "42948.0.0", expectedCode: 4294800000},
|
||||
{versionStr: "12345", valid: true, expectedMajor: 12345, expectedMinor: 0, expectedPatch: 0, expectedStr: "12345.0.0", expectedCode: 1234500000},
|
||||
{versionStr: "42948", valid: true, expectedMajor: 42948, expectedMinor: 0, expectedPatch: 0, expectedStr: "42948.0.0", expectedCode: 4294800000},
|
||||
{versionStr: "42949", valid: false},
|
||||
|
||||
{versionStr: "1 ", valid: false},
|
||||
|
@ -82,12 +252,15 @@ func TestParse(t *testing.T) {
|
|||
}
|
||||
|
||||
require.NoError(t, err, "case#%d", caseNum)
|
||||
require.EqualValues(t, c.expectedMajor, v.Major(), "case#%d", caseNum)
|
||||
require.EqualValues(t, c.expectedMinor, v.Minor(), "case#%d", caseNum)
|
||||
require.EqualValues(t, c.expectedPatch, v.Patch(), "case#%d", caseNum)
|
||||
require.EqualValues(t, c.expectedStr, v.String(), "case#%d", caseNum)
|
||||
require.EqualValues(t, c.expectedCode, v.Code(), "case#%d", caseNum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFromCode(t *testing.T) {
|
||||
func TestParseVersionFromCode(t *testing.T) {
|
||||
cases := []struct {
|
||||
code uint32
|
||||
valid bool
|
||||
|
|
Loading…
Reference in New Issue