github.com/quay/claircore@v1.5.28/pkg/pep440/version_test.go (about)

     1  package pep440
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  )
    10  
    11  type versionTestcase struct {
    12  	Name string
    13  	In   string
    14  	Err  bool
    15  	Want Version
    16  }
    17  
    18  func (tc versionTestcase) Run(t *testing.T) {
    19  	t.Logf("%s → %s", tc.In, tc.Want.String())
    20  	v, err := Parse(tc.In)
    21  	if (err != nil) != tc.Err {
    22  		t.Error(err)
    23  	}
    24  	if !cmp.Equal(tc.Want, v) {
    25  		t.Error(cmp.Diff(tc.Want, v))
    26  	}
    27  }
    28  
    29  func TestSimple(t *testing.T) {
    30  	tt := []versionTestcase{
    31  		{
    32  			Name: "Simple",
    33  			In:   "1.0.0",
    34  			Err:  false,
    35  			Want: Version{Release: []int{1, 0, 0}},
    36  		},
    37  		{
    38  			Name: "All",
    39  			In:   "1!2.3.4-a5-post_6.dev7.8",
    40  			Err:  false,
    41  			Want: Version{
    42  				Epoch:   1,
    43  				Release: []int{2, 3, 4},
    44  				Pre: struct {
    45  					Label string
    46  					N     int
    47  				}{
    48  					Label: "a",
    49  					N:     5,
    50  				},
    51  				Post: 6,
    52  				Dev:  7,
    53  			},
    54  		},
    55  		{
    56  			Name: "Date",
    57  			In:   "2019.3",
    58  			Err:  false,
    59  			Want: Version{Release: []int{2019, 3}},
    60  		},
    61  		{
    62  			Name: "Date Leading Zero",
    63  			In:   "2022.12.07",
    64  			Err:  false,
    65  			Want: Version{Release: []int{2022, 12, 7}},
    66  		},
    67  	}
    68  
    69  	for _, tc := range tt {
    70  		t.Run(tc.Name, tc.Run)
    71  	}
    72  }
    73  
    74  type orderTestcase struct {
    75  	Name string
    76  	In   []string
    77  	Want []string
    78  }
    79  
    80  func (tc orderTestcase) Run(t *testing.T) {
    81  	vs := make([]Version, len(tc.In))
    82  	for i, in := range tc.In {
    83  		v, err := Parse(in)
    84  		if err != nil {
    85  			t.Fatal(err)
    86  		}
    87  		vs[i] = v
    88  		t.Logf("%s → %s → %v", in, v.String(), v.Version())
    89  	}
    90  
    91  	sort.Sort(Versions(vs))
    92  
    93  	got := make([]string, len(tc.In))
    94  	for i, v := range vs {
    95  		got[i] = v.String()
    96  	}
    97  	if !cmp.Equal(tc.Want, got) {
    98  		t.Fatal(cmp.Diff(tc.Want, got))
    99  	}
   100  	t.Log("\n" + strings.Join(got, "\n"))
   101  }
   102  
   103  func TestOrdering(t *testing.T) {
   104  	// These are taken from examples in PEP-440.
   105  	tt := []orderTestcase{
   106  		{
   107  			Name: "AllSuffixes",
   108  			Want: []string{
   109  				"1.0.dev456",
   110  				"1.0a1",
   111  				"1.0a2.dev456",
   112  				"1.0a12.dev456",
   113  				"1.0a12",
   114  				"1.0b1.dev456",
   115  				"1.0b2",
   116  				"1.0b2.post345.dev456",
   117  				"1.0b2.post345",
   118  				"1.0rc1.dev456",
   119  				"1.0rc1",
   120  				"1.0",
   121  				"1.0.post456.dev34",
   122  				"1.0.post456",
   123  				"1.1.dev1",
   124  			},
   125  			In: []string{
   126  				"1.0",
   127  				"1.0.dev456",
   128  				"1.0.post456",
   129  				"1.0.post456.dev34",
   130  				"1.0a1",
   131  				"1.0a12",
   132  				"1.0a12.dev456",
   133  				"1.0a2.dev456",
   134  				"1.0b1.dev456",
   135  				"1.0b2",
   136  				"1.0b2.post345",
   137  				"1.0b2.post345.dev456",
   138  				"1.0rc1",
   139  				"1.0rc1.dev456",
   140  				"1.1.dev1",
   141  			},
   142  		},
   143  		{
   144  			Name: "MajorMinor",
   145  			In:   []string{"1.1", "1.0", "0.3", "0.2", "0.1"},
   146  			Want: []string{"0.1", "0.2", "0.3", "1.0", "1.1"},
   147  		},
   148  		{
   149  			Name: "MajorMinorMicro",
   150  			In:   []string{"1.2.0", "1.1.0", "1.1.2", "1.1.1"},
   151  			Want: []string{"1.1.0", "1.1.1", "1.1.2", "1.2.0"},
   152  		},
   153  		{
   154  			Name: "MajorMinorPre",
   155  			In:   []string{"0.9", "1.0", "1.0a", "1.0a1", "1.0a2", "1.0b1", "1.0rc1", "1.1a1"},
   156  			Want: []string{"0.9", "1.0a0", "1.0a1", "1.0a2", "1.0b1", "1.0rc1", "1.0", "1.1a1"},
   157  		},
   158  		{
   159  			Name: "MajorMinorPreDevPost",
   160  			In:   []string{"0.9", "1.0", "1.0.dev1", "1.0.dev2", "1.0.dev3", "1.0.dev4", "1.0.post1", "1.0c1", "1.0c2", "1.1.dev1"},
   161  			Want: []string{"0.9", "1.0.dev1", "1.0.dev2", "1.0.dev3", "1.0.dev4", "1.0rc1", "1.0rc2", "1.0", "1.0.post1", "1.1.dev1"},
   162  		},
   163  		{
   164  			Name: "Date",
   165  			In:   []string{"2013.2", "2013.1", "2012.3", "2012.2", "2012.15", "2012.1"},
   166  			Want: []string{"2012.1", "2012.2", "2012.3", "2012.15", "2013.1", "2013.2"},
   167  		},
   168  		{
   169  			Name: "Epoch",
   170  			In:   []string{"1!1.0", "1!1.1", "1!2.0", "2013.10", "2014.04"},
   171  			Want: []string{"2013.10", "2014.4", "1!1.0", "1!1.1", "1!2.0"},
   172  		},
   173  	}
   174  
   175  	for _, tc := range tt {
   176  		t.Run(tc.Name, tc.Run)
   177  	}
   178  }