Skip to content

Commit

Permalink
code: Add .show() method to TableD.
Browse files Browse the repository at this point in the history
This is the actual method that would print out a pretty printed
table to stdout.
  • Loading branch information
tommyip committed Jan 22, 2017
1 parent 9891b79 commit 0423fd6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
11 changes: 11 additions & 0 deletions tabled/tabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

from typing import Any, List, Optional, Text

import sys

from .pretty_print import generate_table


class TableD:
""" tableD's main interface. """
Expand Down Expand Up @@ -73,3 +77,10 @@ def add_rows(self, rows: List[List[Any]]) -> None:

for row in rows:
self.data.append(row)

def show(self) -> None:
""" Display the generated table to standard output. """

output = generate_table(self.headings, self.data, self.style)

print(output, file=sys.stdout)
28 changes: 23 additions & 5 deletions tests/test_tabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
:license: MIT
"""

from tabled import tabled
from tabled.tabled import TableD


class TestTableD:
def test_create_new_table_blank(self) -> None:
table = tabled.TableD()
table = TableD()

assert table.headings == []
assert table.data == []
Expand All @@ -24,7 +24,7 @@ def test_create_new_table(self) -> None:
data = [['Tommy Ip', 17, 'hkmp7tommy@gmail.com', 'example.com'],
['Someone', 23, 'someone@example.com', 'me.example.net']]

table = tabled.TableD(headings, data, 'fancy', 'rst')
table = TableD(headings, data, 'fancy', 'rst')

assert table.headings == headings
assert table.data == data
Expand All @@ -34,7 +34,7 @@ def test_create_new_table(self) -> None:
def test_add_row_1(self) -> None:
data = ['x1', 'x2', 'x3']

table = tabled.TableD()
table = TableD()
table.add_row(data)

assert table.data == [data]
Expand All @@ -44,9 +44,27 @@ def test_add_row_2(self) -> None:
['y1', 'y2', 'y3'],
['z1', 'z2', 'z3']]

table = tabled.TableD()
table = TableD()

for data in datas:
table.add_row(data)

assert table.data == datas

def test_show(self, capfd) -> None:
headings = ['x', 'f : x -> x^x']
data = [[str(x), str(x ** x)] for x in range(1, 6)]

TableD(headings, data).show()

out, err = capfd.readouterr()

assert out == ('+---+--------------+\n'
'| x | f : x -> x^x |\n'
'+---+--------------+\n'
'| 1 | 1 |\n'
'| 2 | 4 |\n'
'| 3 | 27 |\n'
'| 4 | 256 |\n'
'| 5 | 3125 |\n'
'+---+--------------+\n')

0 comments on commit 0423fd6

Please sign in to comment.