Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] How can be tested the way a Text object is rendered? #3310

Closed
mmartinortiz opened this issue Mar 25, 2024 · 4 comments
Closed

[QUESTION] How can be tested the way a Text object is rendered? #3310

mmartinortiz opened this issue Mar 25, 2024 · 4 comments

Comments

@mmartinortiz
Copy link

Hi,

I'd like to test that a Text object is rendered in a specific way. For example, given the following function:

def greetings(who: str) -> Text:
    greeting = Text("Hello")
    return greeting.append(f" {who}", style="blue")

I would like to write a test that makes sure that the format is as expected, kind of:

def test_greetings():
    assert greetings("Jon") == "Hello [blue]Jon[/blue]"

Looking into possible solution I found this suggestion about capturing the output. In that case, I can compare with something like Hello\x1b[34m Jon\x1b[0m\n, but I find this a bit cryptic for a test.

Copy link

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

Text objects have markup attribute which returns the equivalent console markup.

@mmartinortiz
Copy link
Author

Thanks,

That is exactly what I was looking for. As reference, this is how the test would look like:

def test_greetings():
    assert greetings("Jon").markup == "Hello [blue]Jon[/blue]"

Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants