github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/modfetch/pseudo_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 modfetch
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  var pseudoTests = []struct {
    13  	major   string
    14  	older   string
    15  	version string
    16  }{
    17  	{"", "", "v0.0.0-20060102150405-hash"},
    18  	{"v0", "", "v0.0.0-20060102150405-hash"},
    19  	{"v1", "", "v1.0.0-20060102150405-hash"},
    20  	{"v2", "", "v2.0.0-20060102150405-hash"},
    21  	{"unused", "v0.0.0", "v0.0.1-0.20060102150405-hash"},
    22  	{"unused", "v1.2.3", "v1.2.4-0.20060102150405-hash"},
    23  	{"unused", "v1.2.99999999999999999", "v1.2.100000000000000000-0.20060102150405-hash"},
    24  	{"unused", "v1.2.3-pre", "v1.2.3-pre.0.20060102150405-hash"},
    25  	{"unused", "v1.3.0-pre", "v1.3.0-pre.0.20060102150405-hash"},
    26  	{"unused", "v0.0.0--", "v0.0.0--.0.20060102150405-hash"},
    27  	{"unused", "v1.0.0+metadata", "v1.0.1-0.20060102150405-hash+metadata"},
    28  	{"unused", "v2.0.0+incompatible", "v2.0.1-0.20060102150405-hash+incompatible"},
    29  	{"unused", "v2.3.0-pre+incompatible", "v2.3.0-pre.0.20060102150405-hash+incompatible"},
    30  }
    31  
    32  var pseudoTime = time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)
    33  
    34  func TestPseudoVersion(t *testing.T) {
    35  	for _, tt := range pseudoTests {
    36  		v := PseudoVersion(tt.major, tt.older, pseudoTime, "hash")
    37  		if v != tt.version {
    38  			t.Errorf("PseudoVersion(%q, %q, ...) = %v, want %v", tt.major, tt.older, v, tt.version)
    39  		}
    40  	}
    41  }
    42  
    43  func TestIsPseudoVersion(t *testing.T) {
    44  	for _, tt := range pseudoTests {
    45  		if !IsPseudoVersion(tt.version) {
    46  			t.Errorf("IsPseudoVersion(%q) = false, want true", tt.version)
    47  		}
    48  		if IsPseudoVersion(tt.older) {
    49  			t.Errorf("IsPseudoVersion(%q) = true, want false", tt.older)
    50  		}
    51  	}
    52  }
    53  
    54  func TestPseudoVersionTime(t *testing.T) {
    55  	for _, tt := range pseudoTests {
    56  		tm, err := PseudoVersionTime(tt.version)
    57  		if tm != pseudoTime || err != nil {
    58  			t.Errorf("PseudoVersionTime(%q) = %v, %v, want %v, nil", tt.version, tm.Format(time.RFC3339), err, pseudoTime.Format(time.RFC3339))
    59  		}
    60  		tm, err = PseudoVersionTime(tt.older)
    61  		if tm != (time.Time{}) || err == nil {
    62  			t.Errorf("PseudoVersionTime(%q) = %v, %v, want %v, error", tt.older, tm.Format(time.RFC3339), err, time.Time{}.Format(time.RFC3339))
    63  		}
    64  	}
    65  }
    66  
    67  func TestInvalidPseudoVersionTime(t *testing.T) {
    68  	const v = "---"
    69  	if _, err := PseudoVersionTime(v); err == nil {
    70  		t.Error("expected error, got nil instead")
    71  	}
    72  }
    73  
    74  func TestPseudoVersionRev(t *testing.T) {
    75  	for _, tt := range pseudoTests {
    76  		rev, err := PseudoVersionRev(tt.version)
    77  		if rev != "hash" || err != nil {
    78  			t.Errorf("PseudoVersionRev(%q) = %q, %v, want %q, nil", tt.older, rev, err, "hash")
    79  		}
    80  		rev, err = PseudoVersionRev(tt.older)
    81  		if rev != "" || err == nil {
    82  			t.Errorf("PseudoVersionRev(%q) = %q, %v, want %q, error", tt.older, rev, err, "")
    83  		}
    84  	}
    85  }
    86  
    87  func TestPseudoVersionBase(t *testing.T) {
    88  	for _, tt := range pseudoTests {
    89  		base, err := PseudoVersionBase(tt.version)
    90  		if err != nil {
    91  			t.Errorf("PseudoVersionBase(%q): %v", tt.version, err)
    92  		} else if base != tt.older {
    93  			t.Errorf("PseudoVersionBase(%q) = %q; want %q", tt.version, base, tt.older)
    94  		}
    95  	}
    96  }
    97  
    98  func TestInvalidPseudoVersionBase(t *testing.T) {
    99  	for _, in := range []string{
   100  		"v0.0.0",
   101  		"v0.0.0-",                                 // malformed: empty prerelease
   102  		"v0.0.0-0.20060102150405-hash",            // Z+1 == 0
   103  		"v0.1.0-0.20060102150405-hash",            // Z+1 == 0
   104  		"v1.0.0-0.20060102150405-hash",            // Z+1 == 0
   105  		"v0.0.0-20060102150405-hash+incompatible", // "+incompatible without base version
   106  		"v0.0.0-20060102150405-hash+metadata",     // other metadata without base version
   107  	} {
   108  		base, err := PseudoVersionBase(in)
   109  		if err == nil || base != "" {
   110  			t.Errorf(`PseudoVersionBase(%q) = %q, %v; want "", error`, in, base, err)
   111  		}
   112  	}
   113  }
   114  
   115  func TestIncDecimal(t *testing.T) {
   116  	cases := []struct {
   117  		in, want string
   118  	}{
   119  		{"0", "1"},
   120  		{"1", "2"},
   121  		{"99", "100"},
   122  		{"100", "101"},
   123  		{"101", "102"},
   124  	}
   125  
   126  	for _, tc := range cases {
   127  		got := incDecimal(tc.in)
   128  		if got != tc.want {
   129  			t.Fatalf("incDecimal(%q) = %q; want %q", tc.in, tc.want, got)
   130  		}
   131  	}
   132  }
   133  
   134  func TestDecDecimal(t *testing.T) {
   135  	cases := []struct {
   136  		in, want string
   137  	}{
   138  		{"", ""},
   139  		{"0", ""},
   140  		{"00", ""},
   141  		{"1", "0"},
   142  		{"2", "1"},
   143  		{"99", "98"},
   144  		{"100", "99"},
   145  		{"101", "100"},
   146  	}
   147  
   148  	for _, tc := range cases {
   149  		got := decDecimal(tc.in)
   150  		if got != tc.want {
   151  			t.Fatalf("decDecimal(%q) = %q; want %q", tc.in, tc.want, got)
   152  		}
   153  	}
   154  }