github.com/yandex/pandora@v0.5.32/lib/str/string_test.go (about)

     1  package str
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestParseStringFunc(t *testing.T) {
    11  	testCases := []struct {
    12  		name          string
    13  		input         string
    14  		expectedName  string
    15  		expectedArgs  []string
    16  		expectedError error
    17  	}{
    18  		{
    19  			name:          "TestValidInputNoArgs",
    20  			input:         "functionName",
    21  			expectedName:  "functionName",
    22  			expectedArgs:  nil,
    23  			expectedError: nil,
    24  		},
    25  		{
    26  			name:          "TestValidInputWithArgs",
    27  			input:         "functionName(arg1, arg2, arg3)",
    28  			expectedName:  "functionName",
    29  			expectedArgs:  []string{"arg1", "arg2", "arg3"},
    30  			expectedError: nil,
    31  		},
    32  		{
    33  			name:          "TestInvalidCloseBracket",
    34  			input:         "functionName(arg1, arg2, arg3",
    35  			expectedName:  "",
    36  			expectedArgs:  nil,
    37  			expectedError: errors.New("invalid close bracket position"),
    38  		},
    39  		{
    40  			name:          "TestValidInputOneArg",
    41  			input:         "functionName(arg1)",
    42  			expectedName:  "functionName",
    43  			expectedArgs:  []string{"arg1"},
    44  			expectedError: nil,
    45  		},
    46  		{
    47  			name:          "TestEmptyInput",
    48  			input:         "",
    49  			expectedName:  "",
    50  			expectedArgs:  nil,
    51  			expectedError: nil,
    52  		},
    53  		{
    54  			name:          "TestOnlyOpenBracket",
    55  			input:         "(",
    56  			expectedName:  "",
    57  			expectedArgs:  nil,
    58  			expectedError: errors.New("invalid close bracket position"),
    59  		},
    60  		{
    61  			name:          "TestOnlyCloseBracket",
    62  			input:         ")",
    63  			expectedName:  "",
    64  			expectedArgs:  nil,
    65  			expectedError: errors.New("invalid close bracket position"),
    66  		},
    67  		{
    68  			name:          "TestSingleEmptyArgument",
    69  			input:         "functionName()",
    70  			expectedName:  "functionName",
    71  			expectedArgs:  []string{""},
    72  			expectedError: nil,
    73  		},
    74  		{
    75  			name:          "TestBracketInFunctionName",
    76  			input:         "functionName)arg1, arg2, arg3)",
    77  			expectedName:  "",
    78  			expectedArgs:  nil,
    79  			expectedError: errors.New("invalid close bracket position"),
    80  		},
    81  		{
    82  			name:          "TestExtraCloseBracket",
    83  			input:         "functionName(arg1, arg2, arg3))",
    84  			expectedName:  "",
    85  			expectedArgs:  nil,
    86  			expectedError: errors.New("invalid close bracket position"),
    87  		},
    88  	}
    89  
    90  	for _, tc := range testCases {
    91  		t.Run(tc.name, func(t *testing.T) {
    92  			name, args, err := ParseStringFunc(tc.input)
    93  
    94  			// Assert the values
    95  			assert.Equal(t, tc.expectedName, name)
    96  			assert.Equal(t, tc.expectedArgs, args)
    97  			assert.Equal(t, tc.expectedError, err)
    98  		})
    99  	}
   100  }