Skip to content

Commit

Permalink
fix pylint warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Sep 7, 2023
1 parent 3c5e4bf commit 72951d1
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 42 deletions.
11 changes: 10 additions & 1 deletion cat_win/tests/mocks/error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"""
error
"""

class ErrorDefGen:
def get_def(error):
"""
generate a function that raises a specific error
"""
def get_def(error: Exception):
"""
return a function that raises a specific error
"""
def func(*args, **kwargs):
raise error
return func
36 changes: 33 additions & 3 deletions cat_win/tests/mocks/std.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""
std
"""

import io


class StdOutMock(io.StringIO):
pass
"""
mock for stdout stream
"""
# def reconfigure(self, encoding = None) -> None:
# return

Expand All @@ -11,13 +17,19 @@ class StdOutMock(io.StringIO):


class StdOutMockIsAtty(io.StringIO):
"""
mock for atty stdout stream
"""
closed: bool = False

def isatty(self) -> bool:
return True


class StdInMockIter:
"""
mock for stdin stream iterable
"""
def __init__(self, mock: object) -> None:
self.input_value = mock.input_value
self.splitted_input_value = self.input_value.split('\n')
Expand All @@ -34,27 +46,45 @@ def __next__(self) -> str:


class StdInMock:
"""
mock for stdin stream
"""
def __init__(self, input_value: str = '') -> None:
self.input_value = input_value

def set_content(self, input_value: str) -> None:
"""
set the stdin mock content
"""
self.input_value = input_value

# def reconfigure(self, encoding = None) -> None:
# return

def readline(self) -> str:
"""
return the next line from stdin mock
"""
return self.input_value.split('\n')[0] + '\n'

def __iter__(self) -> StdInMockIter:
return StdInMockIter(self)

class StdInHelperMock:
"""
helper for stdin mock
"""
def __init__(self, content: str = '') -> None:
self.content = content

def set_content(self, content: str) -> None:
"""
set stdin content
"""
self.content = content

def get_stdin_content(self, one_line: bool = False):
yield from self.content.split('\n')
def get_stdin_content(self, *_):
"""
generate stdin content line by line
"""
yield from self.content.split('\n')
21 changes: 14 additions & 7 deletions cat_win/tests/test_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def test_get_arguments_empty(self):

def test_get_arguments_duplicate(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT', '-n', '-n', '-c'])
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', '-n', '-n', '-c'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-n', '-c', '-n'])
Expand All @@ -47,7 +48,8 @@ def test_get_arguments_concatenated(self):

def test_get_arguments_concatenated_unknown(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT', '-+abce?fϵg'])
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', '-+abce?fϵg'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-a', '-b', '-c', '-e', '-f', '-g'])
Expand All @@ -58,7 +60,8 @@ def test_get_arguments_concatenated_unknown(self):

def test_get_arguments_concatenated_invalid(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT', '--abcde?fg'])
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', '--abcde?fg'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, [])
Expand Down Expand Up @@ -117,7 +120,8 @@ def test_get_arguments_replace(self):

def test_get_arguments_dir(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT', test_file_dir])
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', test_file_dir])
known_files = arg_parser.get_files()
self.assertCountEqual(args, [])
self.assertCountEqual(unknown_args, [])
Expand All @@ -135,7 +139,8 @@ def test_get_arguments_files_equal_dir(self):

def test_get_arguments_unknown_file(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT', 'testTesttest', 'test-file.txt'])
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', 'testTesttest', 'test-file.txt'])
known_files = arg_parser.get_files()
self.assertCountEqual(args, [])
self.assertCountEqual(unknown_args, [])
Expand All @@ -152,7 +157,8 @@ def test_get_arguments_unknown_args(self):

def test_get_arguments_echo_args(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(['CAT', '-n', '-E', '-n', 'random', test_file_dir])
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', '-n', '-E', '-n', 'random', test_file_dir])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-n', '-E'])
Expand Down Expand Up @@ -205,6 +211,7 @@ def test_levenshtein(self):
self.assertEqual(levenshtein('', ''), 100.0)
self.assertEqual(levenshtein('', 'test'), 0.0)
self.assertEqual(levenshtein('abc', ''), 0.0)
self.assertAlmostEqual(levenshtein('The dog sat on the cat', 'The cat sat on the mat'), 81.8181, 3)
self.assertAlmostEqual(levenshtein('The dog sat on the cat', 'The cat sat on the mat'),
81.8181, 3)
self.assertAlmostEqual(levenshtein('lower!', 'LOWER?'), 83.3333, 3)
self.assertAlmostEqual(levenshtein('--hecksview', '--hexview'), 66.6666, 3)
14 changes: 10 additions & 4 deletions cat_win/tests/test_checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ def test_checksum(self):
expected_output += '\tCRC32: C7222F64\n'
expected_output += '\tMD5: 0f85f8d2b6783c06d40755ab54906862\n'
expected_output += '\tSHA1: 1c50f01c5be5fc4795817e14f5deccbd51bf0934\n'
expected_output += '\tSHA256: dfb46efd198ad4b1204308c2be1c5e0254effe0de3ed314dea394cafdfd1749f\n'
expected_output += '\tSHA512: bf35256e790288a6dbf93b7327e1f97840349e0490b48848273663d34ad493e73a325df6725d7b0d5eb680d751bfe3cc49bd82ce9e8527412ce2cc8355a391a1\n'
expected_output += '\tSHA256: dfb46efd198ad4b1204308c2be1c5e0254effe0de3'
expected_output += 'ed314dea394cafdfd1749f\n'
expected_output += '\tSHA512: bf35256e790288a6dbf93b7327e1f97840349e0490'
expected_output += 'b48848273663d34ad493e73a325df6725d7b0d5eb680d751bfe3c'
expected_output += 'c49bd82ce9e8527412ce2cc8355a391a1\n'
self.assertEqual(get_checksum_from_file(test_file_path), expected_output)

def test_checksum_colored(self):
expected_output = ''
expected_output += '\tXCRC32: C7222F64Y\n'
expected_output += '\tXMD5: 0f85f8d2b6783c06d40755ab54906862Y\n'
expected_output += '\tXSHA1: 1c50f01c5be5fc4795817e14f5deccbd51bf0934Y\n'
expected_output += '\tXSHA256: dfb46efd198ad4b1204308c2be1c5e0254effe0de3ed314dea394cafdfd1749fY\n'
expected_output += '\tXSHA512: bf35256e790288a6dbf93b7327e1f97840349e0490b48848273663d34ad493e73a325df6725d7b0d5eb680d751bfe3cc49bd82ce9e8527412ce2cc8355a391a1Y\n'
expected_output += '\tXSHA256: dfb46efd198ad4b1204308c2be1c5e0254effe0de3e'
expected_output += 'd314dea394cafdfd1749fY\n'
expected_output += '\tXSHA512: bf35256e790288a6dbf93b7327e1f97840349e0490b'
expected_output += '48848273663d34ad493e73a325df6725d7b0d5eb680d751bfe3cc49'
expected_output += 'bd82ce9e8527412ce2cc8355a391a1Y\n'
self.assertEqual(get_checksum_from_file(test_file_path, ['X', 'Y']), expected_output)
9 changes: 6 additions & 3 deletions cat_win/tests/test_fileattributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test__convert_size_yb(self):
self.assertEqual(_convert_size(1024*1024*1024*1024*1024*1024*1024*1024), '1.0 YB')

def test__convert_size_edge_yb(self):
self.assertEqual(_convert_size(1024*1024*1024*1024*1024*1024*1024*1024 * 1023.99), '1023.99 YB')
self.assertEqual(_convert_size(1024*1024*1024*1024*1024*1024*1024*1024 * 1023.99),
'1023.99 YB')

def test__convert_size_out_of_range(self):
self.assertEqual(_convert_size(1024*1024*1024*1024*1024*1024*1024*1024*1024), '1.0 ?')
Expand All @@ -43,9 +44,11 @@ def test_get_file_meta_data(self):
self.assertIn('MTime:', meta_data)
self.assertIn('CTime:', meta_data)

meta_data = get_file_meta_data('randomFileThatHopefullyDoesNotExistWithWeirdCharsForSafety*!?\\/:<>|', False)
meta_data = get_file_meta_data(
'randomFileThatHopefullyDoesNotExistWithWeirdCharsForSafety*!?\\/:<>|',False)
self.assertEqual(meta_data, '')

def test_get_file_size(self):
self.assertGreater(get_file_size(__file__), 0)
self.assertEqual(get_file_size('randomFileThatHopefullyDoesNotExistWithWeirdCharsForSafety*!?\\/:<>|'), 0)
self.assertEqual(get_file_size(
'randomFileThatHopefullyDoesNotExistWithWeirdCharsForSafety*!?\\/:<>|'), 0)
23 changes: 15 additions & 8 deletions cat_win/tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def test_cat_output_full_b(self):
cat.main()
self.assertEqual(fake_out.getvalue(), expected_output)

@patch('cat_win.cat.sys.argv', ['<CAT>', 'enc:UTF-8', test_file_path, '-ub', '[Sample,TEST]', '-le'])
@patch('cat_win.cat.sys.argv',
['<CAT>', 'enc:UTF-8', test_file_path, '-ub', '[Sample,TEST]', '-le'])
def test_cat_output_full_c(self):
expected_output = ''
with open(test_result_C, 'r', encoding='utf-8') as output:
Expand All @@ -56,7 +57,8 @@ def test_cat_output_full_c(self):
cat.main()
self.assertEqual(fake_out.getvalue(), expected_output)

@patch('cat_win.cat.sys.argv', ['<CAT>', 'enc=utf-8', test_file_path, 'trunc=1:6', '-n', '--reverse', '--chr'])
@patch('cat_win.cat.sys.argv',
['<CAT>', 'enc=utf-8', test_file_path, 'trunc=1:6', '-n', '--reverse', '--chr'])
def test_cat_output_full_d(self):
expected_output = ''
with open(test_result_D, 'r', encoding='utf-8') as output:
Expand Down Expand Up @@ -90,21 +92,25 @@ def test_cat_output_full_latin1(self):
cat.main()
self.assertEqual(fake_out.getvalue(), expected_output)

@patch('cat_win.cat.sys.argv', ['<CAT>', test_file_path, 'enc=utf-8', 'trunc=0:1', 'find=ple ', 'match=:'])
@patch('cat_win.cat.sys.argv',
['<CAT>', test_file_path, 'enc=utf-8', 'trunc=0:1', 'find=ple ', 'match=:'])
def test_cat_output_full_find_found(self):
expected_output = "Sample Text:\n--------------- Found [('ple ', [3, 7])] ---------------\n--------------- Matched [(':', [11, 12])] ---------------\n"
expected_output = "Sample Text:\n--------------- Found [('ple ', [3, 7])] ------"
expected_output += "---------\n--------------- Matched [(':', [11, 12])] ---------------\n"
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), expected_output)

@patch('cat_win.cat.sys.argv', ['<CAT>', test_file_path, 'enc=utf-8', 'trunc=0:1', 'find=NOTINLINE'])
@patch('cat_win.cat.sys.argv',
['<CAT>', test_file_path, 'enc=utf-8', 'trunc=0:1', 'find=NOTINLINE'])
def test_cat_output_full_find_not_found(self):
expected_output = 'Sample Text:\n'
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
self.assertEqual(fake_out.getvalue(), expected_output)

@patch('cat_win.cat.sys.argv', ['<CAT>', test_file_path, 'enc=utf-8', 'trunc=0:1', 'find=Text', '--nk'])
@patch('cat_win.cat.sys.argv',
['<CAT>', test_file_path, 'enc=utf-8', 'trunc=0:1', 'find=Text', '--nk'])
def test_cat_output_full_find_found_nokeyword(self):
expected_output = ''
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
Expand Down Expand Up @@ -162,7 +168,8 @@ def test_cat_output_full_show_count(self):
self.assertIn(test_empty_path, fake_out.getvalue())
self.assertIn(test_peek, fake_out.getvalue())

@patch('cat_win.cat.sys.argv', ['<CAT>', test_file_path, 'trunc=0:0', '--UNIQUE', '--b64', '-?'])
@patch('cat_win.cat.sys.argv',
['<CAT>', test_file_path, 'trunc=0:0', '--UNIQUE', '--b64', '-?'])
def test_cat_output_suggestions(self):
with patch('cat_win.cat.sys.stderr', new=StdOutMockIsAtty()) as fake_out:
cat.main()
Expand All @@ -173,7 +180,7 @@ def test_cat_output_suggestions(self):
self.assertIn("Unknown argument: '-?'", fake_out.getvalue())

@patch('cat_win.cat.sys.argv', ['<CAT>', test_file_path, '--GREP', 'find=T'])
def test_cat_output_GREP(self):
def test_cat_output_grep_upper(self):
expected_output = 'T\nT,T\nT\nT\nT\nT\n'
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
cat.main()
Expand Down
29 changes: 19 additions & 10 deletions cat_win/tests/test_holder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import TestCase
import os

from cat_win.const.argconstants import ARGS_B64E, ARGS_NOCOL, ARGS_LLENGTH, ARGS_NUMBER, ARGS_TABS, ARGS_ENDS, ARGS_REPLACE
from cat_win.const.argconstants import ARGS_B64E, ARGS_NOCOL, ARGS_LLENGTH
from cat_win.const.argconstants import ARGS_NUMBER, ARGS_TABS, ARGS_ENDS, ARGS_REPLACE
from cat_win.util.holder import Holder, reduce_list, diff_list
# import sys
# sys.path.append('../cat_win')
Expand Down Expand Up @@ -99,12 +100,17 @@ def test__get_file_display_name(self):
holder = Holder()
holder.set_temp_file_stdin('STDINFILE')
holder.set_temp_file_echo('TEMPFILEECHO')
holder.set_files([test_file_edge_case_3, 'STDINFILE', test_file_edge_case_4, 'TEMPFILEECHO'])

self.assertEqual(holder._get_file_display_name('STDINFILE'), '<STDIN>')
self.assertEqual(holder._get_file_display_name('TEMPFILEECHO'), '<ECHO>')
self.assertEqual(holder._get_file_display_name(test_file_edge_case_3), test_file_edge_case_3)
self.assertEqual(holder._get_file_display_name(test_file_edge_case_4), test_file_edge_case_4)
holder.set_files([test_file_edge_case_3, 'STDINFILE',
test_file_edge_case_4, 'TEMPFILEECHO'])

self.assertEqual(
holder._get_file_display_name('STDINFILE'), '<STDIN>')
self.assertEqual(
holder._get_file_display_name('TEMPFILEECHO'), '<ECHO>')
self.assertEqual(
holder._get_file_display_name(test_file_edge_case_3), test_file_edge_case_3)
self.assertEqual(
holder._get_file_display_name(test_file_edge_case_4), test_file_edge_case_4)

def test_set_decoding_temp_files(self):
holder = Holder()
Expand All @@ -122,7 +128,8 @@ def test_add_args(self):
self.assertEqual(holder.args_id.count(True), 2)

holder.add_args([(ARGS_TABS, 'c')])
self.assertListEqual(holder.args, [(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b'), (ARGS_TABS, 'c')])
self.assertListEqual(holder.args,
[(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b'), (ARGS_TABS, 'c')])
self.assertEqual(holder.args_id.count(True), 3)

def test_delete_args(self):
Expand All @@ -143,11 +150,13 @@ def test_reduce_list(self):

test_list += [(ARGS_NUMBER, 'c'), (ARGS_ENDS, 'd')]
reduced_list = reduce_list(test_list)
self.assertListEqual(reduced_list, [(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b'), (ARGS_ENDS, 'd')])
self.assertListEqual(reduced_list,
[(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b'), (ARGS_ENDS, 'd')])

test_list += [(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b'), (ARGS_NUMBER, 'c'), (ARGS_ENDS, 'd')]
reduced_list = reduce_list(test_list)
self.assertListEqual(reduced_list, [(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b'), (ARGS_ENDS, 'd')])
self.assertListEqual(reduced_list,
[(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b'), (ARGS_ENDS, 'd')])

def test_diff_list(self):
test_list = [(ARGS_NUMBER, 'a'), (ARGS_LLENGTH, 'b')]
Expand Down
3 changes: 2 additions & 1 deletion cat_win/tests/test_rawviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def test_mode_x_upper_colored(self):
*000000A0! 65 21 0D 0A 54 68 69 73 20 4C 69 6E 65 20 69 73 *#! e ! ␍ ␤ T h i s L i n e i s
*000000B0! 20 61 20 44 75 70 6C 69 63 61 74 65 21 *#! a D u p l i c a t e !"""

self.assertEqual('\n'.join(get_raw_view_lines_gen(test_file_path, 'X', ['*', '!'])), expected_result)
self.assertEqual('\n'.join(get_raw_view_lines_gen(test_file_path, 'X', ['*', '!'])),
expected_result)

def test_encoding_error(self):
result = '\n'.join(get_raw_view_lines_gen(test_file_path, 'X', None, 'utf-16'))
Expand Down
3 changes: 2 additions & 1 deletion cat_win/tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def test_cat_shell_add_param(self):

def test_cat_shell_delete_param(self):
stdinhelpermock.set_content('abc\n!add -ln\n!del -l\nabc')
expected_output = ['abc', "successfully added ['-l', '-n'].", "successfully removed ['-l'].", '2) abc']
expected_output = ['abc', "successfully added ['-l', '-n'].",
"successfully removed ['-l'].", '2) abc']
with patch('cat_win.cat.sys.stdout', new=StdOutMock()) as fake_out:
cat.shell_main()
fake_output = [line.lstrip('>>> ') for line in fake_out.getvalue().splitlines()[2:-1]]
Expand Down
4 changes: 3 additions & 1 deletion cat_win/tests/test_stringfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def test_find_keywords(self):
line = 'ThisIsATest!1!'
intervals, f_keywords, m_keywords = string_finder.find_keywords(line)

self.assertCountEqual(intervals, [[14, 'reset_matched'], [12, 'matched_pattern'], [11, 'reset_found'], [7, 'found_keyword'], [6, 'reset_found'], [4, 'found_keyword']])
self.assertCountEqual(intervals, [[14, 'reset_matched'], [12, 'matched_pattern'],
[11, 'reset_found'], [7, 'found_keyword'],
[6, 'reset_found'], [4, 'found_keyword']])
self.assertCountEqual(f_keywords, [('Is', [4, 6]), ('Test', [7, 11])])
self.assertCountEqual(m_keywords, [(r"[0-9]\!", [12, 14])])
6 changes: 3 additions & 3 deletions cat_win/tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def test_sep_valid_urls(self):
'ftp:///test_url;param=1?query#fragment',
'https://www google.com',
]
input = valid_expected+invalid_expected
random.shuffle(input)
valid_output, invalid_output = sep_valid_urls(input)
test_input = valid_expected+invalid_expected
random.shuffle(test_input)
valid_output, invalid_output = sep_valid_urls(test_input)
self.assertCountEqual(valid_expected, valid_output)
self.assertCountEqual(invalid_expected, invalid_output)

Expand Down

0 comments on commit 72951d1

Please sign in to comment.