-
Notifications
You must be signed in to change notification settings - Fork 43
/
parse_test.go
66 lines (59 loc) · 1.77 KB
/
parse_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 gowd
import (
"bytes"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func testOuput(t *testing.T, elem *Element, expected string) {
output := bytes.NewBuffer(make([]byte, 0))
Output = output
Order = 0
elem.Render()
assert.Equal(t, expected+"\n", output.String())
}
func TestParseElement(t *testing.T) {
em := NewElementMap()
elem, err := ParseElement(`<br/><p/><div></div>`, em)
assert.EqualError(t, err, `The provided html must yield only one html element, I have: [3:'br', 3:'p', 3:'div', ]`)
assert.Nil(t, elem)
_, err = ParseElement("", nil)
assert.EqualError(t, err, "The provided html must yield only one html element, I have: []")
}
func TestParseElement2(t *testing.T) {
em := NewElementMap()
elem, err := ParseElement(`<div id="myDiv"><h1>My First Heading</h1><p>My first paragraph.</p></div>`, em)
if err != nil {
t.Fatal(err)
}
testOuput(t, elem, "<div id=\"myDiv\"><h1>My First Heading</h1><p>My first paragraph.</p></div>")
div := elem.Find("myDiv")
assert.NotNil(t, div)
assert.EqualValues(t, div.GetID(), em["myDiv"].GetID())
p, err := div.AddHTML(`<p id="myP">another paragraph</p>`, em)
assert.NoError(t, err)
assert.NotNil(t, p)
assert.EqualValues(t, em["myP"].data, p[0].data)
}
func TestParseElementFromFile(t *testing.T) {
f, err := ioutil.TempFile(os.TempDir(), "gowd_test")
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(`<div id="myDiv"><h1>My First Heading</h1><p>My first paragraph.</p></div>`)
if err != nil {
t.Fatal(err)
}
err = f.Close()
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
em := NewElementMap()
elem, err := ParseElementFromFile(f.Name(), em)
assert.NoError(t, err)
div := elem.Find("myDiv")
assert.NotNil(t, div)
assert.EqualValues(t, div.GetID(), em["myDiv"].GetID())
}