@@ -12,9 +12,8 @@ func TestQuote(t *testing.T) {
12
12
name string
13
13
in string
14
14
15
- want []string
16
- wantErr []bool
17
- wantEscaped []string
15
+ want []string
16
+ wantErr []bool
18
17
}{
19
18
{
20
19
name : "bareword" ,
@@ -30,11 +29,6 @@ func TestQuote(t *testing.T) {
30
29
ast .SingleQuoted : false ,
31
30
ast .Bareword : false ,
32
31
},
33
- wantEscaped : []string {
34
- ast .DoubleQuoted : `"bareword"` ,
35
- ast .SingleQuoted : `'bareword'` ,
36
- ast .Bareword : `bareword` ,
37
- },
38
32
},
39
33
{
40
34
name : "multiple words" ,
@@ -50,11 +44,6 @@ func TestQuote(t *testing.T) {
50
44
ast .SingleQuoted : false ,
51
45
ast .Bareword : true ,
52
46
},
53
- wantEscaped : []string {
54
- ast .DoubleQuoted : `"multiple words"` ,
55
- ast .SingleQuoted : `'multiple words'` ,
56
- ast .Bareword : `multiple_words` ,
57
- },
58
47
},
59
48
{
60
49
name : "double quote" ,
@@ -70,11 +59,6 @@ func TestQuote(t *testing.T) {
70
59
ast .SingleQuoted : false ,
71
60
ast .Bareword : true ,
72
61
},
73
- wantEscaped : []string {
74
- ast .DoubleQuoted : `"value with \" (double quote)"` ,
75
- ast .SingleQuoted : `'value with " (double quote)'` ,
76
- ast .Bareword : `value_with____double_quote_` ,
77
- },
78
62
},
79
63
{
80
64
name : "escaped double quote" ,
@@ -90,11 +74,6 @@ func TestQuote(t *testing.T) {
90
74
ast .SingleQuoted : false ,
91
75
ast .Bareword : true ,
92
76
},
93
- wantEscaped : []string {
94
- ast .DoubleQuoted : `"value with \" (escaped double quote)"` ,
95
- ast .SingleQuoted : `'value with \" (escaped double quote)'` ,
96
- ast .Bareword : `value_with_____escaped_double_quote_` ,
97
- },
98
77
},
99
78
{
100
79
name : "single quote" ,
@@ -110,11 +89,6 @@ func TestQuote(t *testing.T) {
110
89
ast .SingleQuoted : true ,
111
90
ast .Bareword : true ,
112
91
},
113
- wantEscaped : []string {
114
- ast .DoubleQuoted : `"value with ' (single quote)"` ,
115
- ast .SingleQuoted : `'value with \' (single quote)'` ,
116
- ast .Bareword : `value_with____single_quote_` ,
117
- },
118
92
},
119
93
{
120
94
name : "escaped single quote" ,
@@ -130,11 +104,6 @@ func TestQuote(t *testing.T) {
130
104
ast .SingleQuoted : false ,
131
105
ast .Bareword : true ,
132
106
},
133
- wantEscaped : []string {
134
- ast .DoubleQuoted : `"value with \' (escaped single quote)"` ,
135
- ast .SingleQuoted : `'value with \' (escaped single quote)'` ,
136
- ast .Bareword : `value_with_____escaped_single_quote_` ,
137
- },
138
107
},
139
108
}
140
109
@@ -147,18 +116,106 @@ func TestQuote(t *testing.T) {
147
116
for _ , tc := range tt {
148
117
t .Run (tc .name , func (t * testing.T ) {
149
118
if len (tc .want ) != 4 && len (tc .wantErr ) != 4 {
119
+ t .Error ("test case has an invalid number of want or wantErr values" )
150
120
}
151
121
for name , quoteType := range quoteTypes {
152
122
t .Run (name , func (t * testing.T ) {
153
- got , err := astutil .Quote (tc .in , quoteType , false )
123
+ got , err := astutil .Quote (tc .in , quoteType )
154
124
if tc .wantErr [quoteType ] != (err != nil ) {
155
125
t .Errorf ("wantErr %t, err: %v" , tc .wantErr [quoteType ], err )
156
126
}
157
127
if tc .want [quoteType ] != got {
158
128
t .Errorf ("want: %q, got: %q" , tc .want [quoteType ], got )
159
129
}
130
+ })
131
+ }
132
+ })
133
+ }
134
+ }
160
135
161
- gotEscaped , _ := astutil .Quote (tc .in , quoteType , true )
136
+ func TestQuoteWithEscape (t * testing.T ) {
137
+ tt := []struct {
138
+ name string
139
+ in string
140
+
141
+ wantEscaped []string
142
+ }{
143
+ {
144
+ name : "bareword" ,
145
+ in : `bareword` ,
146
+
147
+ wantEscaped : []string {
148
+ ast .DoubleQuoted : `"bareword"` ,
149
+ ast .SingleQuoted : `'bareword'` ,
150
+ ast .Bareword : `bareword` ,
151
+ },
152
+ },
153
+ {
154
+ name : "multiple words" ,
155
+ in : `multiple words` ,
156
+
157
+ wantEscaped : []string {
158
+ ast .DoubleQuoted : `"multiple words"` ,
159
+ ast .SingleQuoted : `'multiple words'` ,
160
+ ast .Bareword : `multiple_words` ,
161
+ },
162
+ },
163
+ {
164
+ name : "double quote" ,
165
+ in : `value with " (double quote)` ,
166
+
167
+ wantEscaped : []string {
168
+ ast .DoubleQuoted : `"value with \" (double quote)"` ,
169
+ ast .SingleQuoted : `'value with " (double quote)'` ,
170
+ ast .Bareword : `value_with____double_quote_` ,
171
+ },
172
+ },
173
+ {
174
+ name : "escaped double quote" ,
175
+ in : `value with \" (escaped double quote)` ,
176
+
177
+ wantEscaped : []string {
178
+ ast .DoubleQuoted : `"value with \" (escaped double quote)"` ,
179
+ ast .SingleQuoted : `'value with \" (escaped double quote)'` ,
180
+ ast .Bareword : `value_with_____escaped_double_quote_` ,
181
+ },
182
+ },
183
+ {
184
+ name : "single quote" ,
185
+ in : `value with ' (single quote)` ,
186
+
187
+ wantEscaped : []string {
188
+ ast .DoubleQuoted : `"value with ' (single quote)"` ,
189
+ ast .SingleQuoted : `'value with \' (single quote)'` ,
190
+ ast .Bareword : `value_with____single_quote_` ,
191
+ },
192
+ },
193
+ {
194
+ name : "escaped single quote" ,
195
+ in : `value with \' (escaped single quote)` ,
196
+
197
+ wantEscaped : []string {
198
+ ast .DoubleQuoted : `"value with \' (escaped single quote)"` ,
199
+ ast .SingleQuoted : `'value with \' (escaped single quote)'` ,
200
+ ast .Bareword : `value_with_____escaped_single_quote_` ,
201
+ },
202
+ },
203
+ }
204
+
205
+ quoteTypes := map [string ]ast.StringAttributeType {
206
+ "double quote" : ast .DoubleQuoted ,
207
+ "single quote" : ast .SingleQuoted ,
208
+ "bareword" : ast .Bareword ,
209
+ }
210
+
211
+ for _ , tc := range tt {
212
+ t .Run (tc .name , func (t * testing.T ) {
213
+ if len (tc .wantEscaped ) != 4 {
214
+ t .Error ("test case has an invalid number of want values" )
215
+ }
216
+ for name , quoteType := range quoteTypes {
217
+ t .Run (name , func (t * testing.T ) {
218
+ gotEscaped := astutil .QuoteWithEscape (tc .in , quoteType )
162
219
if tc .wantEscaped [quoteType ] != gotEscaped {
163
220
t .Errorf ("want: %q, got: %q" , tc .wantEscaped [quoteType ], gotEscaped )
164
221
}
0 commit comments