Skip to content

Commit 69cc9b2

Browse files
committed
Added set_color_library class method to TableColors
This allows the ability to manually override which color library is being used, which is necessary to ensure reproducible unit tests regardless of what color libraries are installed. Added the first bare-bones template for unit tests with a single test to start.
1 parent d32d4e9 commit 69cc9b2

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

tableformatter.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,44 @@ class TableColors(object):
303303
BOLD = ''
304304
RESET = ''
305305

306+
@classmethod
307+
def set_color_library(cls, library_name: str) -> None:
308+
"""Manually override the color library being used."""
309+
if library_name == 'colored':
310+
from colored import fg, bg, attr
311+
312+
cls.TEXT_COLOR_WHITE = fg('white')
313+
cls.TEXT_COLOR_YELLOW = fg(226)
314+
cls.TEXT_COLOR_RED = fg(196)
315+
cls.TEXT_COLOR_GREEN = fg(119)
316+
cls.TEXT_COLOR_BLUE = fg(27)
317+
cls.BG_COLOR_ROW = bg(234)
318+
cls.BG_RESET = bg(0)
319+
cls.BOLD = attr('bold')
320+
cls.RESET = attr('reset')
321+
elif library_name == 'colorama':
322+
from colorama import Fore, Back, Style
323+
324+
cls.TEXT_COLOR_WHITE = Fore.WHITE
325+
cls.TEXT_COLOR_YELLOW = Fore.LIGHTYELLOW_EX
326+
cls.TEXT_COLOR_RED = Fore.LIGHTRED_EX
327+
cls.TEXT_COLOR_GREEN = Fore.LIGHTGREEN_EX
328+
cls.TEXT_COLOR_BLUE = Fore.LIGHTBLUE_EX
329+
cls.BG_COLOR_ROW = Back.LIGHTBLACK_EX
330+
cls.BG_RESET = Back.RESET
331+
cls.BOLD = Style.BRIGHT
332+
cls.RESET = Fore.RESET + Back.RESET
333+
else:
334+
cls.TEXT_COLOR_WHITE = ''
335+
cls.TEXT_COLOR_YELLOW = ''
336+
cls.TEXT_COLOR_RED = ''
337+
cls.TEXT_COLOR_GREEN = ''
338+
cls.TEXT_COLOR_BLUE = ''
339+
cls.BG_COLOR_ROW = ''
340+
cls.BG_RESET = ''
341+
cls.BOLD = ''
342+
cls.RESET = ''
343+
306344

307345
class ColumnAlignment(enum.Enum):
308346
"""Column alignment"""

tests/test_simple_object.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# coding=utf-8
2+
"""
3+
Unit testing of tableformatter with simple cases using a list of objects for the table entries.
4+
"""
5+
import pytest
6+
7+
import tableformatter as tf
8+
9+
10+
class MyRowObject(object):
11+
"""Simple object to demonstrate using a list of objects with TableFormatter"""
12+
def __init__(self, field1: str, field2: str, field3: str, field4: str):
13+
self.field1 = field1
14+
self.field2 = field2
15+
self._field3 = field3
16+
self.field4 = field4
17+
18+
def get_field3(self):
19+
"""Demonstrates accessing object functions"""
20+
return self._field3
21+
22+
23+
# Make the test results reproducible regardless of what color libraries are installed
24+
tf.TableColors.set_color_library('none')
25+
26+
27+
@pytest.fixture
28+
def rows():
29+
r = [MyRowObject('A1', 'A2', 'A3', 'A4'),
30+
MyRowObject('B1', 'B2\nB2\nB2', 'B3', 'B4'),
31+
MyRowObject('C1', 'C2', 'C3', 'C4'),
32+
MyRowObject('D1', 'D2', 'D3', 'D4')]
33+
return r
34+
35+
36+
@pytest.fixture
37+
def cols():
38+
columns = (tf.Column('Col1', attrib='field1'),
39+
tf.Column('Col2', attrib='field2'),
40+
tf.Column('Col3', attrib='get_field3'),
41+
tf.Column('Col4', attrib='field4'))
42+
return columns
43+
44+
45+
def test_object_table_with_header(rows, cols):
46+
expected = '╔══════╤══════╤══════╤══════╗\n║ Col1 │ Col2 │ Col3 │ Col4 ║\n╠══════╪══════╪══════╪══════╣\n║ A1 │ A2 │ A3 │ A4 ║\n║ B1 │ B2 │ B3 │ B4 ║\n║ │ B2 │ │ ║\n║ │ B2 │ │ ║\n║ C1 │ C2 │ C3 │ C4 ║\n║ D1 │ D2 │ D3 │ D4 ║\n╚══════╧══════╧══════╧══════╝\n'
47+
table = tf.generate_table(rows, cols)
48+
assert table == expected

0 commit comments

Comments
 (0)