tests/test_treeview.py
changeset 37 54dd866b8951
parent 19 5e78d52ebb24
child 38 c6e170452c7f
equal deleted inserted replaced
36:caf927c3f10b 37:54dd866b8951
     1 #!/usr/bin/env python3
     1 #!/usr/bin/env python3
     2 
     2 
     3 import sys
     3 import sys
     4 sys.path.append('..')
     4 sys.path.insert(0, '..')
     5 
     5 
     6 from tuikit.treeview import *
     6 from tuikit.treeview import *
     7 import unittest
     7 import unittest
     8 
     8 
     9 
     9 
    10 class TestTreeView(unittest.TestCase):
    10 class TestTreeView(unittest.TestCase):
    11     def test_treemodel(self):
    11     def setUp(self):
    12         '''Build tree model, iterate through the tree, test result.'''
    12         """Build tree model
    13         # build tree model
    13 
    14         # root
    14         # root
    15         # ├ a
    15         # ├ a
    16         # │ ├ c
    16         # │ ├ c
    17         # │ └ d
    17         # │ └ d
    18         # │   ├ e
    18         # │   ├ e
    19         # │   │ └ g
    19         # │   │ └ g
    20         # │   └ f
    20         # │   └ f
    21         # │     └ h
    21         # │     └ h
    22         # └ b
    22         # └ b
    23         model = TreeModel()
    23 
    24         model.add('/',  ['a', 'b'])
    24         """
    25         model.add('/a', ['c', 'd'])
    25         self.model = TreeModel()
    26         model.add((0,1), ['e', 'f'])
    26         self.model.add('/',  ['a', 'b'])
    27         model.add('/0/1/0', 'g')
    27         self.model.add('/a', [TreeNode('c'), TreeNode('d')])
    28         model.add('/a/d/f', 'h')
    28         self.model.add((0,1), ['e', TreeNode('f')])
    29         
    29         self.model.add([0,1,0], 'g')
       
    30         self.model.add('/a/d/f', TreeNode('h'))
       
    31 
       
    32     def test_treeiter(self):
       
    33         """Iterate through the tree, test result."""
    30         res = ''
    34         res = ''
    31         for l, i, c, n in model:
    35         for l, i, c, n in self.model:
    32             res += str(l) + str(i) + str(c) + n.name
    36             res += str(l) + str(i) + str(c) + n.name
    33             
    37 
    34         self.assertEqual(res, '112a212c222d312e411g322f411h122b')
    38         self.assertEqual(res, '112a212c222d312e411g322f411h122b')
    35 
    39 
       
    40     def test_treemodel_find(self):
       
    41         # good path
       
    42         node = self.model.find('/a/d/f/h')
       
    43         self.assertEqual(node.name, 'h')
       
    44         self.assertEqual(node.path, '/a/d/f/h')
       
    45         # bad path
       
    46         self.assertRaises(ValueError, self.model.find, '/a/b/c')
       
    47         self.assertRaises(IndexError, self.model.find, [0,1,3])
    36 
    48 
    37 if __name__ == '__main__':
    49 if __name__ == '__main__':
    38     unittest.main()
    50     unittest.main()
    39 
    51