Skip to content

Commit

Permalink
Made sure format_table doesn't change its input
Browse files Browse the repository at this point in the history
  • Loading branch information
sigvaldm committed Sep 5, 2018
1 parent 13ffac7 commit ee3b26b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import math
import numpy as np
import shutil
from copy import deepcopy

def fit_text(text, width=None, align='<', suffix="..."):
"""
Expand Down Expand Up @@ -180,6 +181,8 @@ def format_table(table,
'truncate' is 0, column 0 will have a width of -16 which is not permitted.
"""

table = deepcopy(table)

if not isinstance(align, list):
align = [align]

Expand Down
37 changes: 37 additions & 0 deletions test_frmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,45 @@ def test_format_table_example2b():
"Stuart 48:54\n"\
"John 1:01:12\n"

def test_format_table_example2c():

header = ['Name' , 'Time']
table = [['John' , 3672 ],
['Martha', 2879 ],
['Stuart', 2934 ],
['Eduard', 2592 ]]

table.sort(key=lambda row: row[1])

header.insert(0, '')
for i, row in enumerate(table):
row.insert(0, i+1)

table.insert(0, header)

def index(num):
if isinstance(num, int):
return "{}.".format(num)
else:
return num

assert format_table(table, '<<>', format=[index,format_time]) == \
" Name Time\n"\
"1. Eduard 43:12\n"\
"2. Martha 47:59\n"\
"3. Stuart 48:54\n"\
"4. John 1:01:12\n"

def test_format_table_numcols():

table = [[1, 2, 3],[1, 2]]
with pytest.raises(ValueError) as e_info:
format_table(table)

def test_format_table_inputonlyargument():

# Make sure format_table isn't changing its input arguments.
# TBD: This test could perhaps be generalized to something.
table = [[1,2],[3,4]]
format_table(table, format=format_time)
assert table[1][1]==4

0 comments on commit ee3b26b

Please sign in to comment.