forked from k3a/html2text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml2text_test.go
77 lines (65 loc) · 3.98 KB
/
html2text_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
67
68
69
70
71
72
73
74
75
76
77
package html2text
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestHTML2Text(t *testing.T) {
Convey("HTML2Text should work", t, func() {
Convey("Links", func() {
So(HTML2Text(`<div></div>`), ShouldEqual, "")
So(HTML2Text(`<div>simple text</div>`), ShouldEqual, "simple text")
So(HTML2Text(`click <a href="test">here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a class="x" href="test">here</a>`), ShouldEqual, "click test")
So(HTML2Text(`click <a href="ents/'x'">here</a>`), ShouldEqual, "click ents/'x'")
So(HTML2Text(`click <a href="javascript:void(0)">here</a>`), ShouldEqual, "click ")
})
Convey("Inlines", func() {
So(HTML2Text(`strong <strong>text</strong>`), ShouldEqual, "strong text")
So(HTML2Text(`some <div id="a" class="b">div</div>`), ShouldEqual, "some div")
})
Convey("Line breaks", func() {
So(HTML2Text("should \nignore \r\nnew lines"), ShouldEqual, "should ignore new lines")
So(HTML2Text(`two<br/>line<br/>breaks`), ShouldEqual, "two\r\n\r\nline\r\n\r\nbreaks")
So(HTML2Text(`<p>two</p><p>paragraphs</p>`), ShouldEqual, "\r\n\r\ntwo\r\n\r\nparagraphs")
})
Convey("Headings", func() {
So(HTML2Text("<h1>First</h1>main text"), ShouldEqual, "\r\n\r\nFirst\r\n\r\nmain text")
So(HTML2Text("First<h2>Second</h2>next section"), ShouldEqual, "First\r\n\r\nSecond\r\n\r\nnext section")
So(HTML2Text("<h2>Second</h2>next section"), ShouldEqual, "\r\n\r\nSecond\r\n\r\nnext section")
So(HTML2Text("Second<h3>Third</h3>next section"), ShouldEqual, "Second\r\n\r\nThird\r\n\r\nnext section")
So(HTML2Text("<h3>Third</h3>next section"), ShouldEqual, "\r\n\r\nThird\r\n\r\nnext section")
So(HTML2Text("Third<h4>Fourth</h4>next section"), ShouldEqual, "Third\r\n\r\nFourth\r\n\r\nnext section")
So(HTML2Text("<h4>Fourth</h4>next section"), ShouldEqual, "\r\n\r\nFourth\r\n\r\nnext section")
So(HTML2Text("Fourth<h5>Fifth</h5>next section"), ShouldEqual, "Fourth\r\n\r\nFifth\r\n\r\nnext section")
So(HTML2Text("<h5>Fifth</h5>next section"), ShouldEqual, "\r\n\r\nFifth\r\n\r\nnext section")
So(HTML2Text("Fifth<h6>Sixth</h6>next section"), ShouldEqual, "Fifth\r\n\r\nSixth\r\n\r\nnext section")
So(HTML2Text("<h6>Sixth</h6>next section"), ShouldEqual, "\r\n\r\nSixth\r\n\r\nnext section")
So(HTML2Text("<h7>Not Header</h7>next section"), ShouldEqual, "Not Headernext section")
})
Convey("HTML entities", func() {
So(HTML2Text(`two spaces`), ShouldEqual, "two spaces")
So(HTML2Text(`© 2017 K3A`), ShouldEqual, "© 2017 K3A")
So(HTML2Text("<printtag>"), ShouldEqual, "<printtag>")
So(HTML2Text(`would you pay in ¢, £, ¥ or €?`),
ShouldEqual, "would you pay in ¢, £, ¥ or €?")
So(HTML2Text(`Tom & Jerry is not an entity`), ShouldEqual, "Tom & Jerry is not an entity")
So(HTML2Text(`this &neither; as you see`), ShouldEqual, "this &neither; as you see")
So(HTML2Text(`fish & chips`), ShouldEqual, "fish & chips")
So(HTML2Text(`"I'm sorry, Dave. I'm afraid I can't do that." – HAL, 2001: A Space Odyssey`), ShouldEqual, "\"I'm sorry, Dave. I'm afraid I can't do that.\" – HAL, 2001: A Space Odyssey")
So(HTML2Text(`Google ®`), ShouldEqual, "Google ®")
So(HTML2Text(`list of items<ul><li>One</li><li>Two</li><li>Three</li></ul>`), ShouldEqual, "list of items\r\nOne\r\nTwo\r\nThree")
})
Convey("Large Entity", func() {
So(HTMLEntitiesToText("&abcdefghij;"), ShouldEqual, "&abcdefghij;")
})
Convey("Full HTML structure", func() {
So(HTML2Text(``), ShouldEqual, "")
So(HTML2Text(`<html><head><title>Good</title></head><body>x</body>`), ShouldEqual, "x")
So(HTML2Text(`we are not <script type="javascript"></script>interested in scripts`),
ShouldEqual, "we are not interested in scripts")
})
Convey("Custom Line Endingd", func() {
So(HTML2TextCustomLine(`list of items<ul><li>One</li><li>Two</li><li>Three</li></ul>`, "<br/>"), ShouldEqual, "list of items<br/>One<br/>Two<br/>Three")
})
})
}