| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from prettytable import HRuleStyle, PrettyTable, VRuleStyle |
| 4 |
|
| 5 |
|
| 6 |
class TestLatexOutput: |
| 7 |
def test_latex_output(self, helper_table: PrettyTable) -> None: |
| 8 |
assert helper_table.get_latex_string() == ( |
| 9 |
"\\begin{tabular}{cccc}\r\n" |
| 10 |
" & Field 1 & Field 2 & Field 3 \\\\\r\n" |
| 11 |
"1 & value 1 & value2 & value3 \\\\\r\n" |
| 12 |
"4 & value 4 & value5 & value6 \\\\\r\n" |
| 13 |
"7 & value 7 & value8 & value9 \\\\\r\n" |
| 14 |
"\\end{tabular}" |
| 15 |
) |
| 16 |
options = {"fields": ["Field 1", "Field 3"]} |
| 17 |
assert helper_table.get_latex_string(**options) == ( |
| 18 |
"\\begin{tabular}{cc}\r\n" |
| 19 |
"Field 1 & Field 3 \\\\\r\n" |
| 20 |
"value 1 & value3 \\\\\r\n" |
| 21 |
"value 4 & value6 \\\\\r\n" |
| 22 |
"value 7 & value9 \\\\\r\n" |
| 23 |
"\\end{tabular}" |
| 24 |
) |
| 25 |
|
| 26 |
def test_latex_output_formatted(self, helper_table: PrettyTable) -> None: |
| 27 |
assert helper_table.get_latex_string(format=True) == ( |
| 28 |
"\\begin{tabular}{|c|c|c|c|}\r\n" |
| 29 |
"\\hline\r\n" |
| 30 |
" & Field 1 & Field 2 & Field 3 \\\\\r\n" |
| 31 |
"1 & value 1 & value2 & value3 \\\\\r\n" |
| 32 |
"4 & value 4 & value5 & value6 \\\\\r\n" |
| 33 |
"7 & value 7 & value8 & value9 \\\\\r\n" |
| 34 |
"\\hline\r\n" |
| 35 |
"\\end{tabular}" |
| 36 |
) |
| 37 |
|
| 38 |
options = {"fields": ["Field 1", "Field 3"]} |
| 39 |
assert helper_table.get_latex_string(format=True, **options) == ( |
| 40 |
"\\begin{tabular}{|c|c|}\r\n" |
| 41 |
"\\hline\r\n" |
| 42 |
"Field 1 & Field 3 \\\\\r\n" |
| 43 |
"value 1 & value3 \\\\\r\n" |
| 44 |
"value 4 & value6 \\\\\r\n" |
| 45 |
"value 7 & value9 \\\\\r\n" |
| 46 |
"\\hline\r\n" |
| 47 |
"\\end{tabular}" |
| 48 |
) |
| 49 |
|
| 50 |
vrule_options: dict[str, VRuleStyle] = {"vrules": VRuleStyle.FRAME} |
| 51 |
assert helper_table.get_latex_string(format=True, **vrule_options) == ( |
| 52 |
"\\begin{tabular}{|cccc|}\r\n" |
| 53 |
"\\hline\r\n" |
| 54 |
" & Field 1 & Field 2 & Field 3 \\\\\r\n" |
| 55 |
"1 & value 1 & value2 & value3 \\\\\r\n" |
| 56 |
"4 & value 4 & value5 & value6 \\\\\r\n" |
| 57 |
"7 & value 7 & value8 & value9 \\\\\r\n" |
| 58 |
"\\hline\r\n" |
| 59 |
"\\end{tabular}" |
| 60 |
) |
| 61 |
|
| 62 |
hrule_options: dict[str, HRuleStyle] = {"hrules": HRuleStyle.ALL} |
| 63 |
assert helper_table.get_latex_string(format=True, **hrule_options) == ( |
| 64 |
"\\begin{tabular}{|c|c|c|c|}\r\n" |
| 65 |
"\\hline\r\n" |
| 66 |
" & Field 1 & Field 2 & Field 3 \\\\\r\n" |
| 67 |
"\\hline\r\n" |
| 68 |
"1 & value 1 & value2 & value3 \\\\\r\n" |
| 69 |
"\\hline\r\n" |
| 70 |
"4 & value 4 & value5 & value6 \\\\\r\n" |
| 71 |
"\\hline\r\n" |
| 72 |
"7 & value 7 & value8 & value9 \\\\\r\n" |
| 73 |
"\\hline\r\n" |
| 74 |
"\\end{tabular}" |
| 75 |
) |
| 76 |
|
| 77 |
def test_latex_output_header(self, helper_table: PrettyTable) -> None: |
| 78 |
assert helper_table.get_latex_string(format=True, hrules=HRuleStyle.HEADER) == ( |
| 79 |
"\\begin{tabular}{|c|c|c|c|}\r\n" |
| 80 |
" & Field 1 & Field 2 & Field 3 \\\\\r\n" |
| 81 |
"\\hline\r\n" |
| 82 |
"1 & value 1 & value2 & value3 \\\\\r\n" |
| 83 |
"4 & value 4 & value5 & value6 \\\\\r\n" |
| 84 |
"7 & value 7 & value8 & value9 \\\\\r\n" |
| 85 |
"\\end{tabular}" |
| 86 |
) |
| 87 |
|
| 88 |
def test_internal_border_preserved_latex(self, helper_table: PrettyTable) -> None: |
| 89 |
helper_table.border = False |
| 90 |
helper_table.format = True |
| 91 |
helper_table.preserve_internal_border = True |
| 92 |
|
| 93 |
assert helper_table.get_latex_string().strip() == ( |
| 94 |
"\\begin{tabular}{c|c|c|c}\r\n" |
| 95 |
" & Field 1 & Field 2 & Field 3 \\\\\r\n" |
| 96 |
"1 & value 1 & value2 & value3 \\\\\r\n" |
| 97 |
"4 & value 4 & value5 & value6 \\\\\r\n" |
| 98 |
"7 & value 7 & value8 & value9 \\\\\r\n" |
| 99 |
"\\end{tabular}" |
| 100 |
) |