|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import inspect |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ..utils import deprecation |
| 8 | +from .utils import call_method |
| 9 | + |
| 10 | +CALL_PATH = inspect.getfile(call_method) |
| 11 | + |
| 12 | +def test_deprecation(): |
| 13 | + caller_path = inspect.stack(context=0)[1].filename |
| 14 | + with pytest.deprecated_call() as record: |
| 15 | + deprecation('Deprecated call') |
| 16 | + # Make sure the deprecation pointed to the external function calling this test function |
| 17 | + assert len(record) == 1 |
| 18 | + assert record[0].filename == caller_path |
| 19 | + |
| 20 | + with pytest.deprecated_call() as record: |
| 21 | + deprecation('Deprecated call', ['ipywidgets/widgets/tests']) |
| 22 | + # Make sure the deprecation pointed to the external function calling this test function |
| 23 | + assert len(record) == 1 |
| 24 | + assert record[0].filename == caller_path |
| 25 | + |
| 26 | + with pytest.deprecated_call() as record: |
| 27 | + deprecation('Deprecated call', 'ipywidgets/widgets/tests') |
| 28 | + # Make sure the deprecation pointed to the external function calling this test function |
| 29 | + assert len(record) == 1 |
| 30 | + assert record[0].filename == caller_path |
| 31 | + |
| 32 | + with pytest.deprecated_call() as record: |
| 33 | + deprecation('Deprecated call', []) |
| 34 | + # Make sure the deprecation pointed to *this* file |
| 35 | + assert len(record) == 1 |
| 36 | + assert record[0].filename == __file__ |
| 37 | + |
| 38 | +def test_deprecation_indirect(): |
| 39 | + # If the line that calls "deprecation" is not internal, it is considered the source: |
| 40 | + with pytest.warns(DeprecationWarning) as record: |
| 41 | + call_method(deprecation, "test message", []) |
| 42 | + assert len(record) == 1 |
| 43 | + assert record[0].filename == CALL_PATH |
| 44 | + |
| 45 | +def test_deprecation_indirect_internal(): |
| 46 | + # If the line that calls "deprecation" is internal, it is not considered the source: |
| 47 | + with pytest.warns(DeprecationWarning) as record: |
| 48 | + call_method(deprecation, "test message", [CALL_PATH]) |
| 49 | + assert len(record) == 1 |
| 50 | + assert record[0].filename == __file__ |
| 51 | + |
| 52 | +def test_deprecation_nested1(): |
| 53 | + def level1(): |
| 54 | + deprecation("test message", []) |
| 55 | + |
| 56 | + with pytest.warns(DeprecationWarning) as record: |
| 57 | + call_method(level1) |
| 58 | + |
| 59 | + assert len(record) == 1 |
| 60 | + assert record[0].filename == __file__ |
| 61 | + |
| 62 | +def test_deprecation_nested2(): |
| 63 | + def level2(): |
| 64 | + deprecation("test message", []) |
| 65 | + def level1(): |
| 66 | + level2() |
| 67 | + |
| 68 | + with pytest.warns(DeprecationWarning) as record: |
| 69 | + call_method(level1) |
| 70 | + |
| 71 | + assert len(record) == 1 |
| 72 | + assert record[0].filename == __file__ |
0 commit comments