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

Code before:
    assert_last_legend_patch_color(h, leg, 'b', edgecolor=True)

    # testing c, fc, and ec combinations for stepfilled histograms
    h = ax.hist(x, histtype='stepfilled', color='r', ec='b',
                label="red stepfilled hist, blue edges, red label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'r', facecolor=True)

    h = ax.hist(x, histtype='stepfilled', fc='r', ec='b',
                label="red stepfilled hist, blue edges, red label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'r', facecolor=True)

    h = ax.hist(x, histtype='stepfilled', fc='none', ec='b',
                label="unfilled blue stepfilled hist, blue label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'b', edgecolor=True)

    h = ax.hist(x, histtype='stepfilled', fc='r', ec='none',
                label="edgeless red stepfilled hist with a red label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'r', facecolor=True)
    plt.close('all')


def assert_last_legend_linemarker_color(plot, leg, expected_color,
                                    color=False, facecolor=False, edgecolor=False):
    """
    Check that line marker color, legend handle color, and legend label color all
    match the expected input. Provide color, facecolor and edgecolor flags to clarify
    which feature to match.
    """
    label_color = leg.texts[-1].get_color()
    leg_marker = leg.get_lines()[-1]
    plot_marker = plot[0]
    assert mpl.colors.same_color(label_color, expected_color)
    if color:
        assert mpl.colors.same_color(label_color, leg_marker.get_color())
        assert mpl.colors.same_color(label_color, plot_marker.get_color())
    if facecolor:
        assert mpl.colors.same_color(label_color, leg_marker.get_markerfacecolor())
        assert mpl.colors.same_color(label_color, plot_marker.get_markerfacecolor())
    if edgecolor:
        assert mpl.colors.same_color(label_color, leg_marker.get_markeredgecolor())
        assert mpl.colors.same_color(label_color, plot_marker.get_markeredgecolor())


def test_legend_labelcolor_linecolor_plot():
    x = np.arange(5)

    # testing line plot

Review comment (suggestion):
```suggestion
def assert_last_legend_linemarker_color(line, leg, expected_color,
```
Let's call this by the created artist. Also I suggest, to use tuple-unpacking on the plot call sites (which is a standard pattern we recommend, see https://matplotlib.org/devdocs/tutorials/pyplot.html#controlling-line-properties)
```
l, = ax.plot(x, c='r', label="red line with a red label")
```
so that the assert can accept a single Line2D instance and you dont have to do `plot_marker = plot[0]`.



Code after:
    assert_last_legend_patch_color(h, leg, 'b', edgecolor=True)

    # testing c, fc, and ec combinations for stepfilled histograms
    h = ax.hist(x, histtype='stepfilled', color='r', ec='b',
                label="red stepfilled hist, blue edges, red label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'r', facecolor=True)

    h = ax.hist(x, histtype='stepfilled', fc='r', ec='b',
                label="red stepfilled hist, blue edges, red label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'r', facecolor=True)

    h = ax.hist(x, histtype='stepfilled', fc='none', ec='b',
                label="unfilled blue stepfilled hist, blue label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'b', edgecolor=True)

    h = ax.hist(x, histtype='stepfilled', fc='r', ec='none',
                label="edgeless red stepfilled hist with a red label")
    leg = ax.legend(labelcolor='linecolor')
    assert_last_legend_patch_color(h, leg, 'r', facecolor=True)


def assert_last_legend_linemarker_color(line_marker, leg, expected_color, color=False,
                                        facecolor=False, edgecolor=False):
    """
    Check that line marker color, legend handle color, and legend label color all
    match the expected input. Provide color, facecolor and edgecolor flags to clarify
    which feature to match.
    """
    label_color = leg.texts[-1].get_color()
    leg_marker = leg.get_lines()[-1]
    assert mpl.colors.same_color(label_color, expected_color)
    if color:
        assert mpl.colors.same_color(label_color, leg_marker.get_color())
        assert mpl.colors.same_color(label_color, line_marker.get_color())
    if facecolor:
        assert mpl.colors.same_color(label_color, leg_marker.get_markerfacecolor())
        assert mpl.colors.same_color(label_color, line_marker.get_markerfacecolor())
    if edgecolor:
        assert mpl.colors.same_color(label_color, leg_marker.get_markeredgecolor())
        assert mpl.colors.same_color(label_color, line_marker.get_markeredgecolor())


def test_legend_labelcolor_linecolor_plot():
    x = np.arange(5)

    # testing line plot
    fig, ax = plt.subplots()
    l, = ax.plot(x, c='r', label="red line with a red label")