aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/html/render_test.go
blob: 0584f35abdb264de4d0c18a5bef7b9cd2572292d (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package html

import (
	"bytes"
	"testing"
)

func TestRenderer(t *testing.T) {
	n := &Node{
		Type: ElementNode,
		Data: "html",
		Child: []*Node{
			{
				Type: ElementNode,
				Data: "head",
			},
			{
				Type: ElementNode,
				Data: "body",
				Child: []*Node{
					{
						Type: TextNode,
						Data: "0<1",
					},
					{
						Type: ElementNode,
						Data: "p",
						Attr: []Attribute{
							{
								Key: "id",
								Val: "A",
							},
							{
								Key: "foo",
								Val: `abc"def`,
							},
						},
						Child: []*Node{
							{
								Type: TextNode,
								Data: "2",
							},
							{
								Type: ElementNode,
								Data: "b",
								Attr: []Attribute{
									{
										Key: "empty",
										Val: "",
									},
								},
								Child: []*Node{
									{
										Type: TextNode,
										Data: "3",
									},
								},
							},
							{
								Type: ElementNode,
								Data: "i",
								Attr: []Attribute{
									{
										Key: "backslash",
										Val: `\`,
									},
								},
								Child: []*Node{
									{
										Type: TextNode,
										Data: "&4",
									},
								},
							},
						},
					},
					{
						Type: TextNode,
						Data: "5",
					},
					{
						Type: ElementNode,
						Data: "blockquote",
					},
					{
						Type: ElementNode,
						Data: "br",
					},
					{
						Type: TextNode,
						Data: "6",
					},
				},
			},
		},
	}
	want := `<html><head></head><body>0&lt;1<p id="A" foo="abc&quot;def">` +
		`2<b empty="">3</b><i backslash="\">&amp;4</i></p>` +
		`5<blockquote></blockquote><br/>6</body></html>`
	b := new(bytes.Buffer)
	if err := Render(b, n); err != nil {
		t.Fatal(err)
	}
	if got := b.String(); got != want {
		t.Errorf("got vs want:\n%s\n%s\n", got, want)
	}
}