Files
slic/tests/test_utils_argfwd.py
T
tligui_y a63e2e5bc5
Run CI Tests / test (push) Successful in 1m34s
Update tests/test_utils_argfwd.py
2025-08-06 16:59:48 +02:00

122 lines
3.9 KiB
Python

import inspect
import pytest
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from slic.utils.argfwd import *
@pytest.mark.parametrize("lst, index, expected", [
(['a', 'b', 'c', 'd'], 2, (['a', 'b'], ['c', 'd'])),
(['x', 'y'], 1, (['x'], ['y'])),
([], 0, ([], [])),
])
def test_split_at(lst, index, expected):
assert split_at(lst, index) == expected
@pytest.mark.parametrize("a, b, expected", [
(['a', 'b'], ['b', 'c'], ['a', 'b', 'c']),
([], ['x'], ['x']),
(['x', 'y'], ['x', 'y'], ['x', 'y']),
])
def test_merge_lists_unique(a, b, expected):
assert merge_lists_unique(a, b) == expected
@pytest.mark.parametrize("a, b, expected", [
({'a': 1}, {'b': 2}, {'a': 1, 'b': 2}),
({'x': 1}, {'x': 9, 'y': 3}, {'x': 1, 'y': 3}),
({}, {'k': 4}, {'k': 4}),
])
def test_merge_dicts_unique(a, b, expected):
assert merge_dicts_unique(a, b) == expected
@pytest.mark.parametrize("pos, expected_names", [
(['a', 'b'], ['a', 'b']),
(['param1', 'value_2', 'Z'], ['param1', 'value_2', 'Z']),
([], []),
])
def test_make_params_pos_basic(pos, expected_names):
params = make_params_pos(pos)
assert all(isinstance(p, inspect.Parameter) for p in params)
assert [p.name for p in params] == expected_names
assert all(p.default == inspect._empty for p in params)
assert all(p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD for p in params)
@pytest.mark.parametrize("kw, expected_keys, expected_defaults", [
({'a': 1, 'b': 2}, ['a', 'b'], [1, 2]),
({'param_x': 0}, ['param_x'], [0]),
({}, [], []),
])
def test_make_params_kw_basic(kw, expected_keys, expected_defaults):
params = make_params_kw(kw)
assert all(isinstance(p, inspect.Parameter) for p in params)
assert [p.name for p in params] == expected_keys
assert [p.default for p in params] == expected_defaults
assert all(p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD for p in params)
@pytest.mark.parametrize("pos, kw, expected_signature", [
(['x', 'y'], {'z': 3}, '(x, y, z=3)'),
(['a'], {'b': 1, 'c': 2}, '(a, b=1, c=2)'),
([], {'flag': False}, '(flag=False)'),
])
def test_make_signature_parametrized(pos, kw, expected_signature):
sig = make_signature(pos, kw)
assert isinstance(sig, inspect.Signature)
assert str(sig) == expected_signature
@pytest.mark.parametrize("func, expected_pos, expected_kw", [
(lambda a, b, c=3, *, d=4: None, ['a', 'b'], {'c': 3, 'd': 4}),
(lambda x=1, *, y, z=0: None, [], {'x': 1, 'y': inspect._empty, 'z': 0}),
(lambda *args, **kwargs: None, [], {}),
])
def test_get_args_parametrized(func, expected_pos, expected_kw):
pos, kw = get_args(func)
assert pos == expected_pos
for k, v in expected_kw.items():
if v is inspect._empty:
assert k in kw and kw[k] == inspect._empty
else:
assert kw.get(k) == v
# Test : forwards_to
def func_target(a, b, c=10, d=20):
return a + b + c + d
@forwards_to(func_target, nfilled=0)
def wrap_all(a, b, *args, d=30, **kwargs):
return func_target(a, b, *args, d=d, **kwargs)
@forwards_to(func_target, nfilled=2)
def wrap_skip(a, b, *args, **kwargs):
return func_target(1, 2, *args, **kwargs)
@forwards_to(func_target, nfilled=4)
def wrap_ignore_all(x, y):
return func_target(1, 2, 3, 4)
@pytest.mark.parametrize("func, expected_sig", [
(wrap_all, '(a, b, d=30, c=10)'),
(wrap_skip, '(a, b, c=10, d=20)'),
(wrap_ignore_all, '(x, y, c=10, d=20)'),
])
def test_signature_visible(func, expected_sig):
assert str(inspect.signature(func)) == expected_sig
@pytest.mark.parametrize("func, args, kwargs, expected_result", [
(wrap_all, (1, 2, 3), {}, 1+2+3+30),
(wrap_all, (1, 2, 3), {'d': 5}, 1+2+3+5),
(wrap_skip, (0, 0), {'c': 3, 'd': 4}, 1+2+3+4),
(wrap_ignore_all, (0, 0), {}, 1+2+3+4),
])
def test_wrapper_behavior(func, args, kwargs, expected_result):
assert func(*args, **kwargs) == expected_result