summaryrefslogtreecommitdiff
path: root/test/test_parsers/test_rst/test_directives/test_subdocs.py
blob: 0f577a6ada7eecf68dc89592524691f633acc993 (plain)
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
#! /usr/bin/env python

# $Id$
# Author: Lea Wiemann <LeWiemann@gmail.com>
# Copyright: This module has been placed in the public domain.

"""
Tests for the "subdocs" and "docset-root" directives.
"""

import os.path

from __init__ import DocutilsTestSupport

from docutils import utils

def suite():
    s = DocutilsTestSupport.ParserTestSuite(suite_settings={
        # Make sure that the doctitle is still transformed in
        # sub-documents even if doctitle_xform is off.
        'doctitle_xform': False,
        })
    s.generateTests(totest)
    return s

# We have to use absolute paths throughout this test suite.  This is
# because no source_path is set for the test snippets, and therefore
# the docset-root directive will only accept absolute paths.  Since
# the docset-root is absolute, all resulting source attributes will be
# absolute, too.
paths = {}
paths['docset-root'] = utils.normalize_path(os.path.join(
    DocutilsTestSupport.testroot,
    'test_parsers/test_rst/test_directives/subdocs'))
for i in os.listdir(paths['docset-root']): # 'single-1.txt', ...
    paths[i] = utils.normalize_path(os.path.join(paths['docset-root'], i))
# Note that not all files are used in this test suite
# (test_subdocs.py); they are used in other test suites though.

totest = {}

totest['subdocs'] = [
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * single-1.txt
   * single
     -2.txt
   * multi-3.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <section ids="document-1" names="document\\ 1" source="%(single-1.txt)s">
        <title>
            Document 1
        <paragraph>
            Contents of document 1.
    <section ids="document-2" names="document\\ 2" source="%(single-2.txt)s">
        <title>
            Document 2
        <comment xml:space="preserve">
            Comments in front of the document title should be allowed.
    <section ids="document-3-section-a" names="document\\ 3,\\ section\\ a" source="%(multi-3.txt)s">
        <title>
            Document 3, section A
        <paragraph>
            Contents of document 3, section A.
    <section ids="document-3-section-b" names="document\\ 3,\\ section\\ b" source="%(multi-3.txt)s">
        <title>
            Document 3, section B
        <paragraph>
            Contents of document 3, section B.
        <section ids="subsection" names="subsection">
            <title>
                Subsection
            <paragraph>
                Subsection contents.
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocuments:: 

   * single-1.txt
   * single-2.txt

     * single-1.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <section ids="document-1" names="document\\ 1" source="%(single-1.txt)s">
        <title>
            Document 1
        <paragraph>
            Contents of document 1.
    <section ids="document-2" names="document\\ 2" source="%(single-2.txt)s">
        <title>
            Document 2
        <comment xml:space="preserve">
            Comments in front of the document title should be allowed.
        <section ids="id1" names="document\\ 1" source="%(single-1.txt)s">
            <title>
                Document 1
            <paragraph>
                Contents of document 1.
""" % paths],
["""\
.. docset-root:: %(docset-root)s//
.. subdocuments:: 

   * single-1.txt

     * single-2.txt

       * single-1.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <section ids="document-1" names="document\\ 1" source="%(single-1.txt)s">
        <title>
            Document 1
        <paragraph>
            Contents of document 1.
        <section ids="document-2" names="document\\ 2" source="%(single-2.txt)s">
            <title>
                Document 2
            <comment xml:space="preserve">
                Comments in front of the document title should be allowed.
            <section ids="id1" names="document\\ 1" source="%(single-1.txt)s">
                <title>
                    Document 1
                <paragraph>
                    Contents of document 1.
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * *funny*-filename.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <section ids="document-with-a-funny-filename" names="document\\ with\\ a\\ funny\\ filename" source="%(*funny*-filename.txt)s">
        <title>
            Document With a Funny Filename
        <paragraph>
            Document contents.
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * subtitle-test.txt
""" % paths,  # The "Document 1" title must not become a subtitle:
"""\
<document docset_root="%(docset-root)s" source="test data">
    <section ids="a-sub-document-with-a-title-only" names="a\\ sub-document\\ with\\ a\\ title\\ only" source="%(subtitle-test.txt)s">
        <title>
            A Sub-Document With a Title Only
        <section ids="document-1" names="document\\ 1" source="%(single-1.txt)s">
            <title>
                Document 1
            <paragraph>
                Contents of document 1.
""" % paths],
]

totest['subdocs-errors'] = [
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * multi-3.txt

     * single-1.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            cannot have nested sub-documents since "multi-3.txt" contains more than one section
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               * multi-3.txt
            \n\
                 * single-1.txt
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * empty.txt
   * single-1.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error with "subdocs" directive, file "%(empty.txt)s": a sub-document must either have a single document-title, or it must consist of one or more top-level sections and optionally transitions.
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               * empty.txt
               * single-1.txt
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * nonexistent.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error with "subdocs" directive: could not read file "%(docset-root)s/nonexistent.txt": [Errno 2] No such file or directory: \'%(docset-root)s/nonexistent.txt\'
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               * nonexistent.txt
""" % paths],
["""\
.. subdocs::

   * single-1.txt
""" % paths,
"""\
<document source="test data">
    <system_message level="3" line="1" source="test data" type="ERROR">
        <paragraph>
            a doc-set root must be declared using the "docset-root" directive before referencing sub-documents
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               * single-1.txt
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * recursive-1.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <section ids="recursive-document-1" names="recursive\\ document\\ 1" source="%(recursive-1.txt)s">
        <title>
            Recursive Document 1
        <paragraph>
            Document contents.
        <section ids="recursive-document-2" names="recursive\\ document\\ 2" source="%(recursive-2.txt)s">
            <title>
                Recursive Document 2
            <paragraph>
                Document contents.
            <system_message level="3" line="9" source="%(recursive-2.txt)s" type="ERROR">
                <paragraph>
                    Error in "subdocs" directive: Recursive subdocument inclusion: "%(recursive-1.txt)s"
                <literal_block xml:space="preserve">
                    .. subdocs::
                    \n\
                       * recursive-1.txt
""" % paths],
]

totest['subdocs-syntax-errors'] = [
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   *

     * nested
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error with "subdocs" directive, line 6: No bullet allowed here.
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               *
            \n\
                 * nested
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   Paragraph.
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error with "subdocs" directive: must contain a bullet list.
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               Paragraph.
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * single-1.txt

     Paragraph.
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error with "subdocs" directive, line 6: Sub-document specification list items may only contain one nested bullet list
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               * single-1.txt
            \n\
                 Paragraph.
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::
   * single-1.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error in "subdocs" directive:
            no arguments permitted; blank line required before content block.
        <literal_block xml:space="preserve">
            .. subdocs::
               * single-1.txt
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   *
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error with "subdocs" directive, line 4: No empty list items allowed in sub-documents specification.
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               *
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. subdocs::

   * :bad:-file.txt
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            Error with "subdocs" directive, line 4: File names may not start with a colon.
        <literal_block xml:space="preserve">
            .. subdocs::
            \n\
               * :bad:-file.txt
""" % paths],
]

totest['docset-root'] = [
["""\
.. docset-root:: relative/path
""" % paths,
"""\
<document source="test data">
    <system_message level="3" line="1" source="test data" type="ERROR">
        <paragraph>
            relative doc-set roots are prohibited if the document source path (reader.source.source_path) cannot be determined
        <literal_block xml:space="preserve">
            .. docset-root:: relative/path
""" % paths],
["""\
.. docset-root:: %(docset-root)s
.. docset-root:: %(docset-root)s/different
""" % paths,
"""\
<document docset_root="%(docset-root)s" source="test data">
    <system_message level="3" line="2" source="test data" type="ERROR">
        <paragraph>
            given doc-set root ("%(docset-root)s/different") conflicts with previously specified doc-set root ("%(docset-root)s")
        <literal_block xml:space="preserve">
            .. docset-root:: %(docset-root)s/different
""" % paths],
]


if __name__ == '__main__':
    import unittest
    unittest.main(defaultTest='suite')