summaryrefslogtreecommitdiffstats
path: root/hugolib/page_test.go
blob: d55edac6205c356dc914086f6e01df2dac4be1be (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package hugolib

import (
	"html/template"
	"path/filepath"
	"strings"
	"testing"
)

var EMPTY_PAGE = ""

var SIMPLE_PAGE = `---
title: Simple
---
Simple Page
`

var INVALID_FRONT_MATTER_MISSING = `This is a test`

var INVALID_FRONT_MATTER_SHORT_DELIM = `
--
title: Short delim start
---
Short Delim
`

var INVALID_FRONT_MATTER_SHORT_DELIM_ENDING = `
---
title: Short delim ending
--
Short Delim
`

var INVALID_FRONT_MATTER_LEADING_WS = `

 ---
title: Leading WS
---
Leading
`

var SIMPLE_PAGE_JSON = `
{
"title": "spf13-vim 3.0 release and new website",
"description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.",
"tags": [ ".vimrc", "plugins", "spf13-vim", "vim" ],
"date": "2012-04-06",
"categories": [
    "Development",
    "VIM"
],
"slug": "spf13-vim-3-0-release-and-new-website"
}

Content of the file goes Here
`

var SIMPLE_PAGE_JSON_MULTIPLE = `
{
	"title": "foobar",
	"customData": { "foo": "bar" },
	"date": "2012-08-06"
}
Some text
`

var SIMPLE_PAGE_JSON_COMPACT = `
{"title":"foobar","customData":{"foo":"bar"},"date":"2012-08-06"}
Text
`

var SIMPLE_PAGE_NOLAYOUT = `---
title: simple_no_layout
---
No Layout called out`

var SIMPLE_PAGE_LAYOUT_FOOBAR = `---
title: simple layout foobar
layout: foobar
---
Layout foobar`

var SIMPLE_PAGE_TYPE_FOOBAR = `---
type: foobar
---
type foobar`

var SIMPLE_PAGE_TYPE_LAYOUT = `---
type: barfoo
layout: buzfoo
---
type and layout set`

func checkError(t *testing.T, err error, expected string) {
	if err == nil {
		t.Fatalf("err is nil")
	}
	if err.Error() != expected {
		t.Errorf("err.Error() returned: '%s'.  Expected: '%s'", err.Error(), expected)
	}
}

func TestDegenerateEmptyPageZeroLengthName(t *testing.T) {
	_, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "")
	if err == nil {
		t.Fatalf("A zero length page name must return an error")
	}

	checkError(t, err, "Zero length page name")
}

func TestDegenerateEmptyPage(t *testing.T) {
	_, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "test")
	if err == nil {
		t.Fatalf("Expected ReadFrom to return an error when an empty buffer is passed.")
	}

	checkError(t, err, "unable to locate front matter")
}

func checkPageTitle(t *testing.T, page *Page, title string) {
	if page.Title != title {
		t.Fatalf("Page title is: %s.  Expected %s", page.Title, title)
	}
}

func checkPageContent(t *testing.T, page *Page, content string) {
	if page.Content != template.HTML(content) {
		t.Fatalf("Page content is: %s.  Expected %s", page.Content, content)
	}
}

func checkPageType(t *testing.T, page *Page, pageType string) {
	if page.Type() != pageType {
		t.Fatalf("Page type is: %s.  Expected: %s", page.Type(), pageType)
	}
}

func checkPageLayout(t *testing.T, page *Page, layout string) {
	if page.Layout() != layout {
		t.Fatalf("Page layout is: %s.  Expected: %s", page.Layout(), layout)
	}
}

func TestCreateNewPage(t *testing.T) {
	p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple")
	if err != nil {
		t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
	}
	checkPageTitle(t, p, "Simple")
	checkPageContent(t, p, "<p>Simple Page</p>\n")
	checkPageType(t, p, "page")
	checkPageLayout(t, p, "page/single.html")
}

func TestCreatePage(t *testing.T) {
	var tests = []struct {
		r string
	}{
		{SIMPLE_PAGE_JSON},
		{SIMPLE_PAGE_JSON_MULTIPLE},
		//{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)},
	}

	for _, test := range tests {
		if _, err := ReadFrom(strings.NewReader(test.r), "page"); err != nil {
			t.Errorf("Unable to parse page: %s", err)
		}
	}
}

func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) {
	var tests = []struct {
		r   string
		err string
	}{
		{INVALID_FRONT_MATTER_SHORT_DELIM, "unable to match beginning front matter delimiter"},
		{INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "unable to match ending front matter delimiter"},
		{INVALID_FRONT_MATTER_MISSING, "unable to detect front matter"},
	}
	for _, test := range tests {
		_, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim")
		checkError(t, err, test.err)
	}
}

func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
	_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
	if err != nil {
		t.Fatalf("Unable to parse front matter given leading whitespace: %s", err)
	}
}

func TestLayoutOverride(t *testing.T) {
	var (
		path_content_one_dir = filepath.Join("content", "gub", "file1.md")
		path_content_two_dir = filepath.Join("content", "dub", "sub", "file1.md")
		path_content_no_dir  = filepath.Join("content", "file1")
		path_one_directory   = filepath.Join("fub", "file1.md")
		path_no_directory    = filepath.Join("file1.md")
	)
	tests := []struct {
		content        string
		path           string
		expectedLayout string
	}{
		{SIMPLE_PAGE_NOLAYOUT, path_content_two_dir, "sub/single.html"},
		{SIMPLE_PAGE_NOLAYOUT, path_content_one_dir, "gub/single.html"},
		{SIMPLE_PAGE_NOLAYOUT, path_content_no_dir, "page/single.html"},
		{SIMPLE_PAGE_NOLAYOUT, path_one_directory, "fub/single.html"},
		{SIMPLE_PAGE_NOLAYOUT, path_no_directory, "page/single.html"},
		{SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_two_dir, "foobar"},
		{SIMPLE_PAGE_LAYOUT_FOOBAR, path_content_one_dir, "foobar"},
		{SIMPLE_PAGE_LAYOUT_FOOBAR, path_one_directory, "foobar"},
		{SIMPLE_PAGE_LAYOUT_FOOBAR, path_no_directory, "foobar"},
		{SIMPLE_PAGE_TYPE_FOOBAR, path_content_two_dir, "foobar/single.html"},
		{SIMPLE_PAGE_TYPE_FOOBAR, path_content_one_dir, "foobar/single.html"},
		{SIMPLE_PAGE_TYPE_FOOBAR, path_content_no_dir, "foobar/single.html"},
		{SIMPLE_PAGE_TYPE_FOOBAR, path_one_directory, "foobar/single.html"},
		{SIMPLE_PAGE_TYPE_FOOBAR, path_no_directory, "foobar/single.html"},
		{SIMPLE_PAGE_TYPE_LAYOUT, path_content_two_dir, "buzfoo"},
		{SIMPLE_PAGE_TYPE_LAYOUT, path_content_one_dir, "buzfoo"},
		{SIMPLE_PAGE_TYPE_LAYOUT, path_content_no_dir, "buzfoo"},
		{SIMPLE_PAGE_TYPE_LAYOUT, path_one_directory, "buzfoo"},
		{SIMPLE_PAGE_TYPE_LAYOUT, path_no_directory, "buzfoo"},
	}
	for _, test := range tests {
		p, err := ReadFrom(strings.NewReader(test.content), test.path)
		if err != nil {
			t.Fatalf("Unable to parse content:\n%s\n", test.content)
		}
		if p.Layout() != test.expectedLayout {
			t.Errorf("Layout mismatch. Expected: %s, got: %s", test.expectedLayout, p.Layout())
		}
	}
}