-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_test.go
89 lines (79 loc) · 2.58 KB
/
markdown_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
package main
import (
"fmt"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"net/url"
"testing"
)
type MarkdownTestSuite struct {
suite.Suite
}
// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *MarkdownTestSuite) SetupTest() {
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestMarkdownTestSuite(t *testing.T) {
suite.Run(t, new(MarkdownTestSuite))
}
func (suite *MarkdownTestSuite) TestUrlToMarkdownUri() {
base, _ := url.Parse("http://myurl.net/")
suite.T().Run("turns a link into a file system path", func(t *testing.T) {
url, _ := url.Parse("http://myurl.net/about/me/index.html")
name, path := urlToMarkdownUri(*base, *url)
assert.Equal(t, "index.md", name)
assert.Equal(t, "about/me/", path)
})
suite.T().Run("turns a link into a file system path using the last segment as filename", func(t *testing.T) {
url, _ := url.Parse("http://myurl.net/about/me/")
name, path := urlToMarkdownUri(*base, *url)
assert.Equal(t, "me.md", name)
assert.Equal(t, "about/", path)
})
}
func (suite *MarkdownTestSuite) TestSaveToMarkdownFile() {
base, _ := url.Parse("http://myurl.net/")
fs := afero.NewMemMapFs()
config := _config{*base, "test", fs}
url, _ := url.Parse("http://myurl.net/about/me/")
body := `
<html>
<head></head>
<body>
<div>
<h1 id="welcome">Welcome</h1>
<p>to this real world example</p>
<ul>
<li>of a typical html</li>
<li>document</li>
</ul>
<pre>
<code>
and some source code
</code>
</pre>
<blockquote>
<p>
and a quote <br>
that needs to be here <br>
and <strong><em>whatnot</em></strong>
</p>
</blockquote>
</div>
</body>
</html>
`
expected := "\n# Welcome\nto this real world example\n* of a typical html\n* document\n`\nand some source code\n`\nand a quote\nthat needs to be here\nand **_whatnot_**\n"
suite.T().Run("converts an html string to markdown and saves it to the filesystem for a given url", func(t *testing.T) {
saveMarkdownFile(config, *url, body)
_, err := fs.Stat("test/about/me.md")
assert.Nil(t, err, fmt.Sprintf("an error occured %v", err))
bytes, err := afero.ReadFile(fs, "test/about/me.md")
assert.Nil(t, err, fmt.Sprintf("an error occured %v", err))
body := string(bytes)
assert.Equal(t, expected, body)
})
}