github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/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 TestHasAnyOfPrefixes(t *testing.T) {
    11  	tests := []struct {
    12  		name     string
    13  		input    string
    14  		prefixes []string
    15  		expected bool
    16  	}{
    17  		{
    18  			name:  "go case",
    19  			input: "this has something",
    20  			prefixes: []string{
    21  				"this has",
    22  				"that does not have",
    23  			},
    24  			expected: true,
    25  		},
    26  		{
    27  			name:  "no match",
    28  			input: "this has something",
    29  			prefixes: []string{
    30  				"this DOES NOT has",
    31  				"that does not have",
    32  			},
    33  			expected: false,
    34  		},
    35  		{
    36  			name:     "empty",
    37  			input:    "this has something",
    38  			prefixes: []string{},
    39  			expected: false,
    40  		},
    41  		{
    42  			name:  "positive match last",
    43  			input: "this has something",
    44  			prefixes: []string{
    45  				"that does not have",
    46  				"this has",
    47  			},
    48  			expected: true,
    49  		},
    50  		{
    51  			name:  "empty input",
    52  			input: "",
    53  			prefixes: []string{
    54  				"that does not have",
    55  				"this has",
    56  			},
    57  			expected: false,
    58  		},
    59  	}
    60  
    61  	for _, test := range tests {
    62  		t.Run(test.name, func(t *testing.T) {
    63  			assert.Equal(t, test.expected, HasAnyOfPrefixes(test.input, test.prefixes...))
    64  		})
    65  	}
    66  }
    67  
    68  func TestTruncateMiddleEllipsis(t *testing.T) {
    69  	tests := []struct {
    70  		input    string
    71  		len      int
    72  		expected string
    73  	}{
    74  		{
    75  			input:    "nobody expects the spanish inquisition",
    76  			len:      39,
    77  			expected: "nobody expects the spanish inquisition",
    78  		},
    79  		{
    80  			input:    "nobody expects the spanish inquisition",
    81  			len:      30,
    82  			expected: "nobody expects ...ish inquisition",
    83  		},
    84  		{
    85  			input:    "nobody expects the spanish inquisition",
    86  			len:      38,
    87  			expected: "nobody expects the spanish inquisition",
    88  		},
    89  		{
    90  			input:    "",
    91  			len:      30,
    92  			expected: "",
    93  		},
    94  		{
    95  			input:    "",
    96  			len:      0,
    97  			expected: "",
    98  		},
    99  	}
   100  
   101  	for _, test := range tests {
   102  		t.Run(test.input+":"+strconv.Itoa(test.len), func(t *testing.T) {
   103  			assert.Equal(t, test.expected, TruncateMiddleEllipsis(test.input, test.len))
   104  		})
   105  	}
   106  }
   107  
   108  func TestSplitAny(t *testing.T) {
   109  
   110  	tests := []struct {
   111  		name   string
   112  		input  string
   113  		fields string
   114  		want   []string
   115  	}{
   116  		{
   117  			name:   "simple",
   118  			input:  "a,b,c",
   119  			fields: ",",
   120  			want:   []string{"a", "b", "c"},
   121  		},
   122  		{
   123  			name:   "empty",
   124  			input:  "",
   125  			fields: ",",
   126  			want:   []string{},
   127  		},
   128  		{
   129  			name:   "multiple separators",
   130  			input:  "a,b\nc:d",
   131  			fields: ",:\n",
   132  			want:   []string{"a", "b", "c", "d"},
   133  		},
   134  	}
   135  	for _, tt := range tests {
   136  		t.Run(tt.name, func(t *testing.T) {
   137  			assert.Equal(t, tt.want, SplitAny(tt.input, tt.fields))
   138  		})
   139  	}
   140  }