From 4281d55b77b3fd6355f7bbd8737b7ba0ecd2593e Mon Sep 17 00:00:00 2001 From: Patrick Elmer Date: Wed, 16 Aug 2023 15:46:44 +0900 Subject: [PATCH] Start implementing new tests --- TEST.py | 0 setup.py | 2 +- tests/{test_failures.py => _test_failures.py} | 0 ...unctionality.py => _test_functionality.py} | 0 tests/_test_help_message.py | 23 ++++++++++++ tests/test_break_lines.py | 11 ++++++ tests/test_cast_types.py | 23 ++++++++++++ tests/test_filter_functions.py | 6 ++++ tests/test_first_calling_frame.py | 5 +++ tests/test_format_args.py | 8 +++++ tests/test_format_block.py | 35 +++++++++++++++++++ tests/test_format_help_message.py | 10 ++++++ tests/test_get_config.py | 12 +++++++ tests/test_help_message.py | 35 +++++++++---------- tests/test_make_lines.py | 6 ++++ tests/test_short_options.py | 9 +++++ tests/test_truncate_docstring.py | 14 ++++++++ 17 files changed, 179 insertions(+), 20 deletions(-) create mode 100644 TEST.py rename tests/{test_failures.py => _test_failures.py} (100%) rename tests/{test_functionality.py => _test_functionality.py} (100%) create mode 100644 tests/_test_help_message.py create mode 100644 tests/test_break_lines.py create mode 100644 tests/test_cast_types.py create mode 100644 tests/test_filter_functions.py create mode 100644 tests/test_first_calling_frame.py create mode 100644 tests/test_format_args.py create mode 100644 tests/test_format_block.py create mode 100644 tests/test_format_help_message.py create mode 100644 tests/test_get_config.py create mode 100644 tests/test_make_lines.py create mode 100644 tests/test_short_options.py create mode 100644 tests/test_truncate_docstring.py diff --git a/TEST.py b/TEST.py new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index c2f1aaa..9907120 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( description=description, long_description=long_description, long_description_content_type="text/markdown", - package_dir={'': 'src'}, + package_dir={'': 'magicli'}, py_modules=[ 'magicli', ], diff --git a/tests/test_failures.py b/tests/_test_failures.py similarity index 100% rename from tests/test_failures.py rename to tests/_test_failures.py diff --git a/tests/test_functionality.py b/tests/_test_functionality.py similarity index 100% rename from tests/test_functionality.py rename to tests/_test_functionality.py diff --git a/tests/_test_help_message.py b/tests/_test_help_message.py new file mode 100644 index 0000000..7135ee6 --- /dev/null +++ b/tests/_test_help_message.py @@ -0,0 +1,23 @@ +from unittest import mock +import pytest +from magicli import magicli + + +def test_help_message(capsys): + inputs = [ + 'appname --help', + 'appname command --help', + 'appname command --name=Name --amount=3 --help', + ] + help_message='Usage:\n appname command_with_arguments --positional --optional\n' + for i in inputs: + args = i.split() + with mock.patch('sys.argv', args): + with pytest.raises(SystemExit): + magicli(exclude=['test_help_message']) + out, err = capsys.readouterr() + assert out.startswith(help_message) + + +def command_with_arguments(positional, optional=True): + pass \ No newline at end of file diff --git a/tests/test_break_lines.py b/tests/test_break_lines.py new file mode 100644 index 0000000..a08adfc --- /dev/null +++ b/tests/test_break_lines.py @@ -0,0 +1,11 @@ +from magicli.magicli import break_lines + + +def test_no_input(): + assert break_lines(None, 0) == [] + +def test_short_line(): + assert break_lines('This is a line.', 8) == [ + 'This is', + 'a line.', + ] diff --git a/tests/test_cast_types.py b/tests/test_cast_types.py new file mode 100644 index 0000000..4c770e0 --- /dev/null +++ b/tests/test_cast_types.py @@ -0,0 +1,23 @@ +from magicli.magicli import cast_types + + +def test_cast_string_in_args_to_int(): + args = ['2'] + config = { + 'arguments': {'amount': {'type': int}}, + 'options': {}, + } + assert cast_types(args, {}, config) == ([2], {}) + +def test_not_cast_string_in_args_to_int(): + args = ['2'] + config = {'arguments': {}, 'options': {}} + assert cast_types(args, {}, config) == (['2'], {}) + +def test_cast_string_in_kwargs_to_int(): + kwargs = {'bbb': '2'} + config = { + 'arguments': {}, + 'options': {'bbb': {'type': int}}, + } + assert cast_types([], kwargs, config) == ([], {'bbb': 2}) diff --git a/tests/test_filter_functions.py b/tests/test_filter_functions.py new file mode 100644 index 0000000..6e13646 --- /dev/null +++ b/tests/test_filter_functions.py @@ -0,0 +1,6 @@ +from magicli.magicli import filter_functions + + +def test_one_function(): + functions = filter_functions(globals()) + assert functions[0] == test_one_function diff --git a/tests/test_first_calling_frame.py b/tests/test_first_calling_frame.py new file mode 100644 index 0000000..c24d413 --- /dev/null +++ b/tests/test_first_calling_frame.py @@ -0,0 +1,5 @@ +from magicli.magicli import first_calling_frame + + +def test_no_calling_frame(): + assert first_calling_frame() == None diff --git a/tests/test_format_args.py b/tests/test_format_args.py new file mode 100644 index 0000000..a9136e5 --- /dev/null +++ b/tests/test_format_args.py @@ -0,0 +1,8 @@ +from magicli.magicli import format_args + + +def test_empty_argv(): + name, args, kwargs = format_args([]) + assert isinstance(name, str) + assert isinstance(args, list) + assert isinstance(kwargs, dict) diff --git a/tests/test_format_block.py b/tests/test_format_block.py new file mode 100644 index 0000000..c733fc5 --- /dev/null +++ b/tests/test_format_block.py @@ -0,0 +1,35 @@ +from magicli.magicli import format_block +from unittest import mock + + +def get_terminal_size(): + return (80, 20) + +def test_empty_lines(): + with mock.patch('os.get_terminal_size', get_terminal_size): + min_column_width=5 + indent=2 + gap=2 + assert format_block([['', '']], + min_column_width=min_column_width, + indent=indent, + gap=gap + ) == ' '*(min_column_width+indent+gap) + +def test_two_lines(): + with mock.patch('os.get_terminal_size', get_terminal_size): + assert format_block([ + ['--help', 'Show help message.'], + ['-v, --version', 'Show version information.'], + ], min_column_width=0) == \ + ' --help Show help message.\n' +\ + ' -v, --version Show version information.' + +def test_two_lines_with_minimum_width(): + with mock.patch('os.get_terminal_size', get_terminal_size): + assert format_block([ + ['--help', 'Show help message.'], + ['-v, --version', 'Show version information.'], + ], min_column_width=20) == \ + ' --help Show help message.\n' +\ + ' -v, --version Show version information.' diff --git a/tests/test_format_help_message.py b/tests/test_format_help_message.py new file mode 100644 index 0000000..97fd2d9 --- /dev/null +++ b/tests/test_format_help_message.py @@ -0,0 +1,10 @@ +from magicli.magicli import format_help_message +from unittest import mock + + +def get_terminal_size(): + return (80, 20) + +def test_(): + with mock.patch('os.get_terminal_size', get_terminal_size): + assert '\n\n' in format_help_message([{'lines': [['', '']]}, {'lines': [['', '']]}]) diff --git a/tests/test_get_config.py b/tests/test_get_config.py new file mode 100644 index 0000000..ee50030 --- /dev/null +++ b/tests/test_get_config.py @@ -0,0 +1,12 @@ +from magicli.magicli import get_config + + +def function_to_call(name, amount=1): ... + +def command(): ... + +def test_basic_config(): + config = get_config(function_to_call, [command]) + assert config['arguments'] == {'name': {}} + assert config['options']['amount'] == {'default': 1, 'type': int} + assert config['commands']['command'] == {'docstring': None} diff --git a/tests/test_help_message.py b/tests/test_help_message.py index 7135ee6..512f88a 100644 --- a/tests/test_help_message.py +++ b/tests/test_help_message.py @@ -1,23 +1,20 @@ +from magicli.magicli import help_message from unittest import mock -import pytest -from magicli import magicli -def test_help_message(capsys): - inputs = [ - 'appname --help', - 'appname command --help', - 'appname command --name=Name --amount=3 --help', - ] - help_message='Usage:\n appname command_with_arguments --positional --optional\n' - for i in inputs: - args = i.split() - with mock.patch('sys.argv', args): - with pytest.raises(SystemExit): - magicli(exclude=['test_help_message']) - out, err = capsys.readouterr() - assert out.startswith(help_message) +def get_terminal_size(): + return (80, 20) - -def command_with_arguments(positional, optional=True): - pass \ No newline at end of file +def test_minimal_example(): + config = { + 'name': '', + 'function': {'docstring': ''}, + 'arguments': {'argument': []}, + 'options': {}, + 'commands': {}, + 'settings': {'display_arguments_more_than': 0}, + } + with mock.patch('os.get_terminal_size', get_terminal_size): + assert isinstance(help_message(config), str) + assert help_message(config).startswith('Usage:') + assert help_message(config).rstrip().endswith('argument') diff --git a/tests/test_make_lines.py b/tests/test_make_lines.py new file mode 100644 index 0000000..6f90206 --- /dev/null +++ b/tests/test_make_lines.py @@ -0,0 +1,6 @@ +from magicli.magicli import make_lines + + +def test_minimal_example(): + config = {'': {}} + assert make_lines(config) == [['', '']] diff --git a/tests/test_short_options.py b/tests/test_short_options.py new file mode 100644 index 0000000..fb92bf1 --- /dev/null +++ b/tests/test_short_options.py @@ -0,0 +1,9 @@ +from magicli.magicli import replace_short_options + + +def test_short_argument(): + kwargs = {'h': True} + short_options = {'h': 'help'} + assert replace_short_options(kwargs, short_options) == { + 'help': True + } diff --git a/tests/test_truncate_docstring.py b/tests/test_truncate_docstring.py new file mode 100644 index 0000000..82b1dbc --- /dev/null +++ b/tests/test_truncate_docstring.py @@ -0,0 +1,14 @@ +from magicli.magicli import truncate_docstring + + +def test_no_docstring(): + assert truncate_docstring(None) == None + +def test_padded_multiline_string(): + assert truncate_docstring("""This + is + a docstring.""") == "This\nis\na docstring." + +def test_remove_bottom(): + assert truncate_docstring("""First line\n\nsecond line.""") == "First line" + assert truncate_docstring("""First line\n \nsecond line.""") == "First line"