def test_init(self): dl = DepList() self.assertEqual(dl.unsorted, []) self.assertEqual(dl.index, {}) self.assertEqual(dl.stack, []) self.assertEqual(dl.pushed, set()) self.assertEqual(dl.sorted, None)
def __sort(cls, descriptors): """ Sort descriptors based on defined dependencies. Dependencies defined by [main].requires @param descriptors: A list of descriptor tuples (name,descriptor) @type descriptors: list @return: The sorted list @rtype: list """ index = {} for d in descriptors: index[d[0]] = d L = DepList() for n,d in descriptors: r = (n, d.__requires()) L.add(r) sorted = [] for name in [x[0] for x in L.sort()]: d = index[name] sorted.append(d) return sorted
def test_sort(self): a = ('a', ('x',)) b = ('b', ('a',)) c = ('c', ('a', 'b')) d = ('d', ('c',)) e = ('e', ('d', 'a')) f = ('f', ('e', 'c', 'd', 'a')) x = ('x', ()) g = ('g', ('G',)) dl = DepList() dl.add(c, e, d, b, f, a, x, g) self.assertEqual( dl.sort(), [ ('x', ()), ('a', ('x',)), ('b', ('a',)), ('c', ('a', 'b')), ('d', ('c',)), ('e', ('d', 'a')), ('f', ('e', 'c', 'd', 'a')), ('g', ('G',)) ])
def test_pop(self): item = ('a', ('b',)) dl = DepList() dl.push(item) # has popped = dl.pop() self.assertEqual(popped, item) # empty popped = dl.pop() self.assertEqual(popped, None)
def test_push(self): item = ('a', ('b',)) dl = DepList() dl.push(item) self.assertEqual(len(dl.stack), 1) self.assertEqual(dl.pushed, {item}) # 2nd push skipped dl.push(item) self.assertEqual(len(dl.stack), 1) self.assertEqual(dl.pushed, {item})
def test_top(self): dl = DepList() dl.stack = [1, 2, 3] self.assertEqual(dl.top(), dl.stack[-1])
def test_add(self): items = [(1, 2), (3, 4)] dl = DepList() dl.add(*items) self.assertEqual(dl.unsorted, items) self.assertEqual(dl.index, {1: (1, 2), 3: (3, 4)})