cuelang.org/go@v0.10.1/internal/mod/semver/semver_test.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package semver 6 7 import ( 8 "math/rand" 9 "slices" 10 "strings" 11 "testing" 12 ) 13 14 var tests = []struct { 15 in string 16 out string 17 }{ 18 {"bad", ""}, 19 {"v1+meta", ""}, 20 {"v1-alpha.beta.gamma", ""}, 21 {"v1-pre", ""}, 22 {"v1-pre+meta", ""}, 23 {"v1.2+meta", ""}, 24 {"v1.2-pre", ""}, 25 {"v1.2-pre+meta", ""}, 26 {"v1.0.0-alpha", "v1.0.0-alpha"}, 27 {"v1.0.0-alpha.1", "v1.0.0-alpha.1"}, 28 {"v1.0.0-alpha.beta", "v1.0.0-alpha.beta"}, 29 {"v1.0.0-beta", "v1.0.0-beta"}, 30 {"v1.0.0-beta.2", "v1.0.0-beta.2"}, 31 {"v1.0.0-beta.11", "v1.0.0-beta.11"}, 32 {"v1.0.0-rc.1", "v1.0.0-rc.1"}, 33 {"v1", "v1.0.0"}, 34 {"v1.0", "v1.0.0"}, 35 {"v1.0.0", "v1.0.0"}, 36 {"v1.2", "v1.2.0"}, 37 {"v1.2.0", "v1.2.0"}, 38 {"v1.2.3-456", "v1.2.3-456"}, 39 {"v1.2.3-456.789", "v1.2.3-456.789"}, 40 {"v1.2.3-456-789", "v1.2.3-456-789"}, 41 {"v1.2.3-456a", "v1.2.3-456a"}, 42 {"v1.2.3-pre", "v1.2.3-pre"}, 43 {"v1.2.3-pre+meta", "v1.2.3-pre"}, 44 {"v1.2.3-pre.1", "v1.2.3-pre.1"}, 45 {"v1.2.3-zzz", "v1.2.3-zzz"}, 46 {"v1.2.3", "v1.2.3"}, 47 {"v1.2.3+meta", "v1.2.3"}, 48 {"v1.2.3+meta-pre", "v1.2.3"}, 49 {"v1.2.3+meta-pre.sha.256a", "v1.2.3"}, 50 } 51 52 func TestIsValid(t *testing.T) { 53 for _, tt := range tests { 54 ok := IsValid(tt.in) 55 if ok != (tt.out != "") { 56 t.Errorf("IsValid(%q) = %v, want %v", tt.in, ok, !ok) 57 } 58 } 59 } 60 61 func TestCanonical(t *testing.T) { 62 for _, tt := range tests { 63 out := Canonical(tt.in) 64 if out != tt.out { 65 t.Errorf("Canonical(%q) = %q, want %q", tt.in, out, tt.out) 66 } 67 } 68 } 69 70 func TestMajor(t *testing.T) { 71 for _, tt := range tests { 72 out := Major(tt.in) 73 want := "" 74 if i := strings.Index(tt.out, "."); i >= 0 { 75 want = tt.out[:i] 76 } 77 if out != want { 78 t.Errorf("Major(%q) = %q, want %q", tt.in, out, want) 79 } 80 } 81 } 82 83 func TestMajorMinor(t *testing.T) { 84 for _, tt := range tests { 85 out := MajorMinor(tt.in) 86 var want string 87 if tt.out != "" { 88 want = tt.in 89 if i := strings.Index(want, "+"); i >= 0 { 90 want = want[:i] 91 } 92 if i := strings.Index(want, "-"); i >= 0 { 93 want = want[:i] 94 } 95 switch strings.Count(want, ".") { 96 case 0: 97 want += ".0" 98 case 1: 99 // ok 100 case 2: 101 want = want[:strings.LastIndex(want, ".")] 102 } 103 } 104 if out != want { 105 t.Errorf("MajorMinor(%q) = %q, want %q", tt.in, out, want) 106 } 107 } 108 } 109 110 func TestPrerelease(t *testing.T) { 111 for _, tt := range tests { 112 pre := Prerelease(tt.in) 113 var want string 114 if tt.out != "" { 115 if i := strings.Index(tt.out, "-"); i >= 0 { 116 want = tt.out[i:] 117 } 118 } 119 if pre != want { 120 t.Errorf("Prerelease(%q) = %q, want %q", tt.in, pre, want) 121 } 122 } 123 } 124 125 func TestBuild(t *testing.T) { 126 for _, tt := range tests { 127 build := Build(tt.in) 128 var want string 129 if tt.out != "" { 130 if i := strings.Index(tt.in, "+"); i >= 0 { 131 want = tt.in[i:] 132 } 133 } 134 if build != want { 135 t.Errorf("Build(%q) = %q, want %q", tt.in, build, want) 136 } 137 } 138 } 139 140 func TestCompare(t *testing.T) { 141 for i, ti := range tests { 142 for j, tj := range tests { 143 cmp := Compare(ti.in, tj.in) 144 var want int 145 if ti.out == tj.out { 146 want = 0 147 } else if i < j { 148 want = -1 149 } else { 150 want = +1 151 } 152 if cmp != want { 153 t.Errorf("Compare(%q, %q) = %d, want %d", ti.in, tj.in, cmp, want) 154 } 155 } 156 } 157 } 158 159 func TestSort(t *testing.T) { 160 versions := make([]string, len(tests)) 161 for i, test := range tests { 162 versions[i] = test.in 163 } 164 sortedVersions := slices.Clone(versions) 165 rand.Shuffle(len(versions), func(i, j int) { versions[i], versions[j] = versions[j], versions[i] }) 166 Sort(versions) 167 if !slices.Equal(versions, sortedVersions) { 168 t.Errorf("list is not sorted:\n%s", strings.Join(versions, "\n")) 169 } 170 } 171 172 func TestMax(t *testing.T) { 173 for i, ti := range tests { 174 for j, tj := range tests { 175 max := Max(ti.in, tj.in) 176 want := Canonical(ti.in) 177 if i < j { 178 want = Canonical(tj.in) 179 } 180 if max != want { 181 t.Errorf("Max(%q, %q) = %q, want %q", ti.in, tj.in, max, want) 182 } 183 } 184 } 185 } 186 187 var ( 188 v1 = "v1.0.0+metadata-dash" 189 v2 = "v1.0.0+metadata-dash1" 190 ) 191 192 func BenchmarkCompare(b *testing.B) { 193 for i := 0; i < b.N; i++ { 194 if Compare(v1, v2) != 0 { 195 b.Fatalf("bad compare") 196 } 197 } 198 }