github.com/gogf/gf/v2@v2.7.4/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/v2/test/gtest"
    15  	"github.com/gogf/gf/v2/text/gstr"
    16  )
    17  
    18  func Test_IsGNUVersion(t *testing.T) {
    19  	gtest.C(t, func(t *gtest.T) {
    20  		t.AssertEQ(gstr.IsGNUVersion(""), false)
    21  		t.AssertEQ(gstr.IsGNUVersion("v"), false)
    22  		t.AssertEQ(gstr.IsGNUVersion("v0"), true)
    23  		t.AssertEQ(gstr.IsGNUVersion("v0."), false)
    24  		t.AssertEQ(gstr.IsGNUVersion("v1."), false)
    25  		t.AssertEQ(gstr.IsGNUVersion("v1.1"), true)
    26  		t.AssertEQ(gstr.IsGNUVersion("v1.1.0"), true)
    27  		t.AssertEQ(gstr.IsGNUVersion("v1.1."), false)
    28  		t.AssertEQ(gstr.IsGNUVersion("v1.1.0.0"), false)
    29  		t.AssertEQ(gstr.IsGNUVersion("v0.0.0"), true)
    30  		t.AssertEQ(gstr.IsGNUVersion("v1.1.-1"), false)
    31  		t.AssertEQ(gstr.IsGNUVersion("v1.1.+1"), false)
    32  	})
    33  }
    34  
    35  func Test_CompareVersion(t *testing.T) {
    36  	gtest.C(t, func(t *gtest.T) {
    37  		t.AssertEQ(gstr.CompareVersion("1", ""), 1)
    38  		t.AssertEQ(gstr.CompareVersion("", ""), 0)
    39  		t.AssertEQ(gstr.CompareVersion("", "v0.1"), -1)
    40  		t.AssertEQ(gstr.CompareVersion("1", "v0.99"), 1)
    41  		t.AssertEQ(gstr.CompareVersion("v1.0", "v0.99"), 1)
    42  		t.AssertEQ(gstr.CompareVersion("v1.0.1", "v1.1.0"), -1)
    43  		t.AssertEQ(gstr.CompareVersion("1.0.1", "v1.1.0"), -1)
    44  		t.AssertEQ(gstr.CompareVersion("1.0.0", "v0.1.0"), 1)
    45  		t.AssertEQ(gstr.CompareVersion("1.0.0", "v1.0.0"), 0)
    46  	})
    47  }
    48  
    49  func Test_CompareVersionGo(t *testing.T) {
    50  	gtest.C(t, func(t *gtest.T) {
    51  		t.AssertEQ(gstr.CompareVersionGo("1", ""), 1)
    52  		t.AssertEQ(gstr.CompareVersionGo("", ""), 0)
    53  		t.AssertEQ(gstr.CompareVersionGo("", "v0.1"), -1)
    54  		t.AssertEQ(gstr.CompareVersionGo("v1.0.1", "v1.1.0"), -1)
    55  		t.AssertEQ(gstr.CompareVersionGo("1.0.1", "v1.1.0"), -1)
    56  		t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v0.1.0"), 1)
    57  		t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v1.0.0"), 0)
    58  		t.AssertEQ(gstr.CompareVersionGo("1.0.0", "v1.0"), 0)
    59  		t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092158-b2ccc519800e", "0.0.0-20190626092158"), 0)
    60  		t.AssertEQ(gstr.CompareVersionGo("v0.0.0-20190626092159-b2ccc519800e", "0.0.0-20190626092158"), 1)
    61  
    62  		// Specially in Golang:
    63  		// "v1.12.2-0.20200413154443-b17e3a6804fa" < "v1.12.2"
    64  		// "v1.12.3-0.20200413154443-b17e3a6804fa" > "v1.12.2"
    65  		t.AssertEQ(gstr.CompareVersionGo("v1.12.2-0.20200413154443-b17e3a6804fa", "v1.12.2"), -1)
    66  		t.AssertEQ(gstr.CompareVersionGo("v1.12.2", "v1.12.2-0.20200413154443-b17e3a6804fa"), 1)
    67  		t.AssertEQ(gstr.CompareVersionGo("v1.12.3-0.20200413154443-b17e3a6804fa", "v1.12.2"), 1)
    68  		t.AssertEQ(gstr.CompareVersionGo("v1.12.2", "v1.12.3-0.20200413154443-b17e3a6804fa"), -1)
    69  		t.AssertEQ(gstr.CompareVersionGo("v1.12.2-0.20200413154443-b17e3a6804fa", "v0.0.0-20190626092158-b2ccc519800e"), 1)
    70  		t.AssertEQ(gstr.CompareVersionGo("v1.12.2-0.20200413154443-b17e3a6804fa", "v1.12.2-0.20200413154444-b2ccc519800e"), -1)
    71  
    72  		// Specially in Golang:
    73  		// "v4.20.1+incompatible" < "v4.20.1"
    74  		t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.0"), -1)
    75  		t.AssertEQ(gstr.CompareVersionGo("4.20.0", "v4.20.0+incompatible"), 1)
    76  		t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "4.20.1"), -1)
    77  		t.AssertEQ(gstr.CompareVersionGo("v4.20.0+incompatible", "v4.20.0+incompatible"), 0)
    78  
    79  	})
    80  }