-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_docverter.py
executable file
·74 lines (58 loc) · 2.59 KB
/
test_docverter.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
72
73
74
#!/usr/bin/env python
import os
import tempfile
import unittest
import docverter
def _test_converter(to, format=None, extra_args=()):
def reader(*args, **kwargs):
return source, format
def processor(*args):
return 'ok'
source = 'foo'
return docverter._convert(
reader, processor, source, to, format, extra_args)
class TestDocverter(unittest.TestCase):
def test_converts_valid_format(self):
self.assertEqual(_test_converter(format='md', to='rest'), 'ok')
def test_does_not_convert_to_invalid_format(self):
try:
_test_converter(format='md', to='invalid')
except RuntimeError:
pass
def test_does_not_convert_from_invalid_format(self):
try:
_test_converter(format='invalid', to='rest')
except RuntimeError:
pass
def test_basic_conversion_from_file(self):
# This will not work on windows:
# http://docs.python.org/2/library/tempfile.html
with tempfile.NamedTemporaryFile('w+t', suffix='.md',
delete=False) as test_file:
file_name = test_file.name
print('test_file = %r' % test_file)
print('file_name = %r' % file_name)
test_file.write('#some title\n')
test_file.flush()
expected = 'some title{0}=========={0}{0}'.format(os.linesep)
received = docverter.convert(file_name, 'rst')
self.assertEqualExceptForNewlineEnd(expected, received)
def test_basic_conversion_from_file_with_format(self):
# This will not work on windows:
# http://docs.python.org/2/library/tempfile.html
with tempfile.NamedTemporaryFile('w+t', suffix='.rst',
delete=False) as test_file:
file_name = test_file.name
print('test_file = %r' % test_file)
print('file_name = %r' % file_name)
test_file.write('#some title\n')
test_file.flush()
expected = 'some title{0}=========={0}{0}'.format(os.linesep)
received = docverter.convert(file_name, 'rst', format='md')
self.assertEqualExceptForNewlineEnd(expected, received)
def test_basic_conversion_from_string(self):
expected = 'some title{0}=========={0}{0}'.format(os.linesep)
received = docverter.convert('#some title', 'rst', format='md')
self.assertEqualExceptForNewlineEnd(expected, received)
def assertEqualExceptForNewlineEnd(self, expected, received):
self.assertEqual(expected.rstrip('\n'), received.rstrip('\n'))