@@ -13,6 +13,14 @@ func TestNULLEscape(t *testing.T) {
13
13
}
14
14
}
15
15
16
+ func Test0Escape (t * testing.T ) {
17
+ result := Escape (`\0` )
18
+ t .Logf ("Test0Escape result: %s" , result )
19
+ if result != `'\\\\0'` {
20
+ t .Fatalf ("escape error" )
21
+ }
22
+ }
23
+
16
24
func TestEmptyStringEscape (t * testing.T ) {
17
25
result := Escape ("" )
18
26
t .Logf ("result :%s" , result )
@@ -87,9 +95,27 @@ func TestStringEscape(t *testing.T) {
87
95
}
88
96
}
89
97
98
+ func TestStringEscape2 (t * testing.T ) {
99
+ s := "hello world"
100
+ result := Escape (s )
101
+ if result != "'hello world'" {
102
+ t .Fatalf ("escape string error" )
103
+
104
+ }
105
+
106
+ s = `hello \' world`
107
+ t .Logf ("TestStringEscape2 raw:%s" , s )
108
+ result = Escape (s )
109
+ t .Logf ("TestStringEscape2 result: %s" , result )
110
+ if result != `'hello \\\\\' world'` {
111
+ t .Fatalf ("escape string error" )
112
+
113
+ }
114
+ }
115
+
90
116
func TestStringCustomEscape (t * testing.T ) {
91
117
s := "hello world"
92
- SetSingleQuoteEscaper ("'" )
118
+ SetSingleQuoteEscaper ("'' " )
93
119
result := Escape (s )
94
120
if result != "'hello world'" {
95
121
t .Fatalf ("escape string error" )
@@ -103,6 +129,8 @@ func TestStringCustomEscape(t *testing.T) {
103
129
t .Fatalf ("escape string error" )
104
130
105
131
}
132
+ SetSingleQuoteEscaper ("\\ '" )
133
+
106
134
}
107
135
108
136
func TestBytesEscape (t * testing.T ) {
@@ -210,13 +238,85 @@ func TestOtherEscape(t *testing.T) {
210
238
result := Escape (x )
211
239
t .Logf ("escape reuslt %s" , result )
212
240
213
- if result != " '{\" key\" :\" test\" ,\" name\" :\" asd\\ 'fsadf\" }'" {
241
+ if result != ` '{\\ "key\\ ":\\ "test\\ ",\\ "name\\ ":\\ "asd\'fsadf\\ "}'` {
214
242
t .Fatalf ("escape map error" )
215
243
216
244
}
217
245
218
246
}
219
247
248
+ func TestNewlineEscape (t * testing.T ) {
249
+ s := "hello\n world"
250
+ result := Escape (s )
251
+ t .Logf ("escape newline reuslt: %s" , result )
252
+
253
+ if result != "'hello\\ \\ nworld'" {
254
+ t .Fatalf ("escape string error" )
255
+
256
+ }
257
+
258
+ }
259
+
260
+ func TestReturnEscape (t * testing.T ) {
261
+ s := "hello\r world"
262
+ result := Escape (s )
263
+ t .Logf ("escape newline reuslt: %s" , result )
264
+
265
+ if result != "'hello\\ \\ rworld'" {
266
+ t .Fatalf ("escape string error" )
267
+
268
+ }
269
+
270
+ }
271
+
272
+ func TestTabEscape (t * testing.T ) {
273
+ s := "hello\t world"
274
+ result := Escape (s )
275
+ t .Logf ("escape tab reuslt: %s" , result )
276
+
277
+ if result != `'hello\\tworld'` {
278
+ t .Fatalf ("escape string error" )
279
+
280
+ }
281
+
282
+ }
283
+
284
+ func TestDoubleBackslashEscape (t * testing.T ) {
285
+ s := "hello\\ world"
286
+ result := Escape (s )
287
+ t .Logf ("escape tab reuslt: %s" , result )
288
+
289
+ if result != `'hello\\\\world'` {
290
+ t .Fatalf ("escape string error" )
291
+
292
+ }
293
+
294
+ }
295
+
296
+ func TestCtrlZEscape (t * testing.T ) {
297
+ s := "hello\x1a world"
298
+ result := Escape (s )
299
+ t .Logf ("escape tab reuslt: %s" , result )
300
+
301
+ if result != `'hello\\Zworld'` {
302
+ t .Fatalf ("escape string error" )
303
+
304
+ }
305
+
306
+ }
307
+
308
+ func TestDoubleQouteEscape (t * testing.T ) {
309
+ s := "hello \" world"
310
+ result := Escape (s )
311
+ t .Logf ("escape tab reuslt: %s" , result )
312
+
313
+ if result != `'hello \\" world'` {
314
+ t .Fatalf ("escape string error" )
315
+
316
+ }
317
+
318
+ }
319
+
220
320
func TestFormatSql (t * testing.T ) {
221
321
222
322
sql := Format ("select * from users where name=? and age=? limit ?,?" , "t'est" , 10 , 10 , 10 )
@@ -282,4 +382,11 @@ func TestFormatSql(t *testing.T) {
282
382
t .Fatalf ("escape format time error" )
283
383
284
384
}
385
+
386
+ sql = Format ("select * from users where name=? and age=? limit ?,?" , `t\'est` , 10 , 10 , 10 )
387
+
388
+ if sql != `'select * from users where name='t\\\\\'est' and age=10 limit 10,10'` {
389
+
390
+ t .Logf ("sql: %s\n " , sql )
391
+ }
285
392
}
0 commit comments