forked from mattn/godown
-
Notifications
You must be signed in to change notification settings - Fork 1
/
godown_test.go
154 lines (134 loc) · 2.76 KB
/
godown_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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package godown
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"testing"
)
func TestGodown(t *testing.T) {
m, err := filepath.Glob("testdata/*.html")
if err != nil {
t.Fatal(err)
}
sort.Strings(m)
for _, file := range m {
f, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
if err = Convert(&buf, f, nil); err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(file[:len(file)-4] + "md")
if err != nil {
t.Fatal(err)
}
if string(b) != buf.String() {
t.Errorf("(%s):\nwant:\n%s}}}\ngot:\n%s}}}\n", file, string(b), buf.String())
}
f.Close()
}
}
type errReader int
func (e errReader) Read(p []byte) (n int, err error) {
return 0, io.ErrUnexpectedEOF
}
func TestError(t *testing.T) {
var buf bytes.Buffer
var e errReader
err := Convert(&buf, e, nil)
if err == nil {
t.Fatal("should be an error")
}
}
func TestGuessLang(t *testing.T) {
var buf bytes.Buffer
err := Convert(&buf, strings.NewReader(`
<pre>
def do_something():
pass
</pre>
`), &Option{
GuessLang: func(s string) (string, error) { return "python", nil },
})
if err != nil {
t.Fatal(err)
}
want := "```python\ndef do_something():\n pass\n```\n\n\n"
if buf.String() != want {
t.Errorf("\nwant:\n%s}}}\ngot:\n%s}}}\n", want, buf.String())
}
}
func TestGuessLangBq(t *testing.T) {
var buf bytes.Buffer
err := Convert(&buf, strings.NewReader(`
<blockquote class="code">
<b>def</b> do_something():
<i>pass</i>
</blockquote>
`), &Option{
GuessLang: func(s string) (string, error) { return "python", nil },
})
if err != nil {
t.Fatal(err)
}
want := "```python\ndef do_something():\n pass\n```\n\n\n"
if buf.String() != want {
t.Errorf("\nwant:\n%s}}}\ngot:\n%s}}}\n", want, buf.String())
}
}
func TestScript(t *testing.T) {
var buf bytes.Buffer
err := Convert(&buf, strings.NewReader(`
<p>here is script</p>
<script type="text/javascript" src="https://code.jqeury.com/jquery-latest.js"></script>
<script type="text/javascript"><!--
alert(1)
--></script>
`), &Option{
Script: true,
})
if err != nil {
t.Fatal(err)
}
want := `here is script
<script type="text/javascript" src="https://code.jqeury.com/jquery-latest.js"></script>
<script type="text/javascript"><!--
alert(1)
--></script>
`
if buf.String() != want {
t.Errorf("\nwant:\n%s}}}\ngot:\n%s}}}\n", want, buf.String())
}
}
func TestStyle(t *testing.T) {
var buf bytes.Buffer
err := Convert(&buf, strings.NewReader(`
<p>here is style</p>
<style><!--
body {
background-color: red;
}
--></style>
`), &Option{
Style: true,
})
if err != nil {
t.Fatal(err)
}
want := `here is style
<style><!--
body {
background-color: red;
}
--></style>
`
if buf.String() != want {
t.Errorf("\nwant:\n%s}}}\ngot:\n%s}}}\n", want, buf.String())
}
}