package versioning_test import ( "testing" "git.bit5.ru/backend/versioning" "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}, {versionStr: "12345.123.12", valid: true, expectedStr: "12345.123.12", expectedCode: 1234512312}, {versionStr: "42948.999.99", valid: true, 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, 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: "12345.123", valid: true, expectedStr: "12345.123.0", expectedCode: 1234512300}, {versionStr: "42948.999", valid: true, 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: "12345", valid: true, expectedStr: "12345.0.0", expectedCode: 1234500000}, {versionStr: "42948", valid: true, 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.expectedStr, v.String(), "case#%d", caseNum) require.EqualValues(t, c.expectedCode, v.Code(), "case#%d", caseNum) } } func TestParseFromCode(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) } }