Start implementing new tests
This commit is contained in:
parent
7cb4202a64
commit
4281d55b77
2
setup.py
2
setup.py
@ -12,7 +12,7 @@ setup(
|
|||||||
description=description,
|
description=description,
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
package_dir={'': 'src'},
|
package_dir={'': 'magicli'},
|
||||||
py_modules=[
|
py_modules=[
|
||||||
'magicli',
|
'magicli',
|
||||||
],
|
],
|
||||||
|
|||||||
23
tests/_test_help_message.py
Normal file
23
tests/_test_help_message.py
Normal file
@ -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
|
||||||
11
tests/test_break_lines.py
Normal file
11
tests/test_break_lines.py
Normal file
@ -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.',
|
||||||
|
]
|
||||||
23
tests/test_cast_types.py
Normal file
23
tests/test_cast_types.py
Normal file
@ -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})
|
||||||
6
tests/test_filter_functions.py
Normal file
6
tests/test_filter_functions.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from magicli.magicli import filter_functions
|
||||||
|
|
||||||
|
|
||||||
|
def test_one_function():
|
||||||
|
functions = filter_functions(globals())
|
||||||
|
assert functions[0] == test_one_function
|
||||||
5
tests/test_first_calling_frame.py
Normal file
5
tests/test_first_calling_frame.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from magicli.magicli import first_calling_frame
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_calling_frame():
|
||||||
|
assert first_calling_frame() == None
|
||||||
8
tests/test_format_args.py
Normal file
8
tests/test_format_args.py
Normal file
@ -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)
|
||||||
35
tests/test_format_block.py
Normal file
35
tests/test_format_block.py
Normal file
@ -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.'
|
||||||
10
tests/test_format_help_message.py
Normal file
10
tests/test_format_help_message.py
Normal file
@ -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': [['', '']]}])
|
||||||
12
tests/test_get_config.py
Normal file
12
tests/test_get_config.py
Normal file
@ -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}
|
||||||
@ -1,23 +1,20 @@
|
|||||||
|
from magicli.magicli import help_message
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
import pytest
|
|
||||||
from magicli import magicli
|
|
||||||
|
|
||||||
|
|
||||||
def test_help_message(capsys):
|
def get_terminal_size():
|
||||||
inputs = [
|
return (80, 20)
|
||||||
'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 test_minimal_example():
|
||||||
def command_with_arguments(positional, optional=True):
|
config = {
|
||||||
pass
|
'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')
|
||||||
|
|||||||
6
tests/test_make_lines.py
Normal file
6
tests/test_make_lines.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from magicli.magicli import make_lines
|
||||||
|
|
||||||
|
|
||||||
|
def test_minimal_example():
|
||||||
|
config = {'': {}}
|
||||||
|
assert make_lines(config) == [['', '']]
|
||||||
9
tests/test_short_options.py
Normal file
9
tests/test_short_options.py
Normal file
@ -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
|
||||||
|
}
|
||||||
14
tests/test_truncate_docstring.py
Normal file
14
tests/test_truncate_docstring.py
Normal file
@ -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"
|
||||||
Loading…
x
Reference in New Issue
Block a user