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

     1  package semver
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestDiff(t *testing.T) {
    10  	cases := []struct {
    11  		name  string
    12  		base  string
    13  		head  string
    14  		drift SemverDrift
    15  		delta uint64
    16  	}{
    17  		{
    18  			"Both versions are same",
    19  			"1.2.2", "1.2.2",
    20  			NoDrift,
    21  			0,
    22  		},
    23  		{
    24  			"Patch version drift",
    25  			"1.2.2", "1.2.3",
    26  			PatchDrift,
    27  			1,
    28  		},
    29  		{
    30  			"Minor version drift",
    31  			"1.2.2", "1.3.5",
    32  			MinorDrift,
    33  			1,
    34  		},
    35  		{
    36  			"Major version drift",
    37  			"1.2.3", "2.0.0",
    38  			MajorDrift,
    39  			1,
    40  		},
    41  		{
    42  			"Both version are same except pre-release tag",
    43  			"1.1.1-rc1", "1.1.1-rc2",
    44  			NoDrift, 0,
    45  		},
    46  		{
    47  			"All components are different",
    48  			"1.2.3-rc1", "4.5.6-rc2",
    49  			MajorDrift, 3,
    50  		},
    51  	}
    52  
    53  	for _, test := range cases {
    54  		t.Run(test.name, func(t *testing.T) {
    55  			dt, dd := Diff(test.base, test.head)
    56  			assert.Equal(t, test.drift, dt)
    57  			assert.Equal(t, test.delta, dd)
    58  		})
    59  	}
    60  }
    61  
    62  func TestDriftHelper(t *testing.T) {
    63  	d, _ := Diff("1.2.3", "1.2.4")
    64  	assert.True(t, d.IsPatch())
    65  	assert.False(t, d.IsMajor())
    66  	assert.False(t, d.IsMinor())
    67  
    68  	d, _ = Diff("1.2.3", "1.3.5")
    69  	assert.True(t, d.IsMinor())
    70  
    71  	d, _ = Diff("1.2.3", "2.3.5")
    72  	assert.True(t, d.IsMajor())
    73  }