Skip to content

Commit 15d75d7

Browse files
committed
Added test.
1 parent b33c80d commit 15d75d7

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

tests/pyscript/test_print.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import sys
77

8+
from rich.text import Text
9+
810
# Test multiple objects and sep
911
print("hello", "world", sep="-")
1012

@@ -16,6 +18,9 @@
1618
print(1, 2, 3, sep=":", end=".")
1719
print() # to get a newline
1820

21+
# Test printing a Rich object
22+
text = Text("I am Rich Text", style="blue")
23+
print(text)
1924

2025
# Test file=sys.stdout
2126
print("this goes to sys.stdout", file=sys.stdout)

tests/test_run_pyscript.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
import pytest
99

10+
from cmd2 import rich_utils as ru
1011
from cmd2.string_utils import quote
1112

1213
from .conftest import (
1314
odd_file_names,
1415
run_cmd,
16+
with_ansi_style,
1517
)
1618

1719

@@ -209,17 +211,19 @@ def test_run_pyscript_app_echo(base_app, request) -> None:
209211
assert out[0] == "Usage: edit [-h] [file_path]"
210212

211213

214+
@with_ansi_style(ru.AllowStyle.ALWAYS)
212215
def test_run_pyscript_print(base_app, request, capsys) -> None:
213216
"""Verify that py_print() (the print() replacement in pyscripts) works correctly."""
214217
test_dir = os.path.dirname(request.module.__file__)
215218
python_script = os.path.join(test_dir, "pyscript", "test_print.py")
216219
out, err = run_cmd(base_app, f"run_pyscript {python_script}")
217220

218221
# Verify contents of self.stdout
219-
assert len(out) == 3
222+
assert len(out) == 4
220223
assert out[0] == "hello-world"
221224
assert out[1] == "no newline here"
222225
assert out[2] == "1:2:3."
226+
assert out[3] == "\x1b[34mI am Rich Text\x1b[0m"
223227

224228
# Verify contents of sys.stderr
225229
assert len(err) == 1
@@ -245,9 +249,11 @@ def test_run_pyscript_print_redirection(base_app, request, tmp_path, capsys) ->
245249
content = f.read()
246250

247251
# Look for everything written to self.stdout
252+
assert len(content.splitlines()) == 4
248253
assert "hello-world\n" in content
249254
assert "no newline here\n" in content
250255
assert "1:2:3.\n" in content
256+
assert "I am Rich Text\n" in content
251257

252258
# Nothing else should have been redirected
253259
assert "this goes to sys.stdout" not in content

0 commit comments

Comments
 (0)