mirror of
				https://github.com/KevinMidboe/tableprint.git
				synced 2025-10-29 18:00:16 +00:00 
			
		
		
		
	Adds test suite for tableprint
This commit is contained in:
		
							
								
								
									
										26
									
								
								tests/test_functions.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								tests/test_functions.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					# -*- coding: utf-8 -*-
 | 
				
			||||||
 | 
					from __future__ import unicode_literals
 | 
				
			||||||
 | 
					from tableprint import top, bottom, row
 | 
				
			||||||
 | 
					import pytest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_borders():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # top
 | 
				
			||||||
 | 
					    assert top(5, width=2, style='round') == '╭──┬──┬──┬──┬──╮'
 | 
				
			||||||
 | 
					    assert top(1, width=6, style='grid') == '+------+'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # bottom
 | 
				
			||||||
 | 
					    assert bottom(3, width=1, style='fancy_grid') == '╘═╧═╧═╛'
 | 
				
			||||||
 | 
					    assert bottom(3, 4, style='clean') == ' ──── ──── ──── '
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_row():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # valid
 | 
				
			||||||
 | 
					    assert row("abc", width=3, style='round') == '│  a│  b│  c│'
 | 
				
			||||||
 | 
					    assert row([1, 2, 3], width=3, style='clean') == '   1   2   3 '
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # invalid
 | 
				
			||||||
 | 
					    with pytest.raises(ValueError) as context:
 | 
				
			||||||
 | 
					        row([{}])
 | 
				
			||||||
@@ -1,14 +0,0 @@
 | 
				
			|||||||
# -*- coding: utf-8 -*-
 | 
					 | 
				
			||||||
from __future__ import unicode_literals
 | 
					 | 
				
			||||||
from tableprint import humantime
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# def table(data, headers=None, format_spec=FMT, width=WIDTH, style=STYLE, out=sys.stdout):
 | 
					 | 
				
			||||||
# def header(headers, width=WIDTH, style=STYLE, add_hr=True):
 | 
					 | 
				
			||||||
# def row(values, width=WIDTH, format_spec=FMT, style=STYLE):
 | 
					 | 
				
			||||||
# def hr(n, width=WIDTH, linestyle=LineStyle('|', '-', '+', '|')):
 | 
					 | 
				
			||||||
# def top(n, width=WIDTH, style=STYLE):
 | 
					 | 
				
			||||||
# def bottom(n, width=WIDTH, style=STYLE):
 | 
					 | 
				
			||||||
# def banner(message, width=30, style='banner', out=sys.stdout):
 | 
					 | 
				
			||||||
# def dataframe(df, **kwargs):
 | 
					 | 
				
			||||||
# def humantime(t):
 | 
					 | 
				
			||||||
# def _format_line(data, linestyle):
 | 
					 | 
				
			||||||
							
								
								
									
										44
									
								
								tests/test_io.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								tests/test_io.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
				
			|||||||
 | 
					# -*- coding: utf-8 -*-
 | 
				
			||||||
 | 
					from __future__ import unicode_literals
 | 
				
			||||||
 | 
					from tableprint import table, banner, dataframe
 | 
				
			||||||
 | 
					from io import StringIO
 | 
				
			||||||
 | 
					import numpy as np
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_table():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    output = StringIO()
 | 
				
			||||||
 | 
					    table([[1, 2, 3], [4, 5 ,6]], "ABC", style='round', width=5, out=output)
 | 
				
			||||||
 | 
					    assert output.getvalue() == '╭─────┬─────┬─────╮\n│  A  │  B  │  C  │\n├─────┼─────┼─────┤\n│    1│    2│    3│\n│    4│    5│    6│\n╰─────┴─────┴─────╯\n'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    output = StringIO()
 | 
				
			||||||
 | 
					    table(["bar"], "foo", style='grid', width=3, out=output)
 | 
				
			||||||
 | 
					    assert output.getvalue() == '+---+---+---+\n| f | o | o |\n+---+---+---+\n|  b|  a|  r|\n+---+---+---+\n'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_frame():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # mock of a pandas DataFrame
 | 
				
			||||||
 | 
					    class DataFrame:
 | 
				
			||||||
 | 
					        def __init__(self, data, headers):
 | 
				
			||||||
 | 
					            self.data = data
 | 
				
			||||||
 | 
					            self.columns = headers
 | 
				
			||||||
 | 
					        def __array__(self):
 | 
				
			||||||
 | 
					            return self.data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # build a mock DataFrame
 | 
				
			||||||
 | 
					    df = DataFrame(np.array([[1, 2, 3]]), ['a', 'b', 'c'])
 | 
				
			||||||
 | 
					    output = StringIO()
 | 
				
			||||||
 | 
					    dataframe(df, width=4, style='fancy_grid', out=output)
 | 
				
			||||||
 | 
					    assert output.getvalue() == '╒════╤════╤════╕\n│ a  │ b  │ c  │\n╞════╪════╪════╡\n│   1│   2│   3│\n╘════╧════╧════╛\n'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_banner():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    output = StringIO()
 | 
				
			||||||
 | 
					    banner('hello world', style='clean', width=11, out=output)
 | 
				
			||||||
 | 
					    assert output.getvalue() == ' ─────────── \n hello world \n ─────────── \n'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    output = StringIO()
 | 
				
			||||||
 | 
					    banner('!', style='banner', width=1, out=output)
 | 
				
			||||||
 | 
					    assert output.getvalue() == '╒═╕\n│!│\n╘═╛\n'
 | 
				
			||||||
@@ -1,9 +1,22 @@
 | 
				
			|||||||
# -*- coding: utf-8 -*-
 | 
					# -*- coding: utf-8 -*-
 | 
				
			||||||
from __future__ import unicode_literals
 | 
					from __future__ import unicode_literals
 | 
				
			||||||
from tableprint import humantime
 | 
					from tableprint import humantime, _format_line, LineStyle
 | 
				
			||||||
import pytest
 | 
					import pytest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_format_line():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # using ASCII
 | 
				
			||||||
 | 
					    assert _format_line(['foo', 'bar'], LineStyle('(', '_', '+', ')')) == '(foo+bar)'
 | 
				
			||||||
 | 
					    assert _format_line("abc", LineStyle('[', '*', '.', ']')) == '[a.b.c]'
 | 
				
			||||||
 | 
					    assert _format_line(["_"], LineStyle('o', '', '!', 'o')) == 'o_o'
 | 
				
			||||||
 | 
					    assert _format_line([], LineStyle(':', '', '', ')')) == ':)'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # using unicode
 | 
				
			||||||
 | 
					    assert _format_line(['.', '.', '.'], LineStyle('★', '_', '╳', '☆')) == '★.╳.╳.☆'
 | 
				
			||||||
 | 
					    assert _format_line("☚☛", LineStyle('♪', '*', '♩', '♫')) == '♪☚♩☛♫'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_humantime():
 | 
					def test_humantime():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # test numeric input
 | 
					    # test numeric input
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user