4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
8 Tests for tables.py directives.
11 from __init__
import DocutilsTestSupport
15 from docutils
.parsers
.rst
.directives
import tables
19 s
= DocutilsTestSupport
.ParserTestSuite()
20 s
.generateTests(totest
)
23 mydir
= 'test_parsers/test_rst/test_directives/'
24 utf_16_csv
= os
.path
.join(mydir
, 'utf-16.csv')
25 utf_16_csv_rel
= DocutilsTestSupport
.utils
.relative_path(None, utf_16_csv
)
26 empty_txt
= os
.path
.join(mydir
, 'empty.txt')
28 unichr_exception
= DocutilsTestSupport
.exception_data(
29 unichr, int("9999999999999", 16))[0]
30 if isinstance(unichr_exception
, OverflowError):
31 unichr_exception_string
= 'code too large (%s)' % unichr_exception
33 unichr_exception_string
= str(unichr_exception
)
37 csv_data
= open(utf_16_csv
, 'rb').read()
38 csv_data
= unicode(csv_data
, 'latin1').splitlines()
39 reader
= csv
.reader([tables
.CSVTable
.encode_for_csv(line
+ '\n')
40 for line
in csv_data
])
43 null_bytes_exception
= DocutilsTestSupport
.exception_data(null_bytes
)[0]
49 .. table:: Truth table for "not"
61 <document source="test data">
62 <table classes="custom" ids="tab-truth-not" names="tab:truth.not">
66 <colspec colwidth="5">
67 <colspec colwidth="5">
101 <document source="test data">
104 <colspec colwidth="10">
105 <colspec colwidth="10">
123 .. table:: title with an *error
130 <document source="test data">
134 <problematic ids="id2" refid="id1">
138 <colspec colwidth="6">
139 <colspec colwidth="5">
148 <system_message backrefs="id2" ids="id1" level="2" line="1" source="test data" type="WARNING">
150 Inline emphasis start-string without end-string.
153 .. table:: Not a table.
158 <document source="test data">
159 <system_message level="3" line="1" source="test data" type="ERROR">
161 Error parsing content block for the "table" directive: exactly one table expected.
162 <literal_block xml:space="preserve">
163 .. table:: Not a table.
171 <document source="test data">
172 <system_message level="2" line="1" source="test data" type="WARNING">
174 Content block expected for the "table" directive; none found.
175 <literal_block xml:space="preserve">
180 totest
['csv-table'] = [
182 .. csv-table:: inline with integral header
187 "Treat", "Quantity", "Description"
188 "Albatross", 2.99, "On a stick!"
189 "Crunchy Frog", 1.49, "If we took the bones out, it wouldn\'t be
190 crunchy, now would it?"
191 "Gannet Ripple", 1.99, "On a stick!"
194 <document source="test data">
197 inline with integral header
199 <colspec colwidth="10" stub="1">
200 <colspec colwidth="20">
201 <colspec colwidth="30">
233 If we took the bones out, it wouldn't be
234 crunchy, now would it?
247 .. csv-table:: inline with separate header
248 :header: "Treat", Quantity, "Description"
251 "Albatross", 2.99, "On a stick!"
254 <document source="test data">
257 inline with separate header
259 <colspec colwidth="10">
260 <colspec colwidth="20">
261 <colspec colwidth="30">
286 .. csv-table:: complex internal structure
287 :header: "Treat", Quantity, "
295 * Albatross", 2.99, "On a stick!"
298 <document source="test data">
301 complex internal structure
303 <colspec colwidth="33">
304 <colspec colwidth="33">
305 <colspec colwidth="33">
315 <bullet_list bullet="*">
328 <bullet_list bullet="*">
346 .. csv-table:: short rows
352 <document source="test data">
357 <colspec colwidth="33">
358 <colspec colwidth="33">
359 <colspec colwidth="33">
381 .. csv-table:: short rows
384 header col 1, header col 2
389 <document source="test data">
394 <colspec colwidth="33">
395 <colspec colwidth="33">
396 <colspec colwidth="33">
425 .. csv-table:: non-ASCII characters
427 Heiz\xf6lr\xfccksto\xdfabd\xe4mpfung
430 <document source="test data">
435 <colspec colwidth="100">
440 Heiz\xf6lr\xfccksto\xdfabd\xe4mpfung
446 <document source="test data">
447 <system_message level="2" line="1" source="test data" type="WARNING">
449 The "csv-table" directive requires content; none supplied.
450 <literal_block xml:space="preserve">
454 .. csv-table:: insufficient header row data
460 <document source="test data">
461 <system_message level="3" line="1" source="test data" type="ERROR">
463 2 header row(s) specified but only 1 row(s) of data supplied ("csv-table" directive).
464 <literal_block xml:space="preserve">
465 .. csv-table:: insufficient header row data
471 .. csv-table:: insufficient body data
477 <document source="test data">
478 <system_message level="3" line="1" source="test data" type="ERROR">
480 Insufficient data supplied (1 row(s)); no data remaining for table body, required by "csv-table" directive.
481 <literal_block xml:space="preserve">
482 .. csv-table:: insufficient body data
488 .. csv-table:: content and external
494 <document source="test data">
495 <system_message level="3" line="1" source="test data" type="ERROR">
497 "csv-table" directive may not both specify an external file and have content.
498 <literal_block xml:space="preserve">
499 .. csv-table:: content and external
505 .. csv-table:: external file and url
507 :url: http://example.org/bogus.csv
510 <document source="test data">
511 <system_message level="3" line="1" source="test data" type="ERROR">
513 The "file" and "url" options may not be simultaneously specified for the "csv-table" directive.
514 <literal_block xml:space="preserve">
515 .. csv-table:: external file and url
517 :url: http://example.org/bogus.csv
520 .. csv-table:: error in the *title
525 <document source="test data">
529 <problematic ids="id2" refid="id1">
533 <colspec colwidth="33">
534 <colspec colwidth="33">
535 <colspec colwidth="33">
547 <system_message backrefs="id2" ids="id1" level="2" line="1" source="test data" type="WARNING">
549 Inline emphasis start-string without end-string.
552 .. csv-table:: no such file
556 <document source="test data">
557 <system_message level="4" line="1" source="test data" type="SEVERE">
559 Problems with "csv-table" directive path:
560 [Errno 2] No such file or directory: 'bogus.csv'.
561 <literal_block xml:space="preserve">
562 .. csv-table:: no such file
565 # note that this output is rewritten below for certain python versions
567 .. csv-table:: bad URL
571 <document source="test data">
572 <system_message level="4" line="1" source="test data" type="SEVERE">
574 Problems with "csv-table" directive URL "bogus.csv":
575 unknown url type: bogus.csv.
576 <literal_block xml:space="preserve">
577 .. csv-table:: bad URL
581 .. csv-table:: column mismatch
587 <document source="test data">
588 <system_message level="3" line="1" source="test data" type="ERROR">
590 "csv-table" widths do not match the number of columns in table (3).
591 <literal_block xml:space="preserve">
592 .. csv-table:: column mismatch
598 .. csv-table:: bad column widths
603 .. csv-table:: bad column widths
609 <document source="test data">
610 <system_message level="3" line="1" source="test data" type="ERROR">
612 Error in "csv-table" directive:
613 invalid option value: (option: "widths"; value: '10,y,z')
615 <literal_block xml:space="preserve">
616 .. csv-table:: bad column widths
620 <system_message level="3" line="6" source="test data" type="ERROR">
622 Error in "csv-table" directive:
623 invalid option value: (option: "widths"; value: '0 0 0')
624 negative or zero value; must be positive.
625 <literal_block xml:space="preserve">
626 .. csv-table:: bad column widths
630 """ % DocutilsTestSupport
.exception_data(int, "y")[1][0]],
632 .. csv-table:: good delimiter
637 .. csv-table:: good delimiter
642 .. csv-table:: good delimiter
647 .. csv-table:: good delimiter
653 <document source="test data">
658 <colspec colwidth="33">
659 <colspec colwidth="33">
660 <colspec colwidth="33">
676 <colspec colwidth="33">
677 <colspec colwidth="33">
678 <colspec colwidth="33">
694 <colspec colwidth="33">
695 <colspec colwidth="33">
696 <colspec colwidth="33">
712 <colspec colwidth="33">
713 <colspec colwidth="33">
714 <colspec colwidth="33">
728 .. csv-table:: bad delimiter
731 .. csv-table:: bad delimiter
732 :delim: U+9999999999999
735 <document source="test data">
736 <system_message level="3" line="1" source="test data" type="ERROR">
738 Error in "csv-table" directive:
739 invalid option value: (option: "delim"; value: 'multiple')
740 'multiple' invalid; must be a single character or a Unicode code.
741 <literal_block xml:space="preserve">
742 .. csv-table:: bad delimiter
744 <system_message level="3" line="4" source="test data" type="ERROR">
746 Error in "csv-table" directive:
747 invalid option value: (option: "delim"; value: 'U+9999999999999')
749 <literal_block xml:space="preserve">
750 .. csv-table:: bad delimiter
751 :delim: U+9999999999999
752 """ % unichr_exception_string
],
754 .. csv-table:: bad CSV data
759 <document source="test data">
760 <system_message level="3" line="1" source="test data" type="ERROR">
762 Error with CSV data in "csv-table" directive:
763 newline inside string
764 <literal_block xml:space="preserve">
765 .. csv-table:: bad CSV data
770 .. csv-table:: bad CSV header data
771 :header: "bad", \"csv, data
776 <document source="test data">
777 <system_message level="3" line="1" source="test data" type="ERROR">
779 Error with CSV data in "csv-table" directive:
780 newline inside string
781 <literal_block xml:space="preserve">
782 .. csv-table:: bad CSV header data
783 :header: "bad", \"csv, data
788 .. csv-table:: bad encoding
792 (7- and 8-bit text encoded as UTF-16 has lots of null/zero bytes.)
795 <document source="test data">
796 <system_message level="3" line="1" source="test data" type="ERROR">
798 Error with CSV data in "csv-table" directive:
800 <literal_block xml:space="preserve">
801 .. csv-table:: bad encoding
805 (7- and 8-bit text encoded as UTF-16 has lots of null/zero bytes.)
806 """ % (null_bytes_exception
, utf_16_csv
)],
808 .. csv-table:: good encoding
814 <document source="test data">
819 <colspec colwidth="33">
820 <colspec colwidth="33">
821 <colspec colwidth="33">
843 \u00a1On a \u03c3\u03c4\u03b9\u03ba!
853 If we took the b\u00f6nes out, it wouldn\u2019t be
854 crunchy, now would it?
864 \u00bfOn a \u03c3\u03c4\u03b9\u03ba?
867 .. csv-table:: no CSV data
871 <document source="test data">
872 <system_message level="3" line="1" source="test data" type="ERROR">
874 No table data detected in CSV file.
875 <literal_block xml:space="preserve">
876 .. csv-table:: no CSV data
881 totest
['list-table'] = [
883 .. list-table:: list table with integral header
896 - If we took the bones out, it wouldn\'t be
897 crunchy, now would it?
903 <document source="test data">
906 list table with integral header
908 <colspec colwidth="10" stub="1">
909 <colspec colwidth="20">
910 <colspec colwidth="30">
942 If we took the bones out, it wouldn\'t be
943 crunchy, now would it?
961 <document source="test data">
962 <system_message level="3" line="1" source="test data" type="ERROR">
964 Error parsing content block for the "list-table" directive: exactly one bullet list expected.
965 <literal_block xml:space="preserve">
973 * not a second-level bullet list
976 <document source="test data">
977 <system_message level="3" line="1" source="test data" type="ERROR">
979 Error parsing content block for the "list-table" directive: two-level bullet list expected, but row 1 does not contain a second-level bullet list.
980 <literal_block xml:space="preserve">
983 * not a second-level bullet list
988 * - columns not uniform
989 * - first row has one,
993 <document source="test data">
994 <system_message level="3" line="1" source="test data" type="ERROR">
996 Error parsing content block for the "list-table" directive: uniform two-level bullet list expected, but row 2 does not contain the same number of items as row 1 (2 vs 1).
997 <literal_block xml:space="preserve">
1000 * - columns not uniform
1001 * - first row has one,
1002 - second row has two
1008 * - ":widths:" option doesn't match columns
1011 <document source="test data">
1012 <system_message level="3" line="1" source="test data" type="ERROR">
1014 "list-table" widths do not match the number of columns in table (1).
1015 <literal_block xml:space="preserve">
1019 * - ":widths:" option doesn\'t match columns
1029 <document source="test data">
1030 <system_message level="3" line="1" source="test data" type="ERROR">
1032 3 stub column(s) specified but only 2 columns(s) of data supplied ("list-table" directive).
1033 <literal_block xml:space="preserve">
1048 <document source="test data">
1049 <system_message level="3" line="1" source="test data" type="ERROR">
1051 Insufficient data supplied (2 columns(s)); no data remaining for table body, required by "list-table" directive.
1052 <literal_block xml:space="preserve">
1060 .. list-table:: empty
1063 <document source="test data">
1064 <system_message level="3" line="1" source="test data" type="ERROR">
1066 The "list-table" directive is empty; content required.
1067 <literal_block xml:space="preserve">
1068 .. list-table:: empty
1074 # Rewrite csv tests that depend on the output of IOError as it is
1075 # platform-dependent before python 2.4 for a unicode path.
1076 # Here only needed for python 2.3 on non-windows
1078 if sys
.version_info
< (2, 4) and not sys
.platform
.startswith('win'):
1079 for i
in range(len(totest
['csv-table'])):
1080 if totest
['csv-table'][i
][1].find("u'bogus.csv'") != -1:
1081 totest
['csv-table'][i
][1] = totest
['csv-table'][i
][1].replace(
1082 "u'bogus.csv'", "'bogus.csv'")
1085 if __name__
== '__main__':
1087 unittest
.main(defaultTest
='suite')