github.com/argoproj/argo-cd/v3@v3.2.1/util/notification/expression/strings/strings_test.go (about)

     1  package strings
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestNewExprs(t *testing.T) {
    10  	funcs := []string{
    11  		"ReplaceAll",
    12  		"ToUpper",
    13  		"ToLower",
    14  	}
    15  	for _, fn := range funcs {
    16  		stringsExprs := NewExprs()
    17  		_, hasFunc := stringsExprs[fn]
    18  		assert.True(t, hasFunc)
    19  	}
    20  }
    21  
    22  func TestReplaceAll(t *testing.T) {
    23  	exprs := NewExprs()
    24  	input := "test_replace"
    25  	expected := "test=replace"
    26  	replaceAllFn, ok := exprs["ReplaceAll"].(func(s, old, new string) string)
    27  	assert.True(t, ok)
    28  	actual := replaceAllFn(input, "_", "=")
    29  	assert.Equal(t, expected, actual)
    30  }
    31  
    32  func TestUpperAndLower(t *testing.T) {
    33  	testCases := []struct {
    34  		fn       string
    35  		input    string
    36  		expected string
    37  	}{
    38  		{
    39  			fn:       "ToUpper",
    40  			input:    "test",
    41  			expected: "TEST",
    42  		},
    43  		{
    44  			fn:       "ToLower",
    45  			input:    "TEST",
    46  			expected: "test",
    47  		},
    48  	}
    49  	exprs := NewExprs()
    50  
    51  	for _, testCase := range testCases {
    52  		t.Run("With success case: Func: "+testCase.fn, func(t *testing.T) {
    53  			toUpperFn, ok := exprs[testCase.fn].(func(s string) string)
    54  			assert.True(t, ok)
    55  
    56  			actual := toUpperFn(testCase.input)
    57  			assert.Equal(t, testCase.expected, actual)
    58  		})
    59  	}
    60  }