Skip to content

Commit b6ce650

Browse files
committed
Convert Message from a tuple into a proper class
1 parent 344558a commit b6ce650

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

pytestqt/_tests/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_qtlog_fixture(qtlog):
5555
qDebug('this is a DEBUG message')
5656
qWarning('this is a WARNING message')
5757
qCritical('this is a CRITICAL message')
58-
messages = [(m.msg_type, m.msg.strip()) for m in qtlog.messages]
58+
messages = [(m.type, m.message.strip()) for m in qtlog.messages]
5959
assert messages == [
6060
(QtDebugMsg, 'this is a DEBUG message'),
6161
(QtWarningMsg, 'this is a WARNING message'),

pytestqt/plugin.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,8 @@ def pytest_runtest_makereport(self, item, call):
461461
long_repr = getattr(report, 'longrepr', None)
462462
if hasattr(long_repr, 'addsection'):
463463
lines = []
464-
for msg_type, msg in item.qt_log_capture.messages:
465-
msg_name = _QtMessageCapture.get_msg_name(msg_type)
466-
lines.append('{0}: {1}'.format(msg_name, msg))
464+
for m in item.qt_log_capture.messages:
465+
lines.append('{m.type_name}: {m.message}'.format(m=m))
467466
if lines:
468467
long_repr.addsection('Captured Qt messages',
469468
'\n'.join(lines))
@@ -481,19 +480,17 @@ class _QtMessageCapture(object):
481480
:attr messages: list of Message named-tuples.
482481
"""
483482

484-
Message = collections.namedtuple('Message', 'msg_type, msg')
485-
486483
def __init__(self):
487484
self._messages = []
488485

489-
def _handle(self, msg_type, msg):
486+
def _handle(self, msg_type, message):
490487
"""
491488
Method to be installed using qInstallMsgHandler, stores each message
492489
into the `messages` attribute.
493490
"""
494-
if isinstance(msg, bytes):
495-
msg = msg.decode('utf-8', errors='replace')
496-
self._messages.append(self.Message(msg_type, msg))
491+
if isinstance(message, bytes):
492+
message = message.decode('utf-8', errors='replace')
493+
self._messages.append(Message(msg_type, message))
497494

498495
@property
499496
def messages(self):
@@ -503,15 +500,40 @@ def messages(self):
503500
"""
504501
return self._messages[:]
505502

503+
504+
class Message(object):
505+
506+
def __init__(self, msg_type, message):
507+
self._type = msg_type
508+
self._message = message
509+
self._type_name = self._get_msg_type_name(msg_type)
510+
511+
@property
512+
def message(self):
513+
return self._message
514+
515+
@property
516+
def type(self):
517+
return self._type
518+
519+
@property
520+
def type_name(self):
521+
return self._type_name
522+
506523
@classmethod
507-
def get_msg_name(cls, msg_type):
508-
"""Return a string representation of the given QtMsgType enum value."""
509-
return {
510-
QtDebugMsg: 'QtDebugMsg',
511-
QtWarningMsg: 'QtWarningMsg',
512-
QtCriticalMsg: 'QtCriticalMsg',
513-
QtFatalMsg: 'QtFatalMsg',
514-
}[msg_type]
524+
def _get_msg_type_name(cls, msg_type):
525+
"""
526+
Return a string representation of the given QtMsgType enum
527+
value.
528+
"""
529+
if not getattr(cls, '_type_name_map', None):
530+
cls._type_name_map = {
531+
QtDebugMsg: 'QtDebugMsg',
532+
QtWarningMsg: 'QtWarningMsg',
533+
QtCriticalMsg: 'QtCriticalMsg',
534+
QtFatalMsg: 'QtFatalMsg',
535+
}
536+
return cls._type_name_map[msg_type]
515537

516538

517539
@pytest.fixture

0 commit comments

Comments
 (0)