def _(lhs=molecule_man, rhs=molecule_woman): diff = Diff(lhs, rhs, 80) diff_lines: List[Text] = list(diff.__rich_console__(None, None)) assert diff_lines == [ Text("{"), Text(" 'age': 29,"), Text( " 'name': 'Molecule Man',", spans=[ Span(0, 22, "green"), Span(22, 23, "white on green"), Span(23, 27, "green"), ], ), Text( " 'name': 'Molecule Woman',", spans=[ Span(0, 22, "red"), Span(22, 25, "white on red"), Span(25, 29, "red"), ], ), Text(" 'powers': ['Turning tiny', 'Radiation blast'],"), Text(" 'secretIdentity': 'Dan Jukes',"), Text("}"), ]
def _(): lhs = "the quick brown fox jumped over the lazy dog" rhs = "the quick brown cat jumped over the lazy dog" diff = Diff(lhs, rhs, 12) render_iter = diff.__rich_console__(None, None) actual_lhs: Text = next(render_iter) actual_rhs: Text = next(render_iter) assert actual_lhs == Text( lhs, spans=[ Span(0, 16, "green"), Span(16, 19, "white on green"), Span(19, 44, "green"), ], ) assert actual_rhs == Text( rhs, spans=[ Span(0, 16, "red"), Span(16, 19, "white on red"), Span(19, 44, "red") ], )
def _(): lhs = "hello" rhs = "hallo" diff = Diff(lhs, rhs, 80) render_iter = diff.__rich_console__(None, None) actual_lhs: Text = next(render_iter) actual_rhs: Text = next(render_iter) expected_lhs = Text( lhs, spans=[ Span(0, 1, "green"), Span(1, 2, "white on green"), Span(2, 5, "green") ], ) expected_rhs = Text(rhs, spans=[ Span(0, 1, "red"), Span(1, 2, "white on red"), Span(2, 5, "red") ]) assert actual_lhs == expected_lhs assert actual_rhs == expected_rhs
def _(lhs=molecule_man, rhs=molecule_woman): diff = Diff(lhs, rhs, 80, show_symbols=True) diff_lines: List[Text] = list(diff.__rich_console__(None, None)) assert diff_lines == [ Text(" {"), Text(" 'age': 29,"), Text("+ 'name': 'Molecule Man',", ), Text("? ^"), Text("- 'name': 'Molecule Woman',", ), Text("? ^^^", ), Text(" 'powers': ['Turning tiny', 'Radiation blast'],"), Text(" 'secretIdentity': 'Dan Jukes',"), Text(" }"), ]
def _(): # In this case, difflib returns a diff without the intraline differences lhs = "hello" rhs = "halo" diff = Diff(lhs, rhs, 80) render_iter = diff.__rich_console__(None, None) actual_lhs: Text = next(render_iter) actual_rhs: Text = next(render_iter) assert actual_lhs == Text(lhs) assert actual_lhs.style == "green" assert actual_rhs == Text(rhs) assert actual_rhs.style == "red"
def _get_diff(self, err: TestAssertionFailure) -> Optional[Diff]: if err.operator in EQUALITY_COMPARISONS: diff = Diff( err.lhs, err.rhs, width=self.terminal_size.width - 24, show_symbols=self.show_diff_symbols, ) if diff.sides_are_different: return diff return None
def _(): lhs = "hello" rhs = "hallo" diff = Diff(lhs, rhs, 80, show_symbols=True) render_iter = diff.__rich_console__(None, None) diff_line_1: Text = next(render_iter) diff_line_2: Text = next(render_iter) diff_line_3: Text = next(render_iter) diff_line_4: Text = next(render_iter) assert diff_line_1 == Text("+ hello") assert diff_line_2 == Text("? ^") assert diff_line_3 == Text("- hallo") assert diff_line_4 == Text("? ^") assert diff_line_1.style == "green" assert diff_line_2.style == "green" assert diff_line_3.style == "red" assert diff_line_4.style == "red"
def print_failure_equals(self, err: TestFailure): diff_msg = Text("LHS", style="pass.textonly") diff_msg.append(" vs ", style="default") diff_msg.append("RHS", style="fail.textonly") diff_msg.append(" shown below", style="default") console.print(Padding(diff_msg, pad=(0, 0, 1, 2))) diff = Diff( err.lhs, err.rhs, width=self.terminal_size.width - 24, show_symbols=self.show_diff_symbols, ) console.print(Padding(diff, pad=(0, 0, 1, 4)))
def get_pretty_failure_for_equals(self, err: TestFailure) -> RenderableType: diff_msg = Text.assemble( ("LHS ", "pass.textonly"), ("vs ", "default"), ("RHS ", "fail.textonly"), ("shown below", "default"), ) diff = Diff( err.lhs, err.rhs, width=self.terminal_size.width - 24, show_symbols=self.show_diff_symbols, ) return RenderGroup( Padding(diff_msg, pad=(0, 0, 1, 2)), Padding(diff, pad=(0, 0, 1, 4)), )