Coverage for core\test_leoFileCommands.py: 100%
58 statements
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
1# -*- coding: utf-8 -*-
2#@+leo-ver=5-thin
3#@+node:ekr.20210910065135.1: * @file ../unittests/core/test_leoFileCommands.py
4#@@first
5"""
6Tests of leoFileCommands.py.
8test-file-commands runs these tests.
9"""
11import leo.core.leoFileCommands as leoFileCommands
12from leo.core.leoTest2 import LeoUnitTest
14#@+others
15#@+node:ekr.20210910065135.2: ** class TestFileCommands (LeoUnitTest)
16class TestFileCommands(LeoUnitTest):
17 #@+others
18 #@+node:ekr.20210909194336.24: *3* TestFileCommands.test_fc_resolveArchivedPosition
19 def test_fc_resolveArchivedPosition(self):
20 c, root = self.c, self.root_p
21 root_v = root.v
22 # Create the test tree. Headlines don't matter.
23 child1 = root.insertAsLastChild()
24 child2 = root.insertAsLastChild()
25 grandChild1 = child2.insertAsLastChild()
26 grandChild2 = child2.insertAsLastChild()
27 greatGrandChild11 = grandChild1.insertAsLastChild()
28 greatGrandChild12 = grandChild1.insertAsLastChild()
29 greatGrandChild21 = grandChild2.insertAsLastChild()
30 greatGrandChild22 = grandChild2.insertAsLastChild()
31 table = (
32 # Errors.
33 (None, '-1'),
34 (None, '1'),
35 (None, '0.2'),
36 (None, '0.0.0'),
37 (None, '0.1.2'),
38 # Valid.
39 (root_v, '0'),
40 (child1.v, '0.0'),
41 (child2.v, '0.1'),
42 (grandChild1.v, '0.1.0'),
43 (greatGrandChild11.v, '0.1.0.0'),
44 (greatGrandChild12.v, '0.1.0.1'),
45 (grandChild2.v, '0.1.1'),
46 (greatGrandChild21.v, '0.1.1.0'),
47 (greatGrandChild22.v, '0.1.1.1'),
48 )
49 for v, archivedPosition in table:
50 v2 = c.fileCommands.resolveArchivedPosition(archivedPosition, root_v)
51 self.assertEqual(v, v2)
52 #@+node:ekr.20210909194336.33: *3* TestFileCommands.test_p_archivedPosition
53 def test_p_archivedPosition(self):
54 p, root = self.c.p, self.root_p
55 # Create the test tree. Headlines don't matter.
56 child1 = root.insertAsLastChild()
57 child2 = root.insertAsLastChild()
58 grandChild1 = child2.insertAsLastChild()
59 grandChild2 = child2.insertAsLastChild()
60 assert child1 and grandChild1 and grandChild2
61 # Tests...
62 val = p.archivedPosition(root_p=p)
63 self.assertEqual(val, [0])
64 for i, z in enumerate(list(p.children_iter())):
65 val = z.archivedPosition(root_p=p)
66 self.assertEqual(val, [0, i])
67 for i, z in enumerate(list(p.firstChild().next().children_iter())):
68 val = z.archivedPosition(root_p=p)
69 self.assertEqual(val, [0, 1, i])
70 #@+node:ekr.20210909194336.38: *3* TestFileCommands.test_putDescendentVnodeUas
71 def test_putDescendentVnodeUas(self):
72 c, root = self.c, self.root_p
73 fc = c.fileCommands
74 # Create the test tree. Headlines don't matter.
75 child1 = root.insertAsLastChild()
76 child2 = root.insertAsLastChild()
77 grandchild2 = child2.insertAsLastChild()
78 # Set the uA's.
79 child1.v.unknownAttributes = {'unit_test_child': 'abcd'}
80 grandchild2.v.unknownAttributes = {'unit_test_grandchild': 'wxyz'}
81 # Test.
82 s = fc.putDescendentVnodeUas(root)
83 assert s.startswith(' descendentVnodeUnknownAttributes='), s
84 #@+node:ekr.20210909194336.39: *4* child
85 #@+node:ekr.20210909194336.40: *5* grandChild
86 #@+node:ekr.20210909194336.41: *3* TestFileCommands.test_putUa
87 def test_putUa(self):
88 c, p = self.c, self.c.p
89 fc = c.fileCommands
90 p.v.unknownAttributes = {'unit_test': 'abcd'}
91 s = fc.putUnknownAttributes(p.v)
92 expected = ' unit_test="58040000006162636471002e"'
93 self.assertEqual(s, expected)
94 #@+node:ekr.20210905052021.32: *3* TestFileCommands.test_fast_readWithElementTree
95 def test_fast_readWithElementTree(self):
96 # Test that readWithElementTree strips all control characters except '\t\r\n'.
97 c = self.c
98 s = chr(0) + 'a' + chr(12) + 'b' + '\t\r\n' + 'c'
99 self.assertEqual(len(s), 8)
100 d = leoFileCommands.FastRead(c, {}).translate_dict
101 s2 = s.translate(d)
102 self.assertEqual(s2, 'ab\t\r\nc')
103 #@-others
104#@-others
105#@-leo