Skip to content

Commit cbf5e0d

Browse files
authored
Merge pull request #6042 from Textualize/content-tests
Content tests
2 parents 1186db8 + 78e7b1c commit cbf5e0d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/textual/widgets/_static.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,25 @@ def __init__(
5353

5454
@property
5555
def visual(self) -> Visual:
56+
"""The visual to be displayed.
57+
58+
Note that the visual is what is ultimately rendered in the widget, but may not be the
59+
same object set with the `update` method or `content` property. For instance, if you
60+
update with a string, then the visual will be a [Content][textual.content.Content] instance.
61+
62+
"""
5663
if self._visual is None:
5764
self._visual = visualize(self, self._content, markup=self._render_markup)
5865
return self._visual
5966

6067
@property
6168
def content(self) -> VisualType:
62-
"""The content set in the constructor."""
69+
"""The original content set in the constructor."""
6370
return self._content
6471

6572
@content.setter
6673
def content(self, content: VisualType) -> None:
74+
self._content = content
6775
self._visual = visualize(self, content, markup=self._render_markup)
6876
self.clear_cached_dimensions()
6977
self.refresh(layout=True)
@@ -77,11 +85,11 @@ def render(self) -> RenderResult:
7785
return self.visual
7886

7987
def update(self, content: VisualType = "", *, layout: bool = True) -> None:
80-
"""Update the widget's content area with new text or Rich renderable.
88+
"""Update the widget's content area with a string, a Visual (such as [Content][textual.content.Content]), or a [Rich renderable](https://rich.readthedocs.io/en/latest/protocol.html).
8189
8290
Args:
8391
content: New content.
84-
layout: Also perform a layout operation (set to `False` if you are certain the size won't change.)
92+
layout: Also perform a layout operation (set to `False` if you are certain the size won't change).
8593
"""
8694

8795
self._content = content

tests/test_static.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from textual.content import Content
2+
from textual.widgets import Static
3+
4+
5+
def test_content_property():
6+
static = Static()
7+
assert static.content == ""
8+
static.content = "Foo"
9+
assert static.content == "Foo"
10+
assert isinstance(static.content, str)
11+
assert static.visual == "Foo"
12+
assert isinstance(static.visual, Content)
13+
14+
static.update("Hello")
15+
assert static.content == "Hello"
16+
assert isinstance(static.content, str)
17+
assert static.visual == "Hello"
18+
assert isinstance(static.visual, Content)

0 commit comments

Comments
 (0)