4
4
5
5
package str
6
6
7
- import "testing"
7
+ import (
8
+ "reflect"
9
+ "strings"
10
+ "testing"
11
+ )
8
12
9
13
var foldDupTests = []struct {
10
14
list []string
@@ -25,3 +29,80 @@ func TestFoldDup(t *testing.T) {
25
29
}
26
30
}
27
31
}
32
+
33
+ func TestSplitQuotedFields (t * testing.T ) {
34
+ for _ , test := range []struct {
35
+ name string
36
+ value string
37
+ want []string
38
+ wantErr string
39
+ }{
40
+ {name : "empty" , value : "" , want : nil },
41
+ {name : "space" , value : " " , want : nil },
42
+ {name : "one" , value : "a" , want : []string {"a" }},
43
+ {name : "leading_space" , value : " a" , want : []string {"a" }},
44
+ {name : "trailing_space" , value : "a " , want : []string {"a" }},
45
+ {name : "two" , value : "a b" , want : []string {"a" , "b" }},
46
+ {name : "two_multi_space" , value : "a b" , want : []string {"a" , "b" }},
47
+ {name : "two_tab" , value : "a\t b" , want : []string {"a" , "b" }},
48
+ {name : "two_newline" , value : "a\n b" , want : []string {"a" , "b" }},
49
+ {name : "quote_single" , value : `'a b'` , want : []string {"a b" }},
50
+ {name : "quote_double" , value : `"a b"` , want : []string {"a b" }},
51
+ {name : "quote_both" , value : `'a '"b "` , want : []string {"a " , "b " }},
52
+ {name : "quote_contains" , value : `'a "'"'b"` , want : []string {`a "` , `'b` }},
53
+ {name : "escape" , value : `\'` , want : []string {`\'` }},
54
+ {name : "quote_unclosed" , value : `'a` , wantErr : "unterminated ' string" },
55
+ } {
56
+ t .Run (test .name , func (t * testing.T ) {
57
+ got , err := SplitQuotedFields (test .value )
58
+ if err != nil {
59
+ if test .wantErr == "" {
60
+ t .Fatalf ("unexpected error: %v" , err )
61
+ } else if errMsg := err .Error (); ! strings .Contains (errMsg , test .wantErr ) {
62
+ t .Fatalf ("error %q does not contain %q" , errMsg , test .wantErr )
63
+ }
64
+ return
65
+ }
66
+ if test .wantErr != "" {
67
+ t .Fatalf ("unexpected success; wanted error containing %q" , test .wantErr )
68
+ }
69
+ if ! reflect .DeepEqual (got , test .want ) {
70
+ t .Errorf ("got %q; want %q" , got , test .want )
71
+ }
72
+ })
73
+ }
74
+ }
75
+
76
+ func TestJoinAndQuoteFields (t * testing.T ) {
77
+ for _ , test := range []struct {
78
+ name string
79
+ args []string
80
+ want , wantErr string
81
+ }{
82
+ {name : "empty" , args : nil , want : "" },
83
+ {name : "one" , args : []string {"a" }, want : "a" },
84
+ {name : "two" , args : []string {"a" , "b" }, want : "a b" },
85
+ {name : "space" , args : []string {"a " , "b" }, want : "'a ' b" },
86
+ {name : "newline" , args : []string {"a\n " , "b" }, want : "'a\n ' b" },
87
+ {name : "quote" , args : []string {`'a ` , "b" }, want : `"'a " b` },
88
+ {name : "unquoteable" , args : []string {`'"` }, wantErr : "contains both single and double quotes and cannot be quoted" },
89
+ } {
90
+ t .Run (test .name , func (t * testing.T ) {
91
+ got , err := JoinAndQuoteFields (test .args )
92
+ if err != nil {
93
+ if test .wantErr == "" {
94
+ t .Fatalf ("unexpected error: %v" , err )
95
+ } else if errMsg := err .Error (); ! strings .Contains (errMsg , test .wantErr ) {
96
+ t .Fatalf ("error %q does not contain %q" , errMsg , test .wantErr )
97
+ }
98
+ return
99
+ }
100
+ if test .wantErr != "" {
101
+ t .Fatalf ("unexpected success; wanted error containing %q" , test .wantErr )
102
+ }
103
+ if got != test .want {
104
+ t .Errorf ("got %s; want %s" , got , test .want )
105
+ }
106
+ })
107
+ }
108
+ }
0 commit comments