forked from eyeseast/python-frontmatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
71 lines (53 loc) · 2.19 KB
/
test.py
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
#!/usr/bin/env python
from __future__ import unicode_literals
from __future__ import print_function
import codecs
import glob
import json
import doctest
import unittest
import six
import frontmatter
class FrontmatterTest(unittest.TestCase):
"""
Tests for parsing various kinds of content and metadata
"""
def test_all_the_tests(self):
"Sanity check that everything in the tests folder loads without errors"
for filename in glob.glob('tests/*'):
frontmatter.load(filename)
def test_with_markdown_content(self):
"Parse frontmatter and only the frontmatter"
post = frontmatter.load('tests/hello-markdown.markdown')
metadata = {'author': 'bob', 'something': 'else', 'test': 'tester'}
for k, v in metadata.items():
self.assertEqual(post[k], v)
def test_unicode_post(self):
"Ensure unicode is parsed correctly"
chinese = frontmatter.load('tests/chinese.txt')
self.assertTrue(isinstance(chinese.content, six.text_type))
# this shouldn't work as ascii, because it's Hanzi
self.assertRaises(UnicodeEncodeError, chinese.content.encode, 'ascii')
def test_no_frontmatter(self):
"This is not a zen exercise."
post = frontmatter.load('tests/no-frontmatter.txt')
with codecs.open('tests/no-frontmatter.txt', 'r', 'utf-8') as f:
content = f.read().strip()
self.assertEqual(post.metadata, {})
self.assertEqual(post.content, content)
def test_empty_frontmatter(self):
"Frontmatter, but no metadata"
post = frontmatter.load('tests/empty-frontmatter.txt')
content = six.text_type("I have frontmatter but no metadata.")
self.assertEqual(post.metadata, {})
self.assertEqual(post.content, content)
def test_to_dict(self):
"Dump a post as a dict, for serializing"
post = frontmatter.load('tests/network-diagrams.markdown')
post_dict = post.to_dict()
for k, v in post.metadata.items():
self.assertEqual(post_dict[k], v)
self.assertEqual(post_dict['content'], post.content)
if __name__ == "__main__":
doctest.testfile('README.md')
unittest.main()