Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_offsetbox.py (Python)

Code before:
    assert not ann._draggable.got_artist
    # A scroll event should not initiate a drag.
    MouseEvent("scroll_event", fig.canvas, 1, 1)._process()
    assert not ann._draggable.got_artist
    # An event outside the annotation should not initiate a drag.
    bbox = ann.get_window_extent()
    MouseEvent("button_press_event", fig.canvas, bbox.x1+2, bbox.y1+2)._process()
    assert not ann._draggable.got_artist



def test_anchored_offsetbox_tuple_and_float_borderpad():
    """
    Test AnchoredOffsetbox correctly handles both float and tuple for borderpad.
    """
    from matplotlib.offsetbox import AnchoredText

    fig, ax = plt.subplots()

    # Case 1: Test the old float behavior (exercises the 'except' block)
    text_float = AnchoredText("float", loc='lower left', borderpad=5)
    ax.add_artist(text_float)

    # Case 2: Test the new tuple behavior (exercises the 'try' block)
    text_tuple = AnchoredText("tuple", loc='lower left', borderpad=(10, 10))
    ax.add_artist(text_tuple)

    # Draw the canvas to calculate final positions
    fig.canvas.draw()

    pos_float = text_float.get_window_extent()
    pos_tuple = text_tuple.get_window_extent()

    # Assert that the tuple with larger padding is further from the origin
    # than the float with smaller padding.
    assert pos_tuple.x0 > pos_float.x0
    assert pos_tuple.y0 > pos_float.y0


Review comment (refactor):
```suggestion
    # Case 1: Test the float behavior
    text_float = AnchoredText("float", loc='lower left', borderpad=5)
    ax.add_artist(text_float)

    # Case 2: Test the tuple behavior
    text_tuple = AnchoredText("tuple", loc='lower left', borderpad=(10, 10))
    ax.add_artist(text_tuple)
```
"Old" and "new" only make sense in the context of this PR, and not to someone reading this in the future.  I also think it's better not to comment about how it's currently implemented: if someone refactors the code at some point, they might just confirm the tests still pass without reading and updating the test comments.

Code after:
    assert not ann._draggable.got_artist
    # A scroll event should not initiate a drag.
    MouseEvent("scroll_event", fig.canvas, 1, 1)._process()
    assert not ann._draggable.got_artist
    # An event outside the annotation should not initiate a drag.
    bbox = ann.get_window_extent()
    MouseEvent("button_press_event", fig.canvas, bbox.x1+2, bbox.y1+2)._process()
    assert not ann._draggable.got_artist


def test_anchored_offsetbox_tuple_and_float_borderpad():
    """
    Test AnchoredOffsetbox correctly handles both float and tuple for borderpad.
    """

    fig, ax = plt.subplots()

    # Case 1: Establish a baseline with float value
    text_float = AnchoredText("float", loc='lower left', borderpad=5)
    ax.add_artist(text_float)

    # Case 2: Test that a symmetric tuple gives the exact same result.
    text_tuple_equal = AnchoredText("tuple", loc='lower left', borderpad=(5, 5))
    ax.add_artist(text_tuple_equal)

    # Case 3: Test that an asymmetric tuple with different values works as expected.
    text_tuple_asym = AnchoredText("tuple_asym", loc='lower left', borderpad=(10, 4))
    ax.add_artist(text_tuple_asym)

    # Draw the canvas to calculate final positions
    fig.canvas.draw()

    pos_float = text_float.get_window_extent()
    pos_tuple_equal = text_tuple_equal.get_window_extent()
    pos_tuple_asym = text_tuple_asym.get_window_extent()

    # Assertion 1: Prove that borderpad=5 is identical to borderpad=(5, 5).
    assert pos_tuple_equal.x0 == pos_float.x0
    assert pos_tuple_equal.y0 == pos_float.y0

    # Assertion 2: Prove that the asymmetric padding moved the box
    # further from the origin than the baseline in the x-direction and less far
    # in the y-direction.
    assert pos_tuple_asym.x0 > pos_float.x0
    assert pos_tuple_asym.y0 < pos_float.y0