208 lines
4 KiB
Go
208 lines
4 KiB
Go
package version
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewVersion(t *testing.T) {
|
|
cases := []struct {
|
|
version string
|
|
err bool
|
|
}{
|
|
{"1.2.3", false},
|
|
{"1.0", false},
|
|
{"1", false},
|
|
{"1.2.beta", true},
|
|
{"foo", true},
|
|
{"1.2-5", false},
|
|
{"1.2-beta.5", false},
|
|
{"\n1.2", true},
|
|
{"1.2.0-x.Y.0+metadata", false},
|
|
{"1.2.0-x.Y.0+metadata-width-hypen", false},
|
|
{"1.2.3-rc1-with-hypen", false},
|
|
{"1.2.3.4", true},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
_, err := NewVersion(tc.version)
|
|
if tc.err && err == nil {
|
|
t.Fatalf("expected error for version: %s", tc.version)
|
|
} else if !tc.err && err != nil {
|
|
t.Fatalf("error for version %s: %s", tc.version, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestVersionCompare(t *testing.T) {
|
|
cases := []struct {
|
|
v1 string
|
|
v2 string
|
|
expected int
|
|
}{
|
|
{"1.2.3", "1.4.5", -1},
|
|
{"1.2-beta", "1.2-beta", 0},
|
|
{"1.2", "1.1.4", 1},
|
|
{"1.2", "1.2-beta", 1},
|
|
{"1.2+foo", "1.2+beta", 0},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
v1, err := NewVersion(tc.v1)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
v2, err := NewVersion(tc.v2)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := v1.Compare(v2)
|
|
expected := tc.expected
|
|
if actual != expected {
|
|
t.Fatalf(
|
|
"%s <=> %s\nexpected: %d\nactual: %d",
|
|
tc.v1, tc.v2,
|
|
expected, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestComparePreReleases(t *testing.T) {
|
|
cases := []struct {
|
|
v1 string
|
|
v2 string
|
|
expected int
|
|
}{
|
|
{"1.2-beta.2", "1.2-beta.2", 0},
|
|
{"1.2-beta.1", "1.2-beta.2", -1},
|
|
{"3.2-alpha.1", "3.2-alpha", 1},
|
|
{"1.2-beta.2", "1.2-beta.1", 1},
|
|
{"1.2-beta", "1.2-beta.3", -1},
|
|
{"1.2-alpha", "1.2-beta.3", -1},
|
|
{"1.2-beta", "1.2-alpha.3", 1},
|
|
{"3.0-alpha.3", "3.0-rc.1", -1},
|
|
{"3.0-alpha3", "3.0-rc1", -1},
|
|
{"3.0-alpha.1", "3.0-alpha.beta", -1},
|
|
{"5.4-alpha", "5.4-alpha.beta", 1},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
v1, err := NewVersion(tc.v1)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
v2, err := NewVersion(tc.v2)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := v1.Compare(v2)
|
|
expected := tc.expected
|
|
if actual != expected {
|
|
t.Fatalf(
|
|
"%s <=> %s\nexpected: %d\nactual: %d",
|
|
tc.v1, tc.v2,
|
|
expected, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestVersionMetadata(t *testing.T) {
|
|
cases := []struct {
|
|
version string
|
|
expected string
|
|
}{
|
|
{"1.2.3", ""},
|
|
{"1.2-beta", ""},
|
|
{"1.2.0-x.Y.0", ""},
|
|
{"1.2.0-x.Y.0+metadata", "metadata"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
v, err := NewVersion(tc.version)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := v.Metadata()
|
|
expected := tc.expected
|
|
if actual != expected {
|
|
t.Fatalf("expected: %s\nactual: %s", expected, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestVersionPrerelease(t *testing.T) {
|
|
cases := []struct {
|
|
version string
|
|
expected string
|
|
}{
|
|
{"1.2.3", ""},
|
|
{"1.2-beta", "beta"},
|
|
{"1.2.0-x.Y.0", "x.Y.0"},
|
|
{"1.2.0-x.Y.0+metadata", "x.Y.0"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
v, err := NewVersion(tc.version)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := v.Prerelease()
|
|
expected := tc.expected
|
|
if actual != expected {
|
|
t.Fatalf("expected: %s\nactual: %s", expected, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestVersionSegments(t *testing.T) {
|
|
cases := []struct {
|
|
version string
|
|
expected []int
|
|
}{
|
|
{"1.2.3", []int{1, 2, 3}},
|
|
{"1.2-beta", []int{1, 2, 0}},
|
|
{"1-x.Y.0", []int{1, 0, 0}},
|
|
{"1.2.0-x.Y.0+metadata", []int{1, 2, 0}},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
v, err := NewVersion(tc.version)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := v.Segments()
|
|
expected := tc.expected
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Fatalf("expected: %#v\nactual: %#v", expected, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestVersionString(t *testing.T) {
|
|
cases := [][]string{
|
|
{"1.2.3", "1.2.3"},
|
|
{"1.2-beta", "1.2.0-beta"},
|
|
{"1.2.0-x.Y.0", "1.2.0-x.Y.0"},
|
|
{"1.2.0-x.Y.0+metadata", "1.2.0-x.Y.0+metadata"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
v, err := NewVersion(tc[0])
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
actual := v.String()
|
|
expected := tc[1]
|
|
if actual != expected {
|
|
t.Fatalf("expected: %s\nactual: %s", expected, actual)
|
|
}
|
|
}
|
|
}
|