github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/semver/utils_test.go (about)

     1  package semver
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestIsSemver(t *testing.T) {
    10  	cases := []struct {
    11  		input  string
    12  		output bool
    13  	}{
    14  		{"1.2.3", true},
    15  		{"1.2.3-alpha", true},
    16  		{"1.2.3-alpha.1", true},
    17  		{"1.2.3-0.3.7", true},
    18  		{"1.2.3-x.7.z.92", true},
    19  		{"1.2.3-x-y-z.-", true},
    20  		{"1.2.3-x-y-z+metadata", true},
    21  		{"1.2.3+metadata", true},
    22  		{"<empty>", false},
    23  		{"a.b", false},
    24  		{"a.b.c", false},
    25  	}
    26  
    27  	for _, test := range cases {
    28  		t.Run(test.input, func(t *testing.T) {
    29  			assert.Equal(t, test.output, IsSemver(test.input))
    30  		})
    31  	}
    32  }
    33  
    34  func TestIsVersionInRange(t *testing.T) {
    35  	cases := []struct {
    36  		version string
    37  		inRange string
    38  		output  bool
    39  	}{
    40  		{
    41  			"1.2.3",
    42  			">=1.2.3",
    43  			true,
    44  		},
    45  		{
    46  			"1.2.3",
    47  			">=1.2.3 <1.3.0",
    48  			true,
    49  		},
    50  		{
    51  			"1.2.3",
    52  			"^1.2.0",
    53  			true,
    54  		},
    55  		{
    56  			"1.2.3",
    57  			"^1.2.3",
    58  			true,
    59  		},
    60  		{
    61  			"1.2.3",
    62  			"^1.2.4",
    63  			false,
    64  		},
    65  		{
    66  			"1.2.3",
    67  			"^1.3.0",
    68  			false,
    69  		},
    70  		{
    71  			"1.2.3",
    72  			"^1.0.0",
    73  			true,
    74  		},
    75  	}
    76  
    77  	for _, test := range cases {
    78  		t.Run(test.version+" in "+test.inRange, func(t *testing.T) {
    79  			assert.Equal(t, test.output, IsVersionInRange(test.version, test.inRange))
    80  		})
    81  	}
    82  }