-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_test.go
179 lines (139 loc) · 4.54 KB
/
write_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package blawg
import (
"bytes"
"html/template"
"io/ioutil"
"os"
"testing"
"time"
)
var testPostOne = testPost("Post Number One", "", 1066, 5, 22)
var testPostTwo = testPost("Post Number Two", "", 1979, 12, 5)
func TestTemplate(t *testing.T) {
assert := NewAssertions(t)
mainTemplate, _ := template.ParseGlob("testTemplates/*")
mainPost := testPost("Main Post Here", "<p>main post body</p>", 1984, 6, 6)
posts := Posts{
testPost("First Post", "First Post Body", 1979, 12, 5),
testPost("Second Post", "Second Post Body", 1989, 12, 5),
mainPost,
}
var b bytes.Buffer
err := writePost(&b, &mainPost, &posts, mainTemplate)
assert.NotError(err)
s := b.String()
assert.StringContains(s, "<h1>Main Post Here</h1>")
assert.StringContains(s, `<li><a href="/post/1979/12/5/first-post/">First Post</a></li>`)
assert.StringContains(s, `<li><a href="/post/1989/12/5/second-post/">Second Post</a></li>`)
assert.StringContains(s, `<p>main post body</p>`)
assert.StringContains(s, `<time datetime="1984-06-06T07:08" >Jun 06, 1984</time>`)
}
func TestMakePosts(t *testing.T) {
assert := NewAssertions(t)
postOne := testPost("Abba", "First Post Body", 1979, 12, 5)
postTwo := testPost("Second Post", "Second Post Body", 1989, 12, 5)
posts := Posts{
postOne,
postTwo,
}
postTemplate, err := template.New("post").Parse(`<p>{{.Post.Title}}</p>"`)
assert.NotError(err)
makePosts(testSiteDirectory, &posts, postTemplate)
for _, post := range posts {
expectedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
assert.FileExists(expectedFile)
contents, _ := ioutil.ReadFile(expectedFile)
assert.StringContains(string(contents), string(post.Title))
}
tearDownTestSite(t)
}
func TestPostsIndex(t *testing.T) {
assert := NewAssertions(t)
postOne := testPost("Abba", "First Post Body", 1979, 12, 5)
postTwo := testPost("Second Post", "Second Post Body", 1989, 12, 5)
posts := Posts{
postOne,
postTwo,
}
indexTemplate, err := template.New("index").Parse(`<p>{{range .}}{{.Title}}{{end}}</p>"`)
assert.NotError(err)
err = makePostIndex(testSiteDirectory, &posts, indexTemplate)
assert.NotError(err)
assert.FileExists(testSiteDirectory + "/posts/index.html")
}
func TestBuildPostPath(t *testing.T) {
assert := NewAssertions(t)
post := testPostOne
expectedPath := "1066/5/22/post-number-one/"
calculatedPath := post.Path()
assert.StringsEqual(calculatedPath, expectedPath)
}
func TestBuildPostPathEscape(t *testing.T) {
assert := NewAssertions(t)
post := testPost("100% Escape?", "", 1989, 1, 1)
expectedPath := "1989/1/1/100-escape/"
calculatedPath := post.Path()
assert.StringsEqual(calculatedPath, expectedPath)
}
func TestMultiplePostPaths(t *testing.T) {
assert := NewAssertions(t)
posts := []Post{testPostOne, testPostTwo}
paths := paths(posts)
expectedPathOne := "1066/5/22/post-number-one/"
expectedPathTwo := "1979/12/5/post-number-two/"
assert.StringsEqual(paths[0], expectedPathOne)
assert.StringsEqual(paths[1], expectedPathTwo)
}
func TestSavePost(t *testing.T) {
assert := NewAssertions(t)
post := testPostOne
err := os.MkdirAll(testSiteDirectory, os.FileMode(0777))
assert.NotError(err)
err = makePost(testSiteDirectory, &post, nil, stubTemplate())
assert.NotError(err)
expectedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
assert.FileExists(expectedFile)
tearDownTestSite(t)
}
func TestNotSavingUnpublishedPost(t *testing.T) {
assert := NewAssertions(t)
post := testPost("do not publish me", "", 1901, 1, 1)
post.Published = false
err := os.MkdirAll(testSiteDirectory, os.FileMode(0777))
assert.NotError(err)
err = makePost(testSiteDirectory, &post, nil, stubTemplate())
assert.NotError(err)
unexpectedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
assert.FileDoesntExist(unexpectedFile)
tearDownTestSite(t)
}
func tearDownTestSite(t *testing.T) {
err := os.RemoveAll(testSiteDirectory)
if err != nil {
t.Errorf("Could not delete test directory: %s", err)
}
}
func paths(posts []Post) []string {
var paths []string
for _, post := range posts {
paths = append(paths, post.Path())
}
return paths
}
func testPost(title, body string, year, month, day int) Post {
publishTime := time.Date(year, time.Month(month), day, 7, 8, 9, 1, time.Local)
return Post{
Body: template.HTML(body),
Date: publishTime,
Title: template.HTML(title),
TitleText: title,
Metadata: Metadata{
Title: title,
Published: true,
},
}
}
func stubTemplate() (mainTemplate *template.Template) {
mainTemplate, _ = template.New("post").Parse("")
return
}