improve version functions for package gstr

This commit is contained in:
John
2020-05-16 00:11:08 +08:00
parent 33e890d225
commit c0df8a3d80
2 changed files with 12 additions and 12 deletions

View File

@ -58,7 +58,7 @@ func CompareVersion(a, b string) int {
return 0
}
// CompareVersion compares <a> and <b> as standard Golang version.
// CompareVersionGo compares <a> and <b> as standard Golang version.
// It returns 1 if <a> > <b>.
// It returns -1 if <a> < <b>.
// It returns 0 if <a> = <b>.
@ -70,7 +70,7 @@ func CompareVersion(a, b string) int {
// v0.0.0-20190626092158-b2ccc519800e
// v4.20.0+incompatible
// etc.
func CompareVersionGolang(a, b string) int {
func CompareVersionGo(a, b string) int {
if a[0] == 'v' {
a = a[1:]
}

View File

@ -26,17 +26,17 @@ func Test_CompareVersion(t *testing.T) {
})
}
func Test_CompareVersionGolang(t *testing.T) {
func Test_CompareVersionGo(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gstr.CompareVersionGolang("v1.0.1", "v1.1.0"), -1)
t.AssertEQ(gstr.CompareVersionGolang("1.0.1", "v1.1.0"), -1)
t.AssertEQ(gstr.CompareVersionGolang("1.0.0", "v0.1.0"), 1)
t.AssertEQ(gstr.CompareVersionGolang("1.0.0", "v1.0.0"), 0)
t.AssertEQ(gstr.CompareVersionGolang("v0.0.0-20190626092158-b2ccc519800e", "0.0.0-20190626092158"), 0)
t.AssertEQ(gstr.CompareVersionGolang("v0.0.0-20190626092159-b2ccc519800e", "0.0.0-20190626092158"), 1)
t.AssertEQ(gstr.CompareVersionGolang("v4.20.0+incompatible", "4.20.0"), 0)
t.AssertEQ(gstr.CompareVersionGolang("v4.20.0+incompatible", "4.20.1"), -1)
t.AssertEQ(gstr.CompareVersionGo("v1.0.1", "v1.1.0"), -1)
t.AssertEQ(gstr.CompareVersionGo("1.0.1", "v1.1.0"), -1)
t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v0.1.0"), 1)
t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v1.0.0"), 0)
t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092158-b2ccc519800e", "0.0.0-20190626092158"), 0)
t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092159-b2ccc519800e", "0.0.0-20190626092158"), 1)
t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.0"), 0)
t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.1"), -1)
// Note that this comparison a < b.
t.AssertEQ(gstr.CompareVersionGolang("v1.12.2-0.20200413154443-b17e3a6804fa", "v1.12.2"), -1)
t.AssertEQ(gstr.CompareVersionGo("v1.12.2-0.20200413154443-b17e3a6804fa", "v1.12.2"), -1)
})
}