github.com/lingyao2333/mo-zero@v1.4.1/core/utils/version_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestCompareVersions(t *testing.T) {
    11  	cases := []struct {
    12  		ver1     string
    13  		ver2     string
    14  		operator string
    15  		out      bool
    16  	}{
    17  		{"1", "1.0.1", ">", false},
    18  		{"1", "0.9.9", ">", true},
    19  		{"1", "1.0-1", "<", true},
    20  		{"1.0.1", "1-0.1", "<", false},
    21  		{"1.0.1", "1.0.1", "==", true},
    22  		{"1.0.1", "1.0.2", "==", false},
    23  		{"1.1-1", "1.0.2", "==", false},
    24  		{"1.0.1", "1.0.2", ">=", false},
    25  		{"1.0.2", "1.0.2", ">=", true},
    26  		{"1.0.3", "1.0.2", ">=", true},
    27  		{"1.0.4", "1.0.2", "<=", false},
    28  		{"1.0.4", "1.0.6", "<=", true},
    29  		{"1.0.4", "1.0.4", "<=", true},
    30  	}
    31  
    32  	for _, each := range cases {
    33  		each := each
    34  		t.Run(each.ver1, func(t *testing.T) {
    35  			actual := CompareVersions(each.ver1, each.operator, each.ver2)
    36  			assert.Equal(t, each.out, actual, fmt.Sprintf("%s vs %s", each.ver1, each.ver2))
    37  		})
    38  	}
    39  }