-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
string_test.go
79 lines (67 loc) · 1.49 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package httpsfv
import (
"strings"
"testing"
"unicode"
)
func TestStringMarshalSFV(t *testing.T) {
t.Parallel()
data := []struct {
in string
expected string
valid bool
}{
{"foo", `"foo"`, true},
{`f"oo`, `"f\"oo"`, true},
{`f\oo`, `"f\\oo"`, true},
{`f\"oo`, `"f\\\"oo"`, true},
{"", `""`, true},
{"H3lLo", `"H3lLo"`, true},
{"hel\tlo", `"hel`, false},
{"hel\x1flo", `"hel`, false},
{"hel\x7flo", `"hel`, false},
{"Kévin", `"K`, false},
{"\t", `"`, false},
}
var b strings.Builder
for _, d := range data {
b.Reset()
err := marshalString(&b, d.in)
if d.valid && err != nil {
t.Errorf("error not expected for %v, got %v", d.in, err)
} else if !d.valid && err == nil {
t.Errorf("error expected for %v, got %v", d.in, err)
}
if b.String() != d.expected {
t.Errorf("got %v; want %v", b.String(), d.expected)
}
}
}
func TestParseString(t *testing.T) {
t.Parallel()
data := []struct {
in string
out string
err bool
}{
{`"foo"`, "foo", false},
{`"b\"a\\r"`, `b"a\r`, false},
{"", "", true},
{"a", "", true},
{`"\`, "", true},
{`"\o`, "", true},
{string([]byte{'"', 0}), "", true},
{string([]byte{'"', unicode.MaxASCII}), "", true},
{`"foo`, "", true},
}
for _, d := range data {
s := &scanner{data: d.in}
i, err := parseString(s)
if d.err && err == nil {
t.Errorf("parse%s): error expected", d.in)
}
if !d.err && d.out != i {
t.Errorf("parse%s) = %v, %v; %v, <nil> expected", d.in, i, err, d.out)
}
}
}