def test_insert(self):
     items = ItemList(str)
     items.insert(0, 'a')
     items.insert(0, 'b')
     items.insert(3, 'c')
     items.insert(1, 'd')
     assert_equal(list(items), ['b', 'd', 'a', 'c'])
Exemplo n.º 2
0
 def test_pop(self):
     items = ItemList(str, items='abcde')
     assert_equal(items.pop(), 'e')
     assert_equal(items.pop(0), 'a')
     assert_equal(items.pop(-2), 'c')
     assert_equal(list(items), ['b', 'd'])
     assert_raises(IndexError, items.pop, 7)
     assert_equal(list(items), ['b', 'd'])
     assert_raises(IndexError, ItemList(int).pop)
Exemplo n.º 3
0
 def test_create_with_args_and_kwargs(self):
     class Item(object):
         def __init__(self, arg1, arg2):
             self.arg1 = arg1
             self.arg2 = arg2
     items = ItemList(Item)
     item = items.create('value 1', arg2='value 2')
     assert_equal(item.arg1, 'value 1')
     assert_equal(item.arg2, 'value 2')
     assert_equal(list(items), [item])
Exemplo n.º 4
0
 def test_common_attrs(self):
     item1 = Object()
     item2 = Object()
     parent = object()
     items = ItemList(Object, {'attr': 2, 'parent': parent}, [item1])
     items.append(item2)
     assert_true(item1.parent is parent)
     assert_equal(item1.attr, 2)
     assert_true(item2.parent is parent)
     assert_equal(item2.attr, 2)
     assert_equal(list(items), [item1, item2])
Exemplo n.º 5
0
 def test_common_attrs(self):
     item1 = Object()
     item2 = Object()
     parent = object()
     items = ItemList(Object, {'attr': 2, 'parent': parent}, [item1])
     items.append(item2)
     assert_true(item1.parent is parent)
     assert_equal(item1.attr, 2)
     assert_true(item2.parent is parent)
     assert_equal(item2.attr, 2)
     assert_equal(list(items), [item1, item2])
Exemplo n.º 6
0
    def test_create_with_args_and_kwargs(self):
        class Item(object):
            def __init__(self, arg1, arg2):
                self.arg1 = arg1
                self.arg2 = arg2

        items = ItemList(Item)
        item = items.create('value 1', arg2='value 2')
        assert_equal(item.arg1, 'value 1')
        assert_equal(item.arg2, 'value 2')
        assert_equal(list(items), [item])
Exemplo n.º 7
0
 def test_remove(self):
     items = ItemList(str, items='abcba')
     items.remove('c')
     assert_equal(list(items), list('abba'))
     items.remove('a')
     assert_equal(list(items), list('bba'))
     items.remove('b')
     items.remove('a')
     items.remove('b')
     assert_equal(list(items), list(''))
     assert_raises(ValueError, items.remove, 'nonex')
Exemplo n.º 8
0
 def test_index_with_start_and_stop(self):
     numbers = [0, 1, 2, 3, 2, 1, 0]
     items = ItemList(int, items=numbers)
     for num in sorted(set(numbers)):
         for start in range(len(numbers)):
             if num in numbers[start:]:
                 assert_equal(items.index(num, start),
                              numbers.index(num, start))
                 for end in range(start, len(numbers)):
                     if num in numbers[start:end]:
                         assert_equal(items.index(num, start, end),
                                      numbers.index(num, start, end))
Exemplo n.º 9
0
 def test_index_with_start_and_stop(self):
     numbers = [0, 1, 2, 3, 2, 1, 0]
     items = ItemList(int, items=numbers)
     for num in sorted(set(numbers)):
         for start in range(len(numbers)):
             if num in numbers[start:]:
                 assert_equal(items.index(num, start),
                              numbers.index(num, start))
                 for end in range(start, len(numbers)):
                     if num in numbers[start:end]:
                         assert_equal(items.index(num, start, end),
                                      numbers.index(num, start, end))
Exemplo n.º 10
0
 def test_modifications_during_reversed(self):
     chars = ItemList(str, items='yxdba')
     for c in reversed(chars):
         if c == 'a':
             chars.remove('x')
         if c == 'b':
             chars.insert(-2, 'c')
         if c == 'c':
             chars.pop(0)
         if c == 'd':
             chars.insert(0, 'e')
         assert_true(c in 'abcde', '%s was unexpected here!' % c)
     assert_equal(list(chars), list('edcba'))
 def test_getitem(self):
     item1 = object()
     item2 = object()
     items = ItemList(object, items=[item1, item2])
     assert_true(items[0] is item1)
     assert_true(items[1] is item2)
     assert_true(items[-1] is item2)
Exemplo n.º 12
0
 def test_setitem_slice_is_not_supported(self):
     assert_raises_with_msg(TypeError,
                            'ItemList instances do not support slicing.',
                            ItemList(int).__setitem__, slice(0), [])
     assert_raises_with_msg(TypeError,
                            'CustomItems instances do not support slicing.',
                            CustomItems(int).__setitem__, slice(0), [])
Exemplo n.º 13
0
 def test_iter(self):
     numbers = list(range(10))
     items = ItemList(int, items=numbers)
     assert_equal(list(items), numbers)
     assert_equal(tuple(items), tuple(numbers))
     for i, n in zip(items, numbers):
         assert_equal(i, n)
Exemplo n.º 14
0
 def test_compare_incompatible(self):
     assert_false(ItemList(int) == ItemList(str))
     assert_false(ItemList(int) == ItemList(int, {'a': 1}))
     assert_raises_with_msg(TypeError,
                            'Cannot order incompatible ItemLists',
                            ItemList(int).__gt__, ItemList(str))
     assert_raises_with_msg(TypeError,
                            'Cannot order incompatible ItemLists',
                            ItemList(int).__gt__, ItemList(int, {'a': 1}))
Exemplo n.º 15
0
 def test_modifications_during_iter(self):
     chars = ItemList(str, items='abdx')
     for c in chars:
         if c == 'a':
             chars.pop()
         if c == 'b':
             chars.insert(2, 'c')
         if c == 'c':
             chars.append('e')
         assert_true(c in 'abcde', '%s was unexpected here!' % c)
     assert_equal(list(chars), list('abcde'))
Exemplo n.º 16
0
 def test_comparisons(self):
     n123 = ItemList(int, items=[1, 2, 3])
     n123b = ItemList(int, items=[1, 2, 3])
     n1234 = ItemList(int, items=[1, 2, 3, 4])
     n124 = ItemList(int, items=[1, 2, 4])
     assert_true(n123 == n123b)
     assert_false(n123 != n123b)
     assert_true(n123 != n1234)
     assert_false(n123 == n1234)
     assert_true(n1234 > n123)
     assert_true(n1234 >= n123)
     assert_false(n1234 < n123)
     assert_false(n1234 <= n123)
     assert_true(n124 > n123)
     assert_true(n124 >= n123)
     assert_false(n124 < n123)
     assert_false(n124 <= n123)
     assert_true(n123 >= n123)
     assert_true(n123 <= n123)
Exemplo n.º 17
0
 def test_delitem_slice(self):
     items = ItemList(str, items='abcde')
     del items[1:3]
     assert_equal(list(items), list('ade'))
     del items[2:]
     assert_equal(list(items), list('ad'))
     del items[10:]
     assert_equal(list(items), list('ad'))
     del items[:]
     assert_equal(list(items), [])
Exemplo n.º 18
0
 def test_delitem(self):
     items = ItemList(str, items='abcde')
     del items[0]
     assert_equal(list(items), list('bcde'))
     del items[1]
     assert_equal(list(items), list('bde'))
     del items[-1]
     assert_equal(list(items), list('bd'))
     assert_raises(IndexError, items.__delitem__, 10)
     assert_equal(list(items), list('bd'))
Exemplo n.º 19
0
 def test_setitem_slice(self):
     items = ItemList(int, items=range(10))
     items[:5] = []
     items[-2:] = [42]
     assert_equal(list(items), [5, 6, 7, 42])
     items = CustomItems(Object, {'a': 1}, [Object(i) for i in range(10)])
     items[1::3] = tuple(Object(c) for c in 'abc')
     assert_true(all(obj.a == 1 for obj in items))
     assert_equal([obj.id for obj in items],
                  [0, 'a', 2, 3, 'b', 5, 6, 'c', 8, 9])
Exemplo n.º 20
0
 def test_setitem(self):
     orig1, orig2 = Object(), Object()
     new1, new2 = Object(), Object()
     items = ItemList(Object, {'attr': 2}, [orig1, orig2])
     items[0] = new1
     assert_equal(list(items), [new1, orig2])
     assert_equal(new1.attr, 2)
     items[-1] = new2
     assert_equal(list(items), [new1, new2])
     assert_equal(new2.attr, 2)
Exemplo n.º 21
0
 def test_insert(self):
     items = ItemList(str)
     items.insert(0, 'a')
     items.insert(0, 'b')
     items.insert(3, 'c')
     items.insert(1, 'd')
     assert_equal(list(items), ['b', 'd', 'a', 'c'])
Exemplo n.º 22
0
 def test_comparisons_with_other_objects(self):
     items = ItemList(int, items=[1, 2, 3])
     assert_false(items == 123)
     assert_false(items == [1, 2, 3])
     assert_false(items == (1, 2, 3))
     assert_true(items != 123)
     assert_true(items != [1, 2, 3])
     assert_true(items != (1, 2, 3))
     assert_raises_with_msg(TypeError, 'Cannot order ItemList and int',
                            items.__gt__, 1)
     assert_raises_with_msg(TypeError, 'Cannot order ItemList and list',
                            items.__lt__, [1, 2, 3])
     assert_raises_with_msg(TypeError, 'Cannot order ItemList and tuple',
                            items.__ge__, (1, 2, 3))
Exemplo n.º 23
0
 def test_pop(self):
     items = ItemList(str, items='abcde')
     assert_equal(items.pop(), 'e')
     assert_equal(items.pop(0), 'a')
     assert_equal(items.pop(-2), 'c')
     assert_equal(list(items), ['b', 'd'])
     assert_raises(IndexError, items.pop, 7)
     assert_equal(list(items), ['b', 'd'])
     assert_raises(IndexError, ItemList(int).pop)
Exemplo n.º 24
0
 def test_getitem_slice(self):
     items = ItemList(int, items=range(10))
     sub = items[:5]
     assert_true(isinstance(sub, ItemList))
     assert_equal(list(sub), range(0, 5))
     assert_equal(list(items), range(10))
     sub.append(5)
     assert_equal(list(sub), range(0, 6))
     assert_equal(list(items), range(10))
     backwards = items[::-1]
     assert_true(isinstance(backwards, ItemList))
     assert_equal(list(backwards), list(reversed(items)))
     empty = items[100:]
     assert_true(isinstance(empty, ItemList))
     assert_equal(list(empty), [])
 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])
Exemplo n.º 26
0
 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_truth(self):
     assert_true(not ItemList(int))
     assert_true(ItemList(int, items=[1]))
Exemplo n.º 28
0
 def test_extend_with_generator(self):
     items = ItemList(str)
     items.extend((c for c in 'Hello, world!'))
     assert_equal(list(items), list('Hello, world!'))
Exemplo n.º 29
0
 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_index(self):
     items = ItemList(str, items=('first', 'second'))
     assert_equal(items.index('first'), 0)
     assert_equal(items.index('second'), 1)
 def test_str(self):
     assert_equal(str(ItemList(str, items=['foo', 'bar', 'quux'])),
                  '[foo, bar, quux]')
 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])
Exemplo n.º 33
0
 def test_index(self):
     items = ItemList(str, items=('first', 'second'))
     assert_equal(items.index('first'), 0)
     assert_equal(items.index('second'), 1)
 def test_only_matching_types_can_be_added(self):
     assert_raises(TypeError, ItemList(int).append, 'not integer')
Exemplo n.º 35
0
 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_getitem_slice_is_not_supported(self):
     assert_raises(TypeError, ItemList(int).__getitem__, slice(0))
Exemplo n.º 37
0
 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_len(self):
     items = ItemList(object)
     assert_equal(len(items), 0)
     items.create()
     assert_equal(len(items), 1)
Exemplo n.º 39
0
 def test_index(self):
     items = ItemList(str, items=("first", "second"))
     assert_equal(items.index("first"), 0)
     assert_equal(items.index("second"), 1)
 def test_clear(self):
     items = ItemList(int, range(10))
     items.clear()
     assert_equal(len(items), 0)
Exemplo n.º 41
0
 def test_len(self):
     items = ItemList(object)
     assert_equal(len(items), 0)
     items.create()
     assert_equal(len(items), 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]')
Exemplo n.º 43
0
 def test_clear(self):
     items = ItemList(int, range(10))
     items.clear()
     assert_equal(len(items), 0)