From 0dfe1c9e2780469e3a4696e8fb3e6f717a7ebeb7 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 16 Sep 2022 11:09:35 +0200 Subject: Adding upstream version 3.1.0. Signed-off-by: Daniel Baumann --- tests/test_ascii_table.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/test_ascii_table.py (limited to 'tests/test_ascii_table.py') diff --git a/tests/test_ascii_table.py b/tests/test_ascii_table.py new file mode 100644 index 0000000..020a443 --- /dev/null +++ b/tests/test_ascii_table.py @@ -0,0 +1,108 @@ +"""Test AsciiTable class.""" + +import pytest + +from terminaltables.other_tables import AsciiTable + +SINGLE_LINE = ( + ('Name', 'Color', 'Type'), + ('Avocado', 'green', 'nut'), + ('Tomato', 'red', 'fruit'), + ('Lettuce', 'green', 'vegetable'), +) + +MULTI_LINE = ( + ('Show', 'Characters'), + ('Rugrats', 'Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles,\nDil Pickles'), + ('South Park', 'Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick'), +) + + +@pytest.fixture(autouse=True) +def patch(monkeypatch): + """Monkeypatch before every test function in this module. + + :param monkeypatch: pytest fixture. + """ + monkeypatch.setattr('terminaltables.ascii_table.terminal_size', lambda: (79, 24)) + monkeypatch.setattr('terminaltables.width_and_alignment.terminal_size', lambda: (79, 24)) + + +@pytest.mark.parametrize('table_data,column_number,expected', [ + ([], 0, IndexError), + ([[]], 0, IndexError), + ([['']], 1, IndexError), + (SINGLE_LINE, 0, 55), + (SINGLE_LINE, 1, 53), + (SINGLE_LINE, 2, 57), + (MULTI_LINE, 0, -11), + (MULTI_LINE, 1, 62), +]) +def test_column_max_width(table_data, column_number, expected): + """Test method in class. + + :param iter table_data: Passed to AsciiTable.__init__(). + :param int column_number: Passed to AsciiTable.column_max_width(). + :param int expected: Expected return value of AsciiTable.column_max_width(). + """ + table = AsciiTable(table_data) + + if expected == IndexError: + with pytest.raises(IndexError): + table.column_max_width(column_number) + return + + actual = table.column_max_width(column_number) + assert actual == expected + + +def test_column_widths(): + """Test method in class.""" + assert AsciiTable([]).column_widths == list() + + table = AsciiTable(SINGLE_LINE) + actual = table.column_widths + assert actual == [7, 5, 9] + + +@pytest.mark.parametrize('table_data,terminal_width,expected', [ + ([], None, True), + ([[]], None, True), + ([['']], None, True), + (SINGLE_LINE, None, True), + (SINGLE_LINE, 30, False), + (MULTI_LINE, None, False), + (MULTI_LINE, 100, True), +]) +def test_ok(monkeypatch, table_data, terminal_width, expected): + """Test method in class. + + :param monkeypatch: pytest fixture. + :param iter table_data: Passed to AsciiTable.__init__(). + :param int terminal_width: Monkeypatch width of terminal_size() if not None. + :param bool expected: Expected return value. + """ + if terminal_width is not None: + monkeypatch.setattr('terminaltables.ascii_table.terminal_size', lambda: (terminal_width, 24)) + table = AsciiTable(table_data) + actual = table.ok + assert actual is expected + + +@pytest.mark.parametrize('table_data,expected', [ + ([], 2), + ([[]], 2), + ([['']], 4), + ([[' ']], 5), + (SINGLE_LINE, 31), + (MULTI_LINE, 100), +]) +def test_table_width(table_data, expected): + """Test method in class. + + :param iter table_data: Passed to AsciiTable.__init__(). + :param int expected: Expected return value. + """ + table = AsciiTable(table_data) + actual = table.table_width + assert actual == expected -- cgit v1.2.3