@@ -13,6 +13,14 @@ func TestNULLEscape(t *testing.T) {
1313 }
1414}
1515
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+
1624func TestEmptyStringEscape (t * testing.T ) {
1725 result := Escape ("" )
1826 t .Logf ("result :%s" , result )
@@ -87,9 +95,27 @@ func TestStringEscape(t *testing.T) {
8795 }
8896}
8997
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+
90116func TestStringCustomEscape (t * testing.T ) {
91117 s := "hello world"
92- SetSingleQuoteEscaper ("'" )
118+ SetSingleQuoteEscaper ("'' " )
93119 result := Escape (s )
94120 if result != "'hello world'" {
95121 t .Fatalf ("escape string error" )
@@ -103,6 +129,8 @@ func TestStringCustomEscape(t *testing.T) {
103129 t .Fatalf ("escape string error" )
104130
105131 }
132+ SetSingleQuoteEscaper ("\\ '" )
133+
106134}
107135
108136func TestBytesEscape (t * testing.T ) {
@@ -210,13 +238,85 @@ func TestOtherEscape(t *testing.T) {
210238 result := Escape (x )
211239 t .Logf ("escape reuslt %s" , result )
212240
213- if result != " '{\" key\" :\" test\" ,\" name\" :\" asd\\ 'fsadf\" }'" {
241+ if result != ` '{\\ "key\\ ":\\ "test\\ ",\\ "name\\ ":\\ "asd\'fsadf\\ "}'` {
214242 t .Fatalf ("escape map error" )
215243
216244 }
217245
218246}
219247
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+
220320func TestFormatSql (t * testing.T ) {
221321
222322 sql := Format ("select * from users where name=? and age=? limit ?,?" , "t'est" , 10 , 10 , 10 )
@@ -282,4 +382,11 @@ func TestFormatSql(t *testing.T) {
282382 t .Fatalf ("escape format time error" )
283383
284384 }
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+ }
285392}
0 commit comments