File: test_qtbot_pep8_aliases.py

package info (click to toggle)
pytest-qt 4.2.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 624 kB
  • sloc: python: 4,098; makefile: 139
file content (92 lines) | stat: -rw-r--r-- 3,259 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import inspect
from unittest.mock import MagicMock

import pytest

from pytestqt.qtbot import QtBot


def _format_pep_8(camel_case_name: str) -> str:
    """
    Helper that creates a pep8_compliant_method_name
    from a given camelCaseMethodName.
    """
    return camel_case_name[0].lower() + "".join(
        f"_{letter.lower()}" if letter.isupper() else letter.lower()
        for letter in camel_case_name[1:]
    )


@pytest.mark.parametrize(
    "expected, camel_case_input",
    [
        ("add_widget", "addWidget"),
        ("wait_active", "waitActive"),
        ("wait_exposed", "waitExposed"),
        ("wait_for_window_shown", "waitForWindowShown"),
        ("wait_signal", "waitSignal"),
        ("wait_signals", "waitSignals"),
        ("assert_not_emitted", "assertNotEmitted"),
        ("wait_until", "waitUntil"),
        ("wait_callback", "waitCallback"),
    ],
)
def test_format_pep8(expected: str, camel_case_input: str):
    assert _format_pep_8(camel_case_input) == expected


def test_pep8_aliases(qtbot):
    """
    Test that defined PEP8 aliases actually refer to the correct implementation.
    Only check methods that have such an alias defined.
    """
    for name, func in inspect.getmembers(qtbot, inspect.ismethod):
        if name != name.lower():
            pep8_name = _format_pep_8(name)
            if hasattr(qtbot, pep8_name):
                # Found a PEP8 alias.
                assert (
                    getattr(qtbot, name).__func__ is getattr(qtbot, pep8_name).__func__
                )


def generate_test_cases_for_test_subclass_of_qtbot_has_overwritten_pep8_aliases():
    """
    For each PEP8 alias found in QtBot, yields a test case consisting of
    a QtBot subclass that has the alias pair’s camelCase implementation
    overwritten with a MagicMock.

    Yields tuples (subclass, camelCaseMethodName, pep8_method_name_alias)
    """
    for name, func in inspect.getmembers(QtBot, inspect.isfunction):
        if name != name.lower():
            subclass_logic_mock = MagicMock()
            pep8_name = _format_pep_8(name)
            if hasattr(QtBot, pep8_name):
                # Found a PEP8 alias.
                methods = QtBot.__dict__.copy()
                # Only overwrite the camelCase method
                methods[name] = subclass_logic_mock
                sub_class = type("QtBotSubclass", (QtBot,), methods)
                yield sub_class, name, pep8_name


@pytest.mark.parametrize(
    "qtbot_subclass, method_name, pep8_name",
    generate_test_cases_for_test_subclass_of_qtbot_has_overwritten_pep8_aliases(),
)
def test_subclass_of_qtbot_has_overwritten_pep8_aliases(
    qtbot_subclass, method_name: str, pep8_name: str
):
    """
    Test that subclassing QtBot does not create surprises,
    by checking that the PEP8 aliases follow overwritten
    camelCase methods.
    """
    instance: QtBot = qtbot_subclass(MagicMock())
    assert isinstance(getattr(instance, method_name), MagicMock)
    assert isinstance(getattr(instance, pep8_name), MagicMock)
    # Now call the pep8_name_method and check that the subclass’s
    # camelCaseMethod implementation was actually called.
    getattr(instance, pep8_name)()
    getattr(instance, method_name).assert_called()