From 2d8aaecd65a1188c71e59c85fbb37ddf5e3d934b Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 11 Aug 2015 17:11:00 +0200 Subject: [PATCH] 100% coverage for utils.debug. --- qutebrowser/utils/debug.py | 2 +- scripts/dev/check_coverage.py | 1 + tests/utils/test_debug.py | 97 ++++++++++++++++++++++++++++++++--- 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/qutebrowser/utils/debug.py b/qutebrowser/utils/debug.py index 0f9f55eb7..0939389c4 100644 --- a/qutebrowser/utils/debug.py +++ b/qutebrowser/utils/debug.py @@ -56,7 +56,7 @@ def log_signals(obj): dbg = dbg_signal(signal, args) try: r = repr(obj) - except RuntimeError: + except RuntimeError: # pragma: no cover r = '' log.signals.debug("Signal in {}: {}".format(r, dbg)) diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index 83b20a9f7..7c9fad45e 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -57,6 +57,7 @@ PERFECT_FILES = [ 'qutebrowser/utils/usertypes.py', 'qutebrowser/utils/utils.py', 'qutebrowser/utils/version.py', + 'qutebrowser/utils/debug.py', ] diff --git a/tests/utils/test_debug.py b/tests/utils/test_debug.py index 0ff7a7e00..2736baa8d 100644 --- a/tests/utils/test_debug.py +++ b/tests/utils/test_debug.py @@ -22,10 +22,11 @@ import logging import re import time +import textwrap import pytest -from PyQt5.QtCore import pyqtSignal, Qt, QEvent, QObject -from PyQt5.QtWidgets import QStyle, QFrame +from PyQt5.QtCore import pyqtSignal, Qt, QEvent, QObject, QTimer +from PyQt5.QtWidgets import QStyle, QFrame, QWidget from qutebrowser.utils import debug @@ -64,9 +65,10 @@ class DecoratedSignalObject(SignalObject): @pytest.fixture(params=[(SignalObject, True), (DecoratedSignalObject, False)]) def signal_obj(request): klass, wrap = request.param + obj = klass() if wrap: - debug.log_signals(klass) - return klass() + debug.log_signals(obj) + return obj def test_log_signals(caplog, signal_obj): @@ -160,8 +162,39 @@ class TestQFlagsKey: debug.qflags_key(Qt, 42) -def test_signal_name(stubs): - assert debug.signal_name(stubs.FakeSignal()) == 'fake' +@pytest.mark.parametrize('signal, expected', [ + (SignalObject().signal1, 'signal1'), + (SignalObject().signal2, 'signal2'), +]) +def test_signal_name(signal, expected): + assert debug.signal_name(signal) == expected + + +@pytest.mark.parametrize('args, kwargs, expected', [ + ([], {}, ''), + (None, None, ''), + (['foo'], None, "'foo'"), + (['foo', 'bar'], None, "'foo', 'bar'"), + (None, {'foo': 'bar'}, "foo='bar'"), + (['foo', 'bar'], {'baz': 'fish'}, "'foo', 'bar', baz='fish'"), + (['x' * 300], None, "'{}".format('x' * 198 + '…')), +]) +def test_format_args(args, kwargs, expected): + assert debug.format_args(args, kwargs) == expected + + +def func(): + pass + + +@pytest.mark.parametrize('func, args, kwargs, full, expected', [ + (func, None, None, False, 'func()'), + (func, [1, 2], None, False, 'func(1, 2)'), + (func, [1, 2], None, True, 'test_debug.func(1, 2)'), + (func, [1, 2], {'foo': 3}, False, 'func(1, 2, foo=3)'), +]) +def test_format_call(func, args, kwargs, full, expected): + assert debug.format_call(func, args, kwargs, full) == expected @pytest.mark.parametrize('args, expected', [ @@ -171,3 +204,55 @@ def test_signal_name(stubs): ]) def test_dbg_signal(stubs, args, expected): assert debug.dbg_signal(stubs.FakeSignal(), args) == expected + + +class ExampleObject(QObject): + + def __init__(self, num, parent=None): + self._num = num + super().__init__(parent) + + def __repr__(self): + return ''.format(self._num) + + +class ExampleWidget(QWidget): + + def __init__(self, num, parent=None): + self._num = num + super().__init__(parent) + + def __repr__(self): + return ''.format(self._num) + + +def test_get_all_objects(qtbot): + w1 = ExampleWidget(1) + qtbot.add_widget(w1) + w2 = ExampleWidget(2) + qtbot.add_widget(w2) + + root = QObject() + o1 = ExampleObject(1, root) + o2 = ExampleObject(2, o1) + o3 = ExampleObject(3, root) + + expected = textwrap.dedent(""" + Qt widgets - 2 objects: + + + + Qt objects - 3 objects: + + + + + global object registry - 0 objects: + """).rstrip('\n') + + assert debug.get_all_objects(start_obj=root) == expected + + +def test_get_all_objects_qapp(): + objects = debug.get_all_objects() + assert '