316 lines
11 KiB
Go
316 lines
11 KiB
Go
package versioning_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.bit5.ru/backend/versioning"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
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,
|
|
},
|
|
|
|
{
|
|
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},
|
|
{versionStr: "42949.999.99", valid: false},
|
|
{versionStr: "42949.999.100", valid: false},
|
|
{versionStr: "42949.1000.99", valid: false},
|
|
{versionStr: "42949.1000.100", valid: false},
|
|
|
|
{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, 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, 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, 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},
|
|
{versionStr: " 1", valid: false},
|
|
{versionStr: "a1", valid: false},
|
|
{versionStr: "1a", valid: false},
|
|
|
|
{versionStr: "1.1 ", valid: false},
|
|
{versionStr: "1. 1", valid: false},
|
|
{versionStr: "1.1a", valid: false},
|
|
{versionStr: "1.a1", valid: false},
|
|
|
|
{versionStr: "1.1.1 ", valid: false},
|
|
{versionStr: "1.1. 1", valid: false},
|
|
{versionStr: "1.1.1a", valid: false},
|
|
{versionStr: "1.1.a1", valid: false},
|
|
{versionStr: "1,1.1", valid: false},
|
|
{versionStr: "1!1.1", valid: false},
|
|
{versionStr: "1 1.1", valid: false},
|
|
}
|
|
|
|
for i, c := range cases {
|
|
caseNum := i + 1
|
|
|
|
v, err := versioning.ParseVersion(c.versionStr)
|
|
|
|
if !c.valid {
|
|
require.Error(t, err, "case#%d", caseNum)
|
|
continue
|
|
}
|
|
|
|
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 TestParseVersionFromCode(t *testing.T) {
|
|
cases := []struct {
|
|
code uint32
|
|
valid bool
|
|
expectedStr string
|
|
}{
|
|
{code: 0, valid: true, expectedStr: "0.0.0"},
|
|
{code: 1, valid: true, expectedStr: "0.0.1"},
|
|
{code: 10, valid: true, expectedStr: "0.0.10"},
|
|
{code: 99, valid: true, expectedStr: "0.0.99"},
|
|
{code: 100, valid: true, expectedStr: "0.1.0"},
|
|
{code: 101, valid: true, expectedStr: "0.1.1"},
|
|
{code: 110, valid: true, expectedStr: "0.1.10"},
|
|
{code: 199, valid: true, expectedStr: "0.1.99"},
|
|
{code: 99900, valid: true, expectedStr: "0.999.0"},
|
|
{code: 99901, valid: true, expectedStr: "0.999.1"},
|
|
{code: 99910, valid: true, expectedStr: "0.999.10"},
|
|
{code: 99999, valid: true, expectedStr: "0.999.99"},
|
|
{code: 100000, valid: true, expectedStr: "1.0.0"},
|
|
{code: 100001, valid: true, expectedStr: "1.0.1"},
|
|
{code: 100099, valid: true, expectedStr: "1.0.99"},
|
|
{code: 100100, valid: true, expectedStr: "1.1.0"},
|
|
{code: 100101, valid: true, expectedStr: "1.1.1"},
|
|
{code: 100199, valid: true, expectedStr: "1.1.99"},
|
|
{code: 199900, valid: true, expectedStr: "1.999.0"},
|
|
{code: 199901, valid: true, expectedStr: "1.999.1"},
|
|
{code: 199999, valid: true, expectedStr: "1.999.99"},
|
|
{code: 4294800000, valid: true, expectedStr: "42948.0.0"},
|
|
{code: 4294800001, valid: true, expectedStr: "42948.0.1"},
|
|
{code: 4294800099, valid: true, expectedStr: "42948.0.99"},
|
|
{code: 4294800100, valid: true, expectedStr: "42948.1.0"},
|
|
{code: 4294800101, valid: true, expectedStr: "42948.1.1"},
|
|
{code: 4294800199, valid: true, expectedStr: "42948.1.99"},
|
|
{code: 4294899900, valid: true, expectedStr: "42948.999.0"},
|
|
{code: 4294899901, valid: true, expectedStr: "42948.999.1"},
|
|
{code: 4294899999, valid: true, expectedStr: "42948.999.99"},
|
|
{code: 4294900000, valid: false},
|
|
}
|
|
|
|
for i, c := range cases {
|
|
caseNum := i + 1
|
|
|
|
v, err := versioning.ParseVersionFromCode(c.code)
|
|
|
|
if !c.valid {
|
|
require.Error(t, err, "case#%d", caseNum)
|
|
continue
|
|
}
|
|
|
|
require.NoError(t, err, "case#%d", caseNum)
|
|
require.EqualValues(t, c.expectedStr, v.String(), "case#%d", caseNum)
|
|
}
|
|
}
|