<s> '''<STR_LIT>''' <EOL> from treemodel import Task , Project , Folder , Context , Visitor , traverse , traverse_list , sort <EOL> import unittest <EOL> class DemoVisitor ( Visitor ) : <EOL> def __init__ ( self ) : <EOL> self . tasks_started = [ ] <EOL> self . tasks_ended = [ ] <EOL> self . projects_started = [ ] <EOL> self . projects_ended = [ ] <EOL> self . folders_started = [ ] <EOL> self . folders_ended = [ ] <EOL> self . contexts_started = [ ] <EOL> self . contexts_ended = [ ] <EOL> def begin_task ( self , task ) : <EOL> self . tasks_started . append ( task ) <EOL> def end_task ( self , task ) : <EOL> self . tasks_ended . append ( task ) <EOL> def begin_project ( self , project ) : <EOL> self . projects_started . append ( project ) <EOL> def end_project ( self , project ) : <EOL> self . projects_ended . append ( project ) <EOL> def begin_folder ( self , folder ) : <EOL> self . folders_started . append ( folder ) <EOL> def end_folder ( self , folder ) : <EOL> self . folders_ended . append ( folder ) <EOL> def begin_context ( self , context ) : <EOL> self . contexts_started . append ( context ) <EOL> def end_context ( self , context ) : <EOL> self . contexts_ended . append ( context ) <EOL> class SortableTask ( Task ) : <EOL> def get_sort_key ( self ) : <EOL> return self . name <EOL> class Test_treemodel ( unittest . TestCase ) : <EOL> def test_project_add_child ( self ) : <EOL> project = Project ( ) <EOL> child = Task ( ) <EOL> project . add_child ( child ) <EOL> self . assertEquals ( project , child . parent ) <EOL> self . assertEquals ( project , child . project ) <EOL> project = Project ( ) <EOL> child = Task ( ) <EOL> grandchild = Task ( ) <EOL> child . add_child ( grandchild ) <EOL> project . add_child ( child ) <EOL> self . assertEquals ( project , child . parent ) <EOL> self . assertEquals ( project , child . project ) <EOL> self . assertEquals ( project , grandchild . project ) <EOL> def test_task_add_child ( self ) : <EOL> project = Project ( ) <EOL> task = Task ( ) <EOL> child = Task ( ) <EOL> project . add_child ( task ) <EOL> task . add_child ( child ) <EOL> self . assertEquals ( task , child . parent ) <EOL> self . assertEquals ( project , child . project ) <EOL> project = Project ( ) <EOL> task = Task ( ) <EOL> child = Task ( ) <EOL> grandchild = Task ( ) <EOL> project . add_child ( task ) <EOL> task . add_child ( child ) <EOL> child . add_child ( grandchild ) <EOL> task . add_child ( child ) <EOL> self . assertEquals ( task , child . parent ) <EOL> self . assertEquals ( project , child . project ) <EOL> self . assertEquals ( child , grandchild . parent ) <EOL> self . assertEquals ( project , grandchild . project ) <EOL> def test_context_add_child ( self ) : <EOL> context = Context ( ) <EOL> child = Context ( ) <EOL> context . add_child ( child ) <EOL> self . assertEquals ( context , child . parent ) <EOL> context = Context ( ) <EOL> child = Task ( ) <EOL> context . add_child ( child ) <EOL> self . assertEquals ( context , child . context ) <EOL> self . assertEquals ( None , child . parent ) <EOL> def test_traverse_list_tasks ( self ) : <EOL> n1 = Task ( name = u'<STR_LIT>' ) <EOL> n2 = Task ( name = u'<STR_LIT>' ) <EOL> nodes = [ n1 , n2 ] <EOL> visitor = DemoVisitor ( ) <EOL> traverse_list ( visitor , nodes ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . tasks_started ) ) <EOL> self . assertTrue ( n1 in visitor . tasks_started ) <EOL> self . assertTrue ( n2 in visitor . tasks_started ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . tasks_ended ) ) <EOL> self . assertTrue ( n1 in visitor . tasks_ended ) <EOL> self . assertTrue ( n2 in visitor . tasks_ended ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . projects_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . folders_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . contexts_started ) ) <EOL> def test_traverse_list_projects ( self ) : <EOL> n1 = Project ( name = u'<STR_LIT>' ) <EOL> n2 = Project ( name = u'<STR_LIT>' ) <EOL> nodes = [ n1 , n2 ] <EOL> visitor = DemoVisitor ( ) <EOL> traverse_list ( visitor , nodes ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . projects_started ) ) <EOL> self . assertTrue ( n1 in visitor . projects_started ) <EOL> self . assertTrue ( n2 in visitor . projects_started ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . projects_ended ) ) <EOL> self . assertTrue ( n1 in visitor . projects_ended ) <EOL> self . assertTrue ( n2 in visitor . projects_ended ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . folders_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . contexts_started ) ) <EOL> def test_traverse_list_folders ( self ) : <EOL> n1 = Folder ( name = u'<STR_LIT>' ) <EOL> n2 = Folder ( name = u'<STR_LIT>' ) <EOL> nodes = [ n1 , n2 ] <EOL> visitor = DemoVisitor ( ) <EOL> traverse_list ( visitor , nodes ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . folders_started ) ) <EOL> self . assertTrue ( n1 in visitor . folders_started ) <EOL> self . assertTrue ( n2 in visitor . folders_started ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . folders_ended ) ) <EOL> self . assertTrue ( n1 in visitor . folders_ended ) <EOL> self . assertTrue ( n2 in visitor . folders_ended ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . projects_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . contexts_started ) ) <EOL> def test_traverse_list_contexts ( self ) : <EOL> n1 = Context ( name = u'<STR_LIT>' ) <EOL> n2 = Context ( name = u'<STR_LIT>' ) <EOL> nodes = [ n1 , n2 ] <EOL> visitor = DemoVisitor ( ) <EOL> traverse_list ( visitor , nodes ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . contexts_started ) ) <EOL> self . assertTrue ( n1 in visitor . contexts_started ) <EOL> self . assertTrue ( n2 in visitor . contexts_started ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . contexts_ended ) ) <EOL> self . assertTrue ( n1 in visitor . contexts_ended ) <EOL> self . assertTrue ( n2 in visitor . contexts_ended ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . projects_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . folders_started ) ) <EOL> def test_traverse_task ( self ) : <EOL> parent = Task ( name = u'<STR_LIT:p>' ) <EOL> n1 = Task ( name = u'<STR_LIT>' ) <EOL> n2 = Task ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> visitor = DemoVisitor ( ) <EOL> traverse ( visitor , parent ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( visitor . tasks_started ) ) <EOL> self . assertTrue ( parent in visitor . tasks_started ) <EOL> self . assertTrue ( n1 in visitor . tasks_started ) <EOL> self . assertTrue ( n2 in visitor . tasks_started ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( visitor . tasks_ended ) ) <EOL> self . assertTrue ( parent in visitor . tasks_ended ) <EOL> self . assertTrue ( n1 in visitor . tasks_ended ) <EOL> self . assertTrue ( n2 in visitor . tasks_ended ) <EOL> def test_traverse_task_when_not_marked ( self ) : <EOL> parent = Task ( name = u'<STR_LIT:p>' ) <EOL> n1 = Task ( name = u'<STR_LIT>' ) <EOL> n2 = Task ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> parent . marked = False <EOL> visitor = DemoVisitor ( ) <EOL> traverse ( visitor , parent ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_ended ) ) <EOL> def test_traverse_project ( self ) : <EOL> parent = Project ( name = u'<STR_LIT:p>' ) <EOL> n1 = Task ( name = u'<STR_LIT>' ) <EOL> n2 = Task ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> visitor = DemoVisitor ( ) <EOL> traverse ( visitor , parent ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( visitor . projects_started ) ) <EOL> self . assertTrue ( parent in visitor . projects_started ) <EOL> self . assertTrue ( n1 in visitor . tasks_started ) <EOL> self . assertTrue ( n2 in visitor . tasks_started ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . tasks_ended ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( visitor . projects_ended ) ) <EOL> self . assertTrue ( parent in visitor . projects_ended ) <EOL> self . assertTrue ( n1 in visitor . tasks_ended ) <EOL> self . assertTrue ( n2 in visitor . tasks_ended ) <EOL> def test_traverse_project_when_not_marked ( self ) : <EOL> parent = Project ( name = u'<STR_LIT:p>' ) <EOL> n1 = Task ( name = u'<STR_LIT>' ) <EOL> n2 = Task ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> parent . marked = False <EOL> visitor = DemoVisitor ( ) <EOL> traverse ( visitor , parent ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . projects_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_ended ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . projects_ended ) ) <EOL> def test_traverse_context ( self ) : <EOL> parent = Context ( name = u'<STR_LIT:p>' ) <EOL> n1 = Task ( name = u'<STR_LIT>' ) <EOL> n2 = Task ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> visitor = DemoVisitor ( ) <EOL> traverse ( visitor , parent ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( visitor . contexts_started ) ) <EOL> self . assertTrue ( parent in visitor . contexts_started ) <EOL> self . assertTrue ( n1 in visitor . tasks_started ) <EOL> self . assertTrue ( n2 in visitor . tasks_started ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . tasks_ended ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( visitor . contexts_ended ) ) <EOL> self . assertTrue ( parent in visitor . contexts_ended ) <EOL> self . assertTrue ( n1 in visitor . tasks_ended ) <EOL> self . assertTrue ( n2 in visitor . tasks_ended ) <EOL> def test_traverse_context_when_not_marked ( self ) : <EOL> parent = Context ( name = u'<STR_LIT:p>' ) <EOL> n1 = Task ( name = u'<STR_LIT>' ) <EOL> n2 = Task ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> parent . marked = False <EOL> visitor = DemoVisitor ( ) <EOL> traverse ( visitor , parent ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . contexts_started ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . tasks_ended ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( visitor . contexts_ended ) ) <EOL> def test_traverse_folder ( self ) : <EOL> parent = Folder ( name = u'<STR_LIT:p>' ) <EOL> n1 = Project ( name = u'<STR_LIT>' ) <EOL> n2 = Folder ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> visitor = DemoVisitor ( ) <EOL> traverse ( visitor , parent ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . folders_started ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( visitor . projects_started ) ) <EOL> self . assertTrue ( parent in visitor . folders_started ) <EOL> self . assertTrue ( n1 in visitor . projects_started ) <EOL> self . assertTrue ( n2 in visitor . folders_started ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( visitor . folders_ended ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( visitor . projects_ended ) ) <EOL> self . assertTrue ( parent in visitor . folders_ended ) <EOL> self . assertTrue ( n1 in visitor . projects_ended ) <EOL> self . assertTrue ( n2 in visitor . folders_ended ) <EOL> def test_sort_order_when_sorted ( self ) : <EOL> parent = SortableTask ( name = u'<STR_LIT:p>' ) <EOL> n1 = SortableTask ( name = u'<STR_LIT>' ) <EOL> n2 = SortableTask ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> sort ( [ parent ] ) <EOL> self . assertEqual ( n1 , parent . children [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( n2 , parent . children [ <NUM_LIT:1> ] ) <EOL> def test_sort_order_when_unsorted ( self ) : <EOL> parent = SortableTask ( name = u'<STR_LIT:p>' ) <EOL> n2 = SortableTask ( name = u'<STR_LIT>' ) <EOL> n1 = SortableTask ( name = u'<STR_LIT>' ) <EOL> parent . children . append ( n1 ) <EOL> parent . children . append ( n2 ) <EOL> sort ( [ parent ] ) <EOL> self . assertEqual ( n1 , parent . children [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( n2 , parent . children [ <NUM_LIT:1> ] ) </s>
