From 9892c10f492e5219d10f2061d39615f8731ab6c8 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 17 Aug 2015 22:48:05 +0200 Subject: [PATCH] 100% test coverage for utils.error. --- scripts/dev/check_coverage.py | 1 + tests/utils/test_error.py | 72 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/utils/test_error.py diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index 1928f025a..a336805ac 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -67,6 +67,7 @@ PERFECT_FILES = [ 'qutebrowser/utils/version.py', 'qutebrowser/utils/debug.py', 'qutebrowser/utils/jinja.py', + 'qutebrowser/utils/error.py', ] diff --git a/tests/utils/test_error.py b/tests/utils/test_error.py new file mode 100644 index 000000000..50ae31a92 --- /dev/null +++ b/tests/utils/test_error.py @@ -0,0 +1,72 @@ +# Copyright 2015 Florian Bruhin (The Compiler) +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Tests for qutebrowser.utils.error.""" + +import collections +import logging + +import pytest + +from qutebrowser.utils import error + +from PyQt5.QtCore import pyqtSlot, QTimer +from PyQt5.QtWidgets import QMessageBox + + +Args = collections.namedtuple('Args', 'no_err_windows') + + +def test_no_err_windows(caplog): + """Test handle_fatal_exc uwith no_err_windows = True.""" + try: + raise ValueError("exception") + except ValueError as e: + with caplog.atLevel(logging.ERROR): + error.handle_fatal_exc(e, Args(no_err_windows=True), 'title', + pre_text='pre', post_text='post') + msgs = [rec.message for rec in caplog.records()] + assert msgs[0] == 'Handling fatal ValueError with --no-err-windows!' + assert msgs[1] == 'title: title' + assert msgs[2] == 'pre_text: pre' + assert msgs[3] == 'post_text: post' + + +@pytest.mark.parametrize('pre_text, post_text, expected', [ + ('', '', 'exception'), + ('foo', '', 'foo: exception'), + ('foo', 'bar', 'foo: exception\n\nbar'), + ('', 'bar', 'exception\n\nbar'), +]) +def test_err_windows(qtbot, qapp, pre_text, post_text, expected): + + @pyqtSlot() + def err_window_check(): + w = qapp.activeModalWidget() + try: + qtbot.add_widget(w) + assert w.windowTitle() == 'title' + assert w.icon() == QMessageBox.Critical + assert w.standardButtons() == QMessageBox.Ok + assert w.text() == expected + finally: + w.close() + + QTimer.singleShot(0, err_window_check) + error.handle_fatal_exc(ValueError("exception"), Args(no_err_windows=False), + 'title', pre_text=pre_text, post_text=post_text)