From 7146b64e9ea6c81b28d4ac2dea9f5e3961678bd6 Mon Sep 17 00:00:00 2001 From: Nelson Elhage Date: Sun, 3 Mar 2019 21:20:50 +0000 Subject: [PATCH] Add some tests for formatting time for display! - Add requirements.dev.txt to install py.test - Refactor Timer slightly so we can call the formatting method without an instance - Add some tests! Just include some examples that span the range from seconds to hours, and verify that they display properly. --- cursewords/cursewords.py | 9 ++++----- requirements.dev.txt | 1 + setup.py | 4 ++++ test/cursewords_test.py | 13 +++++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 requirements.dev.txt create mode 100644 test/cursewords_test.py diff --git a/cursewords/cursewords.py b/cursewords/cursewords.py index 7a36c04..5c298de 100755 --- a/cursewords/cursewords.py +++ b/cursewords/cursewords.py @@ -627,12 +627,11 @@ def show_time(self): x_coord = self.grid.grid_x + self.grid.column_count * 4 - 7 print(self.grid.term.move(y_coord, x_coord) - + self.display_format()) + + self.format_time(self.time_passed)) - def display_format(self): - time_amount = self.time_passed - - m, s = divmod(time_amount, 60) + @classmethod + def format_time(cl, time_passed): + m, s = divmod(time_passed, 60) h, m = divmod(m, 60) ch = '{h:02d}:'.format(h=h) if h else ' ' diff --git a/requirements.dev.txt b/requirements.dev.txt new file mode 100644 index 0000000..1e0e533 --- /dev/null +++ b/requirements.dev.txt @@ -0,0 +1 @@ +pytest==4.3.0 diff --git a/setup.py b/setup.py index db7f199..4485b5b 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,9 @@ with open(path.join(here, 'requirements.txt')) as f: reqs = f.read().split() +with open(path.join(here, 'requirements.dev.txt')) as f: + test_reqs = f.read().split() + with open(path.join(here, 'README.md')) as f: readme = f.read() @@ -30,6 +33,7 @@ packages=find_packages(), python_requires='>=3.4', install_requires=reqs, + tests_require=test_reqs, package_data={ 'cursewords': ['version'] }, diff --git a/test/cursewords_test.py b/test/cursewords_test.py new file mode 100644 index 0000000..caafb7b --- /dev/null +++ b/test/cursewords_test.py @@ -0,0 +1,13 @@ +import cursewords + +def test_format_time(): + test_cases = [ + (1, " 00:01"), + (61, " 01:01"), + (314, " 05:14"), + (1288, " 21:28"), + (1288, " 21:28"), + (3723, "01:02:03"), + ] + for (seconds, expect) in test_cases: + assert cursewords.Timer.format_time(seconds) == expect