def test_after_returns_None_with_passed_last_Position_in_list(self): pl = PositionalList() # create two nodes with proper references in the container: n1 = PositionalList._Node('foo', pl._header, pl._trailer) n2 = PositionalList._Node('bar', n1, pl._trailer) n1._next = n2 pl._header._next = n1 pl._trailer._prev = n2 p2 = PositionalList.Position(pl, n2) result = pl.after(p2) self.assertIsNone(result)
def test_Position_equals_returns_proper_boolean(self): # create a container for the positions: container = PositionalList() # create two nodes with proper references in the container: n1 = PositionalList._Node('foo', container._header, None) n2 = PositionalList._Node('bar', n1, container._trailer) n1._next = n2 container._header._next = n1 container._trailer._prev = n2 # create a position for each respective node: p1 = PositionalList.Position(container, n1) p2 = PositionalList.Position(container, n2) self.assertFalse(p1 == p2)
def test_before_returns_None_with_passed_first_Position_in_list(self): pl = PositionalList() # create two nodes with proper references in the container: n1 = PositionalList._Node('foo', pl._header, pl._trailer) n2 = PositionalList._Node('bar', n1, pl._trailer) n1._next = n2 pl._header._next = n1 pl._trailer._prev = n2 # # create a position for each respective node: p1 = PositionalList.Position(pl, n1) result = pl.before(p1) self.assertIsNone(result)
def test_after_returns_Position_after_passed_Position(self): pl = PositionalList() # create two nodes with proper references in the container: n1 = PositionalList._Node('foo', pl._header, pl._trailer) n2 = PositionalList._Node('bar', n1, pl._trailer) n1._next = n2 pl._header._next = n1 pl._trailer._prev = n2 # # create a position for each respective node: p1 = PositionalList.Position(pl, n1) p2 = PositionalList.Position(pl, n2) result = pl.after(p1) self.assertEqual(result, p2)
def test_iter_returns_a_forward_generation_of_the_elements_in_the_list( self): pl = PositionalList() first_element = 'foo' second_element = 'bar' # create two nodes with proper references in the container: n1 = PositionalList._Node(first_element, pl._header, pl._trailer) n2 = PositionalList._Node(second_element, n1, pl._trailer) n1._next = n2 pl._header._next = n1 pl._trailer._prev = n2 results = [r for r in pl] self.assertIs(results[0], first_element) self.assertIs(results[1], second_element)
def test_validate_raises_ValueError_with_invalidated_position(self): pl = PositionalList() # the node has None as _next reference n = PositionalList._Node(None, pl._header, None) bad_position = PositionalList.Position(pl, n) self.assertRaises(ValueError, pl._validate, bad_position)
def test_validate_raises_ValueError_with_wrong_container(self): # create two containers pl1 = PositionalList() pl2 = PositionalList() n = PositionalList._Node(None, pl1._header, pl1._trailer) # create a Position that is external to the container bad_position = PositionalList.Position(pl1, n) self.assertRaises(ValueError, pl2._validate, bad_position)
def test_Position_constructor_creates_position_properties(self): # create a container to host the position: container = PositionalList() # create a node to be stored in the position: node = PositionalList._Node(None, None, None) p = PositionalList.Position(container, node) self.assertIs(p._container, container) self.assertIs(p._node, node)
def test_last_returns_last_Position_in_the_list(self): pl = PositionalList() n = PositionalList._Node('foo', pl._header, pl._trailer) pl._header._next = n pl._trailer._prev = n expected_position = PositionalList.Position(pl, n) result_position = pl.last() self.assertEqual(result_position, expected_position)
def test_Position_element_returns_the_element_stored_at_position(self): # create a container to host the position: container = PositionalList() # create a node to be stored in the position: expected_element = 'foo' node = PositionalList._Node(expected_element, None, None) p = PositionalList.Position(container, node) returned_element = p.element() self.assertIs(expected_element, returned_element)
def test_make_position_returns_Position_instance_for_given_node(self): pl = PositionalList() n = PositionalList._Node('foo', pl._header, pl._trailer) pl._header._next = n pl._trailer._prev = n expected_position = PositionalList.Position(pl, n) result_position = pl._make_position(n) # comparing the instances done using the Position's __eq__ method self.assertEqual(result_position, expected_position)
def test_validate_returns_correct_node_with_correct_parameters(self): pl = PositionalList() # setup a correctly referenced _Node: expected_node = PositionalList._Node('foo', pl._header, pl._trailer) pl._header._next = expected_node pl._trailer._prev = expected_node good_position = PositionalList.Position(pl, expected_node) result_node = pl._validate(good_position) self.assertIs(result_node, expected_node)
def test_replace_validates_replaces_returns_old_element(self): pl = PositionalList() original_element = 'foo' new_element = 'bar' node = PositionalList._Node(original_element, None, None) pl._validate = MagicMock(return_value=node) position = PositionalList.Position(pl, node) result_element = pl.replace(position, new_element) pl._validate.assert_called_with(position) self.assertIs(result_element, original_element) self.assertIs(node._element, new_element)
def test_delete_validates_and_calls_delete_node_on_position(self): pl = PositionalList() expected_element = 'foo' node = PositionalList._Node(expected_element, None, None) pl._validate = MagicMock(return_value=node) pl._delete_node = MagicMock(return_value=expected_element) position = PositionalList.Position(pl, node) result_element = pl.delete(position) pl._validate.assert_called_with(position) pl._delete_node.assert_called_with(node) self.assertIs(result_element, expected_element)
def test_add_after_inserts_element_after_given_Position_and_returns_Position( self): pl = PositionalList() n = PositionalList._Node('foo', pl._header, pl._trailer) pl._header._next = n pl._trailer._prev = n existing_position = PositionalList.Position(pl, n) element = 'foo' result_position = pl.add_after(existing_position, element) self.assertIs(result_position._node._element, element) self.assertIs(result_position._node._prev, existing_position._node) self.assertIs(result_position._node._next, pl._trailer) self.assertIs(existing_position._node._next, result_position._node)
def test_validate_raises_TypeError_with_wrong_type(self): pl = PositionalList() wrong_type = PositionalList._Node(None, None, None) self.assertRaises(TypeError, pl._validate, wrong_type)