go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/stringutil/split_quoted_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package stringutil 9 10 import ( 11 "fmt" 12 "testing" 13 14 "go.charczuk.com/sdk/assert" 15 ) 16 17 func Test_SplitQuoted(t *testing.T) { 18 testCases := [...]struct { 19 Text string 20 Sep string 21 Expected []string 22 }{ 23 {Text: "", Sep: ",", Expected: nil}, 24 {Text: "foo", Sep: "", Expected: nil}, 25 {Text: "foo", Sep: ",", Expected: []string{"foo"}}, 26 {Text: "foo,bar", Sep: ",", Expected: []string{"foo", "bar"}}, 27 {Text: "foo,bar,baz", Sep: ",", Expected: []string{"foo", "bar", "baz"}}, 28 {Text: "foo,bar,", Sep: ",", Expected: []string{"foo", "bar"}}, 29 {Text: ",foo,bar", Sep: ",", Expected: []string{"foo", "bar"}}, 30 {Text: ",foo,bar,", Sep: ",", Expected: []string{"foo", "bar"}}, 31 {Text: "foo, bar", Sep: ", ", Expected: []string{"foo", "bar"}}, 32 {Text: "foo , bar", Sep: " , ", Expected: []string{"foo", "bar"}}, 33 {Text: "foobarbaz", Sep: "bar", Expected: []string{"foo", "baz"}}, 34 {Text: "foobarbazbarloo", Sep: "bar", Expected: []string{"foo", "baz", "loo"}}, 35 {Text: `foo"foobarbaz"barloobarmoo`, Sep: "bar", Expected: []string{`foo"foobarbaz"`, "loo", "moo"}}, 36 {Text: `"foo","bar","baz,buzz,boo"`, Sep: ",", Expected: []string{`"foo"`, `"bar"`, `"baz,buzz,boo"`}}, 37 {Text: "foobarbazbaloo", Sep: "bar", Expected: []string{"foo", "bazbaloo"}}, 38 {Text: "foobarbazbarlooba", Sep: "bar", Expected: []string{"foo", "baz", "looba"}}, 39 } 40 41 for index, tc := range testCases { 42 output := SplitQuoted(tc.Text, tc.Sep) 43 assert.ItsEqual(t, tc.Expected, output, fmt.Sprintf("test case: %d", index)) 44 } 45 46 }