Formatting changes to facilitate integration of "py3" patchset.
[docutils.git] / docutils / test / test_parsers / test_rst / test_directives / test_unicode.py
blob3ac5aa394af2a51187976b23024e7213b3b13803
1 #! /usr/bin/env python
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests for misc.py "unicode" directive.
9 """
10 from __future__ import absolute_import
12 import sys
14 from . import DocutilsTestSupport
17 if sys.version_info >= (3, 0):
18 unichr = chr # noqa
21 def suite():
22 s = DocutilsTestSupport.ParserTestSuite()
23 s.generateTests(totest)
24 return s
26 unichr_exception = DocutilsTestSupport.exception_data(
27 unichr, int("111111111111111111", 16))[0]
28 if isinstance(unichr_exception, OverflowError):
29 unichr_exception_string = 'code too large (%s)' % unichr_exception
30 else:
31 unichr_exception_string = str(unichr_exception)
33 totest = {}
35 totest['unicode'] = [
36 ["""
37 Insert an em-dash (|mdash|), a copyright symbol (|copy|), a non-breaking
38 space (|nbsp|), a backwards-not-equals (|bne|), and a captial omega (|Omega|).
40 .. |mdash| unicode:: 0x02014
41 .. |copy| unicode:: \\u00A9
42 .. |nbsp| unicode:: &#x000A0;
43 .. |bne| unicode:: U0003D U020E5
44 .. |Omega| unicode:: U+003A9
45 """,
46 u"""\
47 <document source="test data">
48 <paragraph>
49 Insert an em-dash (
50 <substitution_reference refname="mdash">
51 mdash
52 ), a copyright symbol (
53 <substitution_reference refname="copy">
54 copy
55 ), a non-breaking
56 space (
57 <substitution_reference refname="nbsp">
58 nbsp
59 ), a backwards-not-equals (
60 <substitution_reference refname="bne">
61 bne
62 ), and a captial omega (
63 <substitution_reference refname="Omega">
64 Omega
66 <substitution_definition names="mdash">
67 \u2014
68 <substitution_definition names="copy">
69 \u00A9
70 <substitution_definition names="nbsp">
71 \u00A0
72 <substitution_definition names="bne">
74 \u20e5
75 <substitution_definition names="Omega">
76 \u03a9
77 """],
78 ["""
79 Bad input:
81 .. |empty| unicode::
82 .. |empty too| unicode:: .. comment doesn't count as content
83 .. |not hex| unicode:: 0xHEX
84 .. |not all hex| unicode:: UABCX
85 .. unicode:: not in a substitution definition
86 """,
87 """\
88 <document source="test data">
89 <paragraph>
90 Bad input:
91 <system_message level="3" line="4" source="test data" type="ERROR">
92 <paragraph>
93 Error in "unicode" directive:
94 1 argument(s) required, 0 supplied.
95 <literal_block xml:space="preserve">
96 unicode::
97 <system_message level="2" line="4" source="test data" type="WARNING">
98 <paragraph>
99 Substitution definition "empty" empty or invalid.
100 <literal_block xml:space="preserve">
101 .. |empty| unicode::
102 <system_message level="2" line="5" source="test data" type="WARNING">
103 <paragraph>
104 Substitution definition "empty too" empty or invalid.
105 <literal_block xml:space="preserve">
106 .. |empty too| unicode:: .. comment doesn't count as content
107 <substitution_definition names="not\\ hex">
108 0xHEX
109 <substitution_definition names="not\\ all\\ hex">
110 UABCX
111 <system_message level="3" line="8" source="test data" type="ERROR">
112 <paragraph>
113 Invalid context: the "unicode" directive can only be used within a substitution definition.
114 <literal_block xml:space="preserve">
115 .. unicode:: not in a substitution definition
116 """],
117 ["""
118 Testing comments and extra text.
120 Copyright |copy| 2003, |BogusMegaCorp (TM)|.
122 .. |copy| unicode:: 0xA9 .. copyright sign
123 .. |BogusMegaCorp (TM)| unicode:: BogusMegaCorp U+2122
124 .. with trademark sign
125 """,
126 u"""\
127 <document source="test data">
128 <paragraph>
129 Testing comments and extra text.
130 <paragraph>
131 Copyright
132 <substitution_reference refname="copy">
133 copy
134 2003,
135 <substitution_reference refname="BogusMegaCorp (TM)">
136 BogusMegaCorp (TM)
138 <substitution_definition names="copy">
139 \u00A9
140 <substitution_definition names="BogusMegaCorp\\ (TM)">
141 BogusMegaCorp
142 \u2122
143 """],
144 ["""
145 .. |too big for int| unicode:: 0x111111111111111111
146 .. |too big for unicode| unicode:: 0x11111111
147 """,
148 """\
149 <document source="test data">
150 <system_message level="3" line="2" source="test data" type="ERROR">
151 <paragraph>
152 Invalid character code: 0x111111111111111111
153 ValueError: %s
154 <literal_block xml:space="preserve">
155 unicode:: 0x111111111111111111
156 <system_message level="2" line="2" source="test data" type="WARNING">
157 <paragraph>
158 Substitution definition "too big for int" empty or invalid.
159 <literal_block xml:space="preserve">
160 .. |too big for int| unicode:: 0x111111111111111111
161 <system_message level="3" line="3" source="test data" type="ERROR">
162 <paragraph>
163 Invalid character code: 0x11111111
165 <literal_block xml:space="preserve">
166 unicode:: 0x11111111
167 <system_message level="2" line="3" source="test data" type="WARNING">
168 <paragraph>
169 Substitution definition "too big for unicode" empty or invalid.
170 <literal_block xml:space="preserve">
171 .. |too big for unicode| unicode:: 0x11111111
172 """ % (unichr_exception_string,
173 DocutilsTestSupport.exception_data(unichr, int("11111111", 16))[2])]
177 if __name__ == '__main__':
178 import unittest
179 unittest.main(defaultTest='suite')