mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-11 14:07:49 +01:00
refactor(keyboard_coop): remove dead/unreachable code
- Remove unreachable force-submit check (line 351): available_letters always contains the clicked letter after add(letter) - Remove unreachable hover color branch (lines 398-404): the available check at line 396 catches all available letters first - Remove unused KEY_HOVER_COLOR constant - Remove unused mouse_pos variable in _draw_keyboard - Update test from hover to unavailable key color test Coverage: 97% -> 100%
This commit is contained in:
parent
4e64c3deef
commit
57233aa8d5
@ -26,7 +26,6 @@ SCREEN_HEIGHT = 768
|
|||||||
BACKGROUND_COLOR = (30, 30, 40)
|
BACKGROUND_COLOR = (30, 30, 40)
|
||||||
KEYBOARD_COLOR = (60, 60, 70)
|
KEYBOARD_COLOR = (60, 60, 70)
|
||||||
KEY_COLOR = (80, 80, 90)
|
KEY_COLOR = (80, 80, 90)
|
||||||
KEY_HOVER_COLOR = (100, 100, 110)
|
|
||||||
KEY_SELECTED_COLOR = (150, 150, 200)
|
KEY_SELECTED_COLOR = (150, 150, 200)
|
||||||
KEY_AVAILABLE_COLOR = (100, 150, 100)
|
KEY_AVAILABLE_COLOR = (100, 150, 100)
|
||||||
TEXT_COLOR = (255, 255, 255)
|
TEXT_COLOR = (255, 255, 255)
|
||||||
@ -346,10 +345,6 @@ class KeyboardCoopGame:
|
|||||||
f"Player {self.state.current_player + 1}: Choose an adjacent letter!"
|
f"Player {self.state.current_player + 1}: Choose an adjacent letter!"
|
||||||
)
|
)
|
||||||
|
|
||||||
# If no valid moves available, force word submission
|
|
||||||
if not self.keyboard.available_letters:
|
|
||||||
self._submit_word()
|
|
||||||
|
|
||||||
def _submit_word(self) -> None:
|
def _submit_word(self) -> None:
|
||||||
"""Submit the current word and check if it's valid."""
|
"""Submit the current word and check if it's valid."""
|
||||||
if len(self.state.current_word) >= MIN_WORD_LENGTH and self._is_valid_word(
|
if len(self.state.current_word) >= MIN_WORD_LENGTH and self._is_valid_word(
|
||||||
@ -387,19 +382,12 @@ class KeyboardCoopGame:
|
|||||||
|
|
||||||
def _draw_keyboard(self) -> None:
|
def _draw_keyboard(self) -> None:
|
||||||
"""Draw the virtual keyboard."""
|
"""Draw the virtual keyboard."""
|
||||||
mouse_pos = pygame.mouse.get_pos()
|
|
||||||
|
|
||||||
for letter, rect in self.keyboard.positions.items():
|
for letter, rect in self.keyboard.positions.items():
|
||||||
# Determine key color
|
# Determine key color
|
||||||
if letter in self.state.selected_letters:
|
if letter in self.state.selected_letters:
|
||||||
color = KEY_SELECTED_COLOR
|
color = KEY_SELECTED_COLOR
|
||||||
elif letter in self.keyboard.available_letters:
|
elif letter in self.keyboard.available_letters:
|
||||||
color = KEY_AVAILABLE_COLOR
|
color = KEY_AVAILABLE_COLOR
|
||||||
elif (
|
|
||||||
rect.collidepoint(mouse_pos)
|
|
||||||
and letter in self.keyboard.available_letters
|
|
||||||
):
|
|
||||||
color = KEY_HOVER_COLOR
|
|
||||||
else:
|
else:
|
||||||
color = KEY_COLOR
|
color = KEY_COLOR
|
||||||
|
|
||||||
|
|||||||
@ -760,15 +760,14 @@ class TestDrawKeyboard:
|
|||||||
colors_used = [call[0][1] for call in calls]
|
colors_used = [call[0][1] for call in calls]
|
||||||
assert KEY_SELECTED_COLOR in colors_used
|
assert KEY_SELECTED_COLOR in colors_used
|
||||||
|
|
||||||
def test_draw_keyboard_hover_color(self) -> None:
|
def test_draw_keyboard_unavailable_key_color(self) -> None:
|
||||||
"""Test hover color when mouse is over available key."""
|
"""Test unavailable keys get default key color."""
|
||||||
mock_pg = MagicMock()
|
mock_pg = MagicMock()
|
||||||
mock_pg.draw = MagicMock()
|
mock_pg.draw = MagicMock()
|
||||||
# Mouse is at position that collides with the key
|
|
||||||
mock_pg.mouse.get_pos.return_value = (100, 100)
|
|
||||||
|
|
||||||
with patch.dict("sys.modules", {"pygame": mock_pg}):
|
with patch.dict("sys.modules", {"pygame": mock_pg}):
|
||||||
from python_pkg.keyboard_coop.main import (
|
from python_pkg.keyboard_coop.main import (
|
||||||
|
KEY_COLOR,
|
||||||
FontSet,
|
FontSet,
|
||||||
GameState,
|
GameState,
|
||||||
KeyboardCoopGame,
|
KeyboardCoopGame,
|
||||||
@ -785,18 +784,18 @@ class TestDrawKeyboard:
|
|||||||
)
|
)
|
||||||
|
|
||||||
mock_rect_a = MagicMock()
|
mock_rect_a = MagicMock()
|
||||||
# Mouse collides with this rect
|
|
||||||
mock_rect_a.collidepoint.return_value = True
|
|
||||||
mock_rect_a.center = (100, 100)
|
mock_rect_a.center = (100, 100)
|
||||||
|
|
||||||
game.keyboard.positions = {"a": mock_rect_a}
|
game.keyboard.positions = {"a": mock_rect_a}
|
||||||
# Key is available (required for hover color)
|
# Key is NOT available - should get KEY_COLOR
|
||||||
game.keyboard.available_letters = {"a"}
|
game.keyboard.available_letters = set()
|
||||||
|
|
||||||
game._draw_keyboard()
|
game._draw_keyboard()
|
||||||
|
|
||||||
# draw.rect should have been called
|
# Check that KEY_COLOR was used for unavailable key
|
||||||
assert mock_pg.draw.rect.call_count >= 2
|
calls = mock_pg.draw.rect.call_args_list
|
||||||
|
colors_used = [call[0][1] for call in calls]
|
||||||
|
assert KEY_COLOR in colors_used
|
||||||
|
|
||||||
|
|
||||||
class TestDrawUI:
|
class TestDrawUI:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user