kitchen/kitchen2/tests/test_iterutils.py

63 lines
2 KiB
Python
Raw Normal View History

2015-10-08 16:26:18 +00:00
# -*- coding: utf-8 -*-
#
import unittest
from nose import tools
from kitchen import iterutils
2016-07-08 23:18:01 +00:00
class TestIterutils(unittest.TestCase):
2015-10-08 16:26:18 +00:00
iterable_data = (
[0, 1, 2],
[],
(0, 1, 2),
tuple(),
set([0, 1, 2]),
set(),
dict(a=1, b=2),
dict(),
[None],
[False],
[0],
xrange(0, 3),
iter([1, 2, 3]),
)
non_iterable_data = (
None,
False,
True,
0,
1.1,
)
def test_isiterable(self):
for item in self.iterable_data:
tools.ok_(iterutils.isiterable(item) == True)
for item in self.non_iterable_data:
tools.ok_(iterutils.isiterable(item) == False)
# strings
tools.ok_(iterutils.isiterable('a', include_string=True) == True)
tools.ok_(iterutils.isiterable('a', include_string=False) == False)
tools.ok_(iterutils.isiterable('a') == False)
2016-07-08 23:18:01 +00:00
tools.ok_(iterutils.isiterable(u'a', include_string=True) == True)
tools.ok_(iterutils.isiterable(u'a', include_string=False) == False)
tools.ok_(iterutils.isiterable(u'a') == False)
2015-10-08 16:26:18 +00:00
def test_iterate(self):
iterutils.iterate(None)
for item in self.non_iterable_data:
tools.ok_(list(iterutils.iterate(item)) == [item])
for item in self.iterable_data[:-1]:
tools.ok_(list(iterutils.iterate(item)) == list(item))
# iter() is exhausted after use so we have to test separately
tools.ok_(list(iterutils.iterate(iter([1, 2, 3]))) == [1, 2, 3])
# strings
tools.ok_(list(iterutils.iterate('abc')) == ['abc'])
tools.ok_(list(iterutils.iterate('abc', include_string=True)) == ['a', 'b', 'c'])
2016-07-08 23:18:01 +00:00
tools.ok_(list(iterutils.iterate(u'abc')) == [u'abc'])
tools.ok_(list(iterutils.iterate(u'abc', include_string=True)) == [u'a', u'b', u'c'])