github.com/blend/go-sdk@v1.20220411.3/semver/version_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package semver
     9  
    10  import (
    11  	"fmt"
    12  	"sort"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestNewVersion(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	cases := []struct {
    22  		version string
    23  		err     bool
    24  	}{
    25  		{"1.2.3", false},
    26  		{"1.0", false},
    27  		{"1", false},
    28  		{"1.2.beta", true},
    29  		{"foo", true},
    30  		{"1.2-5", false},
    31  		{"1.2-beta.5", false},
    32  		{"\n1.2", true},
    33  		{"1.2.0-x.Y.0+metadata", false},
    34  		{"1.2.0-x.Y.0+metadata-width-hypen", false},
    35  		{"1.2.3-rc1-with-hypen", false},
    36  		{"1.2.3.4", false},
    37  		{"1.2.0.4-x.Y.0+metadata", false},
    38  		{"1.2.0.4-x.Y.0+metadata-width-hypen", false},
    39  		{"1.2.0-X-1.2.0+metadata~dist", false},
    40  		{"1.2.3.4-rc1-with-hypen", false},
    41  		{"1.2.3.4", false},
    42  		{"v1.2.3", false},
    43  		{"foo1.2.3", true},
    44  		{"1.7rc2", false},
    45  		{"v1.7rc2", false},
    46  	}
    47  
    48  	for _, tc := range cases {
    49  		_, err := NewVersion(tc.version)
    50  		assert.False(tc.err && err == nil, fmt.Sprintf("expected error for version: %s", tc.version))
    51  		assert.False(!tc.err && err != nil, fmt.Sprintf("error for version %s: %s", tc.version, err))
    52  	}
    53  }
    54  
    55  func TestVersionCompare(t *testing.T) {
    56  	assert := assert.New(t)
    57  
    58  	cases := []struct {
    59  		v1       string
    60  		v2       string
    61  		expected int
    62  	}{
    63  		{"1.2.3", "1.4.5", -1},
    64  		{"1.2-beta", "1.2-beta", 0},
    65  		{"1.2", "1.1.4", 1},
    66  		{"1.2", "1.2-beta", 1},
    67  		{"1.2+foo", "1.2+beta", 0},
    68  		{"v1.2", "v1.2-beta", 1},
    69  		{"v1.2+foo", "v1.2+beta", 0},
    70  		{"v1.2.3.4", "v1.2.3.4", 0},
    71  		{"v1.2.0.0", "v1.2", 0},
    72  		{"v1.2.0.0.1", "v1.2", 1},
    73  		{"v1.2", "v1.2.0.0", 0},
    74  		{"v1.2", "v1.2.0.0.1", -1},
    75  		{"v1.2.0.0", "v1.2.0.0.1", -1},
    76  		{"v1.2.3.0", "v1.2.3.4", -1},
    77  		{"1.7rc2", "1.7rc1", 1},
    78  		{"1.7rc2", "1.7", -1},
    79  		{"1.2.0", "1.2.0-X-1.2.0+metadata~dist", 1},
    80  	}
    81  
    82  	for _, tc := range cases {
    83  		v1, err := NewVersion(tc.v1)
    84  		assert.Nil(err)
    85  
    86  		v2, err := NewVersion(tc.v2)
    87  		assert.Nil(err)
    88  
    89  		actual := v1.Compare(v2)
    90  		expected := tc.expected
    91  		assert.Equal(expected, actual, fmt.Sprintf("%s <=> %s\nexpected: %d\nactual: %d", tc.v1, tc.v2, expected, actual))
    92  	}
    93  }
    94  
    95  func TestComparePreReleases(t *testing.T) {
    96  	assert := assert.New(t)
    97  
    98  	cases := []struct {
    99  		v1       string
   100  		v2       string
   101  		expected int
   102  	}{
   103  		{"1.2-beta.2", "1.2-beta.2", 0},
   104  		{"1.2-beta.1", "1.2-beta.2", -1},
   105  		{"1.2-beta.2", "1.2-beta.11", -1},
   106  		{"3.2-alpha.1", "3.2-alpha", 1},
   107  		{"1.2-beta.2", "1.2-beta.1", 1},
   108  		{"1.2-beta.11", "1.2-beta.2", 1},
   109  		{"1.2-beta", "1.2-beta.3", -1},
   110  		{"1.2-alpha", "1.2-beta.3", -1},
   111  		{"1.2-beta", "1.2-alpha.3", 1},
   112  		{"3.0-alpha.3", "3.0-rc.1", -1},
   113  		{"3.0-alpha3", "3.0-rc1", -1},
   114  		{"3.0-alpha.1", "3.0-alpha.beta", -1},
   115  		{"5.4-alpha", "5.4-alpha.beta", 1},
   116  		{"v1.2-beta.2", "v1.2-beta.2", 0},
   117  		{"v1.2-beta.1", "v1.2-beta.2", -1},
   118  		{"v3.2-alpha.1", "v3.2-alpha", 1},
   119  		{"v3.2-rc.1-1-g123", "v3.2-rc.2", 1},
   120  	}
   121  
   122  	for _, tc := range cases {
   123  		v1, err := NewVersion(tc.v1)
   124  		assert.Nil(err)
   125  
   126  		v2, err := NewVersion(tc.v2)
   127  		assert.Nil(err)
   128  
   129  		actual := v1.Compare(v2)
   130  		expected := tc.expected
   131  		assert.Equal(expected, actual)
   132  	}
   133  }
   134  
   135  func TestVersionMetadata(t *testing.T) {
   136  	assert := assert.New(t)
   137  
   138  	cases := []struct {
   139  		version  string
   140  		expected string
   141  	}{
   142  		{"1.2.3", ""},
   143  		{"1.2-beta", ""},
   144  		{"1.2.0-x.Y.0", ""},
   145  		{"1.2.0-x.Y.0+metadata", "metadata"},
   146  		{"1.2.0-metadata-1.2.0+metadata~dist", "metadata~dist"},
   147  	}
   148  
   149  	for _, tc := range cases {
   150  		v, err := NewVersion(tc.version)
   151  		assert.Nil(err)
   152  
   153  		actual := v.Metadata()
   154  		expected := tc.expected
   155  		assert.Equal(expected, actual)
   156  	}
   157  }
   158  
   159  func TestVersionPrerelease(t *testing.T) {
   160  	assert := assert.New(t)
   161  
   162  	cases := []struct {
   163  		version  string
   164  		expected string
   165  	}{
   166  		{"1.2.3", ""},
   167  		{"1.2-beta", "beta"},
   168  		{"1.2.0-x.Y.0", "x.Y.0"},
   169  		{"1.2.0-x.Y.0+metadata", "x.Y.0"},
   170  		{"1.2.0-metadata-1.2.0+metadata~dist", "metadata-1.2.0"},
   171  	}
   172  
   173  	for _, tc := range cases {
   174  		v, err := NewVersion(tc.version)
   175  		assert.Nil(err)
   176  
   177  		actual := v.Prerelease()
   178  		expected := tc.expected
   179  		assert.Equal(expected, actual)
   180  	}
   181  }
   182  
   183  func TestVersionSegments(t *testing.T) {
   184  	assert := assert.New(t)
   185  
   186  	cases := []struct {
   187  		version  string
   188  		expected []int
   189  	}{
   190  		{"1.2.3", []int{1, 2, 3}},
   191  		{"1.2-beta", []int{1, 2, 0}},
   192  		{"1-x.Y.0", []int{1, 0, 0}},
   193  		{"1.2.0-x.Y.0+metadata", []int{1, 2, 0}},
   194  		{"1.2.0-metadata-1.2.0+metadata~dist", []int{1, 2, 0}},
   195  	}
   196  
   197  	for _, tc := range cases {
   198  		v, err := NewVersion(tc.version)
   199  		assert.Nil(err)
   200  
   201  		actual := v.Segments()
   202  		expected := tc.expected
   203  		assert.Equal(expected, actual)
   204  	}
   205  }
   206  
   207  func TestVersionSegments64(t *testing.T) {
   208  	assert := assert.New(t)
   209  
   210  	cases := []struct {
   211  		version  string
   212  		expected []int64
   213  	}{
   214  		{"1.2.3", []int64{1, 2, 3}},
   215  		{"1.2-beta", []int64{1, 2, 0}},
   216  		{"1-x.Y.0", []int64{1, 0, 0}},
   217  		{"1.2.0-x.Y.0+metadata", []int64{1, 2, 0}},
   218  		{"1.4.9223372036854775807", []int64{1, 4, 9223372036854775807}},
   219  	}
   220  
   221  	for _, tc := range cases {
   222  		v, err := NewVersion(tc.version)
   223  		assert.Nil(err)
   224  
   225  		actual := v.Segments64()
   226  		expected := tc.expected
   227  		assert.Equal(expected, actual)
   228  	}
   229  }
   230  
   231  func TestVersionString(t *testing.T) {
   232  	assert := assert.New(t)
   233  
   234  	cases := [][]string{
   235  		{"1.2.3", "1.2.3"},
   236  		{"1.2-beta", "1.2.0-beta"},
   237  		{"1.2.0-x.Y.0", "1.2.0-x.Y.0"},
   238  		{"1.2.0-x.Y.0+metadata", "1.2.0-x.Y.0+metadata"},
   239  		{"1.2.0-metadata-1.2.0+metadata~dist", "1.2.0-metadata-1.2.0+metadata~dist"},
   240  	}
   241  
   242  	for _, tc := range cases {
   243  		v, err := NewVersion(tc[0])
   244  		assert.Nil(err)
   245  
   246  		actual := v.String()
   247  		expected := tc[1]
   248  		assert.Equal(expected, actual)
   249  	}
   250  }
   251  
   252  func TestCollection(t *testing.T) {
   253  	assert := assert.New(t)
   254  
   255  	versionsRaw := []string{
   256  		"1.1.1",
   257  		"1.0",
   258  		"1.2",
   259  		"2",
   260  		"0.7.1",
   261  	}
   262  
   263  	versions := make([]*Version, len(versionsRaw))
   264  	for i, raw := range versionsRaw {
   265  		v, err := NewVersion(raw)
   266  		assert.Nil(err)
   267  		versions[i] = v
   268  	}
   269  
   270  	sort.Sort(Collection(versions))
   271  
   272  	actual := make([]string, len(versions))
   273  	for i, v := range versions {
   274  		actual[i] = v.String()
   275  	}
   276  
   277  	expected := []string{
   278  		"0.7.1",
   279  		"1.0.0",
   280  		"1.1.1",
   281  		"1.2.0",
   282  		"2.0.0",
   283  	}
   284  
   285  	assert.Equal(expected, actual)
   286  }