def get_lossy_tree(self): tree = Tree() with tree.node('root', 'tale') as root: root.node('level', 'absurd') root.node('level', 'stupid') root.node('handle', 'lame') return tree
def test_data_node_with_children_and_text(self): tree = Tree() with tree.node('a', 'A', href="http://bigjason.com") as a: a.node('b', "Link") self.assertEqual( tree(), '<a href="http://bigjason.com">A<b>Link</b></a>' )
def test_render_child_node_as_root(self): tree = Tree() with tree.node('root') as root: child = root.node('name') child.node('first', 'Bob') child.node('last', 'Wiley') self.assertIsInstance(child.render(), basestring)
def get_dirty_tree(self): tree = Tree() with tree.node("author") as author: author.node('name', 'Terry Pratchett') with author.node('genre') as genre: genre.node('fantasy', 'true') genre.node('comedy', 'true') genre.node('horror', 'false') return tree
def test_run_as_callable(self): tree = Tree() with tree.node('root') as root: root.node('item', 1) actual = tree('dict') expected = {'root': {'item': 1}} self.assertDictEqual(actual, expected)
def get_unified_tree(self): tree = Tree() with tree.node("author") as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) novels.node('novel', 'Feet of Clay', year=1996) return tree
def test_children_distinct_names_are_different(self): tree = Tree() with tree.node('root') as root: root.node('person', 'One') root.node('different', 'Two') root.node('strokes', 'Three') render = DictRenderer() self.assertSetEqual( render._children_distinct_names(root.__children__), set(["person", "different", "strokes"]) )
def test_render_child_node_as_root(self): tree = Tree() with tree.node('root') as root: child = root.node('name') child.node('first', 'Bob') child.node('last', 'Wiley') self.assertIsInstance( child.render(), basestring )
def test_children_distinct_names(self): tree = Tree() with tree.node('tree') as root: root.node('person', 'One') root.node('person', 'Two') root.node('person', 'Three') render = DictRenderer() self.assertSetEqual( render._children_distinct_names(root.__children__), set(["person"]) )
def _get_complex_structure(self): tree = Tree() tree.instruct('xml') #tree.cdata(r"<b>I am some text.</b>") tree.declare('DOCTYPE', __.author, __.SYSTEM, 'SomeDTD.dtd') with tree.node('author') as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') author.comment("Only 2 books listed") with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) return tree
def get_author(self): tree = Tree() with tree.node('author', rating="<b>5/6 Stars</b>") as author: author.node('name', 'Terry Pratchett') author.node('genere', 'Fantasy/Comedy') author.node('country', abbreviation="UK") author.node('living') with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) with novels.node('shorts', count=2) as shorts: shorts.node('short', "Short Story 1") shorts.node('short', "Short Story 2") return tree
def _get_test_tree(self): tree = Tree() tree.instruct() tree.node('name', 'Bob') tree.node('age', 12) return tree
def test_add_child_node(self): tree = Tree() node = tree.node('A Value') self.assertEqual(tree.__children__[0], node)
def test_render_cdata_string(self): tree = Tree() tree.node('root').cdata("Some Value") self.assertIn('<![cdata[Some Value]]>', tree('xml'))
def test_render_cdata_not_string(self): int_val = 1234567891011121314151617181920 tree = Tree() tree.node('root').cdata(int_val) self.assertIn('<![cdata[{0}]]>'.format(str(int_val)), tree('xml'))
def test_render_comment(self): tree = Tree() tree.node('root').comment("Something Here") self.assertIn('<!-- Something Here -->', tree('xml'))
from datatree import Tree if __name__ == '__main__': tree = Tree() #tree.instruct('xml', version='1.0') with tree.node("author") as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') author.comment("Only 2 books listed") with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) novels.node("novel", "Guards! Guards!", year=1989) print 'XML:' print author(pretty=True) print print print 'JSON:' print author('json', pretty=True) print print print 'YAML:' print author('yaml') print print print 'Dict:' print author('dict', pretty_string=True)
def get_flat_tree(self): tree = Tree() with tree.node("author") as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') return tree
def test_add_node_return(self): tree = Tree() root = tree.node('root') self.assertEqual(root, tree.__children__[0])
def test_data_node_with_children_and_text(self): tree = Tree() with tree.node('a', 'A', href="http://bigjason.com") as a: a.node('b', "Link") self.assertEqual(tree(), '<a href="http://bigjason.com">A<b>Link</b></a>')