【rich function】VersionCompare replace

master
sunwei 4 years ago committed by Kevin Wan
parent e18ca9aac1
commit 4c9b481bdf

@ -10,14 +10,15 @@ func Compare(v1, v2 string) int {
replaceMap := map[string]string{"V": "", "v": "", "-": "."}
for k, v := range replaceMap {
if strings.Contains(v1, k) {
strings.Replace(v1, k, v, -1)
v1 = strings.Replace(v1, k, v, -1)
}
if strings.Contains(v2, k) {
strings.Replace(v2, k, v, -1)
v2 = strings.Replace(v2, k, v, -1)
}
}
verStr1 := strings.Split(v1, ".")
verStr2 := strings.Split(v2, ".")
ver1 := strSlice2IntSlice(verStr1)
ver2 := strSlice2IntSlice(verStr2)
@ -64,8 +65,8 @@ func strSlice2IntSlice(strs []string) []int64 {
return retInt
}
//operator compare returns true if the first field and the second field are equal else false
func CompareVersions(v1, v2, operator string) bool {
//operator compare returns true if the first field and the third field equation holds else false
func CompareVersions(v1, operator, v2 string) bool {
com := Compare(v1, v2)
switch operator {
case "==":

@ -15,7 +15,7 @@ func TestCustomCompareVersions(t *testing.T) {
}{
{"1", "1.0.1", ">", false},
{"1", "0.9.9", ">", true},
{"1", "1-0.1", "<", true},
{"1", "1.0-1", "<", true},
{"1.0.1", "1-0.1", "<", false},
{"1.0.1", "1.0.1", "==", true},
{"1.0.1", "1.0.2", "==", false},
@ -30,7 +30,7 @@ func TestCustomCompareVersions(t *testing.T) {
for _, each := range cases {
t.Run(each.ver1, func(t *testing.T) {
actual := CompareVersions(each.ver1, each.ver2, each.operator)
actual := CompareVersions(each.ver1, each.operator, each.ver2)
assert.Equal(t, each.out, actual)
})
}

Loading…
Cancel
Save