| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for misc.py "raw" directive. |
| 9 |
""" |
| 10 |
|
| 11 |
import os.path |
| 12 |
from pathlib import Path |
| 13 |
import sys |
| 14 |
import unittest |
| 15 |
|
| 16 |
if __name__ == '__main__': |
| 17 |
|
| 18 |
|
| 19 |
sys.path.insert(0, str(Path(__file__).resolve().parents[4])) |
| 20 |
|
| 21 |
from docutils.frontend import get_default_settings |
| 22 |
from docutils.parsers.rst import Parser |
| 23 |
from docutils.utils import new_document |
| 24 |
|
| 25 |
|
| 26 |
TEST_ROOT = os.path.abspath(os.path.join(__file__, '..', '..', '..', '..')) |
| 27 |
|
| 28 |
|
| 29 |
class ParserTestCase(unittest.TestCase): |
| 30 |
def test_parser(self): |
| 31 |
parser = Parser() |
| 32 |
settings = get_default_settings(Parser) |
| 33 |
settings.warning_stream = '' |
| 34 |
settings.halt_level = 5 |
| 35 |
for name, cases in totest.items(): |
| 36 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 37 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 38 |
document = new_document('test data', settings.copy()) |
| 39 |
parser.parse(case_input, document) |
| 40 |
output = document.pformat() |
| 41 |
self.assertEqual(case_expected, output) |
| 42 |
|
| 43 |
|
| 44 |
mydir = os.path.join(TEST_ROOT, 'test_parsers/test_rst/test_directives') |
| 45 |
raw1 = os.path.relpath( |
| 46 |
os.path.join(mydir, 'raw1.rst'), |
| 47 |
os.getcwd()).replace('\\', '/') |
| 48 |
utf_16_file = os.path.relpath( |
| 49 |
os.path.join(TEST_ROOT, 'data/utf-16-le-sig.rst'), |
| 50 |
os.getcwd()).replace('\\', '/') |
| 51 |
utf_16_error_str = ("UnicodeDecodeError: 'ascii' codec can't decode byte 0xff " |
| 52 |
"in position 0: ordinal not in range(128)") |
| 53 |
|
| 54 |
totest = {} |
| 55 |
|
| 56 |
totest['raw'] = [ |
| 57 |
["""\ |
| 58 |
.. raw:: html |
| 59 |
|
| 60 |
<span>This is some plain old raw text.</span> |
| 61 |
""", |
| 62 |
"""\ |
| 63 |
<document source="test data"> |
| 64 |
<raw format="html" xml:space="preserve"> |
| 65 |
<span>This is some plain old raw text.</span> |
| 66 |
"""], |
| 67 |
[f"""\ |
| 68 |
.. raw:: html |
| 69 |
:file: {raw1} |
| 70 |
""", |
| 71 |
f"""\ |
| 72 |
<document source="test data"> |
| 73 |
<raw format="html" source="{raw1}" xml:space="preserve"> |
| 74 |
<p>This file is used by <tt>test_raw.py</tt>.</p> |
| 75 |
"""], |
| 76 |
["""\ |
| 77 |
.. raw:: html |
| 78 |
:file: rawfile.html |
| 79 |
:url: http://example.org/ |
| 80 |
""", |
| 81 |
"""\ |
| 82 |
<document source="test data"> |
| 83 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 84 |
<paragraph> |
| 85 |
The "file" and "url" options may not be simultaneously specified for the "raw" directive. |
| 86 |
<literal_block xml:space="preserve"> |
| 87 |
.. raw:: html |
| 88 |
:file: rawfile.html |
| 89 |
:url: http://example.org/ |
| 90 |
"""], |
| 91 |
["""\ |
| 92 |
.. raw:: html |
| 93 |
:file: rawfile.html |
| 94 |
|
| 95 |
<p>Can't have both content and file attribute.</p> |
| 96 |
""", |
| 97 |
"""\ |
| 98 |
<document source="test data"> |
| 99 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 100 |
<paragraph> |
| 101 |
"raw" directive may not both specify an external file and have content. |
| 102 |
<literal_block xml:space="preserve"> |
| 103 |
.. raw:: html |
| 104 |
:file: rawfile.html |
| 105 |
\n\ |
| 106 |
<p>Can't have both content and file attribute.</p> |
| 107 |
"""], |
| 108 |
[r""" |
| 109 |
.. raw:: latex html |
| 110 |
|
| 111 |
\[ \sum_{n=1}^\infty \frac{1}{n} \text{ etc.} \] |
| 112 |
""", |
| 113 |
"""\ |
| 114 |
<document source="test data"> |
| 115 |
<raw format="latex html" xml:space="preserve"> |
| 116 |
\\[ \\sum_{n=1}^\\infty \\frac{1}{n} \\text{ etc.} \\] |
| 117 |
"""], |
| 118 |
[f"""\ |
| 119 |
.. raw:: html |
| 120 |
:file: {utf_16_file} |
| 121 |
:encoding: utf-16 |
| 122 |
""", |
| 123 |
f"""\ |
| 124 |
<document source="test data"> |
| 125 |
<raw format="html" source="{utf_16_file}" xml:space="preserve"> |
| 126 |
Grüße |
| 127 |
"""], |
| 128 |
[f"""\ |
| 129 |
Raw input file is UTF-16-encoded, and is not valid ASCII. |
| 130 |
|
| 131 |
.. raw:: html |
| 132 |
:file: {utf_16_file} |
| 133 |
:encoding: ascii |
| 134 |
""", |
| 135 |
f"""\ |
| 136 |
<document source="test data"> |
| 137 |
<paragraph> |
| 138 |
Raw input file is UTF-16-encoded, and is not valid ASCII. |
| 139 |
<system_message level="3" line="3" source="test data" type="ERROR"> |
| 140 |
<paragraph> |
| 141 |
Problem with "raw" directive: |
| 142 |
{utf_16_error_str} |
| 143 |
<literal_block xml:space="preserve"> |
| 144 |
.. raw:: html |
| 145 |
:file: {utf_16_file} |
| 146 |
:encoding: ascii |
| 147 |
"""], |
| 148 |
["""\ |
| 149 |
.. raw:: html |
| 150 |
:encoding: utf-8 |
| 151 |
|
| 152 |
Should the parser complain becau\xdfe there is no :file:? BUG? |
| 153 |
""", |
| 154 |
"""\ |
| 155 |
<document source="test data"> |
| 156 |
<raw format="html" xml:space="preserve"> |
| 157 |
Should the parser complain becau\xdfe there is no :file:? BUG? |
| 158 |
"""], |
| 159 |
["""\ |
| 160 |
.. raw:: html |
| 161 |
""", |
| 162 |
"""\ |
| 163 |
<document source="test data"> |
| 164 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 165 |
<paragraph> |
| 166 |
Content block expected for the "raw" directive; none found. |
| 167 |
<literal_block xml:space="preserve"> |
| 168 |
.. raw:: html |
| 169 |
"""], |
| 170 |
["""\ |
| 171 |
.. raw:: html |
| 172 |
:file: non-existent.file |
| 173 |
""", |
| 174 |
"""\ |
| 175 |
<document source="test data"> |
| 176 |
<system_message level="3" line="1" source="test data" type="ERROR"> |
| 177 |
<paragraph> |
| 178 |
Problems with "raw" directive path: |
| 179 |
InputError: [Errno 2] No such file or directory: 'non-existent.file'. |
| 180 |
<literal_block xml:space="preserve"> |
| 181 |
.. raw:: html |
| 182 |
:file: non-existent.file |
| 183 |
"""], |
| 184 |
|
| 185 |
] |
| 186 |
|
| 187 |
if __name__ == '__main__': |
| 188 |
unittest.main() |