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

     1  package ruby
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  )
     9  
    10  func TestNewVersion(t *testing.T) {
    11  	testcases := []struct {
    12  		version string
    13  		valid   bool
    14  	}{
    15  		{
    16  			version: "1",
    17  			valid:   true,
    18  		},
    19  		{
    20  			version: "1.",
    21  			valid:   false,
    22  		},
    23  		{
    24  			version: "1.alpha",
    25  			valid:   true,
    26  		},
    27  		{
    28  			version: "1-alpha",
    29  			valid:   true,
    30  		},
    31  		{
    32  			version: "",
    33  			valid:   true,
    34  		},
    35  		{
    36  			version: ".3",
    37  			valid:   false,
    38  		},
    39  		{
    40  			version: "beta",
    41  			valid:   false,
    42  		},
    43  		{
    44  			version: "beta.1",
    45  			valid:   false,
    46  		},
    47  		{
    48  			version: "-",
    49  			valid:   false,
    50  		},
    51  		{
    52  			version: "0-0",
    53  			valid:   true,
    54  		},
    55  		{
    56  			version: "1/2",
    57  			valid:   false,
    58  		},
    59  		{
    60  			version: "1..2",
    61  			valid:   false,
    62  		},
    63  		{
    64  			version: "1111111111111111111111111111111111111111111111111111111",
    65  			valid:   true,
    66  		},
    67  		{
    68  			version: "1.234567890987654321234567890987654321234567890987654321234567890987654.3.21",
    69  			valid:   true,
    70  		},
    71  	}
    72  
    73  	for _, tc := range testcases {
    74  		t.Run(tc.version, func(t *testing.T) {
    75  			_, err := NewVersion(tc.version)
    76  			if !cmp.Equal(tc.valid, err == nil) {
    77  				t.Error(cmp.Diff(tc.valid, err == nil))
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestVersionCompare(t *testing.T) {
    84  	testcases := []struct {
    85  		a, b string
    86  		want int
    87  	}{
    88  		{
    89  			a:    "1",
    90  			b:    "2",
    91  			want: -1,
    92  		},
    93  		{
    94  			a:    "1.1.2",
    95  			b:    "1.1.0",
    96  			want: +1,
    97  		},
    98  		{
    99  			a:    "1.1.02",
   100  			b:    "1.1.0",
   101  			want: +1,
   102  		},
   103  		{
   104  			a:    "1.1.2",
   105  			b:    "1.1.2",
   106  			want: 0,
   107  		},
   108  		{
   109  			a:    "5",
   110  			b:    "4.2.10",
   111  			want: +1,
   112  		},
   113  		{
   114  			a:    "4.2.10",
   115  			b:    "5",
   116  			want: -1,
   117  		},
   118  		{
   119  			a:    "4.2.10",
   120  			b:    "4.2.10.0.0.0.0.0.0",
   121  			want: 0,
   122  		},
   123  		{
   124  			a:    "0.9",
   125  			b:    "1.0",
   126  			want: -1,
   127  		},
   128  		{
   129  			a:    "0.9",
   130  			b:    "1.0.a.2",
   131  			want: -1,
   132  		},
   133  		{
   134  			a:    "1.0.a.2",
   135  			b:    "1.0.b1",
   136  			want: -1,
   137  		},
   138  		{
   139  			a:    "1.0.b1",
   140  			b:    "1.0",
   141  			want: -1,
   142  		},
   143  		{
   144  			a:    "0.alpha",
   145  			b:    "0",
   146  			want: -1,
   147  		},
   148  		{
   149  			a:    "1-2",
   150  			b:    "1-2",
   151  			want: 0,
   152  		},
   153  		{
   154  			a:    "1-1",
   155  			b:    "1-2",
   156  			want: -1,
   157  		},
   158  		{
   159  			a:    "1-2",
   160  			b:    "1-1",
   161  			want: +1,
   162  		},
   163  		{
   164  			a:    "1.2.3.0.00.0-0.0.0000.3.0000.00000000",
   165  			b:    "1.2.3.0.0.0-0.0.0.3.0.0",
   166  			want: 0,
   167  		},
   168  		{
   169  			a:    "1.2.3.0.00.0-0.0.000000000000000000000000000000000000000000000000000000000000000000.3.0000.00000000",
   170  			b:    "1.2.3.0.0.0-0.0.0.3.0.0",
   171  			want: 0,
   172  		},
   173  		{
   174  			a:    "1.2.3.0.00.0-0.0.0000.3.0000.00000000",
   175  			b:    "1.2.3.0.0.0-0.0.0.3",
   176  			want: 0,
   177  		},
   178  		{
   179  			a:    "1.2.3.0.00.0-0.0.0000.3.0000.00000000",
   180  			b:    "1.2.3-0.0.0.3",
   181  			want: 0,
   182  		},
   183  		{
   184  			a:    "1.0.3.beta",
   185  			b:    "1.beta",
   186  			want: +1,
   187  		},
   188  		{
   189  			a:    "1.0.3.00.0.0.4.0.0.0.0.beta.0.0.2.0.0.00000",
   190  			b:    "1.0.3.00.0.0.4.beta.0.0.2",
   191  			want: 0,
   192  		},
   193  		{
   194  			a:    "1.0.3.00.0.0.4.0.0.0.0.beta.0.0.2.0.0.00000",
   195  			b:    "1.0.3.00.0.0.4.alpha.0.0.2",
   196  			want: +1,
   197  		},
   198  		{
   199  			a:    "   1.alpha.0.1.0.5.00000.0",
   200  			b:    " 1.alpha.0.1.0.5.0          ",
   201  			want: 0,
   202  		},
   203  		{
   204  			a:    "",
   205  			b:    "\t",
   206  			want: 0,
   207  		},
   208  		{
   209  			a:    "1.2.000000000000000000000000000000000000000000000000000000000001",
   210  			b:    "1.2.1",
   211  			want: 0,
   212  		},
   213  		{
   214  			a:    "1.2.000000000000000000000000000000000000000000000000000000000001",
   215  			b:    "1.2.2",
   216  			want: -1,
   217  		},
   218  		{
   219  			a:    "1.234567890987654321234567890987654321234567890987654321234567890987654.3.21",
   220  			b:    "1.000000000000000000234567890987654321234567890987654321234567890987654.3.21",
   221  			want: +1,
   222  		},
   223  		{
   224  			a:    "9999999999999999999999999999999999999999999999999999999999999999999999999999",
   225  			b:    "00000000009999999999999999999999999999999999999999999999999999999999999999999999999999",
   226  			want: 0,
   227  		},
   228  	}
   229  
   230  	for _, tc := range testcases {
   231  		t.Run(fmt.Sprintf("%s_%s", tc.a, tc.b), func(t *testing.T) {
   232  			aVersion, err := NewVersion(tc.a)
   233  			if err != nil {
   234  				t.Fatal(err)
   235  			}
   236  			bVersion, err := NewVersion(tc.b)
   237  			if err != nil {
   238  				t.Fatal(err)
   239  			}
   240  
   241  			got := aVersion.Compare(bVersion)
   242  			if !cmp.Equal(tc.want, got) {
   243  				t.Error(cmp.Diff(tc.want, got))
   244  			}
   245  		})
   246  	}
   247  }