-
Notifications
You must be signed in to change notification settings - Fork 7
/
printer_test.go
85 lines (73 loc) · 1.82 KB
/
printer_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
package xmlwriter
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"strings"
"testing"
"unicode/utf8"
tt "github.com/shabbyrobe/xmlwriter/testtool"
)
func TestEscapeAttrString(t *testing.T) {
ws := func(t *testing.T, in string) (out string) {
t.Helper()
var b bytes.Buffer
p := printer{Writer: bufio.NewWriterSize(&b, 2048)}
p.EscapeAttrString(in)
p.Flush()
if err := p.cachedWriteError(); err != nil {
t.Fatal(err)
}
return b.String()
}
expect := func(t *testing.T, in string, out string) {
tt.Assert(t, ws(t, in) == out, fmt.Sprint(in, "!=", out))
}
expect(t, "abc", "abc")
expect(t, "a-c", "a-c")
expect(t, "a\nb", "a
b")
expect(t, "\nb", "
b")
expect(t, "a\n", "a
")
}
func TestIsInCharacterRange(t *testing.T) {
invalid := []rune{
utf8.MaxRune + 1,
0xD800, // surrogate min
0xDFFF, // surrogate max
-1,
}
for _, r := range invalid {
if isInCharacterRange(r) {
t.Errorf("rune %U considered valid", r)
}
}
}
func BenchmarkEscapeAttrString(b *testing.B) {
for _, sz := range []int{10, 50, 300} {
b.Run(fmt.Sprintf("ascii-%d", sz), func(b *testing.B) {
p := printer{Writer: bufio.NewWriterSize(ioutil.Discard, 2048)}
v := strings.Repeat("1", sz)
for i := 0; i < b.N; i++ {
p.EscapeAttrString(v)
p.Reset(ioutil.Discard)
}
})
b.Run(fmt.Sprintf("utf8-first-%d", sz), func(b *testing.B) {
p := printer{Writer: bufio.NewWriterSize(ioutil.Discard, 2048)}
v := "\uD000" + strings.Repeat("1", sz-1)
for i := 0; i < b.N; i++ {
p.EscapeAttrString(v)
p.Reset(ioutil.Discard)
}
})
b.Run(fmt.Sprintf("utf8-last-%d", sz), func(b *testing.B) {
p := printer{Writer: bufio.NewWriterSize(ioutil.Discard, 2048)}
v := strings.Repeat("1", sz-1) + "\uD000"
for i := 0; i < b.N; i++ {
p.EscapeAttrString(v)
p.Reset(ioutil.Discard)
}
})
}
}