github.com/sdboyer/gps@v0.16.3/version_test.go (about)

     1  package gps
     2  
     3  import "testing"
     4  
     5  func TestVersionSorts(t *testing.T) {
     6  	rev := Revision("flooboofoobooo")
     7  	v1 := NewBranch("master").Is(rev)
     8  	v2 := NewBranch("test").Is(rev)
     9  	v3 := NewVersion("1.0.0").Is(rev)
    10  	v4 := NewVersion("1.0.1").Is(rev)
    11  	v5 := NewVersion("v2.0.5").Is(rev)
    12  	v6 := NewVersion("2.0.5.2").Is(rev)
    13  	v7 := newDefaultBranch("unwrapped").Is(rev)
    14  	v8 := NewVersion("20.0.5.2").Is(rev)
    15  	v9 := NewVersion("v1.5.5-beta.4").Is(rev)
    16  	v10 := NewVersion("v3.0.1-alpha.1").Is(rev)
    17  
    18  	start := []Version{
    19  		v1,
    20  		v2,
    21  		v3,
    22  		v4,
    23  		v5,
    24  		v6,
    25  		v7,
    26  		v8,
    27  		v9,
    28  		v10,
    29  		rev,
    30  	}
    31  
    32  	down := make([]Version, len(start))
    33  	copy(down, start)
    34  	up := make([]Version, len(start))
    35  	copy(up, start)
    36  
    37  	edown := []Version{
    38  		v3, v4, v5, // semvers
    39  		v9, v10, // prerelease semver
    40  		v7, v1, v2, // floating/branches
    41  		v6, v8, // plain versions
    42  		rev, // revs
    43  	}
    44  
    45  	eup := []Version{
    46  		v5, v4, v3, // semvers
    47  		v10, v9, // prerelease semver
    48  		v7, v1, v2, // floating/branches
    49  		v6, v8, // plain versions
    50  		rev, // revs
    51  	}
    52  
    53  	SortForUpgrade(up)
    54  	var wrong []int
    55  	for k, v := range up {
    56  		if eup[k] != v {
    57  			wrong = append(wrong, k)
    58  			t.Errorf("Expected version %s in position %v on upgrade sort, but got %s", eup[k], k, v)
    59  		}
    60  	}
    61  	if len(wrong) > 0 {
    62  		// Just helps with readability a bit
    63  		t.Errorf("Upgrade sort positions with wrong versions: %v", wrong)
    64  	}
    65  
    66  	SortForDowngrade(down)
    67  	wrong = wrong[:0]
    68  	for k, v := range down {
    69  		if edown[k] != v {
    70  			wrong = append(wrong, k)
    71  			t.Errorf("Expected version %s in position %v on downgrade sort, but got %s", edown[k], k, v)
    72  		}
    73  	}
    74  	if len(wrong) > 0 {
    75  		// Just helps with readability a bit
    76  		t.Errorf("Downgrade sort positions with wrong versions: %v", wrong)
    77  	}
    78  
    79  	// Now make sure we sort back the other way correctly...just because
    80  	SortForUpgrade(down)
    81  	wrong = wrong[:0]
    82  	for k, v := range down {
    83  		if eup[k] != v {
    84  			wrong = append(wrong, k)
    85  			t.Errorf("Expected version %s in position %v on down-then-upgrade sort, but got %s", eup[k], k, v)
    86  		}
    87  	}
    88  	if len(wrong) > 0 {
    89  		// Just helps with readability a bit
    90  		t.Errorf("Down-then-upgrade sort positions with wrong versions: %v", wrong)
    91  	}
    92  
    93  	// Now make sure we sort back the other way correctly...just because
    94  	SortForDowngrade(up)
    95  	wrong = wrong[:0]
    96  	for k, v := range up {
    97  		if edown[k] != v {
    98  			wrong = append(wrong, k)
    99  			t.Errorf("Expected version %s in position %v on up-then-downgrade sort, but got %s", edown[k], k, v)
   100  		}
   101  	}
   102  	if len(wrong) > 0 {
   103  		// Just helps with readability a bit
   104  		t.Fatalf("Up-then-downgrade sort positions with wrong versions: %v", wrong)
   105  	}
   106  
   107  	///////////
   108  	// Repeat for PairedVersion slices & sorts
   109  
   110  	pdown, pup := make([]PairedVersion, 0, len(start)), make([]PairedVersion, 0, len(start))
   111  	for _, v := range start {
   112  		if _, ok := v.(Revision); ok {
   113  			continue
   114  		}
   115  		pdown = append(pdown, v.(PairedVersion))
   116  		pup = append(pup, v.(PairedVersion))
   117  	}
   118  
   119  	pedown, peup := make([]PairedVersion, 0, len(edown)), make([]PairedVersion, 0, len(eup))
   120  	for _, v := range edown {
   121  		if _, ok := v.(Revision); ok {
   122  			continue
   123  		}
   124  		pedown = append(pedown, v.(PairedVersion))
   125  	}
   126  	for _, v := range eup {
   127  		if _, ok := v.(Revision); ok {
   128  			continue
   129  		}
   130  		peup = append(peup, v.(PairedVersion))
   131  	}
   132  
   133  	SortPairedForUpgrade(pup)
   134  	for k, v := range pup {
   135  		if peup[k] != v {
   136  			wrong = append(wrong, k)
   137  			t.Errorf("Expected version %s in position %v on upgrade sort, but got %s", peup[k], k, v)
   138  		}
   139  	}
   140  	if len(wrong) > 0 {
   141  		// Just helps with readability a bit
   142  		t.Errorf("Upgrade sort positions with wrong versions: %v", wrong)
   143  	}
   144  
   145  	SortPairedForDowngrade(pdown)
   146  	wrong = wrong[:0]
   147  	for k, v := range pdown {
   148  		if pedown[k] != v {
   149  			wrong = append(wrong, k)
   150  			t.Errorf("Expected version %s in position %v on downgrade sort, but got %s", pedown[k], k, v)
   151  		}
   152  	}
   153  	if len(wrong) > 0 {
   154  		// Just helps with readability a bit
   155  		t.Errorf("Downgrade sort positions with wrong versions: %v", wrong)
   156  	}
   157  
   158  	// Now make sure we sort back the other way correctly...just because
   159  	SortPairedForUpgrade(pdown)
   160  	wrong = wrong[:0]
   161  	for k, v := range pdown {
   162  		if peup[k] != v {
   163  			wrong = append(wrong, k)
   164  			t.Errorf("Expected version %s in position %v on down-then-upgrade sort, but got %s", peup[k], k, v)
   165  		}
   166  	}
   167  	if len(wrong) > 0 {
   168  		// Just helps with readability a bit
   169  		t.Errorf("Down-then-upgrade sort positions with wrong versions: %v", wrong)
   170  	}
   171  
   172  	// Now make sure we sort back the other way correctly...just because
   173  	SortPairedForDowngrade(pup)
   174  	wrong = wrong[:0]
   175  	for k, v := range pup {
   176  		if pedown[k] != v {
   177  			wrong = append(wrong, k)
   178  			t.Errorf("Expected version %s in position %v on up-then-downgrade sort, but got %s", pedown[k], k, v)
   179  		}
   180  	}
   181  	if len(wrong) > 0 {
   182  		// Just helps with readability a bit
   183  		t.Errorf("Up-then-downgrade sort positions with wrong versions: %v", wrong)
   184  	}
   185  }