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_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_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_docpath_creation(self): "Test docpath creation." path = Docpath(DocpathStep(Axis('child'), 'section')) self.assertEqual(str(path), 'child::section')
def assertPredicateRaises(self, axis, predicates, exception): path = Docpath([DocpathStep(axis, 'section')] + predicates) with self.assertRaises(exception): list(path.traverse(self.node))
def assertPredicate(self, axis, predicates, matches): path = Docpath([DocpathStep(axis, 'section')] + predicates) names = [', '.join(n['names']) for n, a in path.traverse(self.node)] self.assertEqual(names, matches)