2015-08-25 18:46:49 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2015-2016 Alexander Cogneau (acogneau) <alexander.cogneau@gmail.com>
|
|
|
|
# Copyright 2015-2016 Florian Bruhin (The-Compiler) <me@the-compiler.org>
|
2015-08-25 18:46:49 +02:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-08-26 12:07:47 +02:00
|
|
|
"""Tests for misc.cmdhistory.History."""
|
2015-08-25 18:46:49 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2015-08-26 12:07:47 +02:00
|
|
|
from qutebrowser.misc import cmdhistory
|
2015-08-25 18:46:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
HISTORY = ['first', 'second', 'third', 'fourth', 'fifth']
|
|
|
|
|
2015-08-26 00:16:18 +02:00
|
|
|
CONFIG_NOT_PRIVATE = {'general': {'private-browsing': False}}
|
|
|
|
CONFIG_PRIVATE = {'general': {'private-browsing': True}}
|
|
|
|
|
2015-08-25 18:46:49 +02:00
|
|
|
|
2015-08-26 12:07:47 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def hist():
|
|
|
|
return cmdhistory.History(history=HISTORY)
|
2015-08-25 18:46:49 +02:00
|
|
|
|
|
|
|
|
2015-08-26 12:07:47 +02:00
|
|
|
def test_no_history():
|
|
|
|
hist = cmdhistory.History()
|
|
|
|
assert hist.history == []
|
2015-08-25 18:46:49 +02:00
|
|
|
|
|
|
|
|
2015-08-26 12:07:47 +02:00
|
|
|
def test_history():
|
|
|
|
hist = cmdhistory.History(history=HISTORY)
|
|
|
|
assert hist.history == HISTORY
|
2015-08-25 18:46:49 +02:00
|
|
|
|
|
|
|
|
2015-08-26 12:07:47 +02:00
|
|
|
@pytest.mark.parametrize('tmphist, expected', [(None, False), (HISTORY, True)])
|
|
|
|
def test_is_browsing(hist, tmphist, expected):
|
|
|
|
hist._tmphist = tmphist
|
|
|
|
assert hist.is_browsing() == expected
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
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(hist):
|
|
|
|
"""Test start with no given text."""
|
|
|
|
hist.start('')
|
|
|
|
assert list(hist._tmphist) == HISTORY
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_no_items(hist):
|
|
|
|
"""Test start with no matching text."""
|
2015-08-27 06:28:26 +02:00
|
|
|
with pytest.raises(cmdhistory.HistoryEmptyError):
|
2015-08-26 12:07:47 +02:00
|
|
|
hist.start('k')
|
|
|
|
assert not hist._tmphist
|
|
|
|
|
|
|
|
|
|
|
|
def test_getitem(hist):
|
|
|
|
"""Test __getitem__."""
|
|
|
|
for i in range(0, len(HISTORY)):
|
|
|
|
assert hist[i] == HISTORY[i]
|
|
|
|
|
|
|
|
|
2015-08-27 06:36:40 +02:00
|
|
|
def test_setitem(hist):
|
|
|
|
"""Test __setitem__."""
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
hist[0] = 'foo'
|
|
|
|
expected = "'History' object does not support item assignment"
|
|
|
|
assert str(excinfo.value) == expected
|
|
|
|
|
|
|
|
|
2015-08-26 12:07:47 +02:00
|
|
|
def test_not_browsing_error(hist):
|
2016-02-10 19:18:47 +01:00
|
|
|
"""Test that next/previtem throws a ValueError."""
|
2015-08-26 12:07:47 +02:00
|
|
|
with pytest.raises(ValueError) as error1:
|
|
|
|
hist.nextitem()
|
|
|
|
assert str(error1.value) == "Currently not browsing history"
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as error2:
|
|
|
|
hist.previtem()
|
|
|
|
assert str(error2.value) == "Currently not browsing history"
|
|
|
|
|
|
|
|
|
|
|
|
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_single(hist, monkeypatch):
|
|
|
|
"""Test previtem() with valid input."""
|
|
|
|
hist.start('f')
|
|
|
|
monkeypatch.setattr(hist._tmphist, 'previtem', lambda: 'item')
|
|
|
|
assert hist.previtem() == 'item'
|
|
|
|
|
|
|
|
|
|
|
|
def test_nextitem_previtem_chain(hist):
|
2016-02-10 19:18:47 +01:00
|
|
|
"""Test a combination of nextitem and previtem statements."""
|
2015-08-26 12:07:47 +02:00
|
|
|
assert hist.start('f') == 'fifth'
|
|
|
|
assert hist.previtem() == 'fourth'
|
|
|
|
assert hist.previtem() == 'first'
|
|
|
|
assert hist.nextitem() == 'fourth'
|
|
|
|
|
|
|
|
|
|
|
|
def test_nextitem_index_error(hist):
|
2016-02-10 19:18:47 +01:00
|
|
|
""""Test nextitem() when _tmphist raises an IndexError."""
|
2015-08-26 12:07:47 +02:00
|
|
|
hist.start('f')
|
2015-08-27 06:28:26 +02:00
|
|
|
with pytest.raises(cmdhistory.HistoryEndReachedError):
|
2015-08-26 12:07:47 +02:00
|
|
|
hist.nextitem()
|
|
|
|
|
|
|
|
|
|
|
|
def test_previtem_index_error(hist):
|
2016-02-10 19:18:47 +01:00
|
|
|
""""Test previtem() when _tmphist raises an IndexError."""
|
2015-08-26 12:07:47 +02:00
|
|
|
hist.start('f')
|
2015-08-27 06:28:26 +02:00
|
|
|
with pytest.raises(cmdhistory.HistoryEndReachedError):
|
2015-08-27 06:40:14 +02:00
|
|
|
for _ in range(10):
|
|
|
|
hist.previtem()
|
2015-08-26 12:07:47 +02:00
|
|
|
|
|
|
|
|
2015-08-27 06:25:10 +02:00
|
|
|
def test_append_private_mode(hist, config_stub):
|
2015-08-26 12:07:47 +02:00
|
|
|
"""Test append in private mode."""
|
|
|
|
hist.handle_private_mode = True
|
|
|
|
# We want general.private-browsing set to True
|
|
|
|
config_stub.data = CONFIG_PRIVATE
|
|
|
|
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'
|
|
|
|
|
|
|
|
|
2015-08-27 06:25:10 +02:00
|
|
|
def test_append_double(hist, config_stub):
|
2015-08-26 12:07:47 +02:00
|
|
|
# Disable private mode
|
|
|
|
config_stub.data = CONFIG_NOT_PRIVATE
|
|
|
|
hist.append('fifth')
|
|
|
|
# assert that the new 'fifth' is not added
|
|
|
|
assert hist.history[-2:] == ['fourth', 'fifth']
|