github.com/gogf/gf@v1.16.9/text/gstr/gstr_z_unit_version_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package gstr_test
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/gogf/gf/test/gtest"
    15  	"github.com/gogf/gf/text/gstr"
    16  )
    17  
    18  func Test_CompareVersion(t *testing.T) {
    19  	gtest.C(t, func(t *gtest.T) {
    20  		t.AssertEQ(gstr.CompareVersion("1", ""), 1)
    21  		t.AssertEQ(gstr.CompareVersion("", ""), 0)
    22  		t.AssertEQ(gstr.CompareVersion("", "v0.1"), -1)
    23  		t.AssertEQ(gstr.CompareVersion("1", "v0.99"), 1)
    24  		t.AssertEQ(gstr.CompareVersion("v1.0", "v0.99"), 1)
    25  		t.AssertEQ(gstr.CompareVersion("v1.0.1", "v1.1.0"), -1)
    26  		t.AssertEQ(gstr.CompareVersion("1.0.1", "v1.1.0"), -1)
    27  		t.AssertEQ(gstr.CompareVersion("1.0.0", "v0.1.0"), 1)
    28  		t.AssertEQ(gstr.CompareVersion("1.0.0", "v1.0.0"), 0)
    29  	})
    30  }
    31  
    32  func Test_CompareVersionGo(t *testing.T) {
    33  	gtest.C(t, func(t *gtest.T) {
    34  		t.AssertEQ(gstr.CompareVersionGo("1", ""), 1)
    35  		t.AssertEQ(gstr.CompareVersionGo("", ""), 0)
    36  		t.AssertEQ(gstr.CompareVersionGo("", "v0.1"), -1)
    37  		t.AssertEQ(gstr.CompareVersionGo("v1.0.1", "v1.1.0"), -1)
    38  		t.AssertEQ(gstr.CompareVersionGo("1.0.1", "v1.1.0"), -1)
    39  		t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v0.1.0"), 1)
    40  		t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v1.0.0"), 0)
    41  		t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092158-b2ccc519800e", "0.0.0-20190626092158"), 0)
    42  		t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092159-b2ccc519800e", "0.0.0-20190626092158"), 1)
    43  		t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.0"), 0)
    44  		t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.1"), -1)
    45  		// Note that this comparison a < b.
    46  		t.AssertEQ(gstr.CompareVersionGo("v1.12.2-0.20200413154443-b17e3a6804fa", "v1.12.2"), -1)
    47  	})
    48  }