Improved Command class management.
This commit is contained in:
@@ -33,9 +33,15 @@ class TestCommandDefault:
|
||||
assert command.description == 'Command description'
|
||||
assert command.execute() == "Hello World"
|
||||
|
||||
def test_command_are_registered(self):
|
||||
def test_commands_are_registered(self):
|
||||
command = Command('test', 'Command description', None, callback)
|
||||
assert CommandsManager.commands.get(str(command.id)) is command
|
||||
|
||||
def test_commands_with_the_same_key_share_the_same_id(self):
|
||||
command1 = Command('test', 'Command description', None, None, key="test_key")
|
||||
command2 = Command('test', 'Command description', None, None, key="test_key")
|
||||
|
||||
assert command1.id is command2.id
|
||||
|
||||
|
||||
class TestCommandBind:
|
||||
@@ -74,7 +80,7 @@ class TestCommandBind:
|
||||
|
||||
res = command.execute()
|
||||
|
||||
assert res == ["another callback result", ("hello", "new value")]
|
||||
assert res == ["another callback result", "hello", "new value"]
|
||||
|
||||
def test_i_can_bind_a_command_to_an_observable_2(self):
|
||||
data = Data("hello")
|
||||
@@ -92,7 +98,7 @@ class TestCommandBind:
|
||||
|
||||
res = command.execute()
|
||||
|
||||
assert res == ["another 1", "another 2", ("hello", "new value")]
|
||||
assert res == ["another 1", "another 2", "hello", "new value"]
|
||||
|
||||
def test_by_default_swap_is_set_to_outer_html(self):
|
||||
command = Command('test', 'Command description', None, callback)
|
||||
@@ -120,6 +126,15 @@ class TestCommandBind:
|
||||
assert "hx_swap_oob" not in res[0].attrs
|
||||
assert res[1].attrs["hx-swap-oob"] == "true"
|
||||
assert res[3].attrs["hx-swap-oob"] == "true"
|
||||
|
||||
def test_i_can_send_parameters(self):
|
||||
command = Command('test', 'Command description', None, None, kwargs={"param": "value"}) # callback is not important
|
||||
elt = Button()
|
||||
updated = command.bind_ft(elt)
|
||||
|
||||
hx_vals = updated.attrs["hx-vals"]
|
||||
assert 'param' in hx_vals
|
||||
assert hx_vals['param'] == 'value'
|
||||
|
||||
|
||||
class TestCommandExecute:
|
||||
@@ -137,7 +152,7 @@ class TestCommandExecute:
|
||||
def callback_with_param(param):
|
||||
return f"Hello {param}"
|
||||
|
||||
command = Command('test', 'Command description', None, callback_with_param, "world")
|
||||
command = Command('test', 'Command description', None, callback_with_param, args=["world"])
|
||||
assert command.execute() == "Hello world"
|
||||
|
||||
def test_i_can_execute_a_command_with_open_parameter(self):
|
||||
@@ -188,9 +203,9 @@ class TestCommandExecute:
|
||||
class TestLambaCommand:
|
||||
|
||||
def test_i_can_create_a_command_from_lambda(self):
|
||||
command = LambdaCommand(None, lambda resp: "Hello World")
|
||||
command = LambdaCommand(None, lambda: "Hello World")
|
||||
assert command.execute() == "Hello World"
|
||||
|
||||
def test_by_default_target_is_none(self):
|
||||
command = LambdaCommand(None, lambda resp: "Hello World")
|
||||
command = LambdaCommand(None, lambda: "Hello World")
|
||||
assert command.get_htmx_params()["hx-swap"] == "none"
|
||||
|
||||
58
tests/core/test_utils.py
Normal file
58
tests/core/test_utils.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
|
||||
from myfasthtml.core.utils import flatten
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input_args,expected,test_description", [
|
||||
# Simple list without nesting
|
||||
(([1, 2, 3],), [1, 2, 3], "simple list"),
|
||||
|
||||
# Nested list (one level)
|
||||
(([1, [2, 3], 4],), [1, 2, 3, 4], "nested list one level"),
|
||||
|
||||
# Nested tuple
|
||||
(((1, (2, 3), 4),), [1, 2, 3, 4], "nested tuple"),
|
||||
|
||||
# Mixed list and tuple
|
||||
(([1, (2, 3), [4, 5]],), [1, 2, 3, 4, 5], "mixed list and tuple"),
|
||||
|
||||
# Deeply nested structure
|
||||
(([1, [2, [3, [4, 5]]]],), [1, 2, 3, 4, 5], "deeply nested structure"),
|
||||
|
||||
# Empty list
|
||||
(([],), [], "empty list"),
|
||||
|
||||
# Empty nested lists
|
||||
(([1, [], [2, []], 3],), [1, 2, 3], "empty nested lists"),
|
||||
|
||||
# Preserves order
|
||||
(([[3, 1], [4, 2]],), [3, 1, 4, 2], "preserves order"),
|
||||
|
||||
# Strings (should not be iterated)
|
||||
((["hello", ["world"]],), ["hello", "world"], "strings not iterated"),
|
||||
|
||||
# Mixed types
|
||||
(([1, "text", [2.5, True], None],), [1, "text", 2.5, True, None], "mixed types"),
|
||||
|
||||
# Multiple arguments with lists
|
||||
(([1, 2], [3, 4], 5), [1, 2, 3, 4, 5], "multiple arguments with lists"),
|
||||
|
||||
# Scalar values only
|
||||
((1, 2, 3), [1, 2, 3], "scalar values only"),
|
||||
|
||||
# Mixed scalars and lists
|
||||
((1, [2, 3], 4, [5, 6]), [1, 2, 3, 4, 5, 6], "mixed scalars and lists"),
|
||||
|
||||
# Multiple nested arguments
|
||||
(([1, [2]], [3, [4]], 5), [1, 2, 3, 4, 5], "multiple nested arguments"),
|
||||
|
||||
# No arguments
|
||||
((), [], "no arguments"),
|
||||
|
||||
# Complex real-world example
|
||||
(([1, [2, 3], [[4, 5], [6, 7]], [[[8, 9]]], 10],), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "complex nesting"),
|
||||
])
|
||||
def test_i_can_flatten(input_args, expected, test_description):
|
||||
"""Test that flatten correctly handles various nested structures and arguments."""
|
||||
result = flatten(*input_args)
|
||||
assert result == expected, f"Failed for test case: {test_description}"
|
||||
Reference in New Issue
Block a user