Skip to content

Commit

Permalink
summary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Dec 13, 2024
1 parent ed71a06 commit 233372b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cat_win/src/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cat_win/src/service/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 45 additions & 2 deletions cat_win/tests/src/service/test_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())

0 comments on commit 233372b

Please sign in to comment.