2017-05-02 19:26:49 +03:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-07-12 21:56:12 +03:00
|
|
|
package gps
|
2016-04-15 04:36:46 +03:00
|
|
|
|
2016-08-31 03:43:18 +03:00
|
|
|
import "testing"
|
2016-04-15 04:36:46 +03:00
|
|
|
|
|
|
|
func TestVersionSorts(t *testing.T) {
|
2016-04-17 03:33:07 +03:00
|
|
|
rev := Revision("flooboofoobooo")
|
2017-06-14 17:44:59 +03:00
|
|
|
v1 := NewBranch("master").Pair(rev)
|
|
|
|
v2 := NewBranch("test").Pair(rev)
|
|
|
|
v3 := NewVersion("1.0.0").Pair(rev)
|
|
|
|
v4 := NewVersion("1.0.1").Pair(rev)
|
|
|
|
v5 := NewVersion("v2.0.5").Pair(rev)
|
|
|
|
v6 := NewVersion("2.0.5.2").Pair(rev)
|
|
|
|
v7 := newDefaultBranch("unwrapped").Pair(rev)
|
|
|
|
v8 := NewVersion("20.0.5.2").Pair(rev)
|
|
|
|
v9 := NewVersion("v1.5.5-beta.4").Pair(rev)
|
|
|
|
v10 := NewVersion("v3.0.1-alpha.1").Pair(rev)
|
2016-04-15 04:36:46 +03:00
|
|
|
|
2016-04-15 05:30:10 +03:00
|
|
|
start := []Version{
|
2016-04-15 04:36:46 +03:00
|
|
|
v1,
|
|
|
|
v2,
|
|
|
|
v3,
|
|
|
|
v4,
|
|
|
|
v5,
|
|
|
|
v6,
|
|
|
|
v7,
|
|
|
|
v8,
|
2016-10-22 02:57:51 +03:00
|
|
|
v9,
|
|
|
|
v10,
|
2016-04-15 04:36:46 +03:00
|
|
|
rev,
|
|
|
|
}
|
|
|
|
|
2016-04-15 05:30:10 +03:00
|
|
|
down := make([]Version, len(start))
|
2016-04-15 04:36:46 +03:00
|
|
|
copy(down, start)
|
2016-04-15 05:30:10 +03:00
|
|
|
up := make([]Version, len(start))
|
2016-04-15 04:36:46 +03:00
|
|
|
copy(up, start)
|
|
|
|
|
2016-04-15 05:30:10 +03:00
|
|
|
edown := []Version{
|
2016-04-15 04:36:46 +03:00
|
|
|
v3, v4, v5, // semvers
|
2016-10-22 02:57:51 +03:00
|
|
|
v9, v10, // prerelease semver
|
2016-09-15 05:09:08 +03:00
|
|
|
v7, v1, v2, // floating/branches
|
2016-09-15 03:25:39 +03:00
|
|
|
v6, v8, // plain versions
|
2016-04-15 04:36:46 +03:00
|
|
|
rev, // revs
|
|
|
|
}
|
|
|
|
|
2016-04-15 05:30:10 +03:00
|
|
|
eup := []Version{
|
2016-04-15 04:36:46 +03:00
|
|
|
v5, v4, v3, // semvers
|
2016-10-22 02:57:51 +03:00
|
|
|
v10, v9, // prerelease semver
|
2016-09-15 05:09:08 +03:00
|
|
|
v7, v1, v2, // floating/branches
|
2016-09-15 03:25:39 +03:00
|
|
|
v6, v8, // plain versions
|
2016-04-15 04:36:46 +03:00
|
|
|
rev, // revs
|
|
|
|
}
|
|
|
|
|
2016-08-31 03:43:18 +03:00
|
|
|
SortForUpgrade(up)
|
2016-04-15 04:36:46 +03:00
|
|
|
var wrong []int
|
|
|
|
for k, v := range up {
|
|
|
|
if eup[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on upgrade sort, but got %s", eup[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Errorf("Upgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
|
2016-08-31 03:43:18 +03:00
|
|
|
SortForDowngrade(down)
|
2016-04-15 04:36:46 +03:00
|
|
|
wrong = wrong[:0]
|
|
|
|
for k, v := range down {
|
|
|
|
if edown[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on downgrade sort, but got %s", edown[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Errorf("Downgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we sort back the other way correctly...just because
|
2016-08-31 03:43:18 +03:00
|
|
|
SortForUpgrade(down)
|
2016-04-15 04:36:46 +03:00
|
|
|
wrong = wrong[:0]
|
|
|
|
for k, v := range down {
|
|
|
|
if eup[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on down-then-upgrade sort, but got %s", eup[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Errorf("Down-then-upgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we sort back the other way correctly...just because
|
2016-08-31 03:43:18 +03:00
|
|
|
SortForDowngrade(up)
|
2016-04-15 04:36:46 +03:00
|
|
|
wrong = wrong[:0]
|
|
|
|
for k, v := range up {
|
|
|
|
if edown[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on up-then-downgrade sort, but got %s", edown[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
2017-04-15 07:21:12 +03:00
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Fatalf("Up-then-downgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Repeat for PairedVersion slices & sorts
|
|
|
|
|
|
|
|
pdown, pup := make([]PairedVersion, 0, len(start)), make([]PairedVersion, 0, len(start))
|
|
|
|
for _, v := range start {
|
|
|
|
if _, ok := v.(Revision); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pdown = append(pdown, v.(PairedVersion))
|
|
|
|
pup = append(pup, v.(PairedVersion))
|
|
|
|
}
|
|
|
|
|
|
|
|
pedown, peup := make([]PairedVersion, 0, len(edown)), make([]PairedVersion, 0, len(eup))
|
|
|
|
for _, v := range edown {
|
|
|
|
if _, ok := v.(Revision); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pedown = append(pedown, v.(PairedVersion))
|
|
|
|
}
|
|
|
|
for _, v := range eup {
|
|
|
|
if _, ok := v.(Revision); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
peup = append(peup, v.(PairedVersion))
|
|
|
|
}
|
|
|
|
|
|
|
|
SortPairedForUpgrade(pup)
|
|
|
|
for k, v := range pup {
|
|
|
|
if peup[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on upgrade sort, but got %s", peup[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Errorf("Upgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
SortPairedForDowngrade(pdown)
|
|
|
|
wrong = wrong[:0]
|
|
|
|
for k, v := range pdown {
|
|
|
|
if pedown[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on downgrade sort, but got %s", pedown[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Errorf("Downgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we sort back the other way correctly...just because
|
|
|
|
SortPairedForUpgrade(pdown)
|
|
|
|
wrong = wrong[:0]
|
|
|
|
for k, v := range pdown {
|
|
|
|
if peup[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on down-then-upgrade sort, but got %s", peup[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Errorf("Down-then-upgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we sort back the other way correctly...just because
|
|
|
|
SortPairedForDowngrade(pup)
|
|
|
|
wrong = wrong[:0]
|
|
|
|
for k, v := range pup {
|
|
|
|
if pedown[k] != v {
|
|
|
|
wrong = append(wrong, k)
|
|
|
|
t.Errorf("Expected version %s in position %v on up-then-downgrade sort, but got %s", pedown[k], k, v)
|
|
|
|
}
|
|
|
|
}
|
2016-04-15 04:36:46 +03:00
|
|
|
if len(wrong) > 0 {
|
|
|
|
// Just helps with readability a bit
|
|
|
|
t.Errorf("Up-then-downgrade sort positions with wrong versions: %v", wrong)
|
|
|
|
}
|
|
|
|
}
|