-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpyquote_test.go
90 lines (82 loc) · 2.57 KB
/
pyquote_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package ogórek
import (
"testing"
)
// CodecTestCase represents 1 test case of a coder or decoder.
//
// Under the given transformation function in must be transformed to outOK.
type CodecTestCase struct {
in string
outOK any // string | error
}
// testCodec tests transform func applied to all test cases from testv.
func testCodec(t *testing.T, transform func(in string)(string, error), testv []CodecTestCase) {
for _, tt := range testv {
s, err := transform(tt.in)
var out any = s
if err != nil {
out = err
}
if out != tt.outOK {
t.Errorf("%q -> unexpected:\nhave: %#v\nwant: %#v", tt.in, out, tt.outOK)
}
}
}
func TestPyQuote(t *testing.T) {
testCodec(t, func(in string) (string, error) {
return pyquote(in), nil
}, []CodecTestCase{
{`\"'`, `"\\\"'"`},
{"\x80hello мир", `"\x80hello мир"`},
{"\n\r\x01", `"\n\r\x01"`},
})
}
func TestPyDecodeStringEscape(t *testing.T) {
testCodec(t, pydecodeStringEscape, []CodecTestCase{
{`hello`, "hello"},
{"hello\\\nworld", "helloworld"},
{`\\`, `\`},
{`\'\"`, `'"`},
{`\b\f\t\n\r\v\a`, "\b\f\t\n\r\v\a"},
{`\000\001\376\377`, "\000\001\376\377"},
{`\x00\x01\x7f\x80\xfe\xff`, "\x00\x01\x7f\x80\xfe\xff"},
// vvv stays as is
{`\u1234\U00001234\c`, `\u1234\U00001234\c`},
})
}
func TestPyEncodeRawUnicodeEscape(t *testing.T) {
testCodec(t, pyencodeRawUnicodeEscape, []CodecTestCase{
{"\x93", errPyRawUnicodeEscapeInvalidUTF8}, // invalid UTF-8
{"\xc3\x28", errPyRawUnicodeEscapeInvalidUTF8}, // invalid UTF-8
{"\x00\x01abc", "\x00\x01abc"},
{`\`, `\u005c`},
{"\n", `\u000a`},
{`"'`, `"'`},
{"hello\nмир", `hello\u000a\u043c\u0438\u0440`},
{"hello\nмиÑ\u0080\x01", `hello\u000aмир`+"\x01"},
{"\u1234\U00004321", `\u1234\u4321`},
{"\U00012345", `\U00012345`},
{"\u007f\u0080\u0093\u00ff", "\x7f\x80\x93\xff"},
})
}
func TestPyDecodeRawUnicodeEscape(t *testing.T) {
testCodec(t, pydecodeRawUnicodeEscape, []CodecTestCase{
{`hello`, "hello"},
{"\x00\x01\x80\xfe\xff", "\u0000\u0001\u0080\u00fe\u00ff"},
{`\`, `\`},
{`\\`, `\\`},
{`\\\`, `\\\`},
{`\\\\`, `\\\\`},
{`\u1234\U00004321`, "\u1234\U00004321"},
{`\\u1234\\U00004321`, `\\u1234\\U00004321`},
{`\\\u1234\\\U00004321`, "\\\\\u1234\\\\\U00004321"},
{`\\\\u1234\\\\U00004321`, `\\\\u1234\\\\U00004321`},
{`\\\\\u1234\\\\\U00004321`, "\\\\\\\\\u1234\\\\\\\\\U00004321"},
// vvv stays as is
{"hello\\\nworld", "hello\\\nworld"},
{`\'\"`, `\'\"`},
{`\b\f\t\n\r\v\a`, `\b\f\t\n\r\v\a`},
{`\000\001\376\377`, `\000\001\376\377`},
{`\x00\x01\x7f\x80\xfe\xff`, `\x00\x01\x7f\x80\xfe\xff`},
})
}