Debian Codegraph @

test/test_parsers/test_rst/test_directives/test_date.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_date.py 9829 2024-08-01 20:47:11Z aa-turner $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for the misc.py "date" directive.
9 """
10
11 import time
12 from pathlib import Path
13 import sys
14 import unittest
15
16 if __name__ == '__main__':
17 # prepend the "docutils root" to the Python library path
18 # so we import the local `docutils` package.
19 sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
20
21 from docutils.io import _locale_encoding
22 from docutils.frontend import get_default_settings
23 from docutils.parsers.rst import Parser
24 from docutils.utils import new_document
25
26
27 class ParserTestCase(unittest.TestCase):
28 def test_parser(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
31 settings.warning_stream = ''
32 for name, cases in totest.items():
33 for casenum, (case_input, case_expected) in enumerate(cases):
34 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
35 document = new_document('test data', settings.copy())
36 parser.parse(case_input, document)
37 output = document.pformat()
38 self.assertEqual(case_expected, output)
39
40
41 totest = {}
42
43 totest['date'] = [
44 ["""\
45 .. |date| date::
46
47 Today's date is |date|.
48 """,
49 """\
50 <document source="test data">
51 <substitution_definition names="date">
52 %s
53 <paragraph>
54 Today's date is \n\
55 <substitution_reference refname="date">
56 date
57 .
58 """ % time.strftime('%Y-%m-%d')],
59 ["""\
60 .. |date| date:: %a, %d %b %Y
61 """,
62 """\
63 <document source="test data">
64 <substitution_definition names="date">
65 %s
66 """ % time.strftime('%a, %d %b %Y')],
67 ["""\
68 .. date::
69 """,
70 """\
71 <document source="test data">
72 <system_message level="3" line="1" source="test data" type="ERROR">
73 <paragraph>
74 Invalid context: the "date" directive can only be used within a substitution definition.
75 <literal_block xml:space="preserve">
76 .. date::
77 """],
78 ]
79
80 # some locales return non-ASCII characters for names of days or months
81 # ensure the directive handles them correctly
82 if _locale_encoding in ('utf-8', 'utf8', 'latin-1', 'iso-8859-1'):
83 totest['decode_date'] = [
84 ["""\
85 .. |date| date:: täglich
86 """,
87 """\
88 <document source="test data">
89 <substitution_definition names="date">
90 täglich
91 """],
92 ]
93
94 if __name__ == '__main__':
95 unittest.main()