From 8411f0021ec6eec46eaa71f7a74114688a749a2e Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 5 Jul 2021 15:29:26 +0100 Subject: [PATCH] test fix --- rich/segment.py | 87 +++++++++++++++++----------------------------- tests/test_tree.py | 20 ++++++++++- 2 files changed, 51 insertions(+), 56 deletions(-) diff --git a/rich/segment.py b/rich/segment.py index 67e44fd1..edffd5bb 100644 --- a/rich/segment.py +++ b/rich/segment.py @@ -154,7 +154,7 @@ class Segment(NamedTuple): apply = style.__add__ result_segments = ( cls(text, None if control else apply(_style), control) - for text, _style, control in segments + for text, _style, control in result_segments ) if post_style: result_segments = ( @@ -167,7 +167,7 @@ class Segment(NamedTuple): ), control, ) - for text, _style, control in segments + for text, _style, control in result_segments ) return result_segments @@ -567,60 +567,37 @@ class SegmentLines: if __name__ == "__main__": - from rich import print + if __name__ == "__main__": # pragma: no cover + from rich.syntax import Syntax + from rich.text import Text + from rich.console import Console - print(Segment("foo").split_cells(0)) - print(Segment("foo").split_cells(1)) - print(Segment("foo").split_cells(2)) - print(Segment("foo").split_cells(3)) - print(Segment("foo").split_cells(4)) + code = """from rich.console import Console + console = Console() + text = Text.from_markup("Hello, [bold magenta]World[/]!") + console.print(text)""" - print() - print(Segment("💩").split_cells(0)) - print(Segment("💩").split_cells(1)) - print(Segment("💩").split_cells(2)) + text = Text.from_markup("Hello, [bold magenta]World[/]!") - print() - print(Segment("💩💩").split_cells(0)) - print(Segment("💩💩").split_cells(1)) - print(Segment("💩💩").split_cells(2)) - print(Segment("💩💩").split_cells(3)) - print(Segment("💩💩").split_cells(4)) + console = Console() - segment = Segment("💩X" * 10) - - for n in range(30): - print(segment.split_cells(n)) - -# if __name__ == "__main__": # pragma: no cover -# from rich.syntax import Syntax -# from rich.text import Text -# from rich.console import Console - -# code = """from rich.console import Console -# console = Console() -# text = Text.from_markup("Hello, [bold magenta]World[/]!") -# console.print(text)""" - -# text = Text.from_markup("Hello, [bold magenta]World[/]!") - -# console = Console() - -# console.rule("rich.Segment") -# console.print( -# "A Segment is the last step in the Rich render process before generating text with ANSI codes." -# ) -# console.print("\nConsider the following code:\n") -# console.print(Syntax(code, "python", line_numbers=True)) -# console.print() -# console.print( -# "When you call [b]print()[/b], Rich [i]renders[/i] the object in to the the following:\n" -# ) -# fragments = list(console.render(text)) -# console.print(fragments) -# console.print() -# console.print("The Segments are then processed to produce the following output:\n") -# console.print(text) -# console.print( -# "\nYou will only need to know this if you are implementing your own Rich renderables." -# ) + console.rule("rich.Segment") + console.print( + "A Segment is the last step in the Rich render process before generating text with ANSI codes." + ) + console.print("\nConsider the following code:\n") + console.print(Syntax(code, "python", line_numbers=True)) + console.print() + console.print( + "When you call [b]print()[/b], Rich [i]renders[/i] the object in to the the following:\n" + ) + fragments = list(console.render(text)) + console.print(fragments) + console.print() + console.print( + "The Segments are then processed to produce the following output:\n" + ) + console.print(text) + console.print( + "\nYou will only need to know this if you are implementing your own Rich renderables." + ) diff --git a/tests/test_tree.py b/tests/test_tree.py index c06bba2a..babcc3cf 100644 --- a/tests/test_tree.py +++ b/tests/test_tree.py @@ -59,7 +59,7 @@ def test_render_ascii(): @pytest.mark.skipif(sys.platform == "win32", reason="different on Windows") -def test_render(): +def test_render_tree_non_win32(): tree = Tree("foo") tree.add("bar", style="italic") baz_tree = tree.add("baz", guide_style="bold red", style="on blue") @@ -76,6 +76,24 @@ def test_render(): assert result == expected +@pytest.mark.skipif(sys.platform != "win32", reason="Windows specific") +def test_render_tree_win32(): + tree = Tree("foo") + tree.add("bar", style="italic") + baz_tree = tree.add("baz", guide_style="bold red", style="on blue") + baz_tree.add("1") + baz_tree.add("2") + tree.add("egg") + + console = Console(width=20, force_terminal=True, color_system="standard") + console.begin_capture() + console.print(tree) + result = console.end_capture() + print(repr(result)) + expected = "foo \n├── \x1b[3mbar\x1b[0m\x1b[3m \x1b[0m\n\x1b[44m├── \x1b[0m\x1b[44mbaz\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m├── \x1b[0m\x1b[44m1\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m└── \x1b[0m\x1b[44m2\x1b[0m\x1b[44m \x1b[0m\n└── egg \n" + assert result == expected + + def test_tree_measure(): tree = Tree("foo") tree.add("bar")