| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for misc.py "unicode" directive. |
| 9 |
""" |
| 10 |
|
| 11 |
|
| 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 |
class ParserTestCase(unittest.TestCase): |
| 27 |
def test_parser(self): |
| 28 |
parser = Parser() |
| 29 |
settings = get_default_settings(Parser) |
| 30 |
settings.warning_stream = '' |
| 31 |
for name, cases in totest.items(): |
| 32 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 33 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 34 |
document = new_document('test data', settings.copy()) |
| 35 |
parser.parse(case_input, document) |
| 36 |
output = document.pformat() |
| 37 |
self.assertEqual(case_expected, output) |
| 38 |
|
| 39 |
|
| 40 |
try: |
| 41 |
chr(0x111111111111111111) |
| 42 |
except OverflowError as unichr_exception: |
| 43 |
unichr_exception_string = f'code too large ({unichr_exception})' |
| 44 |
except Exception as unichr_exception: |
| 45 |
unichr_exception_string = str(unichr_exception) |
| 46 |
else: |
| 47 |
unichr_exception_string = '' |
| 48 |
|
| 49 |
try: |
| 50 |
chr(0x11111111) |
| 51 |
except Exception as detail: |
| 52 |
invalid_char_code = f'{detail.__class__.__name__}: {detail}' |
| 53 |
else: |
| 54 |
invalid_char_code = '' |
| 55 |
|
| 56 |
totest = {} |
| 57 |
|
| 58 |
totest['unicode'] = [ |
| 59 |
[""" |
| 60 |
Insert an em-dash (|mdash|), a copyright symbol (|copy|), a non-breaking |
| 61 |
space (|nbsp|), a backwards-not-equals (|bne|), and a captial omega (|Omega|). |
| 62 |
|
| 63 |
.. |mdash| unicode:: 0x02014 |
| 64 |
.. |copy| unicode:: \\u00A9 |
| 65 |
.. |nbsp| unicode::   |
| 66 |
.. |bne| unicode:: U0003D U020E5 |
| 67 |
.. |Omega| unicode:: U+003A9 |
| 68 |
""", |
| 69 |
"""\ |
| 70 |
<document source="test data"> |
| 71 |
<paragraph> |
| 72 |
Insert an em-dash ( |
| 73 |
<substitution_reference refname="mdash"> |
| 74 |
mdash |
| 75 |
), a copyright symbol ( |
| 76 |
<substitution_reference refname="copy"> |
| 77 |
copy |
| 78 |
), a non-breaking |
| 79 |
space ( |
| 80 |
<substitution_reference refname="nbsp"> |
| 81 |
nbsp |
| 82 |
), a backwards-not-equals ( |
| 83 |
<substitution_reference refname="bne"> |
| 84 |
bne |
| 85 |
), and a captial omega ( |
| 86 |
<substitution_reference refname="Omega"> |
| 87 |
Omega |
| 88 |
). |
| 89 |
<substitution_definition names="mdash"> |
| 90 |
\u2014 |
| 91 |
<substitution_definition names="copy"> |
| 92 |
\u00A9 |
| 93 |
<substitution_definition names="nbsp"> |
| 94 |
\u00A0 |
| 95 |
<substitution_definition names="bne"> |
| 96 |
= |
| 97 |
\u20e5 |
| 98 |
<substitution_definition names="Omega"> |
| 99 |
\u03a9 |
| 100 |
"""], |
| 101 |
[""" |
| 102 |
Bad input: |
| 103 |
|
| 104 |
.. |empty| unicode:: |
| 105 |
.. |empty too| unicode:: .. comment doesn't count as content |
| 106 |
.. |not hex| unicode:: 0xHEX |
| 107 |
.. |not all hex| unicode:: UABCX |
| 108 |
.. unicode:: not in a substitution definition |
| 109 |
""", |
| 110 |
"""\ |
| 111 |
<document source="test data"> |
| 112 |
<paragraph> |
| 113 |
Bad input: |
| 114 |
<system_message level="3" line="4" source="test data" type="ERROR"> |
| 115 |
<paragraph> |
| 116 |
Error in "unicode" directive: |
| 117 |
1 argument(s) required, 0 supplied. |
| 118 |
<literal_block xml:space="preserve"> |
| 119 |
unicode:: |
| 120 |
<system_message level="2" line="4" source="test data" type="WARNING"> |
| 121 |
<paragraph> |
| 122 |
Substitution definition "empty" empty or invalid. |
| 123 |
<literal_block xml:space="preserve"> |
| 124 |
.. |empty| unicode:: |
| 125 |
<system_message level="2" line="5" source="test data" type="WARNING"> |
| 126 |
<paragraph> |
| 127 |
Substitution definition "empty too" empty or invalid. |
| 128 |
<literal_block xml:space="preserve"> |
| 129 |
.. |empty too| unicode:: .. comment doesn't count as content |
| 130 |
<substitution_definition names="not\\ hex"> |
| 131 |
0xHEX |
| 132 |
<substitution_definition names="not\\ all\\ hex"> |
| 133 |
UABCX |
| 134 |
<system_message level="3" line="8" source="test data" type="ERROR"> |
| 135 |
<paragraph> |
| 136 |
Invalid context: the "unicode" directive can only be used within a substitution definition. |
| 137 |
<literal_block xml:space="preserve"> |
| 138 |
.. unicode:: not in a substitution definition |
| 139 |
"""], |
| 140 |
[""" |
| 141 |
Testing comments and extra text. |
| 142 |
|
| 143 |
Copyright |copy| 2003, |BogusMegaCorp (TM)|. |
| 144 |
|
| 145 |
.. |copy| unicode:: 0xA9 .. copyright sign |
| 146 |
.. |BogusMegaCorp (TM)| unicode:: BogusMegaCorp U+2122 |
| 147 |
.. with trademark sign |
| 148 |
""", |
| 149 |
"""\ |
| 150 |
<document source="test data"> |
| 151 |
<paragraph> |
| 152 |
Testing comments and extra text. |
| 153 |
<paragraph> |
| 154 |
Copyright \n\ |
| 155 |
<substitution_reference refname="copy"> |
| 156 |
copy |
| 157 |
2003, \n\ |
| 158 |
<substitution_reference refname="BogusMegaCorp (TM)"> |
| 159 |
BogusMegaCorp (TM) |
| 160 |
. |
| 161 |
<substitution_definition names="copy"> |
| 162 |
\u00A9 |
| 163 |
<substitution_definition names="BogusMegaCorp\\ (TM)"> |
| 164 |
BogusMegaCorp |
| 165 |
\u2122 |
| 166 |
"""], |
| 167 |
[""" |
| 168 |
.. |too big for int| unicode:: 0x111111111111111111 |
| 169 |
.. |too big for unicode| unicode:: 0x11111111 |
| 170 |
""", |
| 171 |
"""\ |
| 172 |
<document source="test data"> |
| 173 |
<system_message level="3" line="2" source="test data" type="ERROR"> |
| 174 |
<paragraph> |
| 175 |
Invalid character code: 0x111111111111111111 |
| 176 |
ValueError: %s |
| 177 |
<literal_block xml:space="preserve"> |
| 178 |
unicode:: 0x111111111111111111 |
| 179 |
<system_message level="2" line="2" source="test data" type="WARNING"> |
| 180 |
<paragraph> |
| 181 |
Substitution definition "too big for int" empty or invalid. |
| 182 |
<literal_block xml:space="preserve"> |
| 183 |
.. |too big for int| unicode:: 0x111111111111111111 |
| 184 |
<system_message level="3" line="3" source="test data" type="ERROR"> |
| 185 |
<paragraph> |
| 186 |
Invalid character code: 0x11111111 |
| 187 |
%s |
| 188 |
<literal_block xml:space="preserve"> |
| 189 |
unicode:: 0x11111111 |
| 190 |
<system_message level="2" line="3" source="test data" type="WARNING"> |
| 191 |
<paragraph> |
| 192 |
Substitution definition "too big for unicode" empty or invalid. |
| 193 |
<literal_block xml:space="preserve"> |
| 194 |
.. |too big for unicode| unicode:: 0x11111111 |
| 195 |
""" % (unichr_exception_string, invalid_char_code)] |
| 196 |
] |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == '__main__': |
| 200 |
unittest.main() |