forked from influxdata/line-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescaper_test.go
66 lines (61 loc) · 1.18 KB
/
escaper_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
package protocol
import (
"testing"
)
func BenchmarkStringEscFunc4(b *testing.B) {
s := "h\tello ⛵ \t \t"
for i := 0; i < b.N; i++ {
b := escape(s)
_ = b
}
}
func BenchmarkEscFunc4(b *testing.B) {
s := []byte("h\tello ⛵ \t \t")
dest := make([]byte, 32)
for i := 0; i < b.N; i++ {
escapeBytes(&dest, s)
dest = dest[:0]
}
}
func TestBytesEscape(t *testing.T) {
cases := []struct {
// we use strings in test because its easier to read and write them for humans
name string
arg string
want string
}{
{
name: "sailboat",
arg: `⛵`,
want: `⛵`,
},
{
name: "sentence",
arg: `hello I like to ⛵but do not like ☠`,
want: `hello\ I\ like\ to\ ⛵but\ do\ not\ like\ ☠`,
},
{
name: "escapes",
arg: "\t\n\f\r ,=",
want: `\t\n\f\r\ \,\=`,
},
{
name: "nameEscapes",
arg: "\t\n\f\r ,",
want: `\t\n\f\r\ \,`,
},
{
name: "stringFieldEscapes",
arg: "\t\n\f\r\\\"",
want: `\t\n\f\r\"`,
},
}
got := []byte{}
for _, x := range cases {
escapeBytes(&got, []byte(x.arg))
if string(got) != string(x.want) {
t.Fatalf("did not escape %s properly, expected %s got %s", x.name, x.want, got)
}
got = got[:0]
}
}