2011-10-09 21:44:37 -06:00
|
|
|
// 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{
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: ElementNode,
|
|
|
|
Data: "head",
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: ElementNode,
|
|
|
|
Data: "body",
|
|
|
|
Child: []*Node{
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: TextNode,
|
|
|
|
Data: "0<1",
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: ElementNode,
|
|
|
|
Data: "p",
|
|
|
|
Attr: []Attribute{
|
2011-12-01 15:33:24 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Key: "id",
|
|
|
|
Val: "A",
|
|
|
|
},
|
2011-12-01 15:33:24 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Key: "foo",
|
|
|
|
Val: `abc"def`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Child: []*Node{
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: TextNode,
|
|
|
|
Data: "2",
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: ElementNode,
|
|
|
|
Data: "b",
|
|
|
|
Attr: []Attribute{
|
2011-12-01 15:33:24 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Key: "empty",
|
|
|
|
Val: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Child: []*Node{
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: TextNode,
|
|
|
|
Data: "3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: ElementNode,
|
|
|
|
Data: "i",
|
|
|
|
Attr: []Attribute{
|
2011-12-01 15:33:24 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Key: "backslash",
|
|
|
|
Val: `\`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Child: []*Node{
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: TextNode,
|
|
|
|
Data: "&4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: TextNode,
|
|
|
|
Data: "5",
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: ElementNode,
|
|
|
|
Data: "blockquote",
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: ElementNode,
|
|
|
|
Data: "br",
|
|
|
|
},
|
2011-12-02 12:14:25 -07:00
|
|
|
{
|
2011-10-09 21:44:37 -06:00
|
|
|
Type: TextNode,
|
|
|
|
Data: "6",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
want := `<html><head></head><body>0<1<p id="A" foo="abc"def">` +
|
|
|
|
`2<b empty="">3</b><i backslash="\">&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)
|
|
|
|
}
|
|
|
|
}
|