github.com/mponton/terratest@v0.44.0/modules/collections/stringslicevalue_test.go (about)

     1  package collections
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestGetSliceLastValue(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	var testCases = []struct {
    15  		testName       string
    16  		sliceSource    string
    17  		sliceSeperator string
    18  		expectedReturn string
    19  		expectedError  bool
    20  	}{
    21  		{"longSlice", "this/is/a/long/slash/separated/string/success", "/", "success", false},
    22  		{"shortendSlice", "this/is/a/long/slash/separated", "/", "separated", false},
    23  		{"dashSlice", "this-is-a-long-dash-separated-string-success", "-", "success", false},
    24  		{"seperatorNotPresent", "this-is-a-long-dash-separated-string-success", "/", "", true},
    25  		{"sourceNoSeperator", "noslicepresent", "/", "", true},
    26  		{"emptyStrings", "", "", "", true},
    27  	}
    28  
    29  	for _, tc := range testCases {
    30  		testFor := tc //necessary range capture
    31  
    32  		t.Run(testFor.testName, func(t *testing.T) {
    33  			actualReturn, err := GetSliceLastValueE(testFor.sliceSource, testFor.sliceSeperator)
    34  			switch testFor.expectedError {
    35  			case true:
    36  				require.Error(t, err)
    37  			case false:
    38  				require.NoError(t, err)
    39  			}
    40  			assert.Equal(t, testFor.expectedReturn, actualReturn)
    41  		})
    42  	}
    43  }
    44  
    45  func TestGetSliceIndexValue(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	var testCases = []struct {
    49  		sliceIndex     int
    50  		expectedReturn string
    51  		expectedError  bool
    52  	}{
    53  		{-1, "", true},
    54  		{0, "this", false},
    55  		{4, "slash", false},
    56  		{7, "success", false},
    57  		{10, "", true},
    58  	}
    59  
    60  	sliceSource := "this/is/a/long/slash/separated/string/success"
    61  	sliceSeperator := "/"
    62  
    63  	for _, tc := range testCases {
    64  		testFor := tc //necessary range capture
    65  
    66  		t.Run(fmt.Sprintf("Index_%v", testFor.sliceIndex), func(t *testing.T) {
    67  			actualReturn, err := GetSliceIndexValueE(sliceSource, sliceSeperator, testFor.sliceIndex)
    68  			switch testFor.expectedError {
    69  			case true:
    70  				require.Error(t, err)
    71  			case false:
    72  				require.NoError(t, err)
    73  			}
    74  			assert.Equal(t, testFor.expectedReturn, actualReturn)
    75  		})
    76  	}
    77  }