From 26058f4e80735594f6c0de5f2faf017a93536481 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Tue, 25 Aug 2015 18:46:49 +0200 Subject: [PATCH 1/8] A few cmdhistory tests --- qutebrowser/misc/cmdhistory.py | 6 +- qutebrowser/utils/usertypes.py | 2 +- tests/unit/misc/test_cmdhistory.py | 94 ++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 tests/unit/misc/test_cmdhistory.py diff --git a/qutebrowser/misc/cmdhistory.py b/qutebrowser/misc/cmdhistory.py index 2c8ada291..04e0f0067 100644 --- a/qutebrowser/misc/cmdhistory.py +++ b/qutebrowser/misc/cmdhistory.py @@ -63,9 +63,9 @@ class History(QObject): super().__init__(parent) self.handle_private_mode = False self._tmphist = None - if history is None: - self.history = [] - else: + + self.history = [] + if history: self.history = history def __getitem__(self, idx): diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 42ce0b93a..a8b33e9c9 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -53,7 +53,7 @@ def enum(name, items, start=1, is_int=False): class NeighborList(collections.abc.Sequence): - """A list of items which saves it current position. + """A list of items which saves its current position. Class attributes: Modes: Different modes, see constructor documentation. diff --git a/tests/unit/misc/test_cmdhistory.py b/tests/unit/misc/test_cmdhistory.py new file mode 100644 index 000000000..7579225e5 --- /dev/null +++ b/tests/unit/misc/test_cmdhistory.py @@ -0,0 +1,94 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Alexander Cogneau (acogneau) : +# +# 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 misc.History.""" + +import pytest + +from qutebrowser.misc.cmdhistory import History + + +HISTORY = ['first', 'second', 'third', 'fourth', 'fifth'] + + +class TestConstructor: + + """Tests for the constructor.""" + + def test_no_history(self): + hist = History() + # .history should equal [] + assert len(hist.history) == 0 + + def test_history(self): + hist = History(history=HISTORY) + assert hist.history == HISTORY + + +class TestCommandHistory: + + """Create a setup for inheritance""" + + @pytest.fixture(autouse=True) + def setup(self): + self.hist = History(history=HISTORY) + + +class TestBrowsing(TestCommandHistory): + + """Tests for the history browsing.""" + + def test_append_private_mode(self, monkeypatch): + """Test append in private mode.""" + self.hist.handle_private_mode = True + # We want general.private-browsing set to True + monkeypatch.setattr('qutebrowser.config.config.get', + lambda s1, s2: True) + self.hist.append('new item') + assert self.hist.history == HISTORY + + def test_append(self, monkeypatch): + """Test append outside private mode.""" + + # Private mode is disabled (general.private-browsing is set to False) + monkeypatch.setattr('qutebrowser.config.config.get', + lambda s1, s2: False) + self.hist.append('new item') + assert 'new item' in self.hist.history + self.hist.history.remove('new item') + assert self.hist.history == HISTORY + + def test_is_browsing(self): + """Test is_browsing().""" + + self.hist._tmphist = None + assert not self.hist.is_browsing() + + self.hist._tmphist = HISTORY + assert self.hist.is_browsing() + + def test_start_stop_is_browsing(self): + """Test the start/stop.""" + + # We can use is_browsing() because it is tested above + assert not self.hist.is_browsing() + self.hist.start('s') + assert self.hist.is_browsing() + self.hist.stop() + assert not self.hist.is_browsing() From 18d42d1f0af31df21e00efdacb8be26c34408a0e Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Tue, 25 Aug 2015 22:17:42 +0200 Subject: [PATCH 2/8] More tests for cmdhistory --- qutebrowser/misc/cmdhistory.py | 12 +++++--- tests/unit/misc/test_cmdhistory.py | 44 ++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/qutebrowser/misc/cmdhistory.py b/qutebrowser/misc/cmdhistory.py index 04e0f0067..c4d20737d 100644 --- a/qutebrowser/misc/cmdhistory.py +++ b/qutebrowser/misc/cmdhistory.py @@ -29,7 +29,11 @@ class HistoryEmptyError(Exception): """Raised when the history is empty.""" - pass + def __init__(self, value): + self.value = value + + def __str__(self): + return self.value class HistoryEndReachedError(Exception): @@ -90,7 +94,7 @@ class History(QObject): else: items = self.history if not items: - raise HistoryEmptyError + raise HistoryEmptyError("History is empty.") self._tmphist = usertypes.NeighborList(items) return self._tmphist.lastitem() @@ -109,7 +113,7 @@ class History(QObject): try: return self._tmphist.previtem() except IndexError: - raise HistoryEndReachedError + raise HistoryEndReachedError("History end reached") def nextitem(self): """Get the next item in the temp history. @@ -121,7 +125,7 @@ class History(QObject): try: return self._tmphist.nextitem() except IndexError: - raise HistoryEndReachedError + raise HistoryEndReachedError("History end reached") def append(self, text): """Append a new item to the history. diff --git a/tests/unit/misc/test_cmdhistory.py b/tests/unit/misc/test_cmdhistory.py index 7579225e5..ef983fc81 100644 --- a/tests/unit/misc/test_cmdhistory.py +++ b/tests/unit/misc/test_cmdhistory.py @@ -21,7 +21,7 @@ import pytest -from qutebrowser.misc.cmdhistory import History +from qutebrowser.misc.cmdhistory import History, HistoryEmptyError HISTORY = ['first', 'second', 'third', 'fourth', 'fifth'] @@ -83,7 +83,7 @@ class TestBrowsing(TestCommandHistory): self.hist._tmphist = HISTORY assert self.hist.is_browsing() - def test_start_stop_is_browsing(self): + def test_start_stop(self): """Test the start/stop.""" # We can use is_browsing() because it is tested above @@ -92,3 +92,43 @@ class TestBrowsing(TestCommandHistory): assert self.hist.is_browsing() self.hist.stop() assert not self.hist.is_browsing() + + def test_start_with_text(self): + """Test start with given 'text'.""" + self.hist.start('f') + assert 'first' in self.hist._tmphist + assert 'fourth' in self.hist._tmphist + assert 'second' not in self.hist._tmphist + + def test_start_no_text(self): + """Test start with no given text.""" + self.hist.start('') + + # There is probably a better way for NeighbourList? + for i in self.hist._tmphist: + assert i in HISTORY + + for i in HISTORY: + assert i in self.hist._tmphist + + def test_start_no_items(self): + """Test start with no matching text.""" + with pytest.raises(HistoryEmptyError) as excinfo: + self.hist.start('k') + assert str(excinfo.value) == "History is empty." + assert not self.hist._tmphist + + def test_get_item(self): + """Test __get_item__.""" + for i in range(0, len(HISTORY)): + assert self.hist[i] == HISTORY[i] + + def test_not_browsing_error(self): + """Test that next/previtem throws a ValueError""" + with pytest.raises(ValueError) as error1: + self.hist.nextitem() + assert str(error1.value) == "Currently not browsing history" + + with pytest.raises(ValueError) as error2: + self.hist.previtem() + assert str(error2.value) == "Currently not browsing history" From d3488172ec06421ccd59c96f813e782c6698afa2 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Tue, 25 Aug 2015 22:41:54 +0200 Subject: [PATCH 3/8] Lift duration for test_debug --- tests/unit/utils/test_debug.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_debug.py b/tests/unit/utils/test_debug.py index e1e4a6615..5fae9def6 100644 --- a/tests/unit/utils/test_debug.py +++ b/tests/unit/utils/test_debug.py @@ -96,7 +96,7 @@ def test_log_time(caplog): assert match duration = float(match.group(1)) - assert 0 < duration < 1 + assert 0 < duration < 5 class TestQEnumKey: From a1dff7d5354692186d5ec9badf58b92ec4708b1a Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Wed, 26 Aug 2015 00:16:18 +0200 Subject: [PATCH 4/8] Final cmdhistory tests --- qutebrowser/misc/cmdhistory.py | 6 +- tests/unit/misc/test_cmdhistory.py | 109 +++++++++++++++++++++++------ 2 files changed, 89 insertions(+), 26 deletions(-) diff --git a/qutebrowser/misc/cmdhistory.py b/qutebrowser/misc/cmdhistory.py index c4d20737d..708c2a9e6 100644 --- a/qutebrowser/misc/cmdhistory.py +++ b/qutebrowser/misc/cmdhistory.py @@ -29,11 +29,7 @@ class HistoryEmptyError(Exception): """Raised when the history is empty.""" - def __init__(self, value): - self.value = value - - def __str__(self): - return self.value + pass class HistoryEndReachedError(Exception): diff --git a/tests/unit/misc/test_cmdhistory.py b/tests/unit/misc/test_cmdhistory.py index ef983fc81..480cb9dfc 100644 --- a/tests/unit/misc/test_cmdhistory.py +++ b/tests/unit/misc/test_cmdhistory.py @@ -21,11 +21,15 @@ import pytest -from qutebrowser.misc.cmdhistory import History, HistoryEmptyError +from qutebrowser.misc.cmdhistory import (History, HistoryEmptyError, + HistoryEndReachedError) HISTORY = ['first', 'second', 'third', 'fourth', 'fifth'] +CONFIG_NOT_PRIVATE = {'general': {'private-browsing': False}} +CONFIG_PRIVATE = {'general': {'private-browsing': True}} + class TestConstructor: @@ -54,26 +58,6 @@ class TestBrowsing(TestCommandHistory): """Tests for the history browsing.""" - def test_append_private_mode(self, monkeypatch): - """Test append in private mode.""" - self.hist.handle_private_mode = True - # We want general.private-browsing set to True - monkeypatch.setattr('qutebrowser.config.config.get', - lambda s1, s2: True) - self.hist.append('new item') - assert self.hist.history == HISTORY - - def test_append(self, monkeypatch): - """Test append outside private mode.""" - - # Private mode is disabled (general.private-browsing is set to False) - monkeypatch.setattr('qutebrowser.config.config.get', - lambda s1, s2: False) - self.hist.append('new item') - assert 'new item' in self.hist.history - self.hist.history.remove('new item') - assert self.hist.history == HISTORY - def test_is_browsing(self): """Test is_browsing().""" @@ -132,3 +116,86 @@ class TestBrowsing(TestCommandHistory): with pytest.raises(ValueError) as error2: self.hist.previtem() assert str(error2.value) == "Currently not browsing history" + + def return_item(self): + return 'item' + + def test_nextitem_single(self): + """Test nextitem() with valid input.""" + self.hist.start('f') + self.hist._tmphist.nextitem = self.return_item + assert self.hist.nextitem() == 'item' + + def test_previtem_single(self): + """Test previtem() with valid input.""" + self.hist.start('f') + self.hist._tmphist.previtem = self.return_item + assert self.hist.previtem() == 'item' + + def test_nextitem_previtem_chain(self): + """Test a combination of nextitem and previtem statements""" + assert self.hist.start('f') == 'fifth' + assert self.hist.previtem() == 'fourth' + assert self.hist.previtem() == 'first' + assert self.hist.nextitem() == 'fourth' + + def raise_index_error(self): + raise IndexError() + + def test_nextitem_index_error(self): + """"Test nextitem() when _tmphist raises an IndexError""" + self.hist.start('f') + self.hist._tmphist.nextitem = self.raise_index_error + with pytest.raises(HistoryEndReachedError) as excinfo: + self.hist.nextitem() + assert str(excinfo.value) == "History end reached" + + def test_previtem_index_error(self): + """"Test previtem() when _tmphist raises an IndexError""" + self.hist.start('f') + self.hist._tmphist.previtem = self.raise_index_error + with pytest.raises(HistoryEndReachedError) as excinfo: + self.hist.previtem() + assert str(excinfo.value) == "History end reached" + + def test_append_private_mode(self, monkeypatch, config_stub): + """Test append in private mode.""" + self.hist.handle_private_mode = True + # We want general.private-browsing set to True + config_stub.data = CONFIG_PRIVATE + monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', + config_stub) + self.hist.append('new item') + assert self.hist.history == HISTORY + + def test_append(self, monkeypatch, config_stub): + """Test append outside private mode.""" + + # Private mode is disabled (general.private-browsing is set to False) + config_stub.data = CONFIG_NOT_PRIVATE + monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', + config_stub) + self.hist.append('new item') + assert 'new item' in self.hist.history + self.hist.history.remove('new item') + assert self.hist.history == HISTORY + + def test_append_empty_history(self, monkeypatch, config_stub): + """Test append when .history is empty.""" + # Disable private mode + config_stub.data = CONFIG_NOT_PRIVATE + monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', + config_stub) + self.hist.history = [] + self.hist.append('item') + assert self.hist[0] == 'item' + + def test_append_double(self, monkeypatch, config_stub): + # Disable private mode + config_stub.data = CONFIG_NOT_PRIVATE + monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', + config_stub) + self.hist.append('fifth') + # assert that the new 'fifth' is not added + assert self.hist.history[-1] == 'fifth' + assert self.hist.history[-2] == 'fourth' From 9ef9224c3229a0ab331cb774de3f7a7ea788038e Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Wed, 26 Aug 2015 00:20:14 +0200 Subject: [PATCH 5/8] Remove unecessary class --- tests/unit/misc/test_cmdhistory.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/unit/misc/test_cmdhistory.py b/tests/unit/misc/test_cmdhistory.py index 480cb9dfc..99d3f10c0 100644 --- a/tests/unit/misc/test_cmdhistory.py +++ b/tests/unit/misc/test_cmdhistory.py @@ -47,17 +47,12 @@ class TestConstructor: class TestCommandHistory: - """Create a setup for inheritance""" + """Tests for Command History.""" @pytest.fixture(autouse=True) def setup(self): self.hist = History(history=HISTORY) - -class TestBrowsing(TestCommandHistory): - - """Tests for the history browsing.""" - def test_is_browsing(self): """Test is_browsing().""" From 933d683ff47655b7537769220b35949869de41a4 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Wed, 26 Aug 2015 01:20:57 +0200 Subject: [PATCH 6/8] Add cmdhistory.py to PERFECT_FILES --- scripts/dev/check_coverage.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index f5fefaf76..ec7cec73b 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -52,6 +52,7 @@ PERFECT_FILES = [ 'qutebrowser/misc/checkpyver.py', 'qutebrowser/misc/guiprocess.py', 'qutebrowser/misc/editor.py', + 'qutebrowser/misc/cmdhistory.py' 'qutebrowser/mainwindow/statusbar/keystring.py', 'qutebrowser/mainwindow/statusbar/percentage.py', From 3a4069667a78a467231adc7f02c8586c7c804bf0 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Wed, 26 Aug 2015 12:07:47 +0200 Subject: [PATCH 7/8] Remove test functions out of class --- qutebrowser/misc/cmdhistory.py | 7 +- scripts/dev/check_coverage.py | 2 +- tests/unit/misc/test_cmdhistory.py | 267 ++++++++++++++--------------- 3 files changed, 135 insertions(+), 141 deletions(-) diff --git a/qutebrowser/misc/cmdhistory.py b/qutebrowser/misc/cmdhistory.py index 708c2a9e6..51a001aa1 100644 --- a/qutebrowser/misc/cmdhistory.py +++ b/qutebrowser/misc/cmdhistory.py @@ -64,8 +64,9 @@ class History(QObject): self.handle_private_mode = False self._tmphist = None - self.history = [] - if history: + if history is None: + self.history = [] + else: self.history = history def __getitem__(self, idx): @@ -90,7 +91,7 @@ class History(QObject): else: items = self.history if not items: - raise HistoryEmptyError("History is empty.") + raise HistoryEmptyError self._tmphist = usertypes.NeighborList(items) return self._tmphist.lastitem() diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index ec7cec73b..a8e9d3d85 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -52,7 +52,7 @@ PERFECT_FILES = [ 'qutebrowser/misc/checkpyver.py', 'qutebrowser/misc/guiprocess.py', 'qutebrowser/misc/editor.py', - 'qutebrowser/misc/cmdhistory.py' + 'qutebrowser/misc/cmdhistory.py', 'qutebrowser/mainwindow/statusbar/keystring.py', 'qutebrowser/mainwindow/statusbar/percentage.py', diff --git a/tests/unit/misc/test_cmdhistory.py b/tests/unit/misc/test_cmdhistory.py index 99d3f10c0..e24eaf407 100644 --- a/tests/unit/misc/test_cmdhistory.py +++ b/tests/unit/misc/test_cmdhistory.py @@ -17,12 +17,11 @@ # You should have received a copy of the GNU General Public License # along with qutebrowser. If not, see . -"""Tests for misc.History.""" +"""Tests for misc.cmdhistory.History.""" import pytest -from qutebrowser.misc.cmdhistory import (History, HistoryEmptyError, - HistoryEndReachedError) +from qutebrowser.misc import cmdhistory HISTORY = ['first', 'second', 'third', 'fourth', 'fifth'] @@ -31,166 +30,160 @@ CONFIG_NOT_PRIVATE = {'general': {'private-browsing': False}} CONFIG_PRIVATE = {'general': {'private-browsing': True}} -class TestConstructor: - - """Tests for the constructor.""" - - def test_no_history(self): - hist = History() - # .history should equal [] - assert len(hist.history) == 0 - - def test_history(self): - hist = History(history=HISTORY) - assert hist.history == HISTORY +@pytest.fixture +def hist(): + return cmdhistory.History(history=HISTORY) -class TestCommandHistory: +def test_no_history(): + hist = cmdhistory.History() + # .history should equal [] + assert hist.history == [] - """Tests for Command History.""" - @pytest.fixture(autouse=True) - def setup(self): - self.hist = History(history=HISTORY) +def test_history(): + hist = cmdhistory.History(history=HISTORY) + assert hist.history == HISTORY - def test_is_browsing(self): - """Test is_browsing().""" - self.hist._tmphist = None - assert not self.hist.is_browsing() +@pytest.mark.parametrize('tmphist, expected', [(None, False), (HISTORY, True)]) +def test_is_browsing(hist, tmphist, expected): + hist._tmphist = tmphist + assert hist.is_browsing() == expected - self.hist._tmphist = HISTORY - assert self.hist.is_browsing() - def test_start_stop(self): - """Test the start/stop.""" +def test_start_stop(hist): + # We can use is_browsing() because it is tested above + assert not hist.is_browsing() + hist.start('s') + assert hist.is_browsing() + hist.stop() + assert not hist.is_browsing() - # We can use is_browsing() because it is tested above - assert not self.hist.is_browsing() - self.hist.start('s') - assert self.hist.is_browsing() - self.hist.stop() - assert not self.hist.is_browsing() - def test_start_with_text(self): - """Test start with given 'text'.""" - self.hist.start('f') - assert 'first' in self.hist._tmphist - assert 'fourth' in self.hist._tmphist - assert 'second' not in self.hist._tmphist +def test_start_with_text(hist): + """Test start with given 'text'.""" + hist.start('f') + assert 'first' in hist._tmphist + assert 'fourth' in hist._tmphist + assert 'second' not in hist._tmphist - def test_start_no_text(self): - """Test start with no given text.""" - self.hist.start('') - # There is probably a better way for NeighbourList? - for i in self.hist._tmphist: - assert i in HISTORY +def test_start_no_text(hist): + """Test start with no given text.""" + hist.start('') + assert list(hist._tmphist) == HISTORY - for i in HISTORY: - assert i in self.hist._tmphist - def test_start_no_items(self): - """Test start with no matching text.""" - with pytest.raises(HistoryEmptyError) as excinfo: - self.hist.start('k') - assert str(excinfo.value) == "History is empty." - assert not self.hist._tmphist +def test_start_no_items(hist): + """Test start with no matching text.""" + with pytest.raises(cmdhistory.HistoryEmptyError) as excinfo: + hist.start('k') + assert excinfo is not None + assert not hist._tmphist - def test_get_item(self): - """Test __get_item__.""" - for i in range(0, len(HISTORY)): - assert self.hist[i] == HISTORY[i] - def test_not_browsing_error(self): - """Test that next/previtem throws a ValueError""" - with pytest.raises(ValueError) as error1: - self.hist.nextitem() - assert str(error1.value) == "Currently not browsing history" +def test_getitem(hist): + """Test __getitem__.""" + for i in range(0, len(HISTORY)): + assert hist[i] == HISTORY[i] - with pytest.raises(ValueError) as error2: - self.hist.previtem() - assert str(error2.value) == "Currently not browsing history" - def return_item(self): - return 'item' +def test_not_browsing_error(hist): + """Test that next/previtem throws a ValueError""" + with pytest.raises(ValueError) as error1: + hist.nextitem() + assert str(error1.value) == "Currently not browsing history" - def test_nextitem_single(self): - """Test nextitem() with valid input.""" - self.hist.start('f') - self.hist._tmphist.nextitem = self.return_item - assert self.hist.nextitem() == 'item' + with pytest.raises(ValueError) as error2: + hist.previtem() + assert str(error2.value) == "Currently not browsing history" - def test_previtem_single(self): - """Test previtem() with valid input.""" - self.hist.start('f') - self.hist._tmphist.previtem = self.return_item - assert self.hist.previtem() == 'item' - def test_nextitem_previtem_chain(self): - """Test a combination of nextitem and previtem statements""" - assert self.hist.start('f') == 'fifth' - assert self.hist.previtem() == 'fourth' - assert self.hist.previtem() == 'first' - assert self.hist.nextitem() == 'fourth' +def return_item(self=None): + return 'item' - def raise_index_error(self): - raise IndexError() - def test_nextitem_index_error(self): - """"Test nextitem() when _tmphist raises an IndexError""" - self.hist.start('f') - self.hist._tmphist.nextitem = self.raise_index_error - with pytest.raises(HistoryEndReachedError) as excinfo: - self.hist.nextitem() - assert str(excinfo.value) == "History end reached" +def test_nextitem_single(hist, monkeypatch): + """Test nextitem() with valid input.""" + hist.start('f') + monkeypatch.setattr(hist._tmphist, 'nextitem', lambda: 'item') + assert hist.nextitem() == 'item' - def test_previtem_index_error(self): - """"Test previtem() when _tmphist raises an IndexError""" - self.hist.start('f') - self.hist._tmphist.previtem = self.raise_index_error - with pytest.raises(HistoryEndReachedError) as excinfo: - self.hist.previtem() - assert str(excinfo.value) == "History end reached" - def test_append_private_mode(self, monkeypatch, config_stub): - """Test append in private mode.""" - self.hist.handle_private_mode = True - # We want general.private-browsing set to True - config_stub.data = CONFIG_PRIVATE - monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', - config_stub) - self.hist.append('new item') - assert self.hist.history == HISTORY +def test_previtem_single(hist, monkeypatch): + """Test previtem() with valid input.""" + hist.start('f') + monkeypatch.setattr(hist._tmphist, 'previtem', lambda: 'item') + assert hist.previtem() == 'item' - def test_append(self, monkeypatch, config_stub): - """Test append outside private mode.""" - # Private mode is disabled (general.private-browsing is set to False) - config_stub.data = CONFIG_NOT_PRIVATE - monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', - config_stub) - self.hist.append('new item') - assert 'new item' in self.hist.history - self.hist.history.remove('new item') - assert self.hist.history == HISTORY +def test_nextitem_previtem_chain(hist): + """Test a combination of nextitem and previtem statements""" + assert hist.start('f') == 'fifth' + assert hist.previtem() == 'fourth' + assert hist.previtem() == 'first' + assert hist.nextitem() == 'fourth' - def test_append_empty_history(self, monkeypatch, config_stub): - """Test append when .history is empty.""" - # Disable private mode - config_stub.data = CONFIG_NOT_PRIVATE - monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', - config_stub) - self.hist.history = [] - self.hist.append('item') - assert self.hist[0] == 'item' - def test_append_double(self, monkeypatch, config_stub): - # Disable private mode - config_stub.data = CONFIG_NOT_PRIVATE - monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', - config_stub) - self.hist.append('fifth') - # assert that the new 'fifth' is not added - assert self.hist.history[-1] == 'fifth' - assert self.hist.history[-2] == 'fourth' +def raise_index_error(self=None): + raise IndexError() + + +def test_nextitem_index_error(hist): + """"Test nextitem() when _tmphist raises an IndexError""" + hist.start('f') + hist._tmphist.nextitem = raise_index_error + with pytest.raises(cmdhistory.HistoryEndReachedError) as excinfo: + hist.nextitem() + assert str(excinfo.value) == "History end reached" + + +def test_previtem_index_error(hist): + """"Test previtem() when _tmphist raises an IndexError""" + hist.start('f') + hist._tmphist.previtem = raise_index_error + with pytest.raises(cmdhistory.HistoryEndReachedError) as excinfo: + hist.previtem() + assert str(excinfo.value) == "History end reached" + + +def test_append_private_mode(hist, monkeypatch, config_stub): + """Test append in private mode.""" + hist.handle_private_mode = True + # We want general.private-browsing set to True + config_stub.data = CONFIG_PRIVATE + monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', + config_stub) + hist.append('new item') + assert hist.history == HISTORY + + +def test_append(hist, config_stub): + """Test append outside private mode.""" + + # Private mode is disabled (general.private-browsing is set to False) + config_stub.data = CONFIG_NOT_PRIVATE + hist.append('new item') + assert 'new item' in hist.history + hist.history.remove('new item') + assert hist.history == HISTORY + + +def test_append_empty_history(hist, config_stub): + """Test append when .history is empty.""" + # Disable private mode + config_stub.data = CONFIG_NOT_PRIVATE + hist.history = [] + hist.append('item') + assert hist[0] == 'item' + + +def test_append_double(hist, monkeypatch, config_stub): + # Disable private mode + config_stub.data = CONFIG_NOT_PRIVATE + monkeypatch.setattr('qutebrowser.misc.cmdhistory.config', + config_stub) + hist.append('fifth') + # assert that the new 'fifth' is not added + assert hist.history[-2:] == ['fourth', 'fifth'] From fd6e0559a6c94410ffa41aca2165377d81bcd298 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Wed, 26 Aug 2015 12:10:25 +0200 Subject: [PATCH 8/8] remove unnecessary function --- tests/unit/misc/test_cmdhistory.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/unit/misc/test_cmdhistory.py b/tests/unit/misc/test_cmdhistory.py index e24eaf407..096a26a82 100644 --- a/tests/unit/misc/test_cmdhistory.py +++ b/tests/unit/misc/test_cmdhistory.py @@ -100,10 +100,6 @@ def test_not_browsing_error(hist): assert str(error2.value) == "Currently not browsing history" -def return_item(self=None): - return 'item' - - def test_nextitem_single(hist, monkeypatch): """Test nextitem() with valid input.""" hist.start('f')