Welcome to mirror list, hosted at ThFree Co, Russian Federation.

parse_frontmatter_test.go « parser - github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14e69abfdcc223346b8c58cae5885cd4988e94ce (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

// TODO Support Mac Encoding (\r)

import (
	"bufio"
	"bytes"
	"io"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

const (
	contentNoFrontmatter                        = "a page with no front matter"
	contentWithFrontmatter                      = "---\ntitle: front matter\n---\nContent with front matter"
	contentHTMLNoDoctype                        = "<html>\n\t<body>\n\t</body>\n</html>"
	contentHTMLWithDoctype                      = "<!doctype html><html><body></body></html>"
	contentHTMLWithFrontmatter                  = "---\ntitle: front matter\n---\n<!doctype><html><body></body></html>"
	contentHTML                                 = "    <html><body></body></html>"
	contentLinefeedAndHTML                      = "\n<html><body></body></html>"
	contentIncompleteEndFrontmatterDelim        = "---\ntitle: incomplete end fm delim\n--\nincomplete frontmatter delim"
	contentMissingEndFrontmatterDelim           = "---\ntitle: incomplete end fm delim\nincomplete frontmatter delim"
	contentSlugWorking                          = "---\ntitle: slug doc 2\nslug: slug-doc-2\n\n---\nslug doc 2 content"
	contentSlugWorkingVariation                 = "---\ntitle: slug doc 3\nslug: slug-doc 3\n---\nslug doc 3 content"
	contentSlugBug                              = "---\ntitle: slug doc 2\nslug: slug-doc-2\n---\nslug doc 2 content"
	contentSlugWithJSONFrontMatter              = "{\n  \"categories\": \"d\",\n  \"tags\": [\n    \"a\", \n    \"b\", \n    \"c\"\n  ]\n}\nJSON Front Matter with tags and categories"
	contentWithJSONLooseFrontmatter             = "{\n  \"categories\": \"d\"\n  \"tags\": [\n    \"a\" \n    \"b\" \n    \"c\"\n  ]\n}\nJSON Front Matter with tags and categories"
	contentSlugWithJSONFrontMatterFirstLineOnly = "{\"categories\":\"d\",\"tags\":[\"a\",\"b\",\"c\"]}\nJSON Front Matter with tags and categories"
	contentSlugWithJSONFrontMatterFirstLine     = "{\"categories\":\"d\",\n  \"tags\":[\"a\",\"b\",\"c\"]}\nJSON Front Matter with tags and categories"
)

var lineEndings = []string{"\n", "\r\n"}
var delimiters = []string{"---", "+++"}

func pageMust(p Page, err error) *page {
	if err != nil {
		panic(err)
	}
	return p.(*page)
}

func TestDegenerateCreatePageFrom(t *testing.T) {
	tests := []struct {
		content string
	}{
		{contentMissingEndFrontmatterDelim},
		{contentIncompleteEndFrontmatterDelim},
	}

	for _, test := range tests {
		for _, ending := range lineEndings {
			test.content = strings.Replace(test.content, "\n", ending, -1)
			_, err := ReadFrom(strings.NewReader(test.content))
			if err == nil {
				t.Errorf("Content should return an err:\n%q\n", test.content)
			}
		}
	}
}

func checkPageRender(t *testing.T, p *page, expected bool) {
	if p.render != expected {
		t.Errorf("page.render should be %t, got: %t", expected, p.render)
	}
}

func checkPageFrontMatterIsNil(t *testing.T, p *page, content string, expected bool) {
	if bool(p.frontmatter == nil) != expected {
		t.Logf("\n%q\n", content)
		t.Errorf("page.frontmatter == nil? %t, got %t", expected, p.frontmatter == nil)
	}
}

func checkPageFrontMatterContent(t *testing.T, p *page, frontMatter string) {
	if p.frontmatter == nil {
		return
	}
	if !bytes.Equal(p.frontmatter, []byte(frontMatter)) {
		t.Errorf("frontmatter mismatch\nexp: %q\ngot: %q", frontMatter, p.frontmatter)
	}
}

func checkPageContent(t *testing.T, p *page, expected string) {
	if !bytes.Equal(p.content, []byte(expected)) {
		t.Errorf("content mismatch\nexp: %q\ngot: %q", expected, p.content)
	}
}

func TestStandaloneCreatePageFrom(t *testing.T) {
	tests := []struct {
		content            string
		expectedMustRender bool
		frontMatterIsNil   bool
		frontMatter        string
		bodycontent        string
	}{

		{contentNoFrontmatter, true, true, "", "a page with no front matter"},
		{contentWithFrontmatter, true, false, "---\ntitle: front matter\n---\n", "Content with front matter"},
		{contentHTMLNoDoctype, false, true, "", "<html>\n\t<body>\n\t</body>\n</html>"},
		{contentHTMLWithDoctype, false, true, "", "<!doctype html><html><body></body></html>"},
		{contentHTMLWithFrontmatter, true, false, "---\ntitle: front matter\n---\n", "<!doctype><html><body></body></html>"},
		{contentHTML, false, true, "", "<html><body></body></html>"},
		{contentLinefeedAndHTML, false, true, "", "<html><body></body></html>"},
		{contentSlugWithJSONFrontMatter, true, false, "{\n  \"categories\": \"d\",\n  \"tags\": [\n    \"a\", \n    \"b\", \n    \"c\"\n  ]\n}", "JSON Front Matter with tags and categories"},
		{contentWithJSONLooseFrontmatter, true, false, "{\n  \"categories\": \"d\"\n  \"tags\": [\n    \"a\" \n    \"b\" \n    \"c\"\n  ]\n}", "JSON Front Matter with tags and categories"},
		{contentSlugWithJSONFrontMatterFirstLineOnly, true, false, "{\"categories\":\"d\",\"tags\":[\"a\",\"b\",\"c\"]}", "JSON Front Matter with tags and categories"},
		{contentSlugWithJSONFrontMatterFirstLine, true, false, "{\"categories\":\"d\",\n  \"tags\":[\"a\",\"b\",\"c\"]}", "JSON Front Matter with tags and categories"},
		{contentSlugWorking, true, false, "---\ntitle: slug doc 2\nslug: slug-doc-2\n\n---\n", "slug doc 2 content"},
		{contentSlugWorkingVariation, true, false, "---\ntitle: slug doc 3\nslug: slug-doc 3\n---\n", "slug doc 3 content"},
		{contentSlugBug, true, false, "---\ntitle: slug doc 2\nslug: slug-doc-2\n---\n", "slug doc 2 content"},
	}

	for _, test := range tests {
		for _, ending := range lineEndings {
			test.content = strings.Replace(test.content, "\n", ending, -1)
			test.frontMatter = strings.Replace(test.frontMatter, "\n", ending, -1)
			test.bodycontent = strings.Replace(test.bodycontent, "\n", ending, -1)

			p := pageMust(ReadFrom(strings.NewReader(test.content)))

			checkPageRender(t, p, test.expectedMustRender)
			checkPageFrontMatterIsNil(t, p, test.content, test.frontMatterIsNil)
			checkPageFrontMatterContent(t, p, test.frontMatter)
			checkPageContent(t, p, test.bodycontent)
		}
	}
}

func BenchmarkLongFormRender(b *testing.B) {

	tests := []struct {
		filename string
		buf      []byte
	}{
		{filename: "long_text_test.md"},
	}
	for i, test := range tests {
		path := filepath.FromSlash(test.filename)
		f, err := os.Open(path)
		if err != nil {
			b.Fatalf("Unable to open %s: %s", path, err)
		}
		defer f.Close()
		membuf := new(bytes.Buffer)
		if _, err := io.Copy(membuf, f); err != nil {
			b.Fatalf("Unable to read %s: %s", path, err)
		}
		tests[i].buf = membuf.Bytes()
	}

	b.ResetTimer()

	for i := 0; i <= b.N; i++ {
		for _, test := range tests {
			ReadFrom(bytes.NewReader(test.buf))
		}
	}
}

func TestPageShouldRender(t *testing.T) {
	tests := []struct {
		content  []byte
		expected bool
	}{
		{[]byte{}, false},
		{[]byte{'<'}, false},
		{[]byte{'-'}, true},
		{[]byte("--"), true},
		{[]byte("---"), true},
		{[]byte("---\n"), true},
		{[]byte{'a'}, true},
	}

	for _, test := range tests {
		for _, ending := range lineEndings {
			test.content = bytes.Replace(test.content, []byte("\n"), []byte(ending), -1)
			if render := shouldRender(test.content); render != test.expected {

				t.Errorf("Expected %s to shouldRender = %t, got: %t", test.content, test.expected, render)
			}
		}
	}
}

func TestPageHasFrontMatter(t *testing.T) {
	tests := []struct {
		content  []byte
		expected bool
	}{
		{[]byte{'-'}, false},
		{[]byte("--"), false},
		{[]byte("---"), false},
		{[]byte("---\n"), true},
		{[]byte("---\n"), true},
		{[]byte("--- \n"), true},
		{[]byte("---  \n"), true},
		{[]byte{'a'}, false},
		{[]byte{'{'}, true},
		{[]byte("{\n  "), true},
		{[]byte{'}'}, false},
	}
	for _, test := range tests {
		for _, ending := range lineEndings {
			test.content = bytes.Replace(test.content, []byte("\n"), []byte(ending), -1)
			if isFrontMatterDelim := isFrontMatterDelim(test.content); isFrontMatterDelim != test.expected {
				t.Errorf("Expected %q isFrontMatterDelim = %t,  got: %t", test.content, test.expected, isFrontMatterDelim)
			}
		}
	}
}

func TestExtractFrontMatter(t *testing.T) {

	tests := []struct {
		frontmatter string
		extracted   []byte
		errIsNil    bool
	}{
		{"", nil, false},
		{"-", nil, false},
		{"---\n", nil, false},
		{"---\nfoobar", nil, false},
		{"---\nfoobar\nbarfoo\nfizbaz\n", nil, false},
		{"---\nblar\n-\n", nil, false},
		{"---\nralb\n---\n", []byte("---\nralb\n---\n"), true},
		{"---\neof\n---", []byte("---\neof\n---"), true},
		{"--- \neof\n---", []byte("---\neof\n---"), true},
		{"---\nminc\n---\ncontent", []byte("---\nminc\n---\n"), true},
		{"---\nminc\n---    \ncontent", []byte("---\nminc\n---\n"), true},
		{"---  \nminc\n--- \ncontent", []byte("---\nminc\n---\n"), true},
		{"---\ncnim\n---\ncontent\n", []byte("---\ncnim\n---\n"), true},
		{"---\ntitle: slug doc 2\nslug: slug-doc-2\n---\ncontent\n", []byte("---\ntitle: slug doc 2\nslug: slug-doc-2\n---\n"), true},
		{"---\npermalink: '/blog/title---subtitle.html'\n---\ncontent\n", []byte("---\npermalink: '/blog/title---subtitle.html'\n---\n"), true},
	}

	for _, test := range tests {
		for _, ending := range lineEndings {
			test.frontmatter = strings.Replace(test.frontmatter, "\n", ending, -1)
			test.extracted = bytes.Replace(test.extracted, []byte("\n"), []byte(ending), -1)
			for _, delim := range delimiters {
				test.frontmatter = strings.Replace(test.frontmatter, "---", delim, -1)
				test.extracted = bytes.Replace(test.extracted, []byte("---"), []byte(delim), -1)
				line, err := peekLine(bufio.NewReader(strings.NewReader(test.frontmatter)))
				if err != nil {
					continue
				}
				l, r := determineDelims(line)
				fm, err := extractFrontMatterDelims(bufio.NewReader(strings.NewReader(test.frontmatter)), l, r)
				if (err == nil) != test.errIsNil {
					t.Logf("\n%q\n", string(test.frontmatter))
					t.Errorf("Expected err == nil => %t, got: %t. err: %s", test.errIsNil, err == nil, err)
					continue
				}
				if !bytes.Equal(fm, test.extracted) {
					t.Errorf("Frontmatter did not match:\nexp: %q\ngot: %q", string(test.extracted), fm)
				}
			}
		}
	}
}

func TestExtractFrontMatterDelim(t *testing.T) {
	var (
		noErrExpected = true
		errExpected   = false
	)
	tests := []struct {
		frontmatter string
		extracted   string
		errIsNil    bool
	}{
		{"", "", errExpected},
		{"{", "", errExpected},
		{"{}", "{}", noErrExpected},
		{"{} ", "{}", noErrExpected},
		{"{ } ", "{ }", noErrExpected},
		{"{ { }", "", errExpected},
		{"{ { } }", "{ { } }", noErrExpected},
		{"{ { } { } }", "{ { } { } }", noErrExpected},
		{"{\n{\n}\n}\n", "{\n{\n}\n}", noErrExpected},
		{"{\n  \"categories\": \"d\",\n  \"tags\": [\n    \"a\", \n    \"b\", \n    \"c\"\n  ]\n}\nJSON Front Matter with tags and categories", "{\n  \"categories\": \"d\",\n  \"tags\": [\n    \"a\", \n    \"b\", \n    \"c\"\n  ]\n}", noErrExpected},
		{"{\n  \"categories\": \"d\"\n  \"tags\": [\n    \"a\" \n    \"b\" \n    \"c\"\n  ]\n}\nJSON Front Matter with tags and categories", "{\n  \"categories\": \"d\"\n  \"tags\": [\n    \"a\" \n    \"b\" \n    \"c\"\n  ]\n}", noErrExpected},
		// Issue #3511
		{`{ "title": "{" }`, `{ "title": "{" }`, noErrExpected},
		{`{ "title": "{}" }`, `{ "title": "{}" }`, noErrExpected},
		// Issue #3661
		{`{ "title": "\"" }`, `{ "title": "\"" }`, noErrExpected},
		{`{ "title": "\"{", "other": "\"{}" }`, `{ "title": "\"{", "other": "\"{}" }`, noErrExpected},
		{`{ "title": "\"Foo\"" }`, `{ "title": "\"Foo\"" }`, noErrExpected},
		{`{ "title": "\"Foo\"\"" }`, `{ "title": "\"Foo\"\"" }`, noErrExpected},
		{`{ "url": "http:\/\/example.com\/play\/url?id=1" }`, `{ "url": "http:\/\/example.com\/play\/url?id=1" }`, noErrExpected},
		{`{ "test": "\"New\r\nString\"" }`, `{ "test": "\"New\r\nString\"" }`, noErrExpected},
		{`{ "test": "RTS\/RPG" }`, `{ "test": "RTS\/RPG" }`, noErrExpected},
	}

	for i, test := range tests {
		fm, err := extractFrontMatterDelims(bufio.NewReader(strings.NewReader(test.frontmatter)), []byte("{"), []byte("}"))
		if (err == nil) != test.errIsNil {
			t.Logf("\n%q\n", string(test.frontmatter))
			t.Errorf("[%d] Expected err == nil => %t, got: %t. err: %s", i, test.errIsNil, err == nil, err)
			continue
		}
		if !bytes.Equal(fm, []byte(test.extracted)) {
			t.Logf("\n%q\n", string(test.frontmatter))
			t.Errorf("[%d] Frontmatter did not match:\nexp: %q\ngot:  %q", i, string(test.extracted), fm)
		}
	}
}