add tests for style

This commit is contained in:
Will McGugan 2020-10-10 18:12:44 +01:00
parent 327d642b84
commit a236d8fd63
3 changed files with 36 additions and 5 deletions

View File

@ -312,7 +312,7 @@ class Segment(NamedTuple):
yield segment
else:
text, style, _is_control = segment
yield cls(text, style.copy(link=None) if style else None)
yield cls(text, style.update_link(None) if style else None)
@classmethod
def strip_styles(cls, segments: Iterable["Segment"]) -> Iterable["Segment"]:

View File

@ -501,7 +501,7 @@ class Style:
iter_styles = iter(styles)
return sum(iter_styles, next(iter_styles))
def copy(self, link: str = None) -> "Style":
def copy(self) -> "Style":
"""Get a copy of this style.
Returns:
@ -516,9 +516,30 @@ class Style:
style._bgcolor = self._bgcolor
style._attributes = self._attributes
style._set_attributes = self._set_attributes
_link = self._link if link is None else link
style._link = _link
style._link_id = f"{time()}-{randint(0, 999999)}" if _link else ""
style._link = self._link
style._link_id = f"{time()}-{randint(0, 999999)}" if self._link else ""
style._hash = self._hash
style._null = False
return style
def update_link(self, link: str = None) -> "Style":
"""Get a copy with a different value for link.
Args:
link (str, optional): New value for link. Defaults to None.
Returns:
Style: A new Style instance.
"""
style = self.__new__(Style)
style._ansi = self._ansi
style._style_definition = self._style_definition
style._color = self._color
style._bgcolor = self._bgcolor
style._attributes = self._attributes
style._set_attributes = self._set_attributes
style._link = link
style._link_id = f"{time()}-{randint(0, 999999)}" if link else ""
style._hash = self._hash
style._null = False
return style

View File

@ -86,3 +86,13 @@ def test_filter_control():
assert list(Segment.filter_control(segments, is_control=True)) == [
Segment("bar", is_control=True)
]
def test_strip_styles():
segments = [Segment("foo", Style(bold=True))]
assert list(Segment.strip_styles(segments)) == [Segment("foo", None)]
def test_strip_links():
segments = [Segment("foo", Style(bold=True, link="https://www.example.org"))]
assert list(Segment.strip_links(segments)) == [Segment("foo", Style(bold=True))]