Skip to content

Commit 2882ae9

Browse files
committed
fixing #40 - beat logging text should use single quotes
1 parent e91145e commit 2882ae9

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

screenpy_selenium/actions/enter.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from screenpy.exceptions import DeliveryError, UnableToAct
99
from screenpy.pacing import aside, beat
10+
from screenpy.speech_tools import represent_prop
1011
from selenium.common.exceptions import WebDriverException
1112

1213
from ..common import pos_args_deprecated
@@ -36,7 +37,7 @@ class Enter:
3637
target: Target | None
3738
following_keys: list[str]
3839
text: str
39-
text_to_log: str
40+
mask: bool
4041

4142
@classmethod
4243
def the_text(cls, text: str) -> Self:
@@ -109,11 +110,22 @@ def then_press(self, *keys: str) -> Self:
109110
"""Alias for :meth:`~screenpy_selenium.actions.Enter.then_hit`."""
110111
return self.then_hit(*keys)
111112

113+
@property
114+
def text_to_log(self) -> str:
115+
"""Get a proper representation of the text."""
116+
if self.mask:
117+
altered_text = "[CENSORED]"
118+
else:
119+
altered_text = self.text
120+
for value, keyname in KEY_NAMES.items():
121+
altered_text = altered_text.replace(value, keyname)
122+
return represent_prop(altered_text)
123+
112124
def describe(self) -> str:
113125
"""Describe the Action in present tense."""
114-
return f'Enter "{self.text_to_log}" into the {self.target}.'
126+
return f"Enter {self.text_to_log} into the {self.target}."
115127

116-
@beat('{} enters "{text_to_log}" into the {target}.')
128+
@beat("{} enters {text_to_log} into the {target}.")
117129
def perform_as(self, the_actor: Actor) -> None:
118130
"""Direct the Actor to enter the text into the element."""
119131
if self.target is None:
@@ -137,7 +149,7 @@ def perform_as(self, the_actor: Actor) -> None:
137149
)
138150
raise DeliveryError(msg) from e
139151

140-
@beat(' Enter "{text_to_log}" into the {target}!')
152+
@beat(" Enter {text_to_log} into the {target}!")
141153
def add_to_chain(self, the_actor: Actor, the_chain: ActionChains) -> None:
142154
"""Add the Enter Action to a Chain of Actions."""
143155
if self.target is None:
@@ -155,11 +167,4 @@ def __init__(self, text: str, mask: bool = False) -> None: # noqa: FBT001, FBT0
155167
self.text = text
156168
self.target = None
157169
self.following_keys = []
158-
159-
if mask:
160-
self.text_to_log = "[CENSORED]"
161-
else:
162-
altered_text = text
163-
for value, keyname in KEY_NAMES.items():
164-
altered_text = altered_text.replace(value, keyname)
165-
self.text_to_log = altered_text
170+
self.mask = mask

tests/test_actions.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import logging
34
import warnings
45
from contextlib import contextmanager
56
from typing import TYPE_CHECKING, Generator, cast
@@ -358,7 +359,7 @@ def test_secret_masks_text(self) -> None:
358359
e = Enter.the_secret(text)
359360

360361
assert e.text == text
361-
assert e.text_to_log == "[CENSORED]"
362+
assert e.text_to_log == "'[CENSORED]'"
362363

363364
def test_text_to_log_humanizes_keys(self) -> None:
364365
"""unicode key values are turned into human-readable text"""
@@ -431,11 +432,16 @@ def test_exception(self, Tester: Actor) -> None:
431432

432433
def test_describe(self) -> None:
433434
assert (
434-
Enter("blah").into(TARGET).describe() == f'Enter "blah" into the {TARGET}.'
435+
Enter("blah").into(TARGET).describe() == f"Enter 'blah' into the {TARGET}."
435436
)
436437
assert (
437438
Enter.the_secret("blah").into(TARGET).describe()
438-
== f'Enter "[CENSORED]" into the {TARGET}.'
439+
== f"Enter '[CENSORED]' into the {TARGET}."
440+
)
441+
442+
assert (
443+
Enter("\ue008 \ue004 \ue007").into(TARGET).describe()
444+
== f"Enter 'SHIFT TAB ENTER' into the {TARGET}."
439445
)
440446

441447
def test_subclass(self) -> None:
@@ -447,6 +453,32 @@ def new_method(self) -> bool:
447453

448454
assert SubEnter.the_text("blah").new_method() is True
449455

456+
def test_beat_logging(
457+
self, Tester: Actor, caplog: pytest.LogCaptureFixture
458+
) -> None:
459+
target, element = get_mocked_target_and_element()
460+
text = 'Speak "Friend" and Enter'
461+
caplog.set_level(logging.INFO)
462+
Enter.the_text(text).into_the(target).perform_as(Tester)
463+
464+
assert [r.msg for r in caplog.records] == [
465+
f"Tester enters 'Speak \"Friend\" and Enter' into the {target}."
466+
]
467+
468+
def test_beat_logging_chain(
469+
self, Tester: Actor, caplog: pytest.LogCaptureFixture
470+
) -> None:
471+
chain = get_mocked_chain()
472+
target, element = get_mocked_target_and_element()
473+
text = "Hello, Champion City."
474+
475+
caplog.set_level(logging.INFO)
476+
Enter.the_text(text).into_the(target).add_to_chain(Tester, chain)
477+
478+
assert [r.msg for r in caplog.records] == [
479+
f" Enter 'Hello, Champion City.' into the {target}!"
480+
]
481+
450482
def test_positional_arg_warns(self) -> None:
451483
with pytest.warns(DeprecationWarning):
452484
Enter("", True)

0 commit comments

Comments
 (0)