Skip to content

Commit

Permalink
add tests for style
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Oct 10, 2020
1 parent 327d642 commit a236d8f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rich/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def strip_links(cls, segments: Iterable["Segment"]) -> Iterable["Segment"]:
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"]:
Expand Down
29 changes: 25 additions & 4 deletions rich/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def chain(cls, *styles: "Style") -> "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:
Expand All @@ -516,9 +516,30 @@ def copy(self, link: str = None) -> "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
Expand Down
10 changes: 10 additions & 0 deletions tests/test_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))]

0 comments on commit a236d8f

Please sign in to comment.