49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
from pathlib import Path
|
|
|
|
from engine.config import DEFAULT_KEYBINDINGS, KeyBindings
|
|
|
|
CONF_PATH = Path(__file__).resolve().parent.parent / "config" / "keybindings.conf"
|
|
|
|
|
|
def test_default_keybindings_used_when_file_missing(tmp_path):
|
|
bindings = KeyBindings.load(tmp_path / "does_not_exist.conf")
|
|
assert bindings.action_for_key("w") == "move_up"
|
|
assert bindings.action_for_key("ArrowUp") == "move_up"
|
|
|
|
|
|
def test_space_alias_normalizes_to_the_real_key_value():
|
|
bindings = KeyBindings.load(Path("/nonexistent"))
|
|
assert bindings.action_for_key(" ") == "leap"
|
|
|
|
|
|
def test_multiple_keys_share_one_action(tmp_path):
|
|
conf = tmp_path / "keybindings.conf"
|
|
conf.write_text("[movement]\nmove_up = w, k, ArrowUp\n")
|
|
bindings = KeyBindings.load(conf)
|
|
assert bindings.action_for_key("w") == "move_up"
|
|
assert bindings.action_for_key("k") == "move_up"
|
|
assert bindings.action_for_key("ArrowUp") == "move_up"
|
|
assert bindings.keys_for_action("move_up") == ["w", "k", "ArrowUp"]
|
|
|
|
|
|
def test_file_overrides_only_the_actions_it_mentions(tmp_path):
|
|
conf = tmp_path / "keybindings.conf"
|
|
conf.write_text("[actions]\nleap = j\n")
|
|
bindings = KeyBindings.load(conf)
|
|
assert bindings.action_for_key("j") == "leap"
|
|
assert bindings.action_for_key(" ") is None # default "space" binding was replaced, not merged
|
|
assert bindings.action_for_key("w") == "move_up" # untouched defaults still present
|
|
|
|
|
|
def test_unbound_key_returns_none():
|
|
bindings = KeyBindings.load(Path("/nonexistent"))
|
|
assert bindings.action_for_key("q") is None
|
|
|
|
|
|
def test_real_project_keybindings_conf_loads_and_covers_expected_actions():
|
|
bindings = KeyBindings.load(CONF_PATH)
|
|
for action in DEFAULT_KEYBINDINGS:
|
|
assert bindings.keys_for_action(action), f"missing action: {action}"
|
|
assert bindings.action_for_key("mouse_left") == "activate_right_hand"
|
|
assert bindings.action_for_key("shift+mouse_left") == "activate_right_hand_2"
|