-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontmatter_parser.test.js
161 lines (141 loc) · 4.79 KB
/
frontmatter_parser.test.js
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
import parseFrontmatter from './frontmatter_parser'
describe('error propagation', () => {
test('throw an error if the first line starts with a colon', () => {
const badFrontmatter = `
: very bad
and then some good: key value pairing
`
expect(() => parseFrontmatter({ frontmatter: badFrontmatter})).toThrow()
})
test('throw an error if the second line starts with a colon', () => {
const badFrontmatter = `
and then some good: key value pairing
: very bad
`
expect(() => parseFrontmatter({ frontmatter: badFrontmatter})).toThrow()
})
test('throw an error if a line has no colon and does not start with a dash', () => {
const badFrontmatter = `
some good: key value pairing
start a list:
- good list
- still good
and then the bad happens
`
expect(() => parseFrontmatter({ frontmatter: badFrontmatter})).toThrow()
})
test('throw an error if a list is started correctly, but the first list item syntax is incorrect', () => {
const badFrontmatter = `
a good list:
- list item
- another one
- great list, bigly best
start a list:
and then it goes wrong
- this list item is okay
`
expect(() => parseFrontmatter({ frontmatter: badFrontmatter})).toThrow()
})
test('throw an error if a list is started correctly, but the second list item syntax is incorrect', () => {
const badFrontmatter = `
a good list:
- list item
- another one
- great list, bigly best
start a list:
- this list item is okay
and then it goes wrong
`
expect(() => parseFrontmatter({ frontmatter: badFrontmatter})).toThrow()
})
test('throw an error if a list item does not have a starter as one of its previous lines', () => {
const badFrontmatter = `
a bad list:
- list item
- another one
and then it goes wrong
`
expect(() => parseFrontmatter({ frontmatter: badFrontmatter})).toThrow()
})
})
describe('valid frontmatter', () => {
test('a key-value pair delimited by a colons is trimmed and converted to a javascript object', () => {
const goodFrontmatter = `
a simple key: value pair
`
const parsedFrontmatter = {
'a simple key': 'value pair',
}
expect(parseFrontmatter({ frontmatter: goodFrontmatter })).toMatchObject(parsedFrontmatter)
})
test('a key-value pair is split only on the first colon', () => {
const goodFrontmatter = `
key: and value: with: colons
`
const parsedFrontmatter = {
key: 'and value: with: colons',
}
expect(parseFrontmatter({ frontmatter: goodFrontmatter })).toMatchObject(parsedFrontmatter)
})
test('key-value pairs are trimmed, converted to a javascript object', () => {
const goodFrontmatter = `
a simple key: value pair
and another key: with it's own value
one more key : for goood luck
`
const parsedFrontmatter = {
'a simple key': 'value pair',
'and another key': 'with it\'s own value',
'one more key': 'for goood luck',
}
expect(parseFrontmatter({ frontmatter: goodFrontmatter })).toMatchObject(parsedFrontmatter)
})
test('lists are named by the string left of the colon and contain items marked by dashes', () => {
const goodFrontmatter = `
a list:
- list item one
- list item two
`
expect(parseFrontmatter({ frontmatter: goodFrontmatter })).toMatchObject({
'a list': ['list item one', 'list item two'],
})
})
test('lists parse correctly when list item contains extra dashes', () => {
const goodFrontmatter = `
a list:
- list item - with some - dashes - one
- list item two
`
expect(parseFrontmatter({ frontmatter: goodFrontmatter })).toMatchObject({
'a list': ['list item - with some - dashes - one', 'list item two'],
})
})
})
test('list items can contain colons (but don\'t create nested objects)', () => {
const goodFrontmatter = `
a list:
- list : with some colons
- testing a list item that ends with a colon:
`
expect(parseFrontmatter({ frontmatter: goodFrontmatter })).toMatchObject({
'a list': ['list : with some colons', 'testing a list item that ends with a colon:'],
})
})
describe('a realistic frontmatter string', () => {
const realisticFrontmatter = `
authors:
- the smoot
- stxalq
date: the year of our lord 2020
description: we write our own frontmatter parser "because we can"
`
const parsedFrontmatter = {
authors: [
'the smoot',
'stxalq',
],
date: 'the year of our lord 2020',
description: 'we write our own frontmatter parser "because we can"',
}
expect(parseFrontmatter({ frontmatter: realisticFrontmatter })).toMatchObject(parsedFrontmatter)
})