| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Tests for the 'math' directive. |
| 9 |
""" |
| 10 |
|
| 11 |
from pathlib import Path |
| 12 |
import sys |
| 13 |
import unittest |
| 14 |
|
| 15 |
if __name__ == '__main__': |
| 16 |
|
| 17 |
|
| 18 |
sys.path.insert(0, str(Path(__file__).resolve().parents[4])) |
| 19 |
|
| 20 |
from docutils.frontend import get_default_settings |
| 21 |
from docutils.parsers.rst import Parser |
| 22 |
from docutils.utils import new_document |
| 23 |
|
| 24 |
|
| 25 |
class ParserTestCase(unittest.TestCase): |
| 26 |
def test_parser(self): |
| 27 |
parser = Parser() |
| 28 |
settings = get_default_settings(Parser) |
| 29 |
settings.warning_stream = '' |
| 30 |
for name, cases in totest.items(): |
| 31 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 32 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 33 |
document = new_document('test data', settings.copy()) |
| 34 |
parser.parse(case_input, document) |
| 35 |
output = document.pformat() |
| 36 |
self.assertEqual(case_expected, output) |
| 37 |
|
| 38 |
|
| 39 |
totest = {} |
| 40 |
|
| 41 |
totest['argument'] = [ |
| 42 |
["""\ |
| 43 |
.. math:: y = f(x) |
| 44 |
""", |
| 45 |
"""\ |
| 46 |
<document source="test data"> |
| 47 |
<math_block xml:space="preserve"> |
| 48 |
y = f(x) |
| 49 |
"""], |
| 50 |
] |
| 51 |
|
| 52 |
totest['content'] = [ |
| 53 |
["""\ |
| 54 |
.. math:: |
| 55 |
|
| 56 |
1+1=2 |
| 57 |
""", |
| 58 |
"""\ |
| 59 |
<document source="test data"> |
| 60 |
<math_block xml:space="preserve"> |
| 61 |
1+1=2 |
| 62 |
"""], |
| 63 |
] |
| 64 |
|
| 65 |
totest['options'] = [ |
| 66 |
["""\ |
| 67 |
.. math:: |
| 68 |
:class: new |
| 69 |
:name: eq:Eulers law |
| 70 |
|
| 71 |
e^i*2*\\pi = 1 |
| 72 |
""", |
| 73 |
"""\ |
| 74 |
<document source="test data"> |
| 75 |
<math_block classes="new" ids="eq-eulers-law" names="eq:eulers\\ law" xml:space="preserve"> |
| 76 |
e^i*2*\\pi = 1 |
| 77 |
"""], |
| 78 |
] |
| 79 |
|
| 80 |
totest['argument_and_content'] = [ |
| 81 |
["""\ |
| 82 |
.. math:: y = f(x) |
| 83 |
|
| 84 |
1+1=2 |
| 85 |
|
| 86 |
""", |
| 87 |
"""\ |
| 88 |
<document source="test data"> |
| 89 |
<math_block xml:space="preserve"> |
| 90 |
y = f(x) |
| 91 |
<math_block xml:space="preserve"> |
| 92 |
1+1=2 |
| 93 |
"""], |
| 94 |
] |
| 95 |
|
| 96 |
totest['content_with_blank_line'] = [ |
| 97 |
["""\ |
| 98 |
.. math:: |
| 99 |
|
| 100 |
1+1=2 |
| 101 |
|
| 102 |
E = mc^2 |
| 103 |
""", |
| 104 |
"""\ |
| 105 |
<document source="test data"> |
| 106 |
<math_block xml:space="preserve"> |
| 107 |
1+1=2 |
| 108 |
<math_block xml:space="preserve"> |
| 109 |
E = mc^2 |
| 110 |
"""], |
| 111 |
] |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == '__main__': |
| 115 |
unittest.main() |