github.com/sirkon/goproxy@v1.4.8/internal/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 "strings" 9 "testing" 10 ) 11 12 var tests = []struct { 13 in string 14 out string 15 }{ 16 {"bad", ""}, 17 {"v1-alpha.beta.gamma", ""}, 18 {"v1-pre", ""}, 19 {"v1+meta", ""}, 20 {"v1-pre+meta", ""}, 21 {"v1.2-pre", ""}, 22 {"v1.2+meta", ""}, 23 {"v1.2-pre+meta", ""}, 24 {"v1.0.0-alpha", "v1.0.0-alpha"}, 25 {"v1.0.0-alpha.1", "v1.0.0-alpha.1"}, 26 {"v1.0.0-alpha.beta", "v1.0.0-alpha.beta"}, 27 {"v1.0.0-beta", "v1.0.0-beta"}, 28 {"v1.0.0-beta.2", "v1.0.0-beta.2"}, 29 {"v1.0.0-beta.11", "v1.0.0-beta.11"}, 30 {"v1.0.0-rc.1", "v1.0.0-rc.1"}, 31 {"v1", "v1.0.0"}, 32 {"v1.0", "v1.0.0"}, 33 {"v1.0.0", "v1.0.0"}, 34 {"v1.2", "v1.2.0"}, 35 {"v1.2.0", "v1.2.0"}, 36 {"v1.2.3-456", "v1.2.3-456"}, 37 {"v1.2.3-456.789", "v1.2.3-456.789"}, 38 {"v1.2.3-456-789", "v1.2.3-456-789"}, 39 {"v1.2.3-456a", "v1.2.3-456a"}, 40 {"v1.2.3-pre", "v1.2.3-pre"}, 41 {"v1.2.3-pre+meta", "v1.2.3-pre"}, 42 {"v1.2.3-pre.1", "v1.2.3-pre.1"}, 43 {"v1.2.3-zzz", "v1.2.3-zzz"}, 44 {"v1.2.3", "v1.2.3"}, 45 {"v1.2.3+meta", "v1.2.3"}, 46 {"v1.2.3+meta-pre", "v1.2.3"}, 47 } 48 49 func TestIsValid(t *testing.T) { 50 for _, tt := range tests { 51 ok := IsValid(tt.in) 52 if ok != (tt.out != "") { 53 t.Errorf("IsValid(%q) = %v, want %v", tt.in, ok, !ok) 54 } 55 } 56 } 57 58 func TestCanonical(t *testing.T) { 59 for _, tt := range tests { 60 out := Canonical(tt.in) 61 if out != tt.out { 62 t.Errorf("Canonical(%q) = %q, want %q", tt.in, out, tt.out) 63 } 64 } 65 } 66 67 func TestMajor(t *testing.T) { 68 for _, tt := range tests { 69 out := Major(tt.in) 70 want := "" 71 if i := strings.Index(tt.out, "."); i >= 0 { 72 want = tt.out[:i] 73 } 74 if out != want { 75 t.Errorf("Major(%q) = %q, want %q", tt.in, out, want) 76 } 77 } 78 } 79 80 func TestMajorMinor(t *testing.T) { 81 for _, tt := range tests { 82 out := MajorMinor(tt.in) 83 var want string 84 if tt.out != "" { 85 want = tt.in 86 if i := strings.Index(want, "+"); i >= 0 { 87 want = want[:i] 88 } 89 if i := strings.Index(want, "-"); i >= 0 { 90 want = want[:i] 91 } 92 switch strings.Count(want, ".") { 93 case 0: 94 want += ".0" 95 case 1: 96 // ok 97 case 2: 98 want = want[:strings.LastIndex(want, ".")] 99 } 100 } 101 if out != want { 102 t.Errorf("MajorMinor(%q) = %q, want %q", tt.in, out, want) 103 } 104 } 105 } 106 107 func TestPrerelease(t *testing.T) { 108 for _, tt := range tests { 109 pre := Prerelease(tt.in) 110 var want string 111 if tt.out != "" { 112 if i := strings.Index(tt.out, "-"); i >= 0 { 113 want = tt.out[i:] 114 } 115 } 116 if pre != want { 117 t.Errorf("Prerelease(%q) = %q, want %q", tt.in, pre, want) 118 } 119 } 120 } 121 122 func TestBuild(t *testing.T) { 123 for _, tt := range tests { 124 build := Build(tt.in) 125 var want string 126 if tt.out != "" { 127 if i := strings.Index(tt.in, "+"); i >= 0 { 128 want = tt.in[i:] 129 } 130 } 131 if build != want { 132 t.Errorf("Build(%q) = %q, want %q", tt.in, build, want) 133 } 134 } 135 } 136 137 func TestCompare(t *testing.T) { 138 for i, ti := range tests { 139 for j, tj := range tests { 140 cmp := Compare(ti.in, tj.in) 141 var want int 142 if ti.out == tj.out { 143 want = 0 144 } else if i < j { 145 want = -1 146 } else { 147 want = +1 148 } 149 if cmp != want { 150 t.Errorf("Compare(%q, %q) = %d, want %d", ti.in, tj.in, cmp, want) 151 } 152 } 153 } 154 } 155 156 func TestMax(t *testing.T) { 157 for i, ti := range tests { 158 for j, tj := range tests { 159 max := Max(ti.in, tj.in) 160 want := Canonical(ti.in) 161 if i < j { 162 want = Canonical(tj.in) 163 } 164 if max != want { 165 t.Errorf("Max(%q, %q) = %q, want %q", ti.in, tj.in, max, want) 166 } 167 } 168 } 169 } 170 171 var ( 172 v1 = "v1.0.0+metadata-dash" 173 v2 = "v1.0.0+metadata-dash1" 174 ) 175 176 func BenchmarkCompare(b *testing.B) { 177 for i := 0; i < b.N; i++ { 178 if Compare(v1, v2) != 0 { 179 b.Fatalf("bad compare") 180 } 181 } 182 }