Drop 2.4 and 2.5 compatibility code, part 2.
[docutils.git] / docutils / test / test_parsers / test_rst / test_directives / test_raw.py
blobb8eeb308133e5dcdd40b8d455370cf8ac03d29ef
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 "raw" directive.
9 """
11 import os.path
12 import sys
13 from __init__ import DocutilsTestSupport
15 def suite():
16 s = DocutilsTestSupport.ParserTestSuite()
17 s.generateTests(totest)
18 return s
20 mydir = 'test_parsers/test_rst/test_directives/'
21 raw1 = os.path.join(mydir, 'raw1.txt')
22 utf_16_file = os.path.join(mydir, 'utf-16.csv')
23 utf_16_file_rel = DocutilsTestSupport.utils.relative_path(None, utf_16_file)
24 utf_16_error_str = ("UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe "
25 "in position 0: ordinal not in range(128)")
26 if sys.version_info < (3,0):
27 utf_16_error_str = ("UnicodeError: Unable to decode input data. "
28 "Tried the following encodings: 'ascii'.\n"
29 " (%s)" % utf_16_error_str)
31 totest = {}
33 totest['raw'] = [
34 ["""\
35 .. raw:: html
37 <span>This is some plain old raw text.</span>
38 """,
39 """\
40 <document source="test data">
41 <raw format="html" xml:space="preserve">
42 <span>This is some plain old raw text.</span>
43 """],
44 ["""\
45 .. raw:: html
46 :file: %s
47 """ % raw1,
48 """\
49 <document source="test data">
50 <raw format="html" source="%s" xml:space="preserve">
51 <p>This file is used by <tt>test_raw.py</tt>.</p>
52 """ % DocutilsTestSupport.utils.relative_path(None, raw1)],
53 ["""\
54 .. raw:: html
55 :file: rawfile.html
56 :url: http://example.org/
57 """,
58 """\
59 <document source="test data">
60 <system_message level="3" line="1" source="test data" type="ERROR">
61 <paragraph>
62 The "file" and "url" options may not be simultaneously specified for the "raw" directive.
63 <literal_block xml:space="preserve">
64 .. raw:: html
65 :file: rawfile.html
66 :url: http://example.org/
67 """],
68 ["""\
69 .. raw:: html
70 :file: rawfile.html
72 <p>Can't have both content and file attribute.</p>
73 """,
74 """\
75 <document source="test data">
76 <system_message level="3" line="1" source="test data" type="ERROR">
77 <paragraph>
78 "raw" directive may not both specify an external file and have content.
79 <literal_block xml:space="preserve">
80 .. raw:: html
81 :file: rawfile.html
83 <p>Can't have both content and file attribute.</p>
84 """],
85 [r"""
86 .. raw:: latex html
88 \[ \sum_{n=1}^\infty \frac{1}{n} \text{ etc.} \]
89 """,
90 """\
91 <document source="test data">
92 <raw format="latex html" xml:space="preserve">
93 \\[ \\sum_{n=1}^\\infty \\frac{1}{n} \\text{ etc.} \\]
94 """],
95 ["""\
96 .. raw:: html
97 :file: %s
98 :encoding: utf-16
99 """ % utf_16_file_rel,
100 b"""\
101 <document source="test data">
102 <raw format="html" source="%s" xml:space="preserve">
103 "Treat", "Quantity", "Description"
104 "Albatr\xb0\xdf", 2.99, "\xa1On a \\u03c3\\u03c4\\u03b9\\u03ba!"
105 "Crunchy Frog", 1.49, "If we took the b\xf6nes out, it wouldn\\u2019t be
106 crunchy, now would it?"
107 "Gannet Ripple", 1.99, "\xbfOn a \\u03c3\\u03c4\\u03b9\\u03ba?"
108 """.decode('raw_unicode_escape') % utf_16_file_rel],
109 ["""\
110 Raw input file is UTF-16-encoded, and is not valid ASCII.
112 .. raw:: html
113 :file: %s
114 :encoding: ascii
115 """ % utf_16_file_rel,
116 """\
117 <document source="test data">
118 <paragraph>
119 Raw input file is UTF-16-encoded, and is not valid ASCII.
120 <system_message level="4" line="3" source="test data" type="SEVERE">
121 <paragraph>
122 Problem with "raw" directive:
124 <literal_block xml:space="preserve">
125 .. raw:: html
126 :file: %s
127 :encoding: ascii
128 """ % (utf_16_error_str, utf_16_file_rel)],
129 [u"""\
130 .. raw:: html
131 :encoding: utf-8
133 Should the parser complain becau\xdfe there is no :file:? BUG?
134 """,
135 """\
136 <document source="test data">
137 <raw format="html" xml:space="preserve">
138 Should the parser complain becau\xdfe there is no :file:? BUG?
139 """],
140 ["""\
141 .. raw:: html
142 """,
143 """\
144 <document source="test data">
145 <system_message level="3" line="1" source="test data" type="ERROR">
146 <paragraph>
147 Content block expected for the "raw" directive; none found.
148 <literal_block xml:space="preserve">
149 .. raw:: html
150 """],
151 ["""\
152 .. raw:: html
153 :file: non-existent.file
154 """,
155 """\
156 <document source="test data">
157 <system_message level="4" line="1" source="test data" type="SEVERE">
158 <paragraph>
159 Problems with "raw" directive path:
160 InputError: [Errno 2] No such file or directory: 'non-existent.file'.
161 <literal_block xml:space="preserve">
162 .. raw:: html
163 :file: non-existent.file
164 """],
165 # note that this output is rewritten below for certain python versions
168 if __name__ == '__main__':
169 import unittest
170 unittest.main(defaultTest='suite')