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