def test_rmul(self): assert_equal(2 * ItemList(int, items=[1, 2, 3]), ItemList(int, items=[1, 2, 3, 1, 2, 3])) assert_raises(TypeError, ItemList(int).__rmul__, ItemList(int))
def test_iadd_wrong_type(self): assert_raises_with_msg(TypeError, 'Only int objects accepted, got str.', ItemList(int).__iadd__, ['a', 'b', 'c'])
def test_imul(self): items = ItemList(int, items=[1, 2]) items *= 2 items *= 1 assert_equal(items, ItemList(int, items=[1, 2, 1, 2]))
def test_add(self): assert_equal( ItemList(int, items=[1, 2]) + ItemList(int, items=[3, 4]), ItemList(int, items=[1, 2, 3, 4]))
def test_iadd_incompatible(self): items = ItemList(int, items=[1, 2]) assert_raises_with_msg(TypeError, 'Cannot add incompatible ItemLists', items.__iadd__, ItemList(str)) assert_raises_with_msg(TypeError, 'Cannot add incompatible ItemLists', items.__iadd__, ItemList(int, {'a': 1}))
def test_unicode(self): assert_equal(unicode(ItemList(int, items=[1, 2, 3, 4])), '[1, 2, 3, 4]') assert_equal( unicode(ItemList(unicode, items=[u'hyv\xe4\xe4', u'y\xf6'])), u'[hyv\xe4\xe4, y\xf6]')
def test_index(self): items = ItemList(str, items=('first', 'second')) assert_equal(items.index('first'), 0) assert_equal(items.index('second'), 1) assert_raises(ValueError, items.index, 'nonex')
def test_str(self): assert_equal(str(ItemList(int, items=[1, 2, 3, 4])), '[1, 2, 3, 4]') assert_equal(str(ItemList(str, items=['foo', 'bar'])), "['foo', 'bar']")
def test_repr(self): assert_equal(repr(ItemList(int, items=[1, 2, 3, 4])), 'ItemList(item_class=int, items=[1, 2, 3, 4])') assert_equal(repr(CustomItems(Object)), 'CustomItems(item_class=Object, items=[])')
def test_contains(self): items = ItemList(str, items='x') assert_true('x' in items) assert_true('y' not in items) assert_false('x' not in items) assert_false('y' in items)
def test_clear(self): items = ItemList(int, items=range(10)) assert_equal(len(items), 10) items.clear() assert_equal(len(items), 0)
def test_truth(self): assert_true(not ItemList(int)) assert_true(ItemList(int, items=[1]))
def test_len(self): items = ItemList(object) assert_equal(len(items), 0) items.create() assert_equal(len(items), 1)
def test_setitem_slice_invalid_type(self): assert_raises_with_msg(TypeError, 'Only int objects accepted, got float.', ItemList(int).__setitem__, slice(0), [1, 1.1])
def test_append_and_extend(self): items = ItemList(int) items.append(1) items.append(2) items.extend((3, 4)) assert_equal(list(items), [1, 2, 3, 4])
def test_sorted(self): chars = ItemList(str, items='asdfg') assert_equal(sorted(chars), sorted('asdfg'))
def test_extend_with_generator(self): items = ItemList(str) items.extend((c for c in 'Hello, world!')) assert_equal(list(items), list('Hello, world!'))
def test_reversed(self): chars = ItemList(str, items='asdfg') assert_equal(list(reversed(chars)), list(reversed('asdfg')))
def test_create_items(self): items = ItemList(str) item = items.create(object=1) assert_true(isinstance(item, str)) assert_equal(item, '1') assert_equal(list(items), [item])
def test_str(self): assert_equal(str(ItemList(str, items=['foo', 'bar', 'quux'])), '[foo, bar, quux]')