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.
12 var tests = []struct {
17 {"v1-alpha.beta.gamma", ""},
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"},
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"},
45 {"v1.2.3+meta", "v1.2.3"},
46 {"v1.2.3+meta-pre", "v1.2.3"},
47 {"v1.2.3+meta-pre.sha.256a", "v1.2.3"},
50 func TestIsValid(t *testing.T) {
51 for _, tt := range tests {
53 if ok != (tt.out != "") {
54 t.Errorf("IsValid(%q) = %v, want %v", tt.in, ok, !ok)
59 func TestCanonical(t *testing.T) {
60 for _, tt := range tests {
61 out := Canonical(tt.in)
63 t.Errorf("Canonical(%q) = %q, want %q", tt.in, out, tt.out)
68 func TestMajor(t *testing.T) {
69 for _, tt := range tests {
72 if i := strings.Index(tt.out, "."); i >= 0 {
76 t.Errorf("Major(%q) = %q, want %q", tt.in, out, want)
81 func TestMajorMinor(t *testing.T) {
82 for _, tt := range tests {
83 out := MajorMinor(tt.in)
87 if i := strings.Index(want, "+"); i >= 0 {
90 if i := strings.Index(want, "-"); i >= 0 {
93 switch strings.Count(want, ".") {
99 want = want[:strings.LastIndex(want, ".")]
103 t.Errorf("MajorMinor(%q) = %q, want %q", tt.in, out, want)
108 func TestPrerelease(t *testing.T) {
109 for _, tt := range tests {
110 pre := Prerelease(tt.in)
113 if i := strings.Index(tt.out, "-"); i >= 0 {
118 t.Errorf("Prerelease(%q) = %q, want %q", tt.in, pre, want)
123 func TestBuild(t *testing.T) {
124 for _, tt := range tests {
125 build := Build(tt.in)
128 if i := strings.Index(tt.in, "+"); i >= 0 {
133 t.Errorf("Build(%q) = %q, want %q", tt.in, build, want)
138 func TestCompare(t *testing.T) {
139 for i, ti := range tests {
140 for j, tj := range tests {
141 cmp := Compare(ti.in, tj.in)
143 if ti.out == tj.out {
151 t.Errorf("Compare(%q, %q) = %d, want %d", ti.in, tj.in, cmp, want)
157 func TestMax(t *testing.T) {
158 for i, ti := range tests {
159 for j, tj := range tests {
160 max := Max(ti.in, tj.in)
161 want := Canonical(ti.in)
163 want = Canonical(tj.in)
166 t.Errorf("Max(%q, %q) = %q, want %q", ti.in, tj.in, max, want)
173 v1 = "v1.0.0+metadata-dash"
174 v2 = "v1.0.0+metadata-dash1"
177 func BenchmarkCompare(b *testing.B) {
178 for i := 0; i < b.N; i++ {
179 if Compare(v1, v2) != 0 {
180 b.Fatalf("bad compare")