summaryrefslogtreecommitdiffhomepage
path: root/parser/page_test.go
blob: 07d7660d427be5f99f763f43d36df3271bd85df9 (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
package parser

import (
	"fmt"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestPage(t *testing.T) {
	cases := []struct {
		raw string

		content     string
		frontmatter string
		renderable  bool
		metadata    map[string]interface{}
	}{
		{
			testPageLeader + jsonPageFrontMatter + "\n" + testPageTrailer + jsonPageContent,
			jsonPageContent,
			jsonPageFrontMatter,
			true,
			map[string]interface{}{
				"title": "JSON Test 1",
				"social": []interface{}{
					[]interface{}{"a", "#"},
					[]interface{}{"b", "#"},
				},
			},
		},
		{
			testPageLeader + tomlPageFrontMatter + testPageTrailer + tomlPageContent,
			tomlPageContent,
			tomlPageFrontMatter,
			true,
			map[string]interface{}{
				"title": "TOML Test 1",
				"social": []interface{}{
					[]interface{}{"a", "#"},
					[]interface{}{"b", "#"},
				},
			},
		},
		{
			testPageLeader + yamlPageFrontMatter + testPageTrailer + yamlPageContent,
			yamlPageContent,
			yamlPageFrontMatter,
			true,
			map[string]interface{}{
				"title": "YAML Test 1",
				"social": []interface{}{
					[]interface{}{"a", "#"},
					[]interface{}{"b", "#"},
				},
			},
		},
		{
			testPageLeader + orgPageFrontMatter + orgPageContent,
			orgPageContent,
			orgPageFrontMatter,
			true,
			map[string]interface{}{
				"TITLE":      "Org Test 1",
				"categories": []string{"a", "b"},
			},
		},
	}

	for i, c := range cases {
		p := pageMust(ReadFrom(strings.NewReader(c.raw)))
		meta, err := p.Metadata()

		mesg := fmt.Sprintf("[%d]", i)

		require.Nil(t, err, mesg)
		assert.Equal(t, c.content, string(p.Content()), mesg+" content")
		assert.Equal(t, c.frontmatter, string(p.FrontMatter()), mesg+" frontmatter")
		assert.Equal(t, c.renderable, p.IsRenderable(), mesg+" renderable")
		assert.Equal(t, c.metadata, meta, mesg+" metadata")
	}
}

var (
	testWhitespace  = "\t\t\n\n"
	testPageLeader  = "\ufeff" + testWhitespace + "<!--[metadata]>\n"
	testPageTrailer = "\n<![end-metadata]-->\n"

	jsonPageContent     = "# JSON Test\n"
	jsonPageFrontMatter = `{
	"title": "JSON Test 1",
	"social": [
		["a", "#"],
		["b", "#"]
	]
}`

	tomlPageContent     = "# TOML Test\n"
	tomlPageFrontMatter = `+++
title = "TOML Test 1"
social = [
	["a", "#"],
	["b", "#"],
]
+++
`

	yamlPageContent     = "# YAML Test\n"
	yamlPageFrontMatter = `---
title: YAML Test 1
social:
  - - "a"
    - "#"
  - - "b"
    - "#"
---
`

	orgPageContent     = "* Org Test\n"
	orgPageFrontMatter = `#+TITLE: Org Test 1
#+categories: a b
`

	pageHTMLComment = `<!--
	This is a sample comment.
-->
`
)