github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/str/strings_test.go (about)

     1  package str_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestUnique(t *testing.T) {
    15  	// given
    16  	testCases := []struct {
    17  		Name     string
    18  		Input    []string
    19  		Expected []string
    20  	}{
    21  		{
    22  			Name:     "Unique values",
    23  			Input:    []string{"foo", "bar", "baz"},
    24  			Expected: []string{"foo", "bar", "baz"},
    25  		},
    26  		{
    27  			Name:     "Duplicates",
    28  			Input:    []string{"foo", "bar", "foo", "baz", "baz"},
    29  			Expected: []string{"foo", "bar", "baz"},
    30  		},
    31  	}
    32  
    33  	for i, testCase := range testCases {
    34  		t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) {
    35  			// when
    36  			result := str.Unique(testCase.Input)
    37  
    38  			// then
    39  			assert.ElementsMatch(t, testCase.Expected, result)
    40  		})
    41  	}
    42  }
    43  
    44  func TestSliceToMap(t *testing.T) {
    45  	// given
    46  	testCases := []struct {
    47  		Name     string
    48  		Input    []string
    49  		Expected map[string]struct{}
    50  	}{
    51  		{
    52  			Name:  "Unique values",
    53  			Input: []string{"foo", "bar", "baz"},
    54  			Expected: map[string]struct{}{
    55  				"foo": {},
    56  				"bar": {},
    57  				"baz": {},
    58  			},
    59  		},
    60  		{
    61  			Name:  "Duplicates",
    62  			Input: []string{"foo", "bar", "foo", "baz", "baz"},
    63  			Expected: map[string]struct{}{
    64  				"foo": {},
    65  				"bar": {},
    66  				"baz": {},
    67  			},
    68  		},
    69  	}
    70  
    71  	for i, testCase := range testCases {
    72  		t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) {
    73  			// when
    74  			result := str.SliceToMap(testCase.Input)
    75  
    76  			// then
    77  			assert.Equal(t, testCase.Expected, result)
    78  		})
    79  	}
    80  }
    81  
    82  func TestMapToSlice(t *testing.T) {
    83  	// given
    84  	testCases := []struct {
    85  		Name     string
    86  		Input    map[string]struct{}
    87  		Expected []string
    88  	}{
    89  		{
    90  			Name: "Unique values",
    91  			Input: map[string]struct{}{
    92  				"foo": {},
    93  				"bar": {},
    94  				"baz": {},
    95  			},
    96  			Expected: []string{"foo", "bar", "baz"},
    97  		},
    98  		{
    99  			Name: "Duplicates",
   100  			Input: map[string]struct{}{
   101  				"foo": {},
   102  				"bar": {},
   103  				"baz": {},
   104  			},
   105  			Expected: []string{"foo", "bar", "baz"},
   106  		},
   107  	}
   108  
   109  	for i, testCase := range testCases {
   110  		t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) {
   111  			// when
   112  			result := str.MapToSlice(testCase.Input)
   113  
   114  			// then
   115  			assert.ElementsMatch(t, testCase.Expected, result)
   116  		})
   117  	}
   118  }
   119  
   120  func TestCast(t *testing.T) {
   121  	t.Run("errors when casting non-string data", func(t *testing.T) {
   122  		_, err := str.Cast([]byte{1, 2})
   123  
   124  		require.EqualError(t, err, "Internal Server Error: unable to cast the value to a string type")
   125  	})
   126  
   127  	t.Run("returns valid string", func(t *testing.T) {
   128  		s, err := str.Cast("abc")
   129  
   130  		require.NoError(t, err)
   131  		require.Equal(t, "abc", s)
   132  	})
   133  }
   134  
   135  func TestPrefixStrings(t *testing.T) {
   136  	in := []string{"foo", "bar", "baz"}
   137  	prefix := "test."
   138  
   139  	expected := []string{"test.foo", "test.bar", "test.baz"}
   140  
   141  	result := str.PrefixStrings(in, prefix)
   142  
   143  	assert.Equal(t, expected, result)
   144  }
   145  
   146  func TestTitle(t *testing.T) {
   147  	const testStr = "Test"
   148  
   149  	testCases := []struct {
   150  		Name  string
   151  		Input string
   152  	}{
   153  		{
   154  			Name:  "when string is all-caps returns string with first capital letter",
   155  			Input: "TEST",
   156  		},
   157  		{
   158  			Name:  "when string is small-caps returns string with first capital letter",
   159  			Input: "test",
   160  		},
   161  		{
   162  			Name:  "when string has randomized all-caps letters returns string with first capital letter",
   163  			Input: "tEsT",
   164  		},
   165  	}
   166  
   167  	for _, testCase := range testCases {
   168  		t.Run(testCase.Name, func(t *testing.T) {
   169  			actual := str.Title(testCase.Input)
   170  			require.Equal(t, testStr, actual)
   171  		})
   172  	}
   173  }