-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
166 lines (145 loc) · 3.98 KB
/
parser_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
package main
import (
"fmt"
"os"
"slices"
"strings"
"testing"
)
const testPageTitle = "Test Page"
const testPageBody = "Test page body."
const testPostTitle = "Test Post"
const testPostBody = "Test post body."
const testEmbed1Type = Vimeo
const testEmbed1Code = "1234567890"
const testEmbed2Type = YouTube
const testEmbed2Code = "a_BcDeFgHiJ-x"
const testEmbed3Type = YouTube
const testEmbed3Code = "A_bCdEfGhIj-X"
const testEmbed1 = "http://vimeo.com/" + testEmbed1Code
const testEmbed2 = "http://youtu.be/" + testEmbed2Code
const testEmbed3 = "http://www.youtube.com/watch?v=" + testEmbed3Code
const testPageLinkPlaceholder = "{%page:sample-page-1%}"
const testPageLinkURI = "/page/sample-page-1" + contentFileExtension
const testPostLinkPlaceholder = "{%post:sample-post-1%}"
const testPostLinkURI = "/post/sample-post-1" + contentFileExtension
const tag1 = "tag1"
const tag2 = "Tag2"
const tag3 = "TAG3"
const pageContentTemplate = `---
title: %s
---
%s
{embed:%s}
{embed:%s}
{embed:%s}
[Page Link](%s)
[Post Link](%s)
`
const postContentTemplate = `---
title: %s
tags:
- %s
- %s
- %s
---
%s
{embed:%s}
{embed:%s}
{embed:%s}
[Page Link](%s)
[Post Link](%s)
`
var testPageContent = fmt.Sprintf(pageContentTemplate,
testPageTitle,
testPageBody,
testEmbed1,
testEmbed2,
testEmbed3,
testPageLinkPlaceholder,
testPostLinkPlaceholder,
)
var testPostContent = fmt.Sprintf(postContentTemplate,
testPostTitle,
tag1,
tag2,
tag3,
testPostBody,
testEmbed1,
testEmbed2,
testEmbed3,
testPageLinkPlaceholder,
testPostLinkPlaceholder,
)
func TestParser(t *testing.T) {
expectedEmbeddedMedia := []embeddedMedia{
{
MediaType: testEmbed1Type,
Code: testEmbed1Code,
},
{
MediaType: testEmbed2Type,
Code: testEmbed2Code,
},
{
MediaType: testEmbed3Type,
Code: testEmbed3Code,
},
}
expectedTags := []string{tag1, tag2, tag3}
defaultThemeTemplatesDir := fmt.Sprintf("%s%c%s%c%s", "themes", os.PathSeparator, defaultThemeName, os.PathSeparator, "templates")
resLoader := resourceLoader{
config: defaultConfig(),
loadTemplate: func(templateFileName string) ([]byte, error) {
templateFilePath := fmt.Sprintf("%s%c%s", defaultThemeTemplatesDir, os.PathSeparator, templateFileName)
return os.ReadFile(templateFilePath)
},
loadInclude: func(includeFileName string, level templateIncludeLevel) ([]byte, error) {
return nil, nil
},
}
config := defaultConfig()
page := parsePage("page", testPageContent, config, resLoader)
verifyStringsEqual(page.Title, testPageTitle, t)
verifyStringContains(page.Body, testPageBody, t)
verifyEmbeddedMedia(page.Body, expectedEmbeddedMedia, t)
verifyStringContains(page.Body, testPageLinkURI, t)
verifyStringContains(page.Body, testPostLinkURI, t)
post := parsePost("post", testPostContent, config, resLoader)
verifyStringsEqual(post.Title, testPostTitle, t)
verifyStringContains(post.Body, testPostBody, t)
verifyEmbeddedMedia(post.Body, expectedEmbeddedMedia, t)
verifyStringContains(post.Body, testPageLinkURI, t)
verifyStringContains(post.Body, testPostLinkURI, t)
verifyStringSlicesEqual(post.Tags, expectedTags, t)
}
func verifyStringsEqual(value string, expected string, t *testing.T) {
if value != expected {
t.Errorf("Expected: %s / Found: %s", expected, value)
}
}
func verifyStringContains(value string, expected string, t *testing.T) {
if !strings.Contains(value, expected) {
t.Errorf("Expected to contain: %s / Found: %s", expected, value)
}
}
func verifyStringSlicesEqual(values []string, expected []string, t *testing.T) {
if !slices.Equal(values, expected) {
t.Errorf("Expected: %s / Found: %s", expected, values)
}
}
func verifyEmbeddedMedia(body string, expected []embeddedMedia, t *testing.T) {
for _, em := range expected {
var url string
switch em.MediaType {
case YouTube:
url = "https://www.youtube.com/embed/"
case Vimeo:
url = "https://player.vimeo.com/video/"
}
url += em.Code
if !strings.Contains(body, url) {
t.Errorf("Expected embedded media not found for: %s", em)
}
}
}