File: htmlescapewriter_test.go

package info (click to toggle)
golang-github-valyala-quicktemplate 1.7.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 468 kB
  • sloc: xml: 15; makefile: 14
file content (29 lines) | stat: -rw-r--r-- 809 bytes parent folder | download | duplicates (3)
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
package quicktemplate

import (
	"testing"
)

func TestHTMLEscapeWriter(t *testing.T) {
	testHTMLEscapeWriter(t, "", "")
	testHTMLEscapeWriter(t, "foobar", "foobar")
	testHTMLEscapeWriter(t, `<h1>fo'"bar&</h1>`, "&lt;h1&gt;fo&#39;&quot;bar&amp;&lt;/h1&gt;")
	testHTMLEscapeWriter(t, "fo<b>bar привет\n\tbaz", "fo&lt;b&gt;bar привет\n\tbaz")
}

func testHTMLEscapeWriter(t *testing.T, s, expectedS string) {
	bb := AcquireByteBuffer()
	w := &htmlEscapeWriter{w: bb}
	n, err := w.Write([]byte(s))
	if err != nil {
		t.Fatalf("unexpected error when writing %q: %s", s, err)
	}
	if n != len(s) {
		t.Fatalf("unexpected n returned: %d. Expecting %d. s=%q", n, len(s), s)
	}

	if string(bb.B) != expectedS {
		t.Fatalf("unexpected result: %q. Expecting %q", bb.B, expectedS)
	}
	ReleaseByteBuffer(bb)
}