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

Code before:
    assert i == cbook.sanitize_sequence(i)
    assert k == cbook.sanitize_sequence(k)


def test_resize_sequence():
    a_list = [1, 2, 3]
    arr = np.array([1, 2, 3])

    # already same length: passthrough
    assert cbook._resize_sequence(a_list, 3) is a_list
    assert cbook._resize_sequence(arr, 3) is arr

    # shortening
    assert cbook._resize_sequence(a_list, 2) == [1, 2]
    assert_array_equal(cbook._resize_sequence(arr, 2), [1, 2])

    # extending
    assert cbook._resize_sequence(a_list, 5) == [1, 2, 3, 1, 2]
    assert_array_equal(cbook._resize_sequence(arr, 5), [1, 2, 3, 1, 2])


fail_mapping: tuple[tuple[dict, type], ...] = (
    (
        {"a": 1, "b": 2},
        _api.define_aliases({"a": ["b"]})(
            type("Type", (mpl.artist.Artist,), {"get_a": lambda self: None})),
    ),
    (
        {"a": 1, "b": 2},
        _api.define_aliases({"a": ["a", "b"]})(
            type("Type", (mpl.artist.Artist,), {"get_a": lambda self: None})),
    ),
)

pass_mapping: tuple[tuple[Any, dict, tuple], ...] = (
    (
        None, {},
        _api.define_aliases({})(
            type("Type", (mpl.artist.Artist,), {"get_a": lambda self: None})),
    ),
    (
        {"a": 1, "b": 2}, {"a": 1, "b": 2},
        _api.define_aliases({})(
            type("Type", (mpl.artist.Artist,), {"get_a": lambda self: None})),
    ),
    (
        {"b": 2}, {"a": 2},
        _api.define_aliases({"a": ["a", "b"]})(
            type("Type", (mpl.artist.Artist,), {"get_a": lambda self: None})),
    ),
)

Review comment (bug):
Since the undecorated type is always the same, is it worth defining it inside the tests and only parametrizing the dictionary?  Or defining it once before `fail_mapping` and `pass_mapping`?

Code after:
    assert i == cbook.sanitize_sequence(i)
    assert k == cbook.sanitize_sequence(k)


def test_resize_sequence():
    a_list = [1, 2, 3]
    arr = np.array([1, 2, 3])

    # already same length: passthrough
    assert cbook._resize_sequence(a_list, 3) is a_list
    assert cbook._resize_sequence(arr, 3) is arr

    # shortening
    assert cbook._resize_sequence(a_list, 2) == [1, 2]
    assert_array_equal(cbook._resize_sequence(arr, 2), [1, 2])

    # extending
    assert cbook._resize_sequence(a_list, 5) == [1, 2, 3, 1, 2]
    assert_array_equal(cbook._resize_sequence(arr, 5), [1, 2, 3, 1, 2])


fail_mapping: list[tuple[dict, dict]] = [
    ({"a": 1, "b": 2}, {"a": ["b"]}),
    ({"a": 1, "b": 2}, {"a": ["a", "b"]}),
]

pass_mapping: list[tuple[Any, dict, dict]] = [
    (None, {}, {}),
    ({"a": 1, "b": 2}, {"a": 1, "b": 2}, {}),
    ({"b": 2}, {"a": 2}, {"a": ["a", "b"]}),
]


@pytest.mark.parametrize('inp, alias_def', fail_mapping)
def test_normalize_kwargs_fail(inp, alias_def):

    @_api.define_aliases(alias_def)
    class Type(mpl.artist.Artist):
        def get_a(self): return None

    with pytest.raises(TypeError):
        cbook.normalize_kwargs(inp, Type)


@pytest.mark.parametrize('inp, expected, alias_def', pass_mapping)
def test_normalize_kwargs_pass(inp, expected, alias_def):

    @_api.define_aliases(alias_def)
    class Type(mpl.artist.Artist):
        def get_a(self): return None