github.com/verrazzano/verrazzano@v1.7.0/pkg/string/slice_test.go (about)

     1  // Copyright (C) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package string
     5  
     6  import (
     7  	"testing"
     8  
     9  	asserts "github.com/stretchr/testify/assert"
    10  )
    11  
    12  // Test_stringSliceContainsString tests the SliceContainsString function
    13  func Test_stringSliceContainsString(t *testing.T) {
    14  	assert := asserts.New(t)
    15  	var slice []string
    16  	var find string
    17  	var found bool
    18  
    19  	// GIVEN a nil slice
    20  	// WHEN an empty string is searched for
    21  	// THEN verify false is returned
    22  	slice = nil
    23  	found = SliceContainsString(slice, find)
    24  	assert.Equal(found, false)
    25  
    26  	// GIVEN a slice with several strings
    27  	// WHEN one of the strings is searched for
    28  	// THEN verify string is found
    29  	slice = []string{"test-value-1", "test-value-2", "test-value-3"}
    30  	find = "test-value-2"
    31  	found = SliceContainsString(slice, find)
    32  	assert.Equal(found, true)
    33  
    34  	// GIVEN a slice with several strings
    35  	// WHEN a string not in the slice is searched for
    36  	// THEN verify string is not found
    37  	slice = []string{"test-value-1", "test-value-2", "test-value-3"}
    38  	find = "test-value-4"
    39  	found = SliceContainsString(slice, find)
    40  	assert.Equal(found, false)
    41  }
    42  
    43  // Test_stringSliceContainsString tests the RemoveStringFromSlice function
    44  func Test_removeStringFromStringSlice(t *testing.T) {
    45  	assert := asserts.New(t)
    46  	var slice []string
    47  	var remove string
    48  	var output []string
    49  
    50  	// GIVEN a nil slice and an empty string to remove
    51  	// WHEN the empty string is removed from the nil slice
    52  	// THEN verify that an empty slice is returned
    53  	slice = nil
    54  	remove = ""
    55  	output = RemoveStringFromSlice(slice, remove)
    56  	assert.NotNil(output)
    57  	assert.Len(output, 0)
    58  
    59  	// GIVEN a slice with several strings
    60  	// WHEN a string in the slice is removed
    61  	// THEN verify slice is correct
    62  	slice = []string{"test-value-1", "test-value-2", "test-value-3"}
    63  	remove = "test-value-2"
    64  	output = RemoveStringFromSlice(slice, remove)
    65  	assert.Equal("test-value-1", slice[0])
    66  	assert.Equal("test-value-2", slice[1])
    67  	assert.Len(output, 2)
    68  }
    69  
    70  // TestUnorderedEqual tests the UnorderedEqual function
    71  func TestUnorderedEqual(t *testing.T) {
    72  	assert := asserts.New(t)
    73  	var mapBool map[string]bool
    74  	var arrayStr []string
    75  
    76  	// GIVEN a map and array with the same elements and order
    77  	// WHEN compared
    78  	// THEN the UnorderedEqual returns true
    79  	arrayStr = []string{"test-value-1", "test-value-2", "test-value-3"}
    80  	mapBool = make(map[string]bool)
    81  	mapBool["test-value-1"] = true
    82  	mapBool["test-value-2"] = true
    83  	mapBool["test-value-3"] = true
    84  	success := UnorderedEqual(mapBool, arrayStr)
    85  	assert.Equal(true, success)
    86  
    87  	// GIVEN a map and array with the same elements and different order
    88  	// WHEN compared
    89  	// THEN the UnorderedEqual returns true
    90  	arrayStr = []string{"test-value-2", "test-value-3", "test-value-1"}
    91  	mapBool = make(map[string]bool)
    92  	mapBool["test-value-1"] = true
    93  	mapBool["test-value-2"] = true
    94  	mapBool["test-value-3"] = true
    95  	success = UnorderedEqual(mapBool, arrayStr)
    96  	assert.Equal(true, success)
    97  
    98  	// GIVEN a map and array with the different number of elements
    99  	// WHEN compared
   100  	// THEN the UnorderedEqual returns false
   101  	arrayStr = []string{"test-value-2", "test-value-3"}
   102  	mapBool = make(map[string]bool)
   103  	mapBool["test-value-1"] = true
   104  	mapBool["test-value-2"] = true
   105  	mapBool["test-value-3"] = true
   106  	success = UnorderedEqual(mapBool, arrayStr)
   107  	assert.Equal(false, success)
   108  
   109  	// GIVEN a map and array with the same number of elements but different elements
   110  	// WHEN compared
   111  	// THEN the UnorderedEqual returns false
   112  	arrayStr = []string{"test-value-2", "test-value-3", "test-value-4"}
   113  	mapBool = make(map[string]bool)
   114  	mapBool["test-value-1"] = true
   115  	mapBool["test-value-5"] = true
   116  	mapBool["test-value-3"] = true
   117  	success = UnorderedEqual(mapBool, arrayStr)
   118  	assert.Equal(false, success)
   119  }
   120  
   121  // TestSliceToSet tests the SliceContainsString function
   122  func TestSliceToSet(t *testing.T) {
   123  	assert := asserts.New(t)
   124  	slice := []string{"s1", "s2", "s3"}
   125  
   126  	// GIVEN a slice with several strings
   127  	// WHEN the slice is converted to a set
   128  	// THEN verify the set is correct
   129  	set := SliceToSet(slice)
   130  	assert.Len(set, 3)
   131  	assert.Contains(slice, "s1", "Set should contain string")
   132  	assert.Contains(slice, "s2", "Set should contain string")
   133  	assert.Contains(slice, "s3", "Set should contain string")
   134  	assert.NotContains(slice, "s4", "Set should not contain string")
   135  }
   136  
   137  // TestEmptyOrNilSliceToSet tests the SliceContainsString function
   138  func TestEmptyOrNilSliceToSet(t *testing.T) {
   139  	assert := asserts.New(t)
   140  	slice := []string{}
   141  
   142  	// GIVEN an empty slice
   143  	// WHEN the slice is converted to a set
   144  	// THEN verify the set is empty
   145  	set := SliceToSet(slice)
   146  	assert.Len(set, 0, "Empty slice should result in empty set")
   147  
   148  	// GIVEN an nil slice
   149  	// WHEN the slice is converted to a set
   150  	// THEN verify the set is empty
   151  	set = SliceToSet(nil)
   152  	assert.Len(set, 0, "Nil slice should result in empty set")
   153  }
   154  
   155  // TestAddString tests the SliceAddString func for the following use case
   156  // GIVEN a request to SliceAddString with an input slice of strings
   157  // WHEN string is added to the input slice
   158  // THEN a new slice is returned with the input string is appended to the end of it
   159  func TestAddString(t *testing.T) {
   160  	tests := []struct {
   161  		name          string
   162  		description   string
   163  		inputSlice    []string
   164  		stringToAdd   string
   165  		added         bool
   166  		expectedSlice []string
   167  	}{
   168  		{
   169  			name:          "AddToEmptySlice",
   170  			inputSlice:    []string{},
   171  			stringToAdd:   "astring",
   172  			added:         true,
   173  			expectedSlice: []string{"astring"},
   174  		},
   175  		{
   176  			name:          "AddToNonEmptySlice",
   177  			inputSlice:    []string{"foo", "bar"},
   178  			stringToAdd:   "astring",
   179  			added:         true,
   180  			expectedSlice: []string{"foo", "bar", "astring"},
   181  		},
   182  		{
   183  			name:          "StringAlreadyExistsInSlice",
   184  			inputSlice:    []string{"foo", "astring", "bar"},
   185  			stringToAdd:   "astring",
   186  			added:         false,
   187  			expectedSlice: []string{"foo", "astring", "bar"},
   188  		},
   189  	}
   190  	for _, test := range tests {
   191  		asserts := asserts.New(t)
   192  		t.Log(test.name)
   193  		result, added := SliceAddString(test.inputSlice, test.stringToAdd)
   194  		asserts.Equal(test.added, added)
   195  		asserts.Equal(test.expectedSlice, result)
   196  	}
   197  }
   198  
   199  // TestAreSlicesEqualWithoutOrder tests the TestAreSlicesEqualWithoutOrder func for the following use case
   200  // GIVEN a request to TestAreSlicesEqualWithoutOrder with an input of two slices
   201  // WHEN function is called
   202  // THEN return true if same, false if not equal
   203  func TestAreSlicesEqualWithoutOrder(t *testing.T) {
   204  	assert := asserts.New(t)
   205  	slice1 := []string{"s1", "s2", "s3"}
   206  	slice2 := []string{"s1", "s3", "s2"}
   207  	slice3 := []string{"s1", "s2", "s4"}
   208  	slice4 := []string{"s1", "s2"}
   209  
   210  	assert.True(AreSlicesEqualWithoutOrder(slice1, slice2))
   211  	assert.False(AreSlicesEqualWithoutOrder(slice1, slice3))
   212  	assert.False(AreSlicesEqualWithoutOrder(slice2, slice3))
   213  	assert.False(AreSlicesEqualWithoutOrder(slice1, slice4))
   214  }