Skip to content

Commit

Permalink
better test mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Feb 29, 2024
1 parent c618111 commit 5cfddc4
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 92 deletions.
36 changes: 18 additions & 18 deletions cat_win/tests/test_ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

@patch('cat_win.cat.default_color_dic', config.color_dic)
@patch('cat_win.cat.color_dic', config.color_dic)
@patch('cat_win.cat.os.isatty', OSAttyDefGen.get_def({1: False}))
@patch('os.isatty', OSAttyDefGen.get_def({1: False}))
class TestAnsiPiped(TestCase):
maxDiff = None

Expand All @@ -44,37 +44,37 @@ def tearDown(self):
cat.arg_parser = ArgParser()
cat.holder = Holder()

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('cat_win.cat.const_dic', strip_color_dic_true)
def test_cat_ansi_input_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_neg_file_content)

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('cat_win.cat.const_dic', strip_color_dic_false)
def test_cat_ansi_input_no_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_pos_file_content)

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('cat_win.cat.const_dic', strip_color_dic_true)
def test_cat_ansi_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_neg_file_content)

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('cat_win.cat.const_dic', strip_color_dic_false)
def test_cat_ansi_no_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_pos_file_content)

@patch('cat_win.cat.default_color_dic', config.color_dic)
@patch('cat_win.cat.color_dic', config.color_dic)
@patch('cat_win.cat.os.isatty', OSAttyDefGen.get_def({1: True}))
@patch('os.isatty', OSAttyDefGen.get_def({1: True}))
class TestAnsiNotPiped(TestCase):
maxDiff = None

Expand All @@ -84,30 +84,30 @@ def tearDown(self):
cat.arg_parser = ArgParser()
cat.holder = Holder()

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('cat_win.cat.const_dic', strip_color_dic_true)
def test_cat_ansi_input_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_pos_file_content)

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('sys.argv', ['<CAT>', ansi_pos_file_path])
@patch('cat_win.cat.const_dic', strip_color_dic_false)
def test_cat_ansi_input_no_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_pos_file_content)

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('cat_win.cat.const_dic', strip_color_dic_true)
def test_cat_ansi_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_pos_file_content)

@patch('cat_win.cat.sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('sys.argv', ['<CAT>', ansi_base_file_path, '-ln'])
@patch('cat_win.cat.const_dic', strip_color_dic_false)
def test_cat_ansi_no_strip(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), ansi_pos_file_content)
22 changes: 11 additions & 11 deletions cat_win/tests/test_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_cat_output_default_file(self):

check_against = '\n'.join(test_file_content) + '\n'

with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.edit_files()
self.assertEqual(fake_out.getvalue(), check_against)

Expand All @@ -44,7 +44,7 @@ def test_cat_output_multiple_files(self):

check_against = '\n'.join(test_file_content * 3) + '\n'

with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.edit_files()
self.assertEqual(fake_out.getvalue(), check_against)

Expand All @@ -56,7 +56,7 @@ def test_cat_output_reverse(self):
check_against.reverse()
check_against = '\n'.join(check_against) + '\n'

with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.edit_files()
self.assertEqual(fake_out.getvalue(), check_against)

Expand All @@ -67,7 +67,7 @@ def test_cat_output_ends_and_tabs(self):
check_against = ('\n'.join(c.replace('\t', '^TAB') + '$' for c in test_file_content) +
'\n')

with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.edit_files()
self.assertEqual(fake_out.getvalue(), check_against)

Expand Down Expand Up @@ -140,7 +140,7 @@ def test_remove_ansi_codes_from_line(self):

@patch('cat_win.cat.print_update_information', new=lambda *_: '')
def test__show_help(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat._show_help()
for arg in cat.ALL_ARGS:
if arg.show_arg:
Expand All @@ -150,7 +150,7 @@ def test__show_help(self):

@patch('cat_win.cat.print_update_information', new=lambda *_: '')
def test__show_help_shell(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat._show_help(True)
for arg in cat.ALL_ARGS:
if arg.show_arg and arg.show_arg_on_shell:
Expand All @@ -160,14 +160,14 @@ def test__show_help_shell(self):

@patch('cat_win.cat.print_update_information', new=lambda *_: '')
def test__show_version(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat._show_version()
self.assertIn('Catw', fake_out.getvalue())
self.assertIn('Author', fake_out.getvalue())

@patch('cat_win.cat.arg_parser.file_search', new=set(['hello', 'world']))
def test__show_debug(self):
with patch('cat_win.cat.sys.stderr', new=StdOutMock()) as fake_out:
with patch('sys.stderr', new=StdOutMock()) as fake_out:
cat._show_debug([], ['test'], [], [], [], [])
self.assertIn('test', fake_out.getvalue())
self.assertIn('DEBUG', fake_out.getvalue())
Expand All @@ -182,7 +182,7 @@ def test__get_file_prefix(self):

@patch('cat_win.cat.arg_parser._known_directories', new=['dirA', 'dirB'])
def test__dirs_output(self):
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat._show_dirs()
self.assertIn('dirA', fake_out.getvalue())
self.assertIn('dirB', fake_out.getvalue())
Expand Down Expand Up @@ -218,7 +218,7 @@ def test__show_wordcount(self):
∑: 1
"""
cat.holder.files = [File(test_file_path, '')]
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat._show_wordcount()
self.assertIn(output, fake_out.getvalue())

Expand Down Expand Up @@ -270,7 +270,7 @@ def test__show_charcount(self):
∑: 1
"""
cat.holder.files = [File(test_file_path, '')]
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat._show_charcount()
self.assertIn(output, fake_out.getvalue())

Expand Down
4 changes: 2 additions & 2 deletions cat_win/tests/test_cconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test__print_get_all_available_colors(self):
all_color_options = list(ColorOptions.Fore.keys()) + list(ColorOptions.Back.keys())
all_color_options = [k for k in all_color_options if k != 'RESET']

with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
config._print_get_all_available_colors()
for i, k in enumerate(all_color_options, start=1):
self.assertIn(str(i), fake_out.getvalue())
Expand All @@ -33,7 +33,7 @@ def test__print_all_available_elements(self):
callable(getattr(CKW, attr)) or attr.startswith('__') or attr.startswith('RESET')
)]

with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
config._print_all_available_elements()
for i, k in enumerate(class_members, start=1):
self.assertIn(str(i), fake_out.getvalue())
Expand Down
2 changes: 1 addition & 1 deletion cat_win/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_load_config(self):
def test__print_all_available_elements(self):
all_elements = list(config.default_dic.keys())

with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
with patch('sys.stdout', new=StdOutMock()) as fake_out:
config._print_all_available_elements()
for i, k in enumerate(all_elements, start=1):
self.assertIn(str(i), fake_out.getvalue())
Expand Down
8 changes: 4 additions & 4 deletions cat_win/tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def test_editor_action_save(self):
editor = Editor(test_file_path, '', 'utf-8')
exc = OSError('TestError')
error_def = ErrorDefGen.get_def(exc)
with patch('cat_win.cat.sys.stderr', new=StdOutMock()) as fake_out:
with patch('sys.stderr', new=StdOutMock()) as fake_out:
self.assertEqual(editor._action_save(error_def), True)
self.assertEqual(editor.error_bar, 'TestError')
self.assertEqual('TestError\n', fake_out.getvalue())
Expand All @@ -411,18 +411,18 @@ def test_editor_interrupt(self):
Editor.debug_mode = True
editor = Editor(test_file_path_oneline, '', 'utf-8')
with self.assertRaises(KeyboardInterrupt):
with patch('cat_win.cat.sys.stderr', new=StdOutMock()) as fake_out:
with patch('sys.stderr', new=StdOutMock()) as fake_out:
editor._action_interrupt(None)
self.assertEqual('Interrupting...\n', fake_out.getvalue())
Editor.debug_mode = False

@patch('cat_win.util.editor.CURSES_MODULE_ERROR', new=True)
def test_editor_no_curses_error(self):
with patch('cat_win.util.editor.sys.stderr', new=StdOutMock()) as fake_out:
with patch('sys.stderr', new=StdOutMock()) as fake_out:
self.assertEqual(Editor.open('', '', '', None, True), False)
self.assertIn('could not be loaded', fake_out.getvalue())
self.assertIn('windows-curses', fake_out.getvalue())
with patch('cat_win.util.editor.sys.stderr', new=StdOutMock()) as fake_out:
with patch('sys.stderr', new=StdOutMock()) as fake_out:
self.assertEqual(Editor.open('', '', '', None, True), False)
self.assertEqual('', fake_out.getvalue())

Expand Down
Loading

0 comments on commit 5cfddc4

Please sign in to comment.