Example #1
0
 def test_01(self):
     """TestTreeAdd: simple add and check .obj."""
     t = Tree.Tree('A')
     t.addChild('B')
     t.addChild('B')
     t.addChild('C')
     self.assertEqual(['B', 'B', 'C'], [v.obj for v in t.children])
Example #2
0
 def test_02(self):
     """TestDuplexAdjacencyList: single parent, two children."""
     t = Tree.DuplexAdjacencyList()
     t.add('parent', 'child_00')
     t.add('parent', 'child_01')
     self.assertEqual([
         'child_00',
         'child_01',
     ], t.children('parent'))
     self.assertEqual([
         'parent',
     ], t.parents('child_00'))
     self.assertEqual([
         'parent',
     ], t.parents('child_01'))
     self.assertEqual(['parent'], list(t.allParents))
     self.assertEqual(['child_00', 'child_01'], sorted(t.allChildren))
     #         print(t.treeParentChild('parent').branches())
     self.assertEqual(
         [
             ['parent'],
             ['parent', 'child_00'],
             ['parent', 'child_01'],
         ],
         t.treeParentChild('parent').branches(),
     )
Example #3
0
 def test_00(self):
     """TestDuplexAdjacencyList: ctor, empty list."""
     t = Tree.DuplexAdjacencyList()
     self.assertRaises(KeyError, t.children, '')
     self.assertRaises(KeyError, t.parents, '')
     self.assertEqual([], list(t.allParents))
     self.assertEqual([], list(t.allChildren))
Example #4
0
 def test_treeChildParent_raises(self):
     """TestDuplexAdjacencyList: Tree with a cycle raises."""
     t = Tree.DuplexAdjacencyList()
     t.add('a', 'b')
     t.add('b', 'c')
     t.add('c', 'a')
     self.assertRaises(ValueError, t.treeChildParent, 'B')
Example #5
0
    def test_01(self):
        """TestTreeAdd: simple add and convert to a Dict Tree."""
        t = Tree.Tree('A')
        t.addChild('AA')
        t.youngestChild.addChild('AAA')
        t.addChild('AB')
        t.youngestChild.addChild('ABA')
        #         print(t.branches())
        self.assertEquals([['A'], ['A', 'AA'], ['A', 'AA', 'AAA'], ['A', 'AB'],
                           ['A', 'AB', 'ABA']], t.branches())
        dt = DictTree.DictTree('list')
        for branch in t.branches():
            dt.add(branch, None)


#         print(dt.indentedStr())
        self.assertEqual(
            """A
  [None]
  AA
    [None]
    AAA
      [None]
  AB
    [None]
    ABA
      [None]""", dt.indentedStr())
Example #6
0
 def test_02(self):
     """TestTreeCtor: test_02(): youngestChild raises when no children."""
     t = Tree.Tree('A')
     try:
         t.youngestChild
         self.fail('IndexError not raised.')
     except IndexError:
         pass
Example #7
0
    def test_str(self):
        t = Tree.DuplexAdjacencyList()
        t.add('parent', 'child_00')
        t.add('parent', 'child_01')
        exp = """Parent -> Children:
parent -> ['child_00', 'child_01']"""
        # print()
        # print(str(t))
        self.assertEqual(exp, str(t))
Example #8
0
 def test_00(self):
     """TestTreeAdd: simple add."""
     t = Tree.Tree('A')
     t.addChild('B')
     self.assertEquals(1, len(t))
     t.addChild('B')
     self.assertEquals(2, len(t))
     t.addChild('C')
     self.assertEquals(3, len(t))
Example #9
0
 def test_20(self):
     """TestDuplexAdjacencyList: __str__ on a Tree."""
     t = Tree.DuplexAdjacencyList()
     t.add('A', 'AA')
     t.add('A', 'AB')
     t.add('AA', 'AAA')
     t.add('AA', 'AAB')
     t.add('AB', 'ABA')
     t.add('AB', 'ABB')
Example #10
0
 def test_00(self):
     """TestTreeAdd: simple add."""
     t = Tree.Tree('A')
     t.addChild('AA')
     t.youngestChild.addChild('AAA')
     t.addChild('AB')
     t.youngestChild.addChild('ABA')
     #         print(t.branches())
     self.assertEquals([['A'], ['A', 'AA'], ['A', 'AA', 'AAA'], ['A', 'AB'],
                        ['A', 'AB', 'ABA']], t.branches())
Example #11
0
 def test_str(self):
     t = Tree.Tree('A')
     t.addChild('AA')
     t.youngestChild.addChild('AAA')
     t.addChild('AB')
     t.youngestChild.addChild('ABA')
     # print()
     # print(str(t))
     exp = "[['A'], ['A', 'AA'], ['A', 'AA', 'AAA'], ['A', 'AB'], ['A', 'AB', 'ABA']]"
     self.assertEquals(exp, str(t))
Example #12
0
 def test_06(self):
     """TestDuplexAdjacencyList: test hasParent(), hasChildren()."""
     t = Tree.DuplexAdjacencyList()
     t.add('parent', 'child_00')
     t.add('parent', 'child_01')
     self.assertTrue(t.hasParent('parent'))
     self.assertTrue(t.hasChild('child_00'))
     self.assertTrue(t.hasChild('child_01'))
     self.assertFalse(t.hasParent('foo'))
     self.assertFalse(t.hasChild('bar'))
Example #13
0
 def test_treeParentChild_raises(self):
     """TestDuplexAdjacencyList: Tree with no cycles."""
     t = Tree.DuplexAdjacencyList()
     t.add('A', 'AA')
     t.add('A', 'AB')
     t.add('AA', 'AAA')
     t.add('AA', 'AAB')
     t.add('AB', 'ABA')
     t.add('AB', 'ABB')
     #         pprint.pprint(t.treeParentChild('A').branches())
     self.assertRaises(ValueError, t.treeParentChild, 'B')
Example #14
0
 def test_01(self):
     """TestDuplexAdjacencyList: single add."""
     t = Tree.DuplexAdjacencyList()
     t.add('parent', 'child')
     self.assertEqual([
         'child',
     ], t.children('parent'))
     self.assertEqual([
         'parent',
     ], t.parents('child'))
     self.assertEqual(['parent'], list(t.allParents))
     self.assertEqual(['child'], list(t.allChildren))
Example #15
0
 def test_03(self):
     """TestDuplexAdjacencyList: simple cycle."""
     t = Tree.DuplexAdjacencyList()
     t.add('A', 'A')
     self.assertEqual([
         'A',
     ], t.children('A'))
     self.assertEqual([
         'A',
     ], t.parents('A'))
     #         print(t.treeParentChild('A').branches())
     self.assertEqual(
         [
             ['A'],
         ],
         t.treeParentChild('A').branches(),
     )
Example #16
0
 def test_treeChildParent(self):
     """TestDuplexAdjacencyList: Tree with a cycle."""
     t = Tree.DuplexAdjacencyList()
     t.add('a', 'b')
     t.add('b', 'c')
     t.add('c', 'a')
     exp = {
         'a': [['a'], ['a', 'c'], ['a', 'c', 'b']],
         'b': [['b'], ['b', 'a'], ['b', 'a', 'c']],
         'c': [['c'], ['c', 'b'], ['c', 'b', 'a']],
     }
     # print()
     # pprint.pprint(t.treeChildParent('a').branches())
     # pprint.pprint(t.treeChildParent('b').branches())
     # pprint.pprint(t.treeChildParent('c').branches())
     for k in exp:
         self.assertEqual(exp[k], t.treeChildParent(k).branches())
Example #17
0
 def test_10(self):
     """TestDuplexAdjacencyList: Tree with no cycles."""
     t = Tree.DuplexAdjacencyList()
     t.add('A', 'AA')
     t.add('A', 'AB')
     t.add('AA', 'AAA')
     t.add('AA', 'AAB')
     t.add('AB', 'ABA')
     t.add('AB', 'ABB')
     #         pprint.pprint(t.treeParentChild('A').branches())
     self.assertEqual(
         [
             ['A'],
             ['A', 'AA'],
             ['A', 'AA', 'AAA'],
             ['A', 'AA', 'AAB'],
             ['A', 'AB'],
             ['A', 'AB', 'ABA'],
             ['A', 'AB', 'ABB'],
         ],
         t.treeParentChild('A').branches(),
     )
Example #18
0
 def test_04(self):
     """TestDuplexAdjacencyList: cycle A -> B -> A."""
     t = Tree.DuplexAdjacencyList()
     t.add('A', 'B')
     t.add('B', 'A')
     self.assertEqual([
         'B',
     ], t.children('A'))
     self.assertEqual([
         'A',
     ], t.parents('B'))
     #         print(t.treeParentChild('A').branches())
     self.assertEqual(
         [
             [
                 'A',
             ],
             [
                 'A',
                 'B',
             ],
         ],
         t.treeParentChild('A').branches(),
     )
     self.assertEqual(
         [
             [
                 'B',
             ],
             [
                 'B',
                 'A',
             ],
         ],
         t.treeParentChild('B').branches(),
     )
Example #19
0
 def test_02(self):
     """TestTreeCtor: test_00(): simple object has len == 0."""
     t = Tree.Tree('A')
     self.assertEquals(0, len(t))
Example #20
0
 def test_00(self):
     """TestTreeCtor: test_00(): constructor."""
     t = Tree.Tree('A')
     self.assertEquals('A', t.obj)
     self.assertEquals(0, len(t))