Skip to content

Commit

Permalink
Tests, style.
Browse files Browse the repository at this point in the history
  • Loading branch information
frinkelpi committed Oct 22, 2016
1 parent cfe7a46 commit 4acbd8f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
13 changes: 8 additions & 5 deletions fahrplan/display.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-
import six
from .tableprinter import Tableprinter
from texttable.texttable import Texttable
# Output formats
class Formats(object):
SIMPLE = 0
FULL = 1

#Get table row for connection

def _getConnectionRow(i,c):
"""
Get table row for connection.
"""
sections = c["sections"]
firstClass = True
#Create row
Expand All @@ -28,8 +29,10 @@ def _getConnectionRow(i,c):
else:
row.append("\n \n".join(["\n".join(p(s)) for s in sections]))
return row
#Display connections
def displayConnections(connections, output_format):
"""
Display connections in the given output format.
"""
table = Texttable(max_width=0)
#Alignments
table.set_cols_valign(["m","t","t","t","t","t","m","t","t"])
Expand All @@ -41,8 +44,8 @@ def displayConnections(connections, output_format):
table.add_row(_getConnectionRow(i,c))
#Display
print(table.draw())

# Old display method:
# from .tableprinter import Tableprinter
# def displayConnections(connections, output_format):
# # Define columns
# cols = (
Expand Down
3 changes: 0 additions & 3 deletions fahrplan/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@
# Base configuration
ENCODING = sys.stdout.encoding or 'utf-8'



# Helper function to print directly to sys.stderr
perror = partial(print, file=sys.stderr)

def main():

output_format = Formats.SIMPLE
proxy_host = None

Expand Down
71 changes: 33 additions & 38 deletions fahrplan/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def testHelp(self):
for arg in args:
r = envoy.run('{0} {1}'.format(BASE_COMMAND, arg))
self.assertTrue(meta.description in r.std_out)
self.assertTrue('Usage:' in r.std_out)
self.assertTrue('Options:' in r.std_out)
self.assertTrue('Arguments:' in r.std_out)
self.assertTrue('usage:' in r.std_out)
self.assertTrue('optional arguments:' in r.std_out)
self.assertTrue('positional arguments:' in r.std_out)
self.assertTrue('Examples:' in r.std_out)


Expand Down Expand Up @@ -164,29 +164,24 @@ def returnStatus(self):
self.assertEqual(0, self.r.status_code)

def testRowCount(self):
"""A normal output table should have 14 rows."""
self.assertEqual(15, len(self.rows))
"""A normal output table should have 16 rows."""
self.assertEqual(16, len(self.rows))

def testHeadline(self):
"""Test the headline items."""
headline_items = ['Station', 'Platform', 'Date', 'Time', 'Duration', 'Chg.', 'Travel with', 'Occupancy']
headline_items = ['Station', 'Platform', 'Date', 'Time', 'Duration', 'Chg.', 'With', 'Occupancy']
for item in headline_items:
self.assertIn(item, self.rows[0])
self.assertIn(item, self.rows[1])

def testEnumeration(self):
"""Each row should be enumerated."""
firstcol = [row[0] for row in self.rows[:-1]]
self.assertEqual(list('#-1 -2 -3 -4 -'), firstcol)
# def testEnumeration(self):
# """Each row should be enumerated."""
# firstcol = [row[0] for row in self.rows[:-1]]
# self.assertEqual(list('#-1 -2 -3 -4 -'), firstcol)

def testStationNames(self):
"""Station names should be "Basel SBB" and "Zürich HB"."""
if six.PY3: # quick and dirty
self.assertTrue(self.rows[2].startswith('1 | Basel SBB'))
self.assertTrue(self.rows[3].startswith(' | Zürich HB'))
else:
self.assertTrue(self.rows[2].startswith('1 | Basel SBB'))
self.assertTrue(self.rows[3].startswith(' | Z\xc3\xbcrich HB'))

self.assertTrue("Basel SBB" in self.rows[3])
self.assertTrue("Zürich HB" in self.rows[4])

class TestLanguages(unittest.TestCase):

Expand All @@ -208,31 +203,31 @@ def testBasicQuery(self):
self.assertTrue(stdout_values[1:] == stdout_values[:-1])


class TestTablePrinter(unittest.TestCase):
# class TestTablePrinter(unittest.TestCase):

def setUp(self):
self.output = StringIO()
self.stdout = sys.stdout
sys.stdout = self.output
# def setUp(self):
# self.output = StringIO()
# self.stdout = sys.stdout
# sys.stdout = self.output

def tearDown(self):
self.output.close()
sys.stdout = self.stdout
# def tearDown(self):
# self.output.close()
# sys.stdout = self.stdout

def testSeparator(self):
printer = Tableprinter((3, 4, 5), ' ')
printer.print_separator('*')
self.assertEqual('******************\n', self.output.getvalue())
# def testSeparator(self):
# printer = Tableprinter((3, 4, 5), ' ')
# printer.print_separator('*')
# self.assertEqual('******************\n', self.output.getvalue())

def testPartialSeparator(self):
printer = Tableprinter((2, 2, 3, 2), '+|+')
printer.print_separator(cols=[1, 2])
self.assertEqual(' +|+--+|+---+|+ +|+\n', self.output.getvalue())
# def testPartialSeparator(self):
# printer = Tableprinter((2, 2, 3, 2), '+|+')
# printer.print_separator(cols=[1, 2])
# self.assertEqual(' +|+--+|+---+|+ +|+\n', self.output.getvalue())

def testLine(self):
printer = Tableprinter((4, 5, 6), '|')
printer.print_line(('Eggs', 'Bacon', 'Spam'))
self.assertEqual('Eggs|Bacon|Spam |\n', self.output.getvalue())
# def testLine(self):
# printer = Tableprinter((4, 5, 6), '|')
# printer.print_line(('Eggs', 'Bacon', 'Spam'))
# self.assertEqual('Eggs|Bacon|Spam |\n', self.output.getvalue())


class RegressionTests(unittest.TestCase):
Expand Down

0 comments on commit 4acbd8f

Please sign in to comment.