|
| 1 | + |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ipywidgets.widgets.utils import deprecation |
| 8 | +from ipywidgets.widgets.tests.utils import call_method |
| 9 | + |
| 10 | +@patch("ipywidgets.widgets.utils._IPYWIDGETS_INTERNAL", []) |
| 11 | +def test_deprecation_direct(): |
| 12 | + with pytest.warns(DeprecationWarning) as record: |
| 13 | + deprecation("test message") |
| 14 | + assert len(record) == 1 |
| 15 | + assert record[0].filename == __file__ |
| 16 | + |
| 17 | +@patch("ipywidgets.widgets.utils._IPYWIDGETS_INTERNAL", []) |
| 18 | +def test_deprecation_indirect(): |
| 19 | + # If the line that calls "deprecation" is not internal, it is considered the source: |
| 20 | + with pytest.warns(DeprecationWarning) as record: |
| 21 | + call_method(deprecation, "test message") |
| 22 | + assert len(record) == 1 |
| 23 | + assert Path(record[0].filename) == Path(__file__, '..', 'utils.py').resolve() |
| 24 | + |
| 25 | +@patch("ipywidgets.widgets.utils._IPYWIDGETS_INTERNAL", [str(Path(__file__, '..', 'utils.py').resolve())]) |
| 26 | +def test_deprecation_indirect_internal(): |
| 27 | + # If the line that calls "deprecation" is internal, it is not considered the source: |
| 28 | + with pytest.warns(DeprecationWarning) as record: |
| 29 | + call_method(deprecation, "test message") |
| 30 | + assert len(record) == 1 |
| 31 | + assert record[0].filename == __file__ |
| 32 | + |
| 33 | +@patch("ipywidgets.widgets.utils._IPYWIDGETS_INTERNAL", []) |
| 34 | +def test_deprecation_nested1(): |
| 35 | + def level1(): |
| 36 | + deprecation("test message") |
| 37 | + |
| 38 | + with pytest.warns(DeprecationWarning) as record: |
| 39 | + call_method(level1) |
| 40 | + |
| 41 | + assert len(record) == 1 |
| 42 | + assert record[0].filename == __file__ |
| 43 | + |
| 44 | +@patch("ipywidgets.widgets.utils._IPYWIDGETS_INTERNAL", []) |
| 45 | +def test_deprecation_nested2(): |
| 46 | + def level2(): |
| 47 | + deprecation("test message") |
| 48 | + def level1(): |
| 49 | + level2() |
| 50 | + |
| 51 | + with pytest.warns(DeprecationWarning) as record: |
| 52 | + call_method(level1) |
| 53 | + |
| 54 | + assert len(record) == 1 |
| 55 | + assert record[0].filename == __file__ |
| 56 | + |
0 commit comments