-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
string_test.go
56 lines (51 loc) · 1.46 KB
/
string_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
// Copyright (c) 2021, Peter Ohler, All rights reserved.
package ojg_test
import (
"testing"
"github.com/ohler55/ojg"
"github.com/ohler55/ojg/tt"
)
func TestStringJSON(t *testing.T) {
type Data struct {
src string
expect string
htmlSafe bool
}
for i, td := range []*Data{
{src: "abc", expect: `"abc"`},
{src: "a\tbc", expect: `"a\tbc"`},
{src: "a<b>c", expect: `"a<b>c"`},
{src: "a<b>c", expect: `"a\u003cb\u003ec"`, htmlSafe: true},
{src: "a 𝄢 note", expect: `"a 𝄢 note"`},
{src: "a\u001ec", expect: `"a\u001ec"`},
{src: "a\u2028b\u2029c", expect: `"a\u2028b\u2029c"`},
{src: "abc\ufffd", expect: `"abc\ufffd"`},
} {
var buf []byte
buf = ojg.AppendJSONString(buf, td.src, td.htmlSafe)
tt.Equal(t, td.expect, string(buf), i, ": ", td.src)
}
}
func TestStringSEN(t *testing.T) {
type Data struct {
src string
expect string
htmlSafe bool
}
for i, td := range []*Data{
{src: "abc", expect: `abc`},
{src: "", expect: `""`},
{src: "a\tbc", expect: "\"a\tbc\""},
{src: "a\"bc", expect: `"a\"bc"`},
{src: "a<b>c", expect: `a<b>c`},
{src: "a<b>c", expect: `"a\u003cb\u003ec"`, htmlSafe: true},
{src: "a 𝄢 note", expect: `"a 𝄢 note"`},
{src: "a\u001ec", expect: `"a\u001ec"`},
{src: "a\u2028b\u2029c", expect: `"a\u2028b\u2029c"`},
{src: "abc\ufffd", expect: `"abc\ufffd"`},
} {
var buf []byte
buf = ojg.AppendSENString(buf, td.src, td.htmlSafe)
tt.Equal(t, td.expect, string(buf), i, ": ", td.src)
}
}