Rewrite test_neighborlist.py to use pytest.

See #660.
This commit is contained in:
Florian Bruhin 2015-05-10 15:25:04 +02:00
parent 021c94eece
commit 392fb3e1d7

View File

@ -21,364 +21,331 @@
"""Tests for the NeighborList class."""
import unittest
from qutebrowser.utils import usertypes
import pytest
class InitTests(unittest.TestCase):
"""Just try to init some neighborlists.
class TestInit:
Attributes:
nl: The NeighborList we're testing.
"""
"""Just try to init some neighborlists."""
def test_empty(self):
"""Test constructing an empty NeighborList."""
nl = usertypes.NeighborList()
self.assertEqual(nl.items, [])
assert nl.items == []
def test_items(self):
"""Test constructing an NeighborList with items."""
nl = usertypes.NeighborList([1, 2, 3])
self.assertEqual(nl.items, [1, 2, 3])
assert nl.items == [1, 2, 3]
def test_len(self):
"""Test len() on NeighborList."""
nl = usertypes.NeighborList([1, 2, 3])
self.assertEqual(len(nl), 3)
assert len(nl) == 3
def test_contains(self):
"""Test 'in' on NeighborList."""
nl = usertypes.NeighborList([1, 2, 3])
self.assertIn(2, nl)
self.assertNotIn(4, nl)
assert 2 in nl
assert 4 not in nl
class DefaultTests(unittest.TestCase):
class TestDefaultArg:
"""Test the default argument.
Attributes:
nl: The NeighborList we're testing.
"""
"""Test the default argument."""
def test_simple(self):
"""Test default with a numeric argument."""
nl = usertypes.NeighborList([1, 2, 3], default=2)
self.assertEqual(nl._idx, 1)
assert nl._idx == 1
def test_none(self):
"""Test default 'None'."""
nl = usertypes.NeighborList([1, 2, None], default=None)
self.assertEqual(nl._idx, 2)
assert nl._idx == 2
def test_unset(self):
"""Test unset default value."""
nl = usertypes.NeighborList([1, 2, 3])
self.assertIsNone(nl._idx)
assert nl._idx is None
class EmptyTests(unittest.TestCase):
class TestEmpty:
"""Tests with no items.
"""Tests with no items."""
Attributes:
nl: The NeighborList we're testing.
"""
@pytest.fixture
def neighborlist(self):
return usertypes.NeighborList()
def setUp(self):
self.nl = usertypes.NeighborList()
def test_curitem(self):
def test_curitem(self, neighborlist):
"""Test curitem with no item."""
with self.assertRaises(IndexError):
self.nl.curitem()
with pytest.raises(IndexError):
neighborlist.curitem()
def test_firstitem(self):
def test_firstitem(self, neighborlist):
"""Test firstitem with no item."""
with self.assertRaises(IndexError):
self.nl.firstitem()
with pytest.raises(IndexError):
neighborlist.firstitem()
def test_lastitem(self):
def test_lastitem(self, neighborlist):
"""Test lastitem with no item."""
with self.assertRaises(IndexError):
self.nl.lastitem()
with pytest.raises(IndexError):
neighborlist.lastitem()
def test_getitem(self):
def test_getitem(self, neighborlist):
"""Test getitem with no item."""
with self.assertRaises(IndexError):
self.nl.getitem(1)
with pytest.raises(IndexError):
neighborlist.getitem(1)
class ItemTests(unittest.TestCase):
class TestItems:
"""Tests with items.
"""Tests with items."""
Attributes:
nl: The NeighborList we're testing.
"""
@pytest.fixture
def neighborlist(self):
return usertypes.NeighborList([1, 2, 3, 4, 5], default=3)
def setUp(self):
self.nl = usertypes.NeighborList([1, 2, 3, 4, 5], default=3)
def test_curitem(self):
def test_curitem(self, neighborlist):
"""Test curitem()."""
self.assertEqual(self.nl._idx, 2)
self.assertEqual(self.nl.curitem(), 3)
self.assertEqual(self.nl._idx, 2)
assert neighborlist._idx == 2
assert neighborlist.curitem() == 3
assert neighborlist._idx == 2
def test_nextitem(self):
def test_nextitem(self, neighborlist):
"""Test nextitem()."""
self.assertEqual(self.nl.nextitem(), 4)
self.assertEqual(self.nl._idx, 3)
self.assertEqual(self.nl.nextitem(), 5)
self.assertEqual(self.nl._idx, 4)
assert neighborlist.nextitem() == 4
assert neighborlist._idx == 3
assert neighborlist.nextitem() == 5
assert neighborlist._idx == 4
def test_previtem(self):
def test_previtem(self, neighborlist):
"""Test previtem()."""
self.assertEqual(self.nl.previtem(), 2)
self.assertEqual(self.nl._idx, 1)
self.assertEqual(self.nl.previtem(), 1)
self.assertEqual(self.nl._idx, 0)
assert neighborlist.previtem() == 2
assert neighborlist._idx == 1
assert neighborlist.previtem() == 1
assert neighborlist._idx == 0
def test_firstitem(self):
def test_firstitem(self, neighborlist):
"""Test firstitem()."""
self.assertEqual(self.nl.firstitem(), 1)
self.assertEqual(self.nl._idx, 0)
assert neighborlist.firstitem() == 1
assert neighborlist._idx == 0
def test_lastitem(self):
def test_lastitem(self, neighborlist):
"""Test lastitem()."""
self.assertEqual(self.nl.lastitem(), 5)
self.assertEqual(self.nl._idx, 4)
assert neighborlist.lastitem() == 5
assert neighborlist._idx == 4
def test_reset(self):
def test_reset(self, neighborlist):
"""Test reset()."""
self.nl.nextitem()
self.assertEqual(self.nl._idx, 3)
self.nl.reset()
self.assertEqual(self.nl._idx, 2)
neighborlist.nextitem()
assert neighborlist._idx == 3
neighborlist.reset()
assert neighborlist._idx == 2
def test_getitem(self):
def test_getitem(self, neighborlist):
"""Test getitem()."""
self.assertEqual(self.nl.getitem(2), 5)
self.assertEqual(self.nl._idx, 4)
self.nl.reset()
self.assertEqual(self.nl.getitem(-2), 1)
self.assertEqual(self.nl._idx, 0)
assert neighborlist.getitem(2) == 5
assert neighborlist._idx == 4
neighborlist.reset()
assert neighborlist.getitem(-2) == 1
assert neighborlist._idx == 0
class OneTests(unittest.TestCase):
class TestSingleItem:
"""Tests with a list with only one item.
"""Tests with a list with only one item."""
Attributes:
nl: The NeighborList we're testing.
"""
@pytest.fixture
def neighborlist(self):
return usertypes.NeighborList([1], default=1)
def setUp(self):
self.nl = usertypes.NeighborList([1], default=1)
def test_first_wrap(self):
def test_first_wrap(self, neighborlist):
"""Test out of bounds previtem() with mode=wrap."""
self.nl._mode = usertypes.NeighborList.Modes.wrap
self.nl.firstitem()
self.assertEqual(self.nl._idx, 0)
self.assertEqual(self.nl.previtem(), 1)
self.assertEqual(self.nl._idx, 0)
neighborlist._mode = usertypes.NeighborList.Modes.wrap
neighborlist.firstitem()
assert neighborlist._idx == 0
assert neighborlist.previtem() == 1
assert neighborlist._idx == 0
def test_first_block(self):
def test_first_block(self, neighborlist):
"""Test out of bounds previtem() with mode=block."""
self.nl._mode = usertypes.NeighborList.Modes.block
self.nl.firstitem()
self.assertEqual(self.nl._idx, 0)
self.assertEqual(self.nl.previtem(), 1)
self.assertEqual(self.nl._idx, 0)
neighborlist._mode = usertypes.NeighborList.Modes.block
neighborlist.firstitem()
assert neighborlist._idx == 0
assert neighborlist.previtem() == 1
assert neighborlist._idx == 0
def test_first_raise(self):
def test_first_raise(self, neighborlist):
"""Test out of bounds previtem() with mode=raise."""
self.nl._mode = usertypes.NeighborList.Modes.exception
self.nl.firstitem()
self.assertEqual(self.nl._idx, 0)
with self.assertRaises(IndexError):
self.nl.previtem()
self.assertEqual(self.nl._idx, 0)
neighborlist._mode = usertypes.NeighborList.Modes.exception
neighborlist.firstitem()
assert neighborlist._idx == 0
with pytest.raises(IndexError):
neighborlist.previtem()
assert neighborlist._idx == 0
def test_last_wrap(self):
def test_last_wrap(self, neighborlist):
"""Test out of bounds nextitem() with mode=wrap."""
self.nl._mode = usertypes.NeighborList.Modes.wrap
self.nl.lastitem()
self.assertEqual(self.nl._idx, 0)
self.assertEqual(self.nl.nextitem(), 1)
self.assertEqual(self.nl._idx, 0)
neighborlist._mode = usertypes.NeighborList.Modes.wrap
neighborlist.lastitem()
assert neighborlist._idx == 0
assert neighborlist.nextitem() == 1
assert neighborlist._idx == 0
def test_last_block(self):
def test_last_block(self, neighborlist):
"""Test out of bounds nextitem() with mode=block."""
self.nl._mode = usertypes.NeighborList.Modes.block
self.nl.lastitem()
self.assertEqual(self.nl._idx, 0)
self.assertEqual(self.nl.nextitem(), 1)
self.assertEqual(self.nl._idx, 0)
neighborlist._mode = usertypes.NeighborList.Modes.block
neighborlist.lastitem()
assert neighborlist._idx == 0
assert neighborlist.nextitem() == 1
assert neighborlist._idx == 0
def test_last_raise(self):
def test_last_raise(self, neighborlist):
"""Test out of bounds nextitem() with mode=raise."""
self.nl._mode = usertypes.NeighborList.Modes.exception
self.nl.lastitem()
self.assertEqual(self.nl._idx, 0)
with self.assertRaises(IndexError):
self.nl.nextitem()
self.assertEqual(self.nl._idx, 0)
neighborlist._mode = usertypes.NeighborList.Modes.exception
neighborlist.lastitem()
assert neighborlist._idx == 0
with pytest.raises(IndexError):
neighborlist.nextitem()
assert neighborlist._idx == 0
class BlockTests(unittest.TestCase):
class TestBlockMode:
"""Tests with mode=block.
"""Tests with mode=block."""
Attributes:
nl: The NeighborList we're testing.
"""
def setUp(self):
self.nl = usertypes.NeighborList(
@pytest.fixture
def neighborlist(self):
return usertypes.NeighborList(
[1, 2, 3, 4, 5], default=3,
mode=usertypes.NeighborList.Modes.block)
def test_first(self):
def test_first(self, neighborlist):
"""Test out of bounds previtem()."""
self.nl.firstitem()
self.assertEqual(self.nl._idx, 0)
self.assertEqual(self.nl.previtem(), 1)
self.assertEqual(self.nl._idx, 0)
neighborlist.firstitem()
assert neighborlist._idx == 0
assert neighborlist.previtem() == 1
assert neighborlist._idx == 0
def test_last(self):
def test_last(self, neighborlist):
"""Test out of bounds nextitem()."""
self.nl.lastitem()
self.assertEqual(self.nl._idx, 4)
self.assertEqual(self.nl.nextitem(), 5)
self.assertEqual(self.nl._idx, 4)
neighborlist.lastitem()
assert neighborlist._idx == 4
assert neighborlist.nextitem() == 5
assert neighborlist._idx == 4
class WrapTests(unittest.TestCase):
class TestWrapMode:
"""Tests with mode=wrap.
"""Tests with mode=wrap."""
Attributes:
nl: The NeighborList we're testing.
"""
def setUp(self):
self.nl = usertypes.NeighborList(
@pytest.fixture
def neighborlist(self):
return usertypes.NeighborList(
[1, 2, 3, 4, 5], default=3, mode=usertypes.NeighborList.Modes.wrap)
def test_first(self):
def test_first(self, neighborlist):
"""Test out of bounds previtem()."""
self.nl.firstitem()
self.assertEqual(self.nl._idx, 0)
self.assertEqual(self.nl.previtem(), 5)
self.assertEqual(self.nl._idx, 4)
neighborlist.firstitem()
assert neighborlist._idx == 0
assert neighborlist.previtem() == 5
assert neighborlist._idx == 4
def test_last(self):
def test_last(self, neighborlist):
"""Test out of bounds nextitem()."""
self.nl.lastitem()
self.assertEqual(self.nl._idx, 4)
self.assertEqual(self.nl.nextitem(), 1)
self.assertEqual(self.nl._idx, 0)
neighborlist.lastitem()
assert neighborlist._idx == 4
assert neighborlist.nextitem() == 1
assert neighborlist._idx == 0
class RaiseTests(unittest.TestCase):
class TestExceptionMode:
"""Tests with mode=exception.
"""Tests with mode=exception."""
Attributes:
nl: The NeighborList we're testing.
"""
def setUp(self):
self.nl = usertypes.NeighborList(
@pytest.fixture
def neighborlist(self):
return usertypes.NeighborList(
[1, 2, 3, 4, 5], default=3,
mode=usertypes.NeighborList.Modes.exception)
def test_first(self):
def test_first(self, neighborlist):
"""Test out of bounds previtem()."""
self.nl.firstitem()
self.assertEqual(self.nl._idx, 0)
with self.assertRaises(IndexError):
self.nl.previtem()
self.assertEqual(self.nl._idx, 0)
neighborlist.firstitem()
assert neighborlist._idx == 0
with pytest.raises(IndexError):
neighborlist.previtem()
assert neighborlist._idx == 0
def test_last(self):
def test_last(self, neighborlist):
"""Test out of bounds nextitem()."""
self.nl.lastitem()
self.assertEqual(self.nl._idx, 4)
with self.assertRaises(IndexError):
self.nl.nextitem()
self.assertEqual(self.nl._idx, 4)
neighborlist.lastitem()
assert neighborlist._idx == 4
with pytest.raises(IndexError):
neighborlist.nextitem()
assert neighborlist._idx == 4
class SnapInTests(unittest.TestCase):
class TestSnapIn:
"""Tests for the fuzzyval/_snap_in features.
"""Tests for the fuzzyval/_snap_in features."""
Attributes:
nl: The NeighborList we're testing.
"""
@pytest.fixture
def neighborlist(self):
return usertypes.NeighborList([20, 9, 1, 5])
def setUp(self):
self.nl = usertypes.NeighborList([20, 9, 1, 5])
def test_bigger(self):
def test_bigger(self, neighborlist):
"""Test fuzzyval with snapping to a bigger value."""
self.nl.fuzzyval = 7
self.assertEqual(self.nl.nextitem(), 9)
self.assertEqual(self.nl._idx, 1)
self.assertEqual(self.nl.nextitem(), 1)
self.assertEqual(self.nl._idx, 2)
neighborlist.fuzzyval = 7
assert neighborlist.nextitem() == 9
assert neighborlist._idx == 1
assert neighborlist.nextitem() == 1
assert neighborlist._idx == 2
def test_smaller(self):
def test_smaller(self, neighborlist):
"""Test fuzzyval with snapping to a smaller value."""
self.nl.fuzzyval = 7
self.assertEqual(self.nl.previtem(), 5)
self.assertEqual(self.nl._idx, 3)
self.assertEqual(self.nl.previtem(), 1)
self.assertEqual(self.nl._idx, 2)
neighborlist.fuzzyval = 7
assert neighborlist.previtem() == 5
assert neighborlist._idx == 3
assert neighborlist.previtem() == 1
assert neighborlist._idx == 2
def test_equal_bigger(self):
def test_equal_bigger(self, neighborlist):
"""Test fuzzyval with matching value, snapping to a bigger value."""
self.nl.fuzzyval = 20
self.assertEqual(self.nl.nextitem(), 9)
self.assertEqual(self.nl._idx, 1)
neighborlist.fuzzyval = 20
assert neighborlist.nextitem() == 9
assert neighborlist._idx == 1
def test_equal_smaller(self):
def test_equal_smaller(self, neighborlist):
"""Test fuzzyval with matching value, snapping to a smaller value."""
self.nl.fuzzyval = 5
self.assertEqual(self.nl.previtem(), 1)
self.assertEqual(self.nl._idx, 2)
neighborlist.fuzzyval = 5
assert neighborlist.previtem() == 1
assert neighborlist._idx == 2
def test_too_big_next(self):
def test_too_big_next(self, neighborlist):
"""Test fuzzyval/next with a value bigger than any in the list."""
self.nl.fuzzyval = 30
self.assertEqual(self.nl.nextitem(), 20)
self.assertEqual(self.nl._idx, 0)
neighborlist.fuzzyval = 30
assert neighborlist.nextitem() == 20
assert neighborlist._idx == 0
def test_too_big_prev(self):
def test_too_big_prev(self, neighborlist):
"""Test fuzzyval/prev with a value bigger than any in the list."""
self.nl.fuzzyval = 30
self.assertEqual(self.nl.previtem(), 20)
self.assertEqual(self.nl._idx, 0)
neighborlist.fuzzyval = 30
assert neighborlist.previtem() == 20
assert neighborlist._idx == 0
def test_too_small_next(self):
def test_too_small_next(self, neighborlist):
"""Test fuzzyval/next with a value smaller than any in the list."""
self.nl.fuzzyval = 0
self.assertEqual(self.nl.nextitem(), 1)
self.assertEqual(self.nl._idx, 2)
neighborlist.fuzzyval = 0
assert neighborlist.nextitem() == 1
assert neighborlist._idx == 2
def test_too_small_prev(self):
def test_too_small_prev(self, neighborlist):
"""Test fuzzyval/prev with a value smaller than any in the list."""
self.nl.fuzzyval = 0
self.assertEqual(self.nl.previtem(), 1)
self.assertEqual(self.nl._idx, 2)
if __name__ == '__main__':
unittest.main()
neighborlist.fuzzyval = 0
assert neighborlist.previtem() == 1
assert neighborlist._idx == 2