3
# $Id: test_tables.py 7463 2012-06-22 19:49:51Z milde $
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]
46
reader = csv.reader(['"\n'], strict=1)
49
unexpected_eod_exception = DocutilsTestSupport.exception_data(unexpected_eod)[0]
55
.. table:: Truth table for "not"
67
<document source="test data">
68
<table classes="custom" ids="tab-truth-not" names="tab:truth.not">
72
<colspec colwidth="5">
73
<colspec colwidth="5">
101
========== ==========
104
========== ==========
107
<document source="test data">
110
<colspec colwidth="10">
111
<colspec colwidth="10">
129
.. table:: title with an *error
136
<document source="test data">
140
<problematic ids="id2" refid="id1">
144
<colspec colwidth="6">
145
<colspec colwidth="5">
154
<system_message backrefs="id2" ids="id1" level="2" line="1" source="test data" type="WARNING">
156
Inline emphasis start-string without end-string.
159
.. table:: Not a table.
164
<document source="test data">
165
<system_message level="3" line="1" source="test data" type="ERROR">
167
Error parsing content block for the "table" directive: exactly one table expected.
168
<literal_block xml:space="preserve">
169
.. table:: Not a table.
177
<document source="test data">
178
<system_message level="2" line="1" source="test data" type="WARNING">
180
Content block expected for the "table" directive; none found.
181
<literal_block xml:space="preserve">
186
totest['csv-table'] = [
188
.. csv-table:: inline with integral header
193
"Treat", "Quantity", "Description"
194
"Albatross", 2.99, "On a stick!"
195
"Crunchy Frog", 1.49, "If we took the bones out, it wouldn\'t be
196
crunchy, now would it?"
197
"Gannet Ripple", 1.99, "On a stick!"
200
<document source="test data">
203
inline with integral header
205
<colspec colwidth="10" stub="1">
206
<colspec colwidth="20">
207
<colspec colwidth="30">
239
If we took the bones out, it wouldn't be
240
crunchy, now would it?
253
.. csv-table:: inline with separate header
254
:header: "Treat", Quantity, "Description"
257
"Albatross", 2.99, "On a stick!"
260
<document source="test data">
263
inline with separate header
265
<colspec colwidth="10">
266
<colspec colwidth="20">
267
<colspec colwidth="30">
292
.. csv-table:: complex internal structure
293
:header: "Treat", Quantity, "
301
* Albatross", 2.99, "On a stick!"
304
<document source="test data">
307
complex internal structure
309
<colspec colwidth="33">
310
<colspec colwidth="33">
311
<colspec colwidth="33">
321
<bullet_list bullet="*">
334
<bullet_list bullet="*">
352
.. csv-table:: short rows
358
<document source="test data">
363
<colspec colwidth="33">
364
<colspec colwidth="33">
365
<colspec colwidth="33">
387
.. csv-table:: short rows
390
header col 1, header col 2
395
<document source="test data">
400
<colspec colwidth="33">
401
<colspec colwidth="33">
402
<colspec colwidth="33">
431
.. csv-table:: non-ASCII characters
433
Heiz\xf6lr\xfccksto\xdfabd\xe4mpfung
436
<document source="test data">
441
<colspec colwidth="100">
446
Heiz\xf6lr\xfccksto\xdfabd\xe4mpfung
452
<document source="test data">
453
<system_message level="2" line="1" source="test data" type="WARNING">
455
The "csv-table" directive requires content; none supplied.
456
<literal_block xml:space="preserve">
460
.. csv-table:: insufficient header row data
466
<document source="test data">
467
<system_message level="3" line="1" source="test data" type="ERROR">
469
2 header row(s) specified but only 1 row(s) of data supplied ("csv-table" directive).
470
<literal_block xml:space="preserve">
471
.. csv-table:: insufficient header row data
477
.. csv-table:: insufficient body data
483
<document source="test data">
484
<system_message level="3" line="1" source="test data" type="ERROR">
486
Insufficient data supplied (1 row(s)); no data remaining for table body, required by "csv-table" directive.
487
<literal_block xml:space="preserve">
488
.. csv-table:: insufficient body data
494
.. csv-table:: content and external
500
<document source="test data">
501
<system_message level="3" line="1" source="test data" type="ERROR">
503
"csv-table" directive may not both specify an external file and have content.
504
<literal_block xml:space="preserve">
505
.. csv-table:: content and external
511
.. csv-table:: external file and url
513
:url: http://example.org/bogus.csv
516
<document source="test data">
517
<system_message level="3" line="1" source="test data" type="ERROR">
519
The "file" and "url" options may not be simultaneously specified for the "csv-table" directive.
520
<literal_block xml:space="preserve">
521
.. csv-table:: external file and url
523
:url: http://example.org/bogus.csv
526
.. csv-table:: error in the *title
531
<document source="test data">
535
<problematic ids="id2" refid="id1">
539
<colspec colwidth="33">
540
<colspec colwidth="33">
541
<colspec colwidth="33">
553
<system_message backrefs="id2" ids="id1" level="2" line="1" source="test data" type="WARNING">
555
Inline emphasis start-string without end-string.
558
.. csv-table:: no such file
562
<document source="test data">
563
<system_message level="4" line="1" source="test data" type="SEVERE">
565
Problems with "csv-table" directive path:
566
[Errno 2] No such file or directory: 'bogus.csv'.
567
<literal_block xml:space="preserve">
568
.. csv-table:: no such file
571
# note that this output is rewritten below for certain python versions
573
.. csv-table:: bad URL
577
<document source="test data">
578
<system_message level="4" line="1" source="test data" type="SEVERE">
580
Problems with "csv-table" directive URL "bogus.csv":
581
unknown url type: bogus.csv.
582
<literal_block xml:space="preserve">
583
.. csv-table:: bad URL
587
.. csv-table:: column mismatch
593
<document source="test data">
594
<system_message level="3" line="1" source="test data" type="ERROR">
596
"csv-table" widths do not match the number of columns in table (3).
597
<literal_block xml:space="preserve">
598
.. csv-table:: column mismatch
604
.. csv-table:: bad column widths
609
.. csv-table:: bad column widths
615
<document source="test data">
616
<system_message level="3" line="1" source="test data" type="ERROR">
618
Error in "csv-table" directive:
619
invalid option value: (option: "widths"; value: '10,y,z')
621
<literal_block xml:space="preserve">
622
.. csv-table:: bad column widths
626
<system_message level="3" line="6" source="test data" type="ERROR">
628
Error in "csv-table" directive:
629
invalid option value: (option: "widths"; value: '0 0 0')
630
negative or zero value; must be positive.
631
<literal_block xml:space="preserve">
632
.. csv-table:: bad column widths
636
""" % DocutilsTestSupport.exception_data(int, "y")[1][0]],
638
.. csv-table:: good delimiter
643
.. csv-table:: good delimiter
648
.. csv-table:: good delimiter
653
.. csv-table:: good delimiter
659
<document source="test data">
664
<colspec colwidth="33">
665
<colspec colwidth="33">
666
<colspec colwidth="33">
682
<colspec colwidth="33">
683
<colspec colwidth="33">
684
<colspec colwidth="33">
700
<colspec colwidth="33">
701
<colspec colwidth="33">
702
<colspec colwidth="33">
718
<colspec colwidth="33">
719
<colspec colwidth="33">
720
<colspec colwidth="33">
734
.. csv-table:: bad delimiter
737
.. csv-table:: bad delimiter
738
:delim: U+9999999999999
741
<document source="test data">
742
<system_message level="3" line="1" source="test data" type="ERROR">
744
Error in "csv-table" directive:
745
invalid option value: (option: "delim"; value: 'multiple')
746
'multiple' invalid; must be a single character or a Unicode code.
747
<literal_block xml:space="preserve">
748
.. csv-table:: bad delimiter
750
<system_message level="3" line="4" source="test data" type="ERROR">
752
Error in "csv-table" directive:
753
invalid option value: (option: "delim"; value: 'U+9999999999999')
755
<literal_block xml:space="preserve">
756
.. csv-table:: bad delimiter
757
:delim: U+9999999999999
758
""" % unichr_exception_string],
760
.. csv-table:: bad CSV data
765
<document source="test data">
766
<system_message level="3" line="1" source="test data" type="ERROR">
768
Error with CSV data in "csv-table" directive:
770
<literal_block xml:space="preserve">
771
.. csv-table:: bad CSV data
774
""" % unexpected_eod_exception],
776
.. csv-table:: bad CSV header data
777
:header: "bad", \"csv, data
782
<document source="test data">
783
<system_message level="3" line="1" source="test data" type="ERROR">
785
Error with CSV data in "csv-table" directive:
787
<literal_block xml:space="preserve">
788
.. csv-table:: bad CSV header data
789
:header: "bad", \"csv, data
792
""" % unexpected_eod_exception],
794
.. csv-table:: bad encoding
798
(7- and 8-bit text encoded as UTF-16 has lots of null/zero bytes.)
801
<document source="test data">
802
<system_message level="3" line="1" source="test data" type="ERROR">
804
Error with CSV data in "csv-table" directive:
806
<literal_block xml:space="preserve">
807
.. csv-table:: bad encoding
811
(7- and 8-bit text encoded as UTF-16 has lots of null/zero bytes.)
812
""" % (null_bytes_exception, utf_16_csv)],
814
.. csv-table:: good encoding
820
<document source="test data">
825
<colspec colwidth="33">
826
<colspec colwidth="33">
827
<colspec colwidth="33">
849
\u00a1On a \u03c3\u03c4\u03b9\u03ba!
859
If we took the b\u00f6nes out, it wouldn\u2019t be
860
crunchy, now would it?
870
\u00bfOn a \u03c3\u03c4\u03b9\u03ba?
873
.. csv-table:: no CSV data
877
<document source="test data">
878
<system_message level="3" line="1" source="test data" type="ERROR">
880
No table data detected in CSV file.
881
<literal_block xml:space="preserve">
882
.. csv-table:: no CSV data
887
totest['list-table'] = [
889
.. list-table:: list table with integral header
902
- If we took the bones out, it wouldn\'t be
903
crunchy, now would it?
909
<document source="test data">
912
list table with integral header
914
<colspec colwidth="10" stub="1">
915
<colspec colwidth="20">
916
<colspec colwidth="30">
948
If we took the bones out, it wouldn\'t be
949
crunchy, now would it?
967
<document source="test data">
968
<system_message level="3" line="1" source="test data" type="ERROR">
970
Error parsing content block for the "list-table" directive: exactly one bullet list expected.
971
<literal_block xml:space="preserve">
979
* not a second-level bullet list
982
<document source="test data">
983
<system_message level="3" line="1" source="test data" type="ERROR">
985
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.
986
<literal_block xml:space="preserve">
989
* not a second-level bullet list
994
* - columns not uniform
995
* - first row has one,
999
<document source="test data">
1000
<system_message level="3" line="1" source="test data" type="ERROR">
1002
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).
1003
<literal_block xml:space="preserve">
1006
* - columns not uniform
1007
* - first row has one,
1008
- second row has two
1014
* - ":widths:" option doesn't match columns
1017
<document source="test data">
1018
<system_message level="3" line="1" source="test data" type="ERROR">
1020
"list-table" widths do not match the number of columns in table (1).
1021
<literal_block xml:space="preserve">
1025
* - ":widths:" option doesn\'t match columns
1035
<document source="test data">
1036
<system_message level="3" line="1" source="test data" type="ERROR">
1038
3 stub column(s) specified but only 2 columns(s) of data supplied ("list-table" directive).
1039
<literal_block xml:space="preserve">
1054
<document source="test data">
1055
<system_message level="3" line="1" source="test data" type="ERROR">
1057
Insufficient data supplied (2 columns(s)); no data remaining for table body, required by "list-table" directive.
1058
<literal_block xml:space="preserve">
1066
.. list-table:: empty
1069
<document source="test data">
1070
<system_message level="3" line="1" source="test data" type="ERROR">
1072
The "list-table" directive is empty; content required.
1073
<literal_block xml:space="preserve">
1074
.. list-table:: empty
1079
if __name__ == '__main__':
1081
unittest.main(defaultTest='suite')