forked from donkirkby/donimoes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_book_parser.py
264 lines (203 loc) · 5.77 KB
/
test_book_parser.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import unittest
from io import StringIO
from book_parser import parse, Styles, ParagraphState, BulletedState, \
ParsingState, DiagramState, MetadataState
class BookParserTest(unittest.TestCase):
def test_paragraph(self):
source = """\
This is a one-line paragraph.
"""
expected_tree = [ParagraphState('This is a one-line paragraph.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_multiline_paragraph(self):
source = """\
This is a
two-line paragraph.
"""
expected_tree = [ParagraphState('This is a two-line paragraph.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_two_paragraphs(self):
source = """\
This is a paragraph.
This is another.
"""
expected_tree = [ParagraphState('This is a paragraph.'),
ParagraphState('This is another.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_long_gap(self):
source = """\
This is a paragraph.
This is another.
"""
expected_tree = [ParagraphState('This is a paragraph.'),
ParagraphState('This is another.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_metadata(self):
source = """\
---
title: Some Title
subtitle: Something else
---
This is a paragraph.
"""
expected_tree = [MetadataState('Some Title'),
ParagraphState('This is a paragraph.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_diagram(self):
source = """\
Indent of four spaces
makes a diagram.
Indent is removed.
"""
expected_diagram = """\
Indent of four spaces
makes a diagram.
Indent is removed.
"""
expected_tree = [DiagramState(expected_diagram)]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_diagram_and_paragraph(self):
source = """\
Short diagram.
Following paragraph.
"""
expected_tree = [DiagramState('Short diagram.\n'),
ParagraphState('Following paragraph.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_links(self):
source = """\
Paragraph with [example link][link].
[link]: http://example.com
"""
expected_tree = [ParagraphState(
'Paragraph with <a href="http://example.com">example link</a>.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_two_links(self):
source = """\
Now [two][first] [links][second].
[first]: http://example.com
[second]: http://bogus.com
"""
expected_tree = [ParagraphState(
'Now <a href="http://example.com">two</a> '
'<a href="http://bogus.com">links</a>.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_bold(self):
source = """\
Paragraph with **emphasized text**.
"""
expected_tree = [
ParagraphState('Paragraph with <b>emphasized text</b>.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_heading(self):
source = """\
# Title #
Some text.
"""
expected_tree = [ParsingState('Title', Styles.Heading1),
ParagraphState('Some text.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_heading2(self):
source = """\
## Title #
Some text.
"""
expected_tree = [ParsingState('Title', Styles.Heading2),
ParagraphState('Some text.')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_numbered(self):
source = """\
Some text.
1. First
2. Second
"""
expected_tree = [ParagraphState('Some text.'),
BulletedState('First', bullet='1'),
BulletedState('Second', bullet='2')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_numbered_multiple_lines(self):
source = """\
Some text.
1. First item
with multiple lines.
2. Second item.
"""
expected_tree = [
ParagraphState('Some text.'),
BulletedState('First item with multiple lines.', bullet='1'),
BulletedState('Second item.', bullet='2')]
tree = parse(source)
self.assertEqual(expected_tree, tree)
def test_raw_text_paragraph(tmpdir):
source = """\
Some text
on two lines.
More text.
"""
tree = parse(source)
converted = StringIO()
for state in tree:
state.write_markdown(converted)
assert converted.getvalue() == source
def test_bullet_list(tmpdir):
source = """\
A paragraph.
* A list item.
* Another item.
More text.
"""
tree = parse(source)
converted = StringIO()
for state in tree:
state.write_markdown(converted)
assert converted.getvalue() == source
def test_linked(tmpdir):
source = """\
A paragraph with a [Link][link] in the middle.
[link]: https://example.com/
"""
tree = parse(source)
converted = StringIO()
for state in tree:
state.write_markdown(converted)
assert converted.getvalue() == source
def test_rule_diagrams():
source = """\
One paragraph
1|2 3
-
5|6 4
Another paragraph
0|0 1
-
2|2 1
Last paragraph
"""
expected_text = """\
One paragraph
![Diagram](images/diagram1.png)
Another paragraph
![Diagram](images/diagram2.png)
Last paragraph
"""
tree = parse(source)
assert len(tree) == 5
tree[1].image_path = 'images/diagram1.png'
tree[3].image_path = 'images/diagram2.png'
converted = StringIO()
for state in tree:
state.write_markdown(converted)
assert converted.getvalue() == expected_text