sources for test_rst.py [rev. unknown]
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
""" rst generation tests
"""
from py.__.rest.rst import *
from py.__.doc.conftest import restcheck
import traceback
tempdir = py.test.ensuretemp('rest')
def checkrest(rest):
    fname = traceback.extract_stack()[-2][2]
    i = 0
    while True:
        if i == 0:
            filename = '%s.txt' % (fname,)
        else:
            filename = '%s_%s.txt' % (fname, i)
        tempfile = tempdir.join(filename)
        if not tempfile.check():
            break
        i += 1
    tempfile.write(rest)
    restcheck(tempfile)
    return tempfile.new(ext='.html').read()
def test_escape_text():
    txt = Paragraph('*escape* ``test``').text()
    assert txt == '\\*escape\\* \\`\\`test\\`\\`'
    html = checkrest(txt)
    assert '*escape* ``test``' in html
def test_escape_markup_simple():
    txt = Paragraph(Em('*em*')).text()
    assert txt == '*\\*em\\**'
    html = checkrest(txt)
    assert '<em>*em*</em>' in html
def test_escape_underscore():
    txt = Rest(Paragraph('foo [1]_')).text()
    assert txt == "foo [1]\\_\n"
    html = checkrest(txt)
    assert 'foo [1]_' in html
def test_escape_markup_spaces_docutils_nastyness():
    txt = Rest(Paragraph(Em('foo *bar* baz'))).text()
    assert txt == '*foo \\*bar\\* baz*\n'
    html = checkrest(txt)
    assert '<em>foo *bar* baz</em>' in html
def test_escape_literal():
    txt = LiteralBlock('*escape* ``test``').text()
    assert txt == '::\n\n  *escape* ``test``'
    html = checkrest(txt)
    assert '>\n*escape* ``test``\n</pre>' in html
def test_escape_markup_obvious():
    txt = Em('foo*bar').text()
    assert txt == '*foo\\*bar*'
    html = checkrest(txt)
    assert '<em>foo*bar</em>' in html
def test_escape_markup_text_1():
    txt = Paragraph(Em('foo'), "*bar").text()
    assert txt == '*foo* \\*bar'
    html = checkrest(txt)
    assert '<em>foo</em> *bar' in html
def test_escape_markup_text_2():
    txt = Paragraph(Em('foo'), "bar*").text()
    assert txt == '*foo* bar\\*'
    html = checkrest(txt)
    assert '<em>foo</em> bar*' in html
def test_illegal_parent():
    Rest(Paragraph(Text('spam')))
    py.test.raises(RestError, 'Rest(Text("spam"))')
    py.test.raises(RestError, 'ListItem(Paragraph(Text("eggs")))')
def test_text_basic():
    txt = Text("dupa").text()
    assert txt == "dupa"
def test_basic_inline():
    txt = Em('foo').text()
    assert txt == '*foo*'
def test_basic_inline_2():
    txt = Strong('bar').text()
    assert txt == '**bar**'
def test_text_multiple_arguments():
    txt = Paragraph(Text("dupa"), Text("dupa")).text()
    assert txt == "dupa dupa"
def test_text_join():
    txt = Paragraph(Text("worse things"))
    txt = txt.join(Text("happen at sea"), Text("you know"))
    assert txt.text() == "worse things happen at sea you know"
def test_text_add():
    p = Paragraph(Text('grmbl'))
    p2 = p.add(Text('grmbl too'))
    assert p2.text() == 'grmbl too'
    assert p.text() == 'grmbl grmbl too'
def test_paragraph_basic():
    txt = Paragraph(Text('spam')).text()
    assert txt == 'spam'
def test_paragraph_string():
    txt = Paragraph("eggs").text()
    assert txt == "eggs"
    checkrest(txt)
def test_paragraph_join():
    txt = Rest(Paragraph(Text("a")), Paragraph(Text("b"))).text()
    assert txt == "a\n\nb\n"
def test_paragraph_indent():
    txt = Paragraph(Text("a"), indent=" ").text()
    assert txt == " a"
    checkrest(txt)
    txt = Paragraph(Text("  a "), indent=" ").text()
    assert txt == " a"
def test_paragraph_width():
    txt = Paragraph(Text("a b c d e f"), width=3, indent=" ").text()
    assert txt == ' a\n b\n c\n d\n e\n f'
    checkrest(txt)
    text = """
Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Vestibulum malesuada
eleifend leo. Sed faucibus commodo libero. 
Mauris elementum fringilla velit. Ut
sem urna, aliquet sed, molestie at, viverra 
id, justo. In ornare lacinia turpis. Etiam 
et ipsum. Quisque at lacus. Etiam 
pellentesque, enim porta pulvinar viverra, 
libero elit iaculis justo, vitae convallis 
pede purus vel arcu. Morbi aliquam lacus 
et urna. Donec commodo pellentesque mi.
"""
    expected = """\
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum malesuada
eleifend leo. Sed faucibus commodo libero. Mauris elementum fringilla velit. Ut
sem urna, aliquet sed, molestie at, viverra id, justo. In ornare lacinia
turpis. Etiam et ipsum. Quisque at lacus. Etiam pellentesque, enim porta
pulvinar viverra, libero elit iaculis justo, vitae convallis pede purus vel
arcu. Morbi aliquam lacus et urna. Donec commodo pellentesque mi."""
    txt = Paragraph(text, width=80).text()
    print repr(txt)
    print repr(expected)
    assert txt == expected
    checkrest(txt)
def test_paragraph_stripping():
    txt = Paragraph(Text('\n foo bar\t')).text()
    assert txt == 'foo bar'
    checkrest(txt)
def test_blockquote():
    expected = """\
Text
::
  def fun():
   some
Paragraph
"""
    txt = Rest(Paragraph("Text"), LiteralBlock("def fun():\n some"), \
               Paragraph("Paragraph")).text()
    print repr(txt)
    assert txt == expected
    checkrest(txt)
def test_blockquote_empty():
    expected = """\
Foo
Bar
"""
    txt = Rest(Paragraph('Foo'), LiteralBlock(''), Paragraph('Bar')).text()
    print repr(txt)
    assert txt == expected
    checkrest(txt)
def test_title():
    txt = Title(Text("Some title"), belowchar="=").text()
    assert txt == "Some title\n=========="
    checkrest(txt)
    txt =  Title("Some title", belowchar="#", abovechar="#").text()
    assert txt == "##########\nSome title\n##########"
    html = checkrest(txt)
    assert '>Some title</h1>' in html
def test_title_long():
    txt = Title('Some very long title that doesn\'t fit on a single line '
                'but still should not be split into multiple lines').text()
    assert txt == ("Some very long title that doesn't fit on a single line "
                   "but still should not be split into multiple lines\n"
                   "======================================================="
                   "=================================================")
    checkrest(txt)
def test_title_long_with_markup():
    txt = Title('Some very long title with', Em('some markup'),
                'to test whether that works as expected too...').text()
    assert txt == ("Some very long title with *some markup* to test whether "
                   "that works as expected too...\n"
                   "========================================================"
                   "=============================")
    checkrest(txt)
def test_title_escaping():
    txt = Title('foo *bar* baz').text()
    assert txt == 'foo \\*bar\\* baz\n==============='
    checkrest(txt)
def test_link():
    expected = "`some link`_\n\n.. _`some link`: http://codespeak.net\n\n"
    txt = Rest(Paragraph(Link("some link", "http://codespeak.net"))).text()
    assert txt == expected
    html = checkrest(txt)
    assert ' href="http://codespeak.net">some link</a>' in html
def test_link_same_text_and_target():
    txt = Rest(Paragraph(Link('some link', 'bar'), 'foo',
                         Link('some link', 'bar'))
                         ).text()
    expected = '`some link`_ foo `some link`_\n\n.. _`some link`: bar\n\n'
    assert txt == expected
def test_link_same_text_different_target():
    py.test.raises(ValueError, ("Rest(Paragraph(Link('some link', 'foo'),"
                                  "Link('some link', 'bar'))).text()"))
def test_text_multiline():
    expected = """\
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum malesuada
eleifend leo. Sed faucibus commodo libero. Mauris elementum fringilla velit. Ut
sem urna, aliquet sed, molestie at, viverra id, justo. In ornare lacinia
turpis. Etiam et ipsum. Quisque at lacus. Etiam pellentesque, enim porta
pulvinar viverra, libero elit iaculis justo, vitae convallis pede purus vel
arcu. Morbi aliquam lacus et urna. Donec commodo pellentesque mi.
"""
    txt = Rest(Paragraph('Lorem ipsum dolor sit amet, consectetuer adipiscing '
                         'elit. Vestibulum malesuada eleifend leo. Sed '
                         'faucibus commodo libero. Mauris elementum fringilla '
                         'velit. Ut sem urna, aliquet sed, molestie at, '
                         'viverra id, justo. In ornare lacinia turpis. Etiam '
                         'et ipsum. Quisque at lacus. Etiam pellentesque, '
                         'enim porta pulvinar viverra, libero elit iaculis '
                         'justo, vitae convallis pede purus vel arcu. Morbi '
                         'aliquam lacus et urna. Donec commodo pellentesque '
                         'mi.')).text()
    assert txt == expected
    checkrest(txt)
def test_text_indented():
    expected = """\
This is a paragraph with some indentation. The indentation should be removed
and the lines split up nicely. This is still part of the first paragraph.
"""
    txt = Rest(Paragraph('This is a paragraph with some\n'
                         '    indentation. The indentation\n'
                         ' should be removed and the lines split up nicely.\n'
                         '\nThis is still part of the first paragraph.')
                         ).text()
    assert txt == expected
    checkrest(txt)
def test_text_strip():
    expected = "foo\n"
    txt = Rest(Paragraph(Text(' foo '))).text()
    assert txt == expected
    checkrest(txt)
def test_list():
    expected = "* a\n\n* b\n"
    txt = Rest(ListItem("a"), ListItem("b")).text()
    assert txt == expected
    checkrest(txt)
def test_list_item_multiple_args():
    expected = "* foo bar baz\n"
    txt = Rest(ListItem('foo', 'bar', 'baz')).text()
    assert txt == expected
    checkrest(txt)
def test_list_multiline():
    expected = """\
* Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum
  malesuada eleifend leo. Sed faucibus commodo libero.
* Mauris elementum fringilla velit. Ut sem urna, aliquet sed, molestie at,
  viverra id, justo. In ornare lacinia turpis. Etiam et ipsum. Quisque at
  lacus.
* Etiam pellentesque, enim porta pulvinar viverra, libero elit iaculis justo,
  vitae convallis pede purus vel arcu. Morbi aliquam lacus et urna. Donec
  commodo pellentesque mi.
"""
    txt = Rest(ListItem('Lorem ipsum dolor sit amet, consectetuer adipiscing '
                        'elit. Vestibulum malesuada eleifend leo. Sed '
                        'faucibus commodo libero.'),
               ListItem('Mauris elementum fringilla velit. Ut sem urna, '
                        'aliquet sed, molestie at, viverra id, justo. In '
                        'ornare lacinia turpis. Etiam et ipsum. Quisque at '
                        'lacus.'),
               ListItem('Etiam pellentesque, enim porta pulvinar viverra, '
                        'libero elit iaculis justo, vitae convallis pede '
                        'purus vel arcu. Morbi aliquam lacus et urna. Donec '
                        'commodo pellentesque mi.')).text()
    assert txt == expected
    checkrest(txt)
def test_list_multiline_no_parent():
    expected = ("* test **strong**\n  thisisaverylongwordthatdoesntfiton"
                "thepreviouslineandthereforeshouldbeindented")
    txt = ListItem(Text('test'), Strong('strong'),
                   Text('thisisaverylongwordthatdoesntfitontheprevious'
                        'lineandthereforeshouldbeindented')).text()
    assert txt == expected
    checkrest(txt)
def test_ordered_list():
    expected = "#. foo\n\n#. bar\n\n#. baz\n"
    txt = Rest(OrderedListItem('foo'), OrderedListItem('bar'),
               OrderedListItem('baz')).text()
    assert txt == expected
    checkrest(txt)
def test_nested_lists():
    expected = """\
* foo
* bar
  + baz
"""
    txt = Rest(ListItem('foo'), ListItem('bar', ListItem('baz'))).text()
    assert txt == expected
    checkrest(txt)
def test_nested_nested_lists():
    expected = """\
* foo
  + bar
    - baz
  + qux
* quux
"""
    txt = Rest(ListItem('foo', ListItem('bar', ListItem('baz')),
                               ListItem('qux')), ListItem('quux')).text()
    print txt
    assert txt == expected
    checkrest(txt)
def test_definition_list():
    expected = """\
foo
  bar, baz and qux!
spam
  eggs, spam, spam, eggs, spam and spam...
"""
    txt = Rest(DListItem("foo", "bar, baz and qux!"),
               DListItem("spam", "eggs, spam, spam, eggs, spam and spam...")
               ).text()
    assert txt == expected
    checkrest(txt)
def test_nested_dlists():
    expected = """\
foo
  bar baz
  qux
    quux
"""
    txt = Rest(DListItem('foo', 'bar baz', DListItem('qux', 'quux'))).text()
    assert txt == expected
def test_nested_list_dlist():
    expected = """\
* foo
  foobar
    baz
"""
    txt = Rest(ListItem('foo', DListItem('foobar', 'baz'))).text()
    assert txt == expected
def test_transition():
    txt = Rest(Paragraph('foo'), Transition(), Paragraph('bar')).text()
    assert txt == 'foo\n\n%s\n\nbar\n' % ('-' * 79,)
    checkrest(txt)
    txt = Rest(Paragraph('foo'), Transition('+'), Paragraph('bar')).text()
    assert txt == 'foo\n\n%s\n\nbar\n' % ('+' * 79,)
    checkrest(txt)
    py.test.raises(ValueError, 'Rest(Transition(), Paragraph("foo")).text()')
    py.test.raises(ValueError, 'Rest(Paragraph("foo"), Transition()).text()')
def test_directive_simple():
    txt = Rest(Directive('image', 'images/foo.png')).text()
    assert txt == '.. image:: images/foo.png\n'
def test_directive_metadata():
    txt = Rest(Directive('image', 'images/foo.png',
                         width=200, height=150)).text()
    assert txt == ('.. image:: images/foo.png\n   :width: 200\n'
                   '   :height: 150\n')
def test_directive_multiline():
    txt = Rest(Directive('note', ('This is some string that is too long to '
                                  'fit on a single line, so it should be '
                                  'broken up.'))).text()
    assert txt == """\
.. note:: This is some string that is too long to fit on a single line, so it
   should be broken up.
"""
def test_directive_content():
    txt = Rest(Directive('image', 'foo.png', width=200, height=100,
                         content=[Paragraph('Some paragraph content.')])).text()
    assert txt == """\
.. image:: foo.png
   :width: 200
   :height: 100
   Some paragraph content.
"""
def test_title_following_links_empty_line():
    expected = """\
Foo, bar and `baz`_
Spam
====
Spam, eggs and spam.
.. _`baz`: http://www.baz.com
"""
    txt = Rest(Paragraph("Foo, bar and ", Link("baz", "http://www.baz.com")),
               Title('Spam'), Paragraph('Spam, eggs and spam.')).text()
    assert txt == expected
    checkrest(txt)
def test_nonstring_text():
    expected = """\
<foobar>
"""
    class FooBar(object):
        def __str__(self):
            return '<foobar>'
    txt = Rest(Paragraph(Text(FooBar()))).text()
    assert txt == expected