diff options
author | Tim Harder <radhermit@gmail.com> | 2017-12-21 08:23:35 -0500 |
---|---|---|
committer | Tim Harder <radhermit@gmail.com> | 2018-04-11 14:39:31 -0400 |
commit | 4f1de96684eb4a87458aeefa0b85770bc1aeeab5 (patch) | |
tree | 490e6a895c801b6ac1917d5ebd7a590f14fc4621 /tests/test_iterables.py | |
parent | setup.cfg: add pytest settings (diff) | |
download | snakeoil-4f1de96684eb4a87458aeefa0b85770bc1aeeab5.tar.gz snakeoil-4f1de96684eb4a87458aeefa0b85770bc1aeeab5.tar.bz2 snakeoil-4f1de96684eb4a87458aeefa0b85770bc1aeeab5.zip |
test: initial conversion from unittest to pytest functionality
Diffstat (limited to 'tests/test_iterables.py')
-rw-r--r-- | tests/test_iterables.py | 114 |
1 files changed, 59 insertions, 55 deletions
diff --git a/tests/test_iterables.py b/tests/test_iterables.py index f9373dfc..3caf2a33 100644 --- a/tests/test_iterables.py +++ b/tests/test_iterables.py @@ -3,46 +3,51 @@ import operator +import pytest + from snakeoil.iterables import expandable_chain, caching_iter, iter_sort -from snakeoil.test import TestCase -class ExpandableChainTest(TestCase): +class TestExpandableChain(object): def test_normal_function(self): i = [iter(range(100)) for x in range(3)] e = expandable_chain() e.extend(i) - self.assertEqual(list(e), list(range(100))*3) + assert list(e) == list(range(100))*3 for x in i + [e]: - self.assertRaises(StopIteration, x.__next__) + pytest.raises(StopIteration, x.__next__) def test_extend(self): e = expandable_chain() e.extend(range(100) for i in (1, 2)) - self.assertEqual(list(e), list(range(100))*2) - self.assertRaises(StopIteration, e.extend, [[]]) + assert list(e) == list(range(100))*2 + with pytest.raises(StopIteration): + e.extend([[]]) def test_extendleft(self): e = expandable_chain(range(20, 30)) e.extendleft([range(10, 20), range(10)]) - self.assertEqual(list(e), list(range(30))) - self.assertRaises(StopIteration, e.extendleft, [[]]) + assert list(e) == list(range(30)) + with pytest.raises(StopIteration): + e.extendleft([[]]) def test_append(self): e = expandable_chain() e.append(range(100)) - self.assertEqual(list(e), list(range(100))) - self.assertRaises(StopIteration, e.append, []) + assert list(e) == list(range(100)) + with pytest.raises(StopIteration): + e.append([]) def test_appendleft(self): e = expandable_chain(range(10, 20)) e.appendleft(range(10)) - self.assertEqual(list(e), list(range(20))) - self.assertRaises(StopIteration, e.appendleft, []) + assert list(e) == list(range(20)) + with pytest.raises(StopIteration): + e.appendleft([]) -class CachingIterTest(TestCase): +class TestCachingIter(object): def test_iter_consumption(self): i = iter(range(100)) @@ -50,36 +55,35 @@ class CachingIterTest(TestCase): i2 = iter(c) for _ in range(20): next(i2) - self.assertEqual(next(i), 20) + assert next(i) == 20 # note we consumed one ourselves - self.assertEqual(c[20], 21) + assert c[20] == 21 list(c) - self.assertRaises(StopIteration, i.__next__) - self.assertEqual(list(c), list(range(20)) + list(range(21, 100))) + pytest.raises(StopIteration, i.__next__) + assert list(c) == list(range(20)) + list(range(21, 100)) def test_init(self): - self.assertEqual(caching_iter(list(range(100)))[0], 0) + assert caching_iter(list(range(100)))[0] == 0 def test_full_consumption(self): i = iter(range(100)) c = caching_iter(i) - self.assertEqual(list(c), list(range(100))) + assert list(c) == list(range(100)) # do it twice, to verify it returns properly - self.assertEqual(list(c), list(range(100))) + assert list(c) == list(range(100)) def test_len(self): - self.assertEqual(100, len(caching_iter(range(100)))) + assert 100 == len(caching_iter(range(100))) def test_hash(self): - self.assertEqual(hash(caching_iter(range(100))), - hash(tuple(range(100)))) + assert hash(caching_iter(range(100))) == hash(tuple(range(100))) def test_bool(self): c = caching_iter(range(100)) - self.assertEqual(bool(c), True) + assert bool(c) == True # repeat to check if it works when cached. - self.assertEqual(bool(c), True) - self.assertEqual(bool(caching_iter(iter([]))), False) + assert bool(c) == True + assert bool(caching_iter(iter([]))) == False @staticmethod def _py3k_protection(*args, **kwds): @@ -87,54 +91,54 @@ class CachingIterTest(TestCase): def test_cmp(self): get_inst = self._py3k_protection - self.assertEqual(get_inst(range(100)), tuple(range(100))) - self.assertNotEqual(get_inst(range(90)), tuple(range(100))) - self.assertTrue(get_inst(range(100)) > tuple(range(90))) - self.assertFalse(get_inst(range(90)) > tuple(range(100))) - self.assertTrue(get_inst(range(100)) >= tuple(range(100))) - self.assertTrue(get_inst(range(90)) < tuple(range(100))) - self.assertFalse(get_inst(range(100)) < tuple(range(90))) - self.assertTrue(get_inst(range(90)) <= tuple(range(100))) + assert get_inst(range(100)) == tuple(range(100)) + assert get_inst(range(90)) != tuple(range(100)) + assert get_inst(range(100)) > tuple(range(90)) + assert not get_inst(range(90)) > tuple(range(100)) + assert get_inst(range(100)) >= tuple(range(100)) + assert get_inst(range(90)) < tuple(range(100)) + assert not get_inst(range(100)) < tuple(range(90)) + assert get_inst(range(90)) <= tuple(range(100)) def test_sorter(self): get_inst = self._py3k_protection - self.assertEqual( - get_inst(range(100, 0, -1), sorted), tuple(range(1, 101))) + assert get_inst(range(100, 0, -1), sorted) == tuple(range(1, 101)) c = caching_iter(range(100, 0, -1), sorted) - self.assertTrue(c) - self.assertEqual(tuple(c), tuple(range(1, 101))) + assert c + assert tuple(c) == tuple(range(1, 101)) c = caching_iter(range(50, 0, -1), sorted) - self.assertEqual(c[10], 11) - self.assertEqual(tuple(range(1, 51)), tuple(c)) + assert c[10] == 11 + assert tuple(range(1, 51)) == tuple(c) def test_getitem(self): c = caching_iter(range(20)) - self.assertEqual(19, c[-1]) - self.assertRaises(IndexError, operator.getitem, c, -21) - self.assertRaises(IndexError, operator.getitem, c, 21) + assert c[-1] == 19 + with pytest.raises(IndexError): + operator.getitem(c, -21) + with pytest.raises(IndexError): + operator.getitem(c, 21) def test_edgecase(self): c = caching_iter(range(5)) - self.assertEqual(c[0], 0) + assert c[0] == 0 # do an off by one access- this actually has broke before - self.assertEqual(c[2], 2) - self.assertEqual(c[1], 1) - self.assertEqual(list(c), list(range(5))) + assert c[2] == 2 + assert c[1] == 1 + assert list(c) == list(range(5)) def test_setitem(self): - self.assertRaises( - TypeError, operator.setitem, caching_iter(range(10)), 3, 4) + with pytest.raises(TypeError): + operator.setitem(caching_iter(range(10)), 3, 4) def test_str(self): # Just make sure this works at all. - self.assertTrue(str(caching_iter(range(10)))) + assert str(caching_iter(range(10))) -class iter_sortTest(TestCase): +class Test_iter_sort(object): def test_ordering(self): def f(l): return sorted(l, key=operator.itemgetter(0)) - self.assertEqual( - list(iter_sort( - f, *[iter(range(x, x + 10)) for x in (30, 20, 0, 10)])), - list(range(40))) + result = list(iter_sort(f, *[iter(range(x, x + 10)) for x in (30, 20, 0, 10)])) + expected = list(range(40)) + assert result == expected |