# Copyright Kevin Deldycke <kevin@deldycke.com> and contributors.
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from __future__ import annotations
import csv
import pytest
import tabulate
from extra_platforms import is_windows
from click_extra import (
Color,
TableFormat,
echo,
pass_context,
style,
table_format_option,
)
from click_extra.pytest import command_decorators
asciidoc_table = (
'[cols="8<,13<",options="header"]\n'
"|====\n"
"| Day | Temperature \n"
"| 1 | 42.9 \n"
"| 2 | \n"
"| Friday | Hot ๐ฅต \n"
"|====\n"
)
csv_table = """\
Day,Temperature\r
1,42.9\r
2,\r
Friday,Hot ๐ฅต\r
"""
csv_excel_table = csv_table
csv_excel_tab_table = """\
Day\tTemperature\r
1\t42.9\r
2\t\r
Friday\tHot ๐ฅต\r
"""
csv_unix_table = """\
"Day","Temperature"
"1","42.9"
"2",""
"Friday","Hot ๐ฅต"
"""
double_grid_table = """\
โโโโโโโโโโฆโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โ โโโโโโโโโฌโโโโโโโโโโโโโโฃ
โ 1 โ 42.9 โ
โ โโโโโโโโโฌโโโโโโโโโโโโโโฃ
โ 2 โ โ
โ โโโโโโโโโฌโโโโโโโโโโโโโโฃ
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโฉโโโโโโโโโโโโโโ
"""
double_outline_table = """\
โโโโโโโโโโฆโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โ โโโโโโโโโฌโโโโโโโโโโโโโโฃ
โ 1 โ 42.9 โ
โ 2 โ โ
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโฉโโโโโโโโโโโโโโ
"""
fancy_grid_table = """\
โโโโโโโโโโคโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โโโโโโโโโโชโโโโโโโโโโโโโโก
โ 1 โ 42.9 โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 2 โ โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโงโโโโโโโโโโโโโโ
"""
fancy_outline_table = """\
โโโโโโโโโโคโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โโโโโโโโโโชโโโโโโโโโโโโโโก
โ 1 โ 42.9 โ
โ 2 โ โ
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโงโโโโโโโโโโโโโโ
"""
github_table = """\
| Day | Temperature |
| ------ | ----------- |
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
"""
grid_table = """\
+--------+-------------+
| Day | Temperature |
+========+=============+
| 1 | 42.9 |
+--------+-------------+
| 2 | |
+--------+-------------+
| Friday | Hot ๐ฅต |
+--------+-------------+
"""
heavy_grid_table = """\
โโโโโโโโโโณโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โฃโโโโโโโโโโโโโโโโโโโโโโโซ
โ 1 โ 42.9 โ
โฃโโโโโโโโโโโโโโโโโโโโโโโซ
โ 2 โ โ
โฃโโโโโโโโโโโโโโโโโโโโโโโซ
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโปโโโโโโโโโโโโโโ
"""
heavy_outline_table = """\
โโโโโโโโโโณโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โฃโโโโโโโโโโโโโโโโโโโโโโโซ
โ 1 โ 42.9 โ
โ 2 โ โ
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโปโโโโโโโโโโโโโโ
"""
html_table = """\
<table>
<thead>
<tr><th>Day </th><th>Temperature</th></tr>
</thead>
<tbody>
<tr><td>1 </td><td>42.9 </td></tr>
<tr><td>2 </td><td> </td></tr>
<tr><td>Friday</td><td>Hot ๐ฅต </td></tr>
</tbody>
</table>
"""
jira_table = """\
|| Day || Temperature ||
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
"""
latex_table = """\
\\begin{tabular}{ll}
\\hline
Day & Temperature \\\\
\\hline
1 & 42.9 \\\\
2 & \\\\
Friday & Hot ๐ฅต \\\\
\\hline
\\end{tabular}
"""
latex_booktabs_table = """\
\\begin{tabular}{ll}
\\toprule
Day & Temperature \\\\
\\midrule
1 & 42.9 \\\\
2 & \\\\
Friday & Hot ๐ฅต \\\\
\\bottomrule
\\end{tabular}
"""
latex_longtable_table = """\
\\begin{longtable}{ll}
\\hline
Day & Temperature \\\\
\\hline
\\endhead
1 & 42.9 \\\\
2 & \\\\
Friday & Hot ๐ฅต \\\\
\\hline
\\end{longtable}
"""
latex_raw_table = """\
\\begin{tabular}{ll}
\\hline
Day & Temperature \\\\
\\hline
1 & 42.9 \\\\
2 & \\\\
Friday & Hot ๐ฅต \\\\
\\hline
\\end{tabular}
"""
mediawiki_table = """\
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
! Day !! Temperature
|-
| 1 || 42.9
|-
| 2 ||
|-
| Friday || Hot ๐ฅต
|}
"""
mixed_grid_table = """\
โโโโโโโโโโฏโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โโโโโโโโโโฟโโโโโโโโโโโโโโฅ
โ 1 โ 42.9 โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 2 โ โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโทโโโโโโโโโโโโโโ
"""
mixed_outline_table = """\
โโโโโโโโโโฏโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โโโโโโโโโโฟโโโโโโโโโโโโโโฅ
โ 1 โ 42.9 โ
โ 2 โ โ
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโทโโโโโโโโโโโโโโ
"""
moinmoin_table = """\
|| ''' Day ''' || ''' Temperature ''' ||
|| 1 || 42.9 ||
|| 2 || ||
|| Friday || Hot ๐ฅต ||
"""
orgtbl_table = """\
| Day | Temperature |
|--------+-------------|
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
"""
outline_table = """\
+--------+-------------+
| Day | Temperature |
+========+=============+
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
+--------+-------------+
"""
pipe_table = """\
| Day | Temperature |
|:-------|:------------|
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
"""
plain_table = """\
Day Temperature
1 42.9
2
Friday Hot ๐ฅต
"""
presto_table = """\
Day | Temperature
--------+-------------
1 | 42.9
2 |
Friday | Hot ๐ฅต
"""
pretty_table = """\
+--------+-------------+
| Day | Temperature |
+--------+-------------+
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
+--------+-------------+
"""
psql_table = """\
+--------+-------------+
| Day | Temperature |
|--------+-------------|
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
+--------+-------------+
"""
rounded_grid_table = """\
โญโโโโโโโโโฌโโโโโโโโโโโโโโฎ
โ Day โ Temperature โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 1 โ 42.9 โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 2 โ โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ Friday โ Hot ๐ฅต โ
โฐโโโโโโโโโดโโโโโโโโโโโโโโฏ
"""
rounded_outline_table = """\
โญโโโโโโโโโฌโโโโโโโโโโโโโโฎ
โ Day โ Temperature โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 1 โ 42.9 โ
โ 2 โ โ
โ Friday โ Hot ๐ฅต โ
โฐโโโโโโโโโดโโโโโโโโโโโโโโฏ
"""
rst_table = """\
====== ===========
Day Temperature
====== ===========
1 42.9
2
Friday Hot ๐ฅต
====== ===========
"""
simple_table = """\
Day Temperature
------ -----------
1 42.9
2
Friday Hot ๐ฅต
"""
simple_grid_table = """\
โโโโโโโโโโฌโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 1 โ 42.9 โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 2 โ โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโดโโโโโโโโโโโโโโ
"""
simple_outline_table = """\
โโโโโโโโโโฌโโโโโโโโโโโโโโ
โ Day โ Temperature โ
โโโโโโโโโโผโโโโโโโโโโโโโโค
โ 1 โ 42.9 โ
โ 2 โ โ
โ Friday โ Hot ๐ฅต โ
โโโโโโโโโโดโโโโโโโโโโโโโโ
"""
textile_table = """\
|_. Day |_. Temperature |
|<. 1 |<. 42.9 |
|<. 2 |<. |
|<. Friday |<. Hot ๐ฅต |
"""
tsv_table = "Day \tTemperature\n1 \t42.9\n2\nFriday\tHot ๐ฅต\n"
unsafehtml_table = """\
<table>
<thead>
<tr><th>Day </th><th>Temperature</th></tr>
</thead>
<tbody>
<tr><td>1 </td><td>42.9 </td></tr>
<tr><td>2 </td><td> </td></tr>
<tr><td>Friday</td><td>Hot ๐ฅต </td></tr>
</tbody>
</table>
"""
vertical_table = (
"***************************[ 1. row ]***************************\n"
"Day | 1\n"
"Temperature | 42.9\n"
"***************************[ 2. row ]***************************\n"
"Day | 2\n"
"Temperature | \n"
"***************************[ 3. row ]***************************\n"
"Day | Friday\n"
"Temperature | Hot ๐ฅต\n"
)
youtrack_table = """\
|| Day || Temperature ||
| 1 | 42.9 |
| 2 | |
| Friday | Hot ๐ฅต |
"""
expected_renderings = {
TableFormat.ASCIIDOC: asciidoc_table,
TableFormat.CSV: csv_table,
TableFormat.CSV_EXCEL: csv_excel_table,
TableFormat.CSV_EXCEL_TAB: csv_excel_tab_table,
TableFormat.CSV_UNIX: csv_unix_table,
TableFormat.DOUBLE_GRID: double_grid_table,
TableFormat.DOUBLE_OUTLINE: double_outline_table,
TableFormat.FANCY_GRID: fancy_grid_table,
TableFormat.FANCY_OUTLINE: fancy_outline_table,
TableFormat.GITHUB: github_table,
TableFormat.GRID: grid_table,
TableFormat.HEAVY_GRID: heavy_grid_table,
TableFormat.HEAVY_OUTLINE: heavy_outline_table,
TableFormat.HTML: html_table,
TableFormat.JIRA: jira_table,
TableFormat.LATEX: latex_table,
TableFormat.LATEX_BOOKTABS: latex_booktabs_table,
TableFormat.LATEX_LONGTABLE: latex_longtable_table,
TableFormat.LATEX_RAW: latex_raw_table,
TableFormat.MEDIAWIKI: mediawiki_table,
TableFormat.MIXED_GRID: mixed_grid_table,
TableFormat.MIXED_OUTLINE: mixed_outline_table,
TableFormat.MOINMOIN: moinmoin_table,
TableFormat.ORGTBL: orgtbl_table,
TableFormat.OUTLINE: outline_table,
TableFormat.PIPE: pipe_table,
TableFormat.PLAIN: plain_table,
TableFormat.PRESTO: presto_table,
TableFormat.PRETTY: pretty_table,
TableFormat.PSQL: psql_table,
TableFormat.ROUNDED_GRID: rounded_grid_table,
TableFormat.ROUNDED_OUTLINE: rounded_outline_table,
TableFormat.RST: rst_table,
TableFormat.SIMPLE: simple_table,
TableFormat.SIMPLE_GRID: simple_grid_table,
TableFormat.SIMPLE_OUTLINE: simple_outline_table,
TableFormat.TEXTILE: textile_table,
TableFormat.TSV: tsv_table,
TableFormat.UNSAFEHTML: unsafehtml_table,
TableFormat.VERTICAL: vertical_table,
TableFormat.YOUTRACK: youtrack_table,
}
[docs]
@pytest.mark.parametrize("cmd_decorator", command_decorators(no_groups=True))
@pytest.mark.parametrize(
"option_decorator", (table_format_option, table_format_option())
)
@pytest.mark.parametrize(
("format_name", "expected"),
(pytest.param(k, v, id=str(k)) for k, v in expected_renderings.items()),
)
def test_all_table_rendering(
invoke, cmd_decorator, option_decorator, format_name, expected
):
@cmd_decorator
@option_decorator
@pass_context
def table_cli(ctx):
format_id = ctx.meta["click_extra.table_format"]
echo(f"Table format: {format_id}")
headers = ("Day", "Temperature")
data = (
(1, 42.9),
(2, None),
(style("Friday", fg=Color.blue), style("Hot ๐ฅต", fg=Color.red, bold=True)),
)
ctx.print_table(data, headers)
# XXX Strip colors for now, while we figure how to lock down the handling of ANSI
# codes in the various table formats.
result = invoke(table_cli, "--table-format", format_name, color=False)
if not is_windows():
expected = expected.replace("\r\n", "\n")
assert result.stdout == f"Table format: {format_name}\n{expected}"
assert not result.stderr
assert result.exit_code == 0