From 7cbb2b079f1ff6ace68517f379a74982457e3caa Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 4 Oct 2017 06:30:39 +0200 Subject: [PATCH] Use existing tabbed_browser_stubs fixture in tests --- tests/unit/browser/test_signalfilter.py | 36 +++++++++---------- tests/unit/commands/test_argparser.py | 11 ++---- tests/unit/config/test_configcommands.py | 11 ++---- .../mainwindow/statusbar/test_backforward.py | 8 ++--- tests/unit/misc/test_sessions.py | 15 ++------ 5 files changed, 27 insertions(+), 54 deletions(-) diff --git a/tests/unit/browser/test_signalfilter.py b/tests/unit/browser/test_signalfilter.py index 9d191e7c6..66b6bd57c 100644 --- a/tests/unit/browser/test_signalfilter.py +++ b/tests/unit/browser/test_signalfilter.py @@ -66,18 +66,11 @@ def objects(): return Objects(signal_filter=signal_filter, signaller=signaller) -@pytest.fixture -def tabbed_browser(stubs, win_registry): - tb = stubs.TabbedBrowserStub() - objreg.register('tabbed-browser', tb, scope='window', window=0) - yield tb - objreg.delete('tabbed-browser', scope='window', window=0) - - @pytest.mark.parametrize('index_of, emitted', [(0, True), (1, False)]) -def test_filtering(objects, tabbed_browser, index_of, emitted): - tabbed_browser.current_index = 0 - tabbed_browser.index_of = index_of +def test_filtering(objects, tabbed_browser_stubs, index_of, emitted): + browser = tabbed_browser_stubs[0] + browser.current_index = 0 + browser.index_of = index_of objects.signaller.signal.emit('foo') if emitted: assert objects.signaller.filtered_signal_arg == 'foo' @@ -86,9 +79,10 @@ def test_filtering(objects, tabbed_browser, index_of, emitted): @pytest.mark.parametrize('index_of, verb', [(0, 'emitting'), (1, 'ignoring')]) -def test_logging(caplog, objects, tabbed_browser, index_of, verb): - tabbed_browser.current_index = 0 - tabbed_browser.index_of = index_of +def test_logging(caplog, objects, tabbed_browser_stubs, index_of, verb): + browser = tabbed_browser_stubs[0] + browser.current_index = 0 + browser.index_of = index_of with caplog.at_level(logging.DEBUG, logger='signals'): objects.signaller.signal.emit('foo') @@ -99,9 +93,10 @@ def test_logging(caplog, objects, tabbed_browser, index_of, verb): @pytest.mark.parametrize('index_of', [0, 1]) -def test_no_logging(caplog, objects, tabbed_browser, index_of): - tabbed_browser.current_index = 0 - tabbed_browser.index_of = index_of +def test_no_logging(caplog, objects, tabbed_browser_stubs, index_of): + browser = tabbed_browser_stubs[0] + browser.current_index = 0 + browser.index_of = index_of with caplog.at_level(logging.DEBUG, logger='signals'): objects.signaller.link_hovered.emit('foo') @@ -109,9 +104,10 @@ def test_no_logging(caplog, objects, tabbed_browser, index_of): assert not caplog.records -def test_runtime_error(objects, tabbed_browser): +def test_runtime_error(objects, tabbed_browser_stubs): """Test that there's no crash if indexOf() raises RuntimeError.""" - tabbed_browser.current_index = 0 - tabbed_browser.index_of = RuntimeError + browser = tabbed_browser_stubs[0] + browser.current_index = 0 + browser.index_of = RuntimeError objects.signaller.signal.emit('foo') assert objects.signaller.filtered_signal_arg is None diff --git a/tests/unit/commands/test_argparser.py b/tests/unit/commands/test_argparser.py index b44fd5dce..2ca6adede 100644 --- a/tests/unit/commands/test_argparser.py +++ b/tests/unit/commands/test_argparser.py @@ -37,13 +37,6 @@ class TestArgumentParser: def parser(self): return argparser.ArgumentParser('foo') - @pytest.fixture - def tabbed_browser(self, stubs, win_registry): - tb = stubs.TabbedBrowserStub() - objreg.register('tabbed-browser', tb, scope='window', window=0) - yield tb - objreg.delete('tabbed-browser', scope='window', window=0) - def test_name(self, parser): assert parser.name == 'foo' @@ -60,14 +53,14 @@ class TestArgumentParser: match="Unrecognized arguments: --foo"): parser.parse_args(['--foo']) - def test_help(self, parser, tabbed_browser): + def test_help(self, parser, tabbed_browser_stubs): parser.add_argument('--help', action=argparser.HelpAction, nargs=0) with pytest.raises(argparser.ArgumentParserExit): parser.parse_args(['--help']) expected_url = QUrl('qute://help/commands.html#foo') - assert tabbed_browser.opened_url == expected_url + assert tabbed_browser_stubs[1].opened_url == expected_url @pytest.mark.parametrize('types, value, expected', [ diff --git a/tests/unit/config/test_configcommands.py b/tests/unit/config/test_configcommands.py index 0f12dd843..a0b646893 100644 --- a/tests/unit/config/test_configcommands.py +++ b/tests/unit/config/test_configcommands.py @@ -38,19 +38,12 @@ class TestSet: """Tests for :set.""" - @pytest.fixture - def tabbed_browser(self, qapp, stubs, win_registry): - tb = stubs.TabbedBrowserStub() - objreg.register('tabbed-browser', tb, scope='window', window=0) - yield tb - objreg.delete('tabbed-browser', scope='window', window=0) - - def test_set_no_args(self, commands, tabbed_browser): + def test_set_no_args(self, commands, tabbed_browser_stubs): """Run ':set'. Should open qute://settings.""" commands.set(win_id=0) - assert tabbed_browser.opened_url == QUrl('qute://settings') + assert tabbed_browser_stubs[0].opened_url == QUrl('qute://settings') def test_get(self, config_stub, commands, message_mock): """Run ':set url.auto_search?'. diff --git a/tests/unit/mainwindow/statusbar/test_backforward.py b/tests/unit/mainwindow/statusbar/test_backforward.py index f2dec3d3f..6a3df1947 100644 --- a/tests/unit/mainwindow/statusbar/test_backforward.py +++ b/tests/unit/mainwindow/statusbar/test_backforward.py @@ -37,12 +37,12 @@ def backforward_widget(qtbot): (False, True, '[>]'), (True, True, '[<>]'), ]) -def test_backforward_widget(backforward_widget, stubs, +def test_backforward_widget(backforward_widget, tabbed_browser_stubs, fake_web_tab, can_go_back, can_go_forward, expected_text): """Ensure the Backforward widget shows the correct text.""" tab = fake_web_tab(can_go_back=can_go_back, can_go_forward=can_go_forward) - tabbed_browser = stubs.TabbedBrowserStub() + tabbed_browser = tabbed_browser_stubs[0] tabbed_browser.current_index = 1 tabbed_browser.tabs = [tab] backforward_widget.on_tab_cur_url_changed(tabbed_browser) @@ -58,10 +58,10 @@ def test_backforward_widget(backforward_widget, stubs, assert not backforward_widget.isVisible() -def test_none_tab(backforward_widget, stubs, fake_web_tab): +def test_none_tab(backforward_widget, tabbed_browser_stubs, fake_web_tab): """Make sure nothing crashes when passing None as tab.""" tab = fake_web_tab(can_go_back=True, can_go_forward=True) - tabbed_browser = stubs.TabbedBrowserStub() + tabbed_browser = tabbed_browser_stubs[0] tabbed_browser.current_index = 1 tabbed_browser.tabs = [tab] backforward_widget.on_tab_cur_url_changed(tabbed_browser) diff --git a/tests/unit/misc/test_sessions.py b/tests/unit/misc/test_sessions.py index 3a387d0dc..771430d5b 100644 --- a/tests/unit/misc/test_sessions.py +++ b/tests/unit/misc/test_sessions.py @@ -138,20 +138,12 @@ class FakeMainWindow(QObject): @pytest.fixture -def fake_window(win_registry, stubs, monkeypatch, qtbot): +def fake_window(tabbed_browser_stubs): """Fixture which provides a fake main windows with a tabbedbrowser.""" win0 = FakeMainWindow(b'fake-geometry-0', win_id=0) objreg.register('main-window', win0, scope='window', window=0) - - webview = QWebView() - qtbot.add_widget(webview) - browser = stubs.TabbedBrowserStub([webview]) - objreg.register('tabbed-browser', browser, scope='window', window=0) - yield - objreg.delete('main-window', scope='window', window=0) - objreg.delete('tabbed-browser', scope='window', window=0) class TestSaveAll: @@ -192,13 +184,12 @@ class TestSave: return state @pytest.fixture - def fake_history(self, win_registry, stubs, monkeypatch, webview): + def fake_history(self, stubs, tabbed_browser_stubs, monkeypatch, webview): """Fixture which provides a window with a fake history.""" win = FakeMainWindow(b'fake-geometry-0', win_id=0) objreg.register('main-window', win, scope='window', window=0) - browser = stubs.TabbedBrowserStub([webview]) - objreg.register('tabbed-browser', browser, scope='window', window=0) + browser = tabbed_browser_stubs[0] qapp = stubs.FakeQApplication(active_window=win) monkeypatch.setattr(sessions, 'QApplication', qapp)