2023-08-19 13:25:53 +00:00
|
|
|
package types
|
2022-10-31 14:37:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2024-03-11 08:23:17 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-31 14:37:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSplitWithEscaping(t *testing.T) {
|
|
|
|
tests := []struct {
|
2024-03-11 10:52:52 +00:00
|
|
|
name string
|
2024-03-11 08:23:17 +00:00
|
|
|
input string
|
|
|
|
output []string
|
2022-10-31 14:37:50 +00:00
|
|
|
}{
|
2024-03-11 10:52:52 +00:00
|
|
|
{name: "empty string", input: "", output: []string{}},
|
|
|
|
{name: "simple comma separated", input: "a,b", output: []string{"a", "b"}},
|
|
|
|
{name: "multiple commas", input: ",,,", output: []string{"", "", "", ""}},
|
|
|
|
{name: "escaped comma", input: ",a\\,", output: []string{"", "a,"}},
|
|
|
|
{name: "escaped backslash", input: "a,b\\,c\\\\d,e", output: []string{"a", "b,c\\\\d", "e"}},
|
2022-10-31 14:37:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 08:23:17 +00:00
|
|
|
for _, tt := range tests {
|
2024-03-11 10:52:52 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
strings := splitWithEscaping(tt.input, ",", "\\")
|
|
|
|
got, want := strings, tt.output
|
2023-02-08 09:16:01 +00:00
|
|
|
|
2024-03-11 10:52:52 +00:00
|
|
|
assert.Equal(t, got, want)
|
|
|
|
})
|
2022-10-31 14:37:50 +00:00
|
|
|
}
|
|
|
|
}
|