From 233372be37b03d78e597c0c706348a3cd1616581 Mon Sep 17 00:00:00 2001 From: Silas Kraume Date: Fri, 13 Dec 2024 01:46:51 +0100 Subject: [PATCH] summary tests --- cat_win/src/cat.py | 4 +- cat_win/src/service/summary.py | 2 +- cat_win/tests/src/service/test_summary.py | 47 ++++++++++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/cat_win/src/cat.py b/cat_win/src/cat.py index 89a5eaf..8ea8c13 100644 --- a/cat_win/src/cat.py +++ b/cat_win/src/cat.py @@ -870,7 +870,7 @@ def edit_files() -> None: if u_args[ARGS_FILES] or u_args[ARGS_DIRECTORIES]: print() if u_args[ARGS_FILES]: - Summary.show_files(u_args[ARGS_FFILES], u_files.files) + Summary.show_files(u_files.files, u_args[ARGS_FFILES]) if u_args[ARGS_DIRECTORIES]: Summary.show_dirs(arg_parser.get_dirs()) if u_args[ARGS_SUM]: @@ -1114,7 +1114,7 @@ def handle_args(tmp_file_helper: TmpFileHelper) -> None: if u_args[ARGS_FFILES] or u_args[ARGS_DDIRECTORIES]: if u_args[ARGS_FFILES]: - Summary.show_files(u_args[ARGS_FFILES], u_files.files) + Summary.show_files(u_files.files, u_args[ARGS_FFILES]) if u_args[ARGS_DDIRECTORIES]: Summary.show_dirs(arg_parser.get_dirs()) return diff --git a/cat_win/src/service/summary.py b/cat_win/src/service/summary.py index 1222fb1..5f8973d 100644 --- a/cat_win/src/service/summary.py +++ b/cat_win/src/service/summary.py @@ -55,7 +55,7 @@ def _unique_list(_l: list) -> list: return unique_elements @staticmethod - def show_files(detailed: bool, files: list) -> None: + def show_files(files: list, detailed: bool) -> None: """ displays u_files.files including their size and calculates their size sum. diff --git a/cat_win/tests/src/service/test_summary.py b/cat_win/tests/src/service/test_summary.py index 7d1df50..ebd389a 100644 --- a/cat_win/tests/src/service/test_summary.py +++ b/cat_win/tests/src/service/test_summary.py @@ -10,11 +10,32 @@ class TestSummary(TestCase): + def test__unique_list(self): + self.assertListEqual(Summary._unique_list([1, 2, 3, 1, 2, 3]), [1, 2, 3]) + self.assertListEqual(Summary._unique_list([1, 3, 3, 3, 2, 3]), [1, 3, 2]) + self.assertListEqual(Summary._unique_list([]), []) + self.assertListEqual(Summary._unique_list([1]), [1]) + self.assertListEqual(Summary._unique_list([5, 1]), [5, 1]) + + def test_show_files_empty(self): + with patch('sys.stdout', new=StdOutMock()) as fake_out: + Summary.show_files([], False) + self.assertEqual('No files have been found!\n', fake_out.getvalue()) + def test_show_dirs(self): with patch('sys.stdout', new=StdOutMock()) as fake_out: - Summary.show_dirs(['dirA', 'dirB']) + Summary.show_dirs(['dirA', 'dirB', 'dirB']) + self.assertIn('dirA', fake_out.getvalue()) + self.assertIn('dirB', fake_out.getvalue()) + self.assertEqual(fake_out.getvalue().count('dirB'), 2) + + @patch('cat_win.src.service.summary.Summary.unique', True) + def test_show_dirs_unique(self): + with patch('sys.stdout', new=StdOutMock()) as fake_out: + Summary.show_dirs(['dirA', 'dirB', 'dirB']) self.assertIn('dirA', fake_out.getvalue()) self.assertIn('dirB', fake_out.getvalue()) + self.assertEqual(fake_out.getvalue().count('dirB'), 1) def test_show_dirs_empty(self): with patch('sys.stdout', new=StdOutMock()) as fake_out: @@ -30,11 +51,23 @@ def test_show_sum(self): def test_show_sum_detailed(self): output = r"""File LineCount test 111 +test 111 + +Lines (Sum): 222 +""" + with patch('sys.stdout', new=StdOutMock()) as fake_out: + Summary.show_sum([File('test', ''), File('test', '')], True, {'test': 111}, 0) + self.assertEqual(fake_out.getvalue(), output) + + @patch('cat_win.src.service.summary.Summary.unique', True) + def test_show_sum_detailed_unique(self): + output = r"""File LineCount +test 111 Lines (Sum): 111 """ with patch('sys.stdout', new=StdOutMock()) as fake_out: - Summary.show_sum([File('test', '')], True, {'test': 111}, 0) + Summary.show_sum([File('test', ''), File('test', '')], True, {'test': 111}, 0) self.assertEqual(fake_out.getvalue(), output) def test_show_wordcount(self): @@ -70,6 +103,11 @@ def test_show_wordcount(self): Summary.show_wordcount([File(test_file_path, '')], 'utf-8') self.assertIn(output, fake_out.getvalue()) + def test_show_wordcount_empty(self): + with patch('sys.stdout', new=StdOutMock()) as fake_out: + Summary.show_wordcount([], 'utf-8') + self.assertEqual('The word count could not be calculated.\n', fake_out.getvalue()) + def test_show_charcount(self): output = r""" ' ': 23 @@ -119,3 +157,8 @@ def test_show_charcount(self): with patch('sys.stdout', new=StdOutMock()) as fake_out: Summary.show_charcount([File(test_file_path, '')], 'utf-8') self.assertIn(output, fake_out.getvalue()) + + def test_show_charcount_empty(self): + with patch('sys.stdout', new=StdOutMock()) as fake_out: + Summary.show_charcount([], 'utf-8') + self.assertEqual('The char count could not be calculated.\n', fake_out.getvalue())