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

Code before:

    with pytest.warns(mpl.MatplotlibDeprecationWarning):
        # passing a family list positionally
        FontProperties(["Times"])

    # still accepted:
    FontProperties(family="Times", style="italic")
    FontProperties(family="Times")
    FontProperties("Times")  # works as pattern and family
    FontProperties("serif-24:style=oblique:weight=bold")  # pattern

    # also still accepted:
    # passing as pattern via family kwarg was not covered by the docs but
    # historically worked. This is left unchanged for now.
    # AFAICT, we cannot detect this: We can determine whether a string
    # works as pattern, but that doesn't help, because there are strings
    # that are both pattern and family. We would need to identify, whether
    # a string is *not* a valid family.
    # Since this case is not covered by docs, I've refrained from jumping
    # extra hoops to detect this possible API misuse.
    FontProperties(family="serif-24:style=oblique:weight=bold")


def test_map_weights():
    assert _normalize_weight('ultralight') == 100
    assert _normalize_weight('light') == 200
    assert _normalize_weight('normal') == 400
    assert _normalize_weight('regular') == 400
    assert _normalize_weight('book') == 400
    assert _normalize_weight('medium') == 500
    assert _normalize_weight('roman') == 500
    assert _normalize_weight('semibold') == 600
    assert _normalize_weight('demibold') == 600
    assert _normalize_weight('demi') == 600
    assert _normalize_weight('bold') == 700
    assert _normalize_weight('heavy') == 800
    assert _normalize_weight('extra bold') == 800
    assert _normalize_weight('black') == 900


def test_font_match_warning(caplog):
    findfont(FontProperties(family=["DejaVu Sans"], weight=750))
    logs = [rec.message for rec in caplog.records]
    assert 'findfont: Failed to find font weight 750, now using 700.' in logs


Review comment (refactor):
Let's reframe this to test the helper instead of the mapping, so that we can also test the passthrough behavior

```suggestion
def test_normalize_weight():
    assert _normalize_weight(300) == 300  # passthough
    assert _normalize_weight('ultralight') == 100
```

Maybe also add an error case e.g. "italic"

Code after:

    with pytest.warns(mpl.MatplotlibDeprecationWarning):
        # passing a family list positionally
        FontProperties(["Times"])

    # still accepted:
    FontProperties(family="Times", style="italic")
    FontProperties(family="Times")
    FontProperties("Times")  # works as pattern and family
    FontProperties("serif-24:style=oblique:weight=bold")  # pattern

    # also still accepted:
    # passing as pattern via family kwarg was not covered by the docs but
    # historically worked. This is left unchanged for now.
    # AFAICT, we cannot detect this: We can determine whether a string
    # works as pattern, but that doesn't help, because there are strings
    # that are both pattern and family. We would need to identify, whether
    # a string is *not* a valid family.
    # Since this case is not covered by docs, I've refrained from jumping
    # extra hoops to detect this possible API misuse.
    FontProperties(family="serif-24:style=oblique:weight=bold")


def test_normalize_weights():
    assert _normalize_weight(300) == 300  # passthrough
    assert _normalize_weight('ultralight') == 100
    assert _normalize_weight('light') == 200
    assert _normalize_weight('normal') == 400
    assert _normalize_weight('regular') == 400
    assert _normalize_weight('book') == 400
    assert _normalize_weight('medium') == 500
    assert _normalize_weight('roman') == 500
    assert _normalize_weight('semibold') == 600
    assert _normalize_weight('demibold') == 600
    assert _normalize_weight('demi') == 600
    assert _normalize_weight('bold') == 700
    assert _normalize_weight('heavy') == 800
    assert _normalize_weight('extra bold') == 800
    assert _normalize_weight('black') == 900
    with pytest.raises(KeyError):
        _normalize_weight('invalid')


def test_font_match_warning(caplog):
    findfont(FontProperties(family=["DejaVu Sans"], weight=750))
    logs = [rec.message for rec in caplog.records]
    assert 'findfont: Failed to find font weight 750, now using 700.' in logs