"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/tests/test_tree.py" (5 Mar 2021, 4871 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 import unittest
    2 
    3 from http_prompt.tree import Node
    4 
    5 
    6 class TestNode(unittest.TestCase):
    7 
    8     def setUp(self):
    9         # Make a tree like this:
   10         #          root
   11         #     a             h
   12         #  b     d        i   n
   13         # c f   e g     k     o
   14         #             l m p
   15         self.root = Node('root')
   16         self.root.add_path('a', 'b', 'c')
   17         self.root.add_path('a', 'b', 'f')
   18         self.root.add_path('a', 'd', 'e')
   19         self.root.add_path('a', 'd', 'g')
   20         self.root.add_path('h', 'i', 'k', 'l')
   21         self.root.add_path('h', 'i', 'k', 'm')
   22         self.root.add_path('h', 'i', 'k', 'p')
   23         self.root.add_path('h', 'n', 'o')
   24 
   25     def test_illegal_name(self):
   26         self.assertRaises(ValueError, Node, '.')
   27         self.assertRaises(ValueError, Node, '..')
   28 
   29     def test_str(self):
   30         node = Node('my node')
   31         self.assertEqual(str(node), 'my node')
   32 
   33     def test_cmp_same_type(self):
   34         a = Node('a', data={'type': 'dir'})
   35         b = Node('b', data={'type': 'dir'})
   36         self.assertTrue(a < b)
   37 
   38     def test_cmp_different_type(self):
   39         a = Node('a', data={'type': 'file'})
   40         b = Node('b', data={'type': 'dir'})
   41         self.assertTrue(b < a)
   42 
   43     def test_eq(self):
   44         a = Node('a', data={'type': 'file'})
   45         b = Node('b', data={'type': 'dir'})
   46         self.assertNotEqual(a, b)
   47 
   48         a = Node('a', data={'type': 'file'})
   49         b = Node('a', data={'type': 'file'})
   50         self.assertEqual(a, b)
   51 
   52     def test_add_path_and_find_child(self):
   53         # Level 1 (root)
   54         self.assertEqual(set(c.name for c in self.root.children), set('ah'))
   55 
   56         # Level 2
   57         node_a = self.root.find_child('a')
   58         node_h = self.root.find_child('h')
   59         self.assertEqual(set(c.name for c in node_a.children), set('bd'))
   60         self.assertEqual(set(c.name for c in node_h.children), set('in'))
   61 
   62         # Level 3
   63         node_b = node_a.find_child('b')
   64         node_i = node_h.find_child('i')
   65         self.assertEqual(set(c.name for c in node_b.children), set('cf'))
   66         self.assertEqual(set(c.name for c in node_i.children), set('k'))
   67 
   68         # Level 4
   69         node_c = node_b.find_child('c')
   70         node_k = node_i.find_child('k')
   71         self.assertEqual(set(c.name for c in node_c.children), set())
   72         self.assertEqual(set(c.name for c in node_k.children), set('lmp'))
   73 
   74         # Return None if child can't be found
   75         self.assertFalse(node_c.find_child('x'))
   76 
   77     def test_find_child_wildcard(self):
   78         root = Node('root')
   79         root.add_path('a')
   80         root.add_path('{b}')
   81         root.add_path('c')
   82 
   83         self.assertEqual(root.find_child('a').name, 'a')
   84         self.assertEqual(root.find_child('c').name, 'c')
   85         self.assertEqual(root.find_child('x').name, '{b}')
   86         self.assertFalse(root.find_child('x', wildcard=False))
   87 
   88     def test_ls(self):
   89         self.assertEqual([n.name for n in self.root.ls('a')], list('bd'))
   90         self.assertEqual([n.name for n in self.root.ls('a', 'b')], list('cf'))
   91         self.assertEqual([n.name for n in self.root.ls('a', 'b', 'c')], [])
   92         self.assertEqual([n.name for n in self.root.ls('h', 'i', 'k')],
   93                          list('lmp'))
   94 
   95     def test_ls_root(self):
   96         self.assertEqual([n.name for n in self.root.ls()], list('ah'))
   97 
   98     def test_ls_non_existing(self):
   99         self.assertEqual([n.name for n in self.root.ls('x')], [])
  100         self.assertEqual([n.name for n in self.root.ls('a', 'b', 'x')], [])
  101 
  102     def test_ls_parent(self):
  103         self.assertEqual([n.name for n in self.root.ls('..')], list('ah'))
  104         self.assertEqual([n.name for n in self.root.ls('..', '..', '..')],
  105                          list('ah'))
  106         self.assertEqual([n.name for n in self.root.ls('..', '..', 'h')],
  107                          list('in'))
  108         self.assertEqual(
  109             [n.name for n in self.root.ls('..', '..', 'h', '..', 'a')],
  110             list('bd'))
  111 
  112     def test_ls_dot(self):
  113         self.assertEqual([n.name for n in self.root.ls('.')], list('ah'))
  114         self.assertEqual([n.name for n in self.root.ls('.', '.', '.')],
  115                          list('ah'))
  116         self.assertEqual([n.name for n in self.root.ls('.', 'a', 'b')],
  117                          list('cf'))
  118         self.assertEqual([n.name for n in self.root.ls('.', 'h', '.')],
  119                          list('in'))
  120         self.assertEqual(
  121             [n.name for n in self.root.ls('.', 'h', '.', '.', 'n')], ['o'])
  122 
  123     def test_ls_sort_by_types(self):
  124         self.root.add_path('q', 'r')
  125         self.root.add_path('q', 's', node_type='file')
  126         self.root.add_path('q', 't', node_type='file')
  127         self.root.add_path('q', 'u')
  128         self.root.add_path('q', 'v', node_type='file')
  129 
  130         self.assertEqual([n.name for n in self.root.ls('q')],
  131                          list('rustv'))