1package protocol
2
3import (
4	"testing"
5)
6
7func BenchmarkStringEscFunc4(b *testing.B) {
8	s := "h\tello ⛵ \t \t"
9	for i := 0; i < b.N; i++ {
10		b := escape(s)
11		_ = b
12	}
13
14}
15
16func BenchmarkEscFunc4(b *testing.B) {
17	s := []byte("h\tello ⛵ \t \t")
18	dest := make([]byte, 32)
19	for i := 0; i < b.N; i++ {
20		escapeBytes(&dest, s)
21		dest = dest[:0]
22	}
23}
24
25func TestBytesEscape(t *testing.T) {
26	cases := []struct {
27		// we use strings in test because its easier to read and write them for humans
28		name string
29		arg  string
30		want string
31	}{
32		{
33			name: "sailboat",
34			arg:  `⛵`,
35			want: `⛵`,
36		},
37		{
38			name: "sentence",
39			arg:  `hello I like tobut do not like ☠`,
40			want: `hello\ I\ like\ to\ ⛵but\ do\ not\ like\ ☠`,
41		},
42		{
43			name: "escapes",
44			arg:  "\t\n\f\r ,=",
45			want: `\t\n\f\r\ \,\=`,
46		},
47		{
48			name: "nameEscapes",
49			arg:  "\t\n\f\r ,",
50			want: `\t\n\f\r\ \,`,
51		},
52		{
53			name: "stringFieldEscapes",
54			arg:  "\t\n\f\r\\\"",
55			want: `\t\n\f\r\"`,
56		},
57	}
58	got := []byte{}
59	for _, x := range cases {
60		escapeBytes(&got, []byte(x.arg))
61		if string(got) != string(x.want) {
62			t.Fatalf("did not escape %s properly, expected %s got %s", x.name, x.want, got)
63		}
64		got = got[:0]
65	}
66}
67