Debian Codegraph @

test/test_transforms/test_strip_elements_with_class.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_strip_elements_with_class.py 9425 2023-06-30 14:56:47Z milde $
4 # Author: Guenter Milde <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for docutils.transforms.universal.StripClassesAndElements.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.transforms.universal import StripClassesAndElements, TestMessages
23 from docutils.utils import new_document
24
25
26 class TransformTestCase(unittest.TestCase):
27 def test_transforms(self):
28 parser = Parser()
29 settings = get_default_settings(Parser)
30 settings.warning_stream = ''
31 settings.strip_elements_with_classes = ['spam', 'no-ham']
32 settings.strip_classes = ['spam', 'noise']
33 for name, (transforms, cases) in totest.items():
34 for casenum, (case_input, case_expected) in enumerate(cases):
35 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
36 document = new_document('test data', settings.copy())
37 parser.parse(case_input, document)
38 # Don't do a ``populate_from_components()`` because that
39 # would enable the Transformer's default transforms.
40 document.transformer.add_transforms(transforms)
41 document.transformer.add_transform(TestMessages)
42 document.transformer.apply_transforms()
43 output = document.pformat()
44 self.assertEqual(case_expected, output)
45
46
47 totest = {}
48
49 totest['strip_spam'] = ((StripClassesAndElements,), [
50 ["""\
51 not classy
52
53 .. class:: spam
54
55 this is spam
56
57 .. class:: ham noise
58
59 this is ham
60
61 .. code::
62 :class: spam
63 \n\
64 print("spam")
65 \n\
66 .. image:: spam.jpg
67 :class: spam
68
69 this is not ham
70 """,
71 """\
72 <document source="test data">
73 <paragraph>
74 not classy
75 <paragraph classes="ham">
76 this is ham
77 <paragraph>
78 this is not ham
79 """],
80 ])
81
82
83 if __name__ == '__main__':
84 unittest.main()