<s> import copy <EOL> import doctest <EOL> import gc <EOL> import os <EOL> import sys <EOL> import unittest <EOL> from tempfile import mkstemp <EOL> from pympler import refbrowser <EOL> from pympler . util . compat import StringIO <EOL> class TreeTest ( unittest . TestCase ) : <EOL> sample_tree = None <EOL> TREE_DEP_1 = """<STR_LIT>""" . strip ( ) <EOL> TREE_DEP_2 = """<STR_LIT>""" . strip ( ) <EOL> TREE_DEP_4 = """<STR_LIT>""" . strip ( ) <EOL> def setUp ( self ) : <EOL> TreeTest . sample_tree = refbrowser . _Node ( '<STR_LIT:root>' ) <EOL> branch1 = refbrowser . _Node ( '<STR_LIT>' ) <EOL> TreeTest . sample_tree . children . append ( branch1 ) <EOL> branch2 = refbrowser . _Node ( '<STR_LIT>' ) <EOL> TreeTest . sample_tree . children . append ( branch2 ) <EOL> branch3 = refbrowser . _Node ( '<STR_LIT>' ) <EOL> TreeTest . sample_tree . children . append ( branch3 ) <EOL> branch2 . children . append ( branch3 ) <EOL> branch3 . children . append ( branch1 ) <EOL> for i in [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' ] : <EOL> branch1 . children . append ( i ) <EOL> branch2 . children . append ( i ) <EOL> branch3 . children . append ( i ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> gc . collect ( ) <EOL> def test_node ( self ) : <EOL> """<STR_LIT>""" <EOL> n = refbrowser . _Node ( <NUM_LIT:1> ) <EOL> expected = str ( <NUM_LIT:1> ) <EOL> self . assert_ ( str ( n ) == expected ) <EOL> expected = '<STR_LIT>' <EOL> def foo ( o ) : return expected <EOL> n = refbrowser . _Node ( <NUM_LIT:1> , foo ) <EOL> self . assert_ ( str ( n ) == expected ) <EOL> n . children . append ( <NUM_LIT:2> ) <EOL> def test_get_tree ( self ) : <EOL> """<STR_LIT>""" <EOL> root = '<STR_LIT>' <EOL> ref1 = [ root , [ ] ] <EOL> ref11 = [ ref1 , root ] <EOL> ref2 = { <NUM_LIT:1> : root , <NUM_LIT:2> : [ ] } <EOL> ref22 = { <NUM_LIT:1> : ref2 } <EOL> res = refbrowser . RefBrowser ( root , repeat = False ) . get_tree ( ) <EOL> refs = [ ref1 , ref2 ] <EOL> children = [ c . o for c in res . children if isinstance ( c , refbrowser . _Node ) ] <EOL> for r in refs : <EOL> self . assert_ ( r in children , "<STR_LIT>" % r ) <EOL> self . assert_ ( ref11 not in children ) <EOL> res = refbrowser . RefBrowser ( root , repeat = True ) . get_tree ( ) <EOL> refs = [ ref1 , ref11 , ref2 ] <EOL> children = [ c . o for c in res . children if isinstance ( c , refbrowser . _Node ) ] <EOL> for r in refs : <EOL> self . assert_ ( r in children ) <EOL> res = refbrowser . RefBrowser ( root , maxdepth = <NUM_LIT:0> ) . get_tree ( ) <EOL> self . assertEqual ( len ( res . children ) , <NUM_LIT:0> ) <EOL> res = refbrowser . RefBrowser ( root , maxdepth = <NUM_LIT:1> ) . get_tree ( ) <EOL> for c in res . children : <EOL> if c == ref1 : <EOL> self . assertEqual ( len ( c . children ) , <NUM_LIT:0> ) <EOL> expected = '<STR_LIT>' <EOL> def foo ( o ) : return expected <EOL> res = refbrowser . RefBrowser ( root , str_func = foo , maxdepth = <NUM_LIT:2> ) . get_tree ( ) <EOL> self . assertEqual ( str ( res ) , expected ) <EOL> res = refbrowser . RefBrowser ( root , str_func = foo , repeat = True , maxdepth = <NUM_LIT:2> ) . get_tree ( ) <EOL> self . assertEqual ( str ( res ) , expected ) <EOL> def test_console_browser ( self ) : <EOL> """<STR_LIT>""" <EOL> crb = refbrowser . ConsoleBrowser ( None , maxdepth = <NUM_LIT:2> ) <EOL> self . assertEqual ( crb . stream , sys . stdout ) <EOL> def test_file_browser ( self ) : <EOL> crb = refbrowser . FileBrowser ( None , maxdepth = <NUM_LIT:1> ) <EOL> fhandle , fname = mkstemp ( prefix = '<STR_LIT>' , text = True ) <EOL> os . close ( fhandle ) <EOL> try : <EOL> crb . print_tree ( fname , tree = self . sample_tree ) <EOL> output = open ( fname ) . read ( ) <EOL> self . assertEqual ( output . strip ( ) , self . TREE_DEP_1 ) <EOL> finally : <EOL> os . unlink ( fname ) <EOL> def test_print_tree ( self ) : <EOL> """<STR_LIT>""" <EOL> out1 = StringIO ( ) <EOL> crb = refbrowser . StreamBrowser ( copy . copy ( self . sample_tree ) , maxdepth = <NUM_LIT:1> , stream = out1 ) <EOL> crb . print_tree ( crb . get_tree ( ) ) <EOL> out2 = StringIO ( ) <EOL> crb = refbrowser . StreamBrowser ( copy . copy ( self . sample_tree ) , maxdepth = <NUM_LIT:1> , stream = out2 ) <EOL> crb . print_tree ( ) <EOL> self . assertEqual ( out1 . getvalue ( ) , out2 . getvalue ( ) ) <EOL> def test_reference_browser_max_depth ( self ) : <EOL> """<STR_LIT>""" <EOL> stream = StringIO ( ) <EOL> crb = refbrowser . StreamBrowser ( None , maxdepth = <NUM_LIT:1> , stream = stream ) <EOL> crb . print_tree ( self . sample_tree ) <EOL> self . assertEqual ( stream . getvalue ( ) . strip ( ) , self . TREE_DEP_1 ) <EOL> stream = StringIO ( ) <EOL> crb = refbrowser . StreamBrowser ( None , maxdepth = <NUM_LIT:2> , stream = stream ) <EOL> crb . print_tree ( self . sample_tree ) <EOL> self . assertEqual ( stream . getvalue ( ) . strip ( ) , self . TREE_DEP_2 ) <EOL> stream = StringIO ( ) <EOL> crb = refbrowser . StreamBrowser ( None , maxdepth = <NUM_LIT:4> , stream = stream ) <EOL> crb . print_tree ( self . sample_tree ) <EOL> self . assertEqual ( stream . getvalue ( ) . strip ( ) , self . TREE_DEP_4 ) <EOL> test_print_tree = """<STR_LIT>""" <EOL> __test__ = { "<STR_LIT>" : test_print_tree } <EOL> def suite ( ) : <EOL> suite = unittest . makeSuite ( TreeTest , '<STR_LIT:test>' ) <EOL> suite . addTest ( doctest . DocTestSuite ( ) ) <EOL> return suite <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ( ) ) </s>
