github.com/anchore/syft@v1.38.2/internal/string_helpers_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestHasAnyOfSuffixes(t *testing.T) {
    11  	tests := []struct {
    12  		name     string
    13  		input    string
    14  		suffixes []string
    15  		expected bool
    16  	}{
    17  		{
    18  			name:  "go case",
    19  			input: "this has something",
    20  			suffixes: []string{
    21  				"has something",
    22  				"has NOT something",
    23  			},
    24  			expected: true,
    25  		},
    26  		{
    27  			name:  "no match",
    28  			input: "this has something",
    29  			suffixes: []string{
    30  				"has NOT something",
    31  			},
    32  			expected: false,
    33  		},
    34  		{
    35  			name:     "empty",
    36  			input:    "this has something",
    37  			suffixes: []string{},
    38  			expected: false,
    39  		},
    40  		{
    41  			name:  "positive match last",
    42  			input: "this has something",
    43  			suffixes: []string{
    44  				"that does not have",
    45  				"something",
    46  			},
    47  			expected: true,
    48  		},
    49  		{
    50  			name:  "empty input",
    51  			input: "",
    52  			suffixes: []string{
    53  				"that does not have",
    54  				"this has",
    55  			},
    56  			expected: false,
    57  		},
    58  	}
    59  
    60  	for _, test := range tests {
    61  		t.Run(test.name, func(t *testing.T) {
    62  			assert.Equal(t, test.expected, HasAnyOfSuffixes(test.input, test.suffixes...))
    63  		})
    64  	}
    65  }
    66  
    67  func TestHasAnyOfPrefixes(t *testing.T) {
    68  	tests := []struct {
    69  		name     string
    70  		input    string
    71  		prefixes []string
    72  		expected bool
    73  	}{
    74  		{
    75  			name:  "go case",
    76  			input: "this has something",
    77  			prefixes: []string{
    78  				"this has",
    79  				"that does not have",
    80  			},
    81  			expected: true,
    82  		},
    83  		{
    84  			name:  "no match",
    85  			input: "this has something",
    86  			prefixes: []string{
    87  				"this DOES NOT has",
    88  				"that does not have",
    89  			},
    90  			expected: false,
    91  		},
    92  		{
    93  			name:     "empty",
    94  			input:    "this has something",
    95  			prefixes: []string{},
    96  			expected: false,
    97  		},
    98  		{
    99  			name:  "positive match last",
   100  			input: "this has something",
   101  			prefixes: []string{
   102  				"that does not have",
   103  				"this has",
   104  			},
   105  			expected: true,
   106  		},
   107  		{
   108  			name:  "empty input",
   109  			input: "",
   110  			prefixes: []string{
   111  				"that does not have",
   112  				"this has",
   113  			},
   114  			expected: false,
   115  		},
   116  	}
   117  
   118  	for _, test := range tests {
   119  		t.Run(test.name, func(t *testing.T) {
   120  			assert.Equal(t, test.expected, HasAnyOfPrefixes(test.input, test.prefixes...))
   121  		})
   122  	}
   123  }
   124  
   125  func TestTruncateMiddleEllipsis(t *testing.T) {
   126  	tests := []struct {
   127  		input    string
   128  		len      int
   129  		expected string
   130  	}{
   131  		{
   132  			input:    "nobody expects the spanish inquisition",
   133  			len:      39,
   134  			expected: "nobody expects the spanish inquisition",
   135  		},
   136  		{
   137  			input:    "nobody expects the spanish inquisition",
   138  			len:      30,
   139  			expected: "nobody expects ...ish inquisition",
   140  		},
   141  		{
   142  			input:    "nobody expects the spanish inquisition",
   143  			len:      38,
   144  			expected: "nobody expects the spanish inquisition",
   145  		},
   146  		{
   147  			input:    "",
   148  			len:      30,
   149  			expected: "",
   150  		},
   151  		{
   152  			input:    "",
   153  			len:      0,
   154  			expected: "",
   155  		},
   156  	}
   157  
   158  	for _, test := range tests {
   159  		t.Run(test.input+":"+strconv.Itoa(test.len), func(t *testing.T) {
   160  			assert.Equal(t, test.expected, TruncateMiddleEllipsis(test.input, test.len))
   161  		})
   162  	}
   163  }
   164  
   165  func TestSplitAny(t *testing.T) {
   166  
   167  	tests := []struct {
   168  		name   string
   169  		input  string
   170  		fields string
   171  		want   []string
   172  	}{
   173  		{
   174  			name:   "simple",
   175  			input:  "a,b,c",
   176  			fields: ",",
   177  			want:   []string{"a", "b", "c"},
   178  		},
   179  		{
   180  			name:   "empty",
   181  			input:  "",
   182  			fields: ",",
   183  			want:   []string{""},
   184  		},
   185  		{
   186  			name:   "multiple separators",
   187  			input:  "a,b\nc:d",
   188  			fields: ",:\n",
   189  			want:   []string{"a", "b", "c", "d"},
   190  		},
   191  	}
   192  	for _, tt := range tests {
   193  		t.Run(tt.name, func(t *testing.T) {
   194  			assert.Equal(t, tt.want, SplitAny(tt.input, tt.fields))
   195  		})
   196  	}
   197  }