mirror of
https://gitee.com/johng/gf
synced 2026-07-06 21:45:34 +08:00
improve version functions for package gstr
This commit is contained in:
@ -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:]
|
||||
}
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user