-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_test.go
133 lines (105 loc) · 2.98 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
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
package blawg
import (
"fmt"
"html/template"
"reflect"
"strings"
"testing"
"time"
)
var rawPost = `---
layout: post
title: "example post"
date: 2016-10-15 23:24:01
tags:
- category1
- category2
published: true
description: a description of a post
---
This is the body of the post...
## A sub header
`
var badMetadata = `---
layout: post
title: "example post"
date: 2016-10-15T23:24:01
tags:
- category1
- category2
published: true
---
This is the body of the post
## A sub header
`
func TestSplitNoMeta(t *testing.T) {
_, err := parse(strings.NewReader(`no meta block here!`))
if err.Error() != "no metadata block" {
t.Error("did not get the expected error: ", err)
}
}
func TestParseNoBody(t *testing.T) {
noBody := `---
no body here
or here`
_, err := parse(strings.NewReader(noBody))
if err.Error() != "no end to the metadata block" {
t.Error("did not get the expected error", err.Error())
}
}
func TestParse(t *testing.T) {
assert := NewAssertions(t)
post, err := parse(strings.NewReader(rawPost))
assert.NotError(err)
assert.StringsEqual(post.Layout, "post")
assert.StringsEqual(string(post.Title), "example post")
assert.StringsEqual(post.Description, "a description of a post")
expectedDate, err := time.Parse(dateFormat, "2016-10-15 23:24:01")
if post.Date != expectedDate {
t.Error("Did not get the expected date", post.Date)
}
if !reflect.DeepEqual(post.Categories, []string{"category1", "category2"}) {
t.Error("Did not get the expected categories", post.Categories)
}
assert.True(post.Published, "expected post to be published")
var expectedHTML = `<p>This is the body of the post…</p>
<h2>A sub header</h2>
`
assert.StringsEqual(string(post.Body), expectedHTML)
assert.StringsEqual(post.Description, "a description of a post")
}
func TestTitleTextParse(t *testing.T) {
assert := NewAssertions(t)
titleWithHTML := "the _title_<h1>is BIG</h1>"
rawPost := fmt.Sprintf(`---
title: %s
date: 2016-10-15 23:24:01
---`, titleWithHTML)
post, err := parse(strings.NewReader(rawPost))
assert.NotError(err)
if post.Title != template.HTML("the <em>title</em><h1>is BIG</h1>") {
t.Errorf("did not get the expected title HTML, %s", post.Title)
}
assert.StringsEqual(post.TitleText, "the title is BIG")
}
func TestMetadataParseError(t *testing.T) {
assert := NewAssertions(t)
_, err := parse(strings.NewReader(badMetadata))
assert.ErrorMessage(err, `parsing time "2016-10-15T23:24:01" as "2006-01-02 15:04:05": cannot parse "T23:24:01" as " "`)
}
func TestParsePage(t *testing.T) {
assert := NewAssertions(t)
var rawPage = `---
title: "_example page_"
---
This is the body of the page...
## A sub header`
page, err := parsePage(strings.NewReader(rawPage))
assert.NotError(err)
assert.StringsEqual(string(page.Title), "<em>example page</em>")
assert.StringsEqual(string(page.TitleText), "example page")
var expectedHTML = `<p>This is the body of the page…</p>
<h2>A sub header</h2>
`
assert.StringsEqual(string(page.Body), expectedHTML)
}