github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/format/encoders_collection_test.go (about)

     1  package format
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/syft/sbom"
     9  )
    10  
    11  func Test_versionMatches(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		version string
    15  		match   string
    16  		matches bool
    17  	}{
    18  		{
    19  			name:    "any version matches number",
    20  			version: string(sbom.AnyVersion),
    21  			match:   "6",
    22  			matches: true,
    23  		},
    24  		{
    25  			name:    "number matches any version",
    26  			version: "6",
    27  			match:   string(sbom.AnyVersion),
    28  			matches: true,
    29  		},
    30  		{
    31  			name:    "same number matches",
    32  			version: "3",
    33  			match:   "3",
    34  			matches: true,
    35  		},
    36  		{
    37  			name:    "same major number matches",
    38  			version: "3.1",
    39  			match:   "3",
    40  			matches: true,
    41  		},
    42  		{
    43  			name:    "same minor number matches",
    44  			version: "3.1",
    45  			match:   "3.1",
    46  			matches: true,
    47  		},
    48  		{
    49  			name:    "wildcard-version matches minor",
    50  			version: "7.1.3",
    51  			match:   "7.*",
    52  			matches: true,
    53  		},
    54  		{
    55  			name:    "wildcard-version matches patch",
    56  			version: "7.4.8",
    57  			match:   "7.4.*",
    58  			matches: true,
    59  		},
    60  		{
    61  			name:    "sub-version matches major",
    62  			version: "7.19.11",
    63  			match:   "7",
    64  			matches: true,
    65  		},
    66  		{
    67  			name:    "sub-version matches minor",
    68  			version: "7.55.2",
    69  			match:   "7.55",
    70  			matches: true,
    71  		},
    72  		{
    73  			name:    "sub-version matches patch",
    74  			version: "7.32.6",
    75  			match:   "7.32.6",
    76  			matches: true,
    77  		},
    78  		// negative tests
    79  		{
    80  			name:    "different number does not match",
    81  			version: "3",
    82  			match:   "4",
    83  			matches: false,
    84  		},
    85  		{
    86  			name:    "sub-version doesn't match major",
    87  			version: "7.2.5",
    88  			match:   "8.2.5",
    89  			matches: false,
    90  		},
    91  		{
    92  			name:    "sub-version doesn't match minor",
    93  			version: "7.2.9",
    94  			match:   "7.1",
    95  			matches: false,
    96  		},
    97  		{
    98  			name:    "sub-version doesn't match patch",
    99  			version: "7.32.6",
   100  			match:   "7.32.5",
   101  			matches: false,
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		t.Run(test.name, func(t *testing.T) {
   107  			matches := versionMatches(test.version, test.match)
   108  			assert.Equal(t, test.matches, matches)
   109  		})
   110  	}
   111  }