def test_predicate_assignment_operator(self): "Test a predicate cannot contain an assignment operator." self.assertPredicateRaises(Axis('child'), [Predicate('./section = "Q"')], SyntaxError) self.assertPredicateRaises(Axis('child'), [Predicate('@attribute = "Q"')], SyntaxError)
def test_axis_traverse_parent(self): "Test the parent doctree traversal." self.assertNameTraversal( Axis('parent').traverse, ['e']) self.assertNameTraversal( Axis('parent').traverse_backwards, ['e'])
def test_axis_traverse_preceding_sibling(self): "Test the preceding sibling doctree traversal." self.assertNameTraversal( Axis('preceding_sibling').traverse, ['h', 'f']) self.assertNameTraversal( Axis('preceding_sibling').traverse_backwards, ['f', 'h'])
def test_axis_traverse_root(self): "Test the root doctree traversal." self.assertNameTraversal( Axis('root').traverse, ['a']) self.assertNameTraversal( Axis('root').traverse_backwards, ['a'])
def test_axis_traverse_self(self): "Test the self doctree traversal." self.assertNameTraversal( Axis('self').traverse, ['i']) self.assertNameTraversal( Axis('self').traverse_backwards, ['i'])
def test_axis_traverse_ancestor(self): "Test the ancestor doctree traversal." self.assertNameTraversal( Axis('ancestor').traverse, ['e', 'a']) self.assertNameTraversal( Axis('ancestor').traverse_backwards, ['a', 'e'])
def test_axis_traverse_ancestor_or_self(self): "Test the ancestor or self doctree traversal." self.assertNameTraversal( Axis('ancestor_or_self').traverse, ['i', 'e', 'a']) self.assertNameTraversal( Axis('ancestor_or_self').traverse_backwards, ['a', 'e', 'i'])
def test_axis_traverse_child(self): "Test the child doctree traversal." self.assertNameTraversal( Axis('child').traverse, ['j', 'k', 'n', 'o', 'p']) self.assertNameTraversal( Axis('child').traverse_backwards, ['p', 'o', 'n', 'k', 'j'])
def test_axis_traverse_descendant_or_self(self): "Test the descendant or self doctree traversal." self.assertNameTraversal( Axis('descendant_or_self').traverse, ['i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']) self.assertNameTraversal( Axis('descendant_or_self').traverse_backwards, ['r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i'])
def test_axis_traverse_following_sibling(self): "Test the following sibling doctree traversal." self.assertNameTraversal( Axis('following_sibling').traverse, ['s', 't']) self.assertNameTraversal( Axis('following_sibling').traverse_backwards, ['t', 's'])
def test_predicate_associates_with_last_step(self): "Test a predicate that finds the first child section of all nodes." path = Docpath([ DocpathStep(Axis('root'), 'node'), DocpathStep(Axis('descendant_or_self'), 'node'), DocpathStep(Axis('child'), 'section'), Predicate('1') ]) self.assertEqual( [' '.join(n['names']) for n, _ in path.traverse(self.node)], ['c', 'f', 'g', 'j', 'l', 'q', 'u'])
def test_predicate_not_element(self): "Test a predicate that find nodes that do not match." path = Docpath([ DocpathStep(Axis('descendant_or_self'), 'node'), Predicate('name() != "section"') ]) self.assertEqual( [n[0].__class__.__name__ for n in path.traverse(self.node)], [ 'title', 'Text', 'title', 'Text', 'title', 'Text', 'title', 'Text', 'title', 'Text', 'title', 'Text', 'title', 'Text', 'title', 'Text', 'title', 'Text', 'title', 'Text' ])
def test_predicate_number(self): "Test a predicate that only contains a number." self.assertPredicate(Axis('child'), [Predicate('1')], ['j'])
def test_predicate_number_expression(self): "Test a predicate that evaluates to a number." self.assertPredicate(Axis('child'), [Predicate('last()')], ['p'])
def test_axis_traverse_attribute(self): "Test the attribute doctree traversal." self.assertAttributeTraversal( Axis('attribute').traverse, set(['backrefs', 'names', 'classes', 'dupnames', 'ids']))
def test_axis_node_address(self): "Test the node address is calculated correctly." self.assertEqual(Axis.node_address(self.node), (1, 4, 3))
def test_predicate_attribute_exists(self): "Test a predicate that tests whether an attribute exists." self.assertPredicate(Axis('child'), [Predicate('@names')], ['j', 'k', 'n', 'o', 'p'])
def test_predicate_double_number(self): "Test multiple predicates that select nodes by number." self.assertPredicate(Axis('child'), [Predicate('4'), Predicate('1')], ['o'])
def test_predicate_attribute_value(self): "Test a predicate that tests an attribute's value." self.assertPredicate(Axis('child'), [Predicate('@names == "n"')], ['n'])
def setUp(self): source = join(dirname(__file__), 'doc', 'doctree.rst') with open(source, 'r', encoding='utf-8') as rst: self.doctree = publish_doctree(rst.read()) self.node = self.doctree.next_node(self._matches_node_i) self.address = Axis.node_address(self.node)
def test_predicate_node_value(self): "Test a predicate that tests a node's value." self.assertPredicate(Axis('child'), [Predicate('./section == "Q"')], ['p'])
def assertFilterNodes(self, node_test, matches): step = DocpathStep(Axis('child'), node_test) filtered_nodes = step.filter_nodes(self.node_addresses) self.assertEqual(list(filtered_nodes), matches)
def test_predicate_count_nodeset(self): "Test a predicate that counts the amount of nodes in a path." self.assertPredicate(Axis('child'), [Predicate('count(^//section) == 21')], ['j', 'k', 'n', 'o', 'p'])
def test_docpath_creation_step_or(self): "Test docpath creation from or steps." path = Docpath(( DocpathStep(Axis('root'), 'node'), DocpathStep(Axis('child'), 'section'))) self.assertEqual(str(path), '(root::node|child::section)')
def test_predicate_forward_axis(self): "Test a predicate that uses a forward axis." self.assertPredicate(Axis('following_sibling'), [Predicate('1')], ['s'])
def test_docpath_creation(self): "Test docpath creation." path = Docpath(DocpathStep(Axis('child'), 'section')) self.assertEqual(str(path), 'child::section')
def test_predicate_relational(self): "Test a predicate that contains a relational expression." self.assertPredicate(Axis('child'), [Predicate('position() > 2')], ['n', 'o', 'p'])
def test_predicate_node_exists(self): "Test a predicate that tests whether a node exists." self.assertPredicate(Axis('child'), [Predicate('./section')], ['k', 'p'])
def test_docpath_creation_steps(self): "Test docpath creation from steps." path = Docpath([ DocpathStep(Axis('root'), 'node'), DocpathStep(Axis('child'), 'section')]) self.assertEqual(str(path), '/child::section')
def test_predicate_reverse_axis(self): "Test a predicate that uses a reverse axis." self.assertPredicate(Axis('preceding_sibling'), [Predicate('1')], ['h'])