From 18d42d1f0af31df21e00efdacb8be26c34408a0e Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Tue, 25 Aug 2015 22:17:42 +0200 Subject: [PATCH] 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"