Coverage for core\test_leoFileCommands.py : 97%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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.parent().children_iter())):
65 val = z.archivedPosition(root_p=p.parent())
66 self.assertEqual(val, [0, i])
67 for i, z in enumerate(list(p.children_iter())):
68 val = z.archivedPosition(root_p=p)
69 self.assertEqual(val, [0, i])
70 for i, z in enumerate(list(p.firstChild().next().children_iter())):
71 val = z.archivedPosition(root_p=p)
72 self.assertEqual(val, [0, 1, i])
73 #@+node:ekr.20210909194336.38: *3* TestFileCommands.test_putDescendentVnodeUas
74 def test_putDescendentVnodeUas(self):
75 c, root = self.c, self.root_p
76 fc = c.fileCommands
77 # Create the test tree. Headlines don't matter.
78 child1 = root.insertAsLastChild()
79 child2 = root.insertAsLastChild()
80 grandchild2 = child2.insertAsLastChild()
81 # Set the uA's.
82 child1.v.unknownAttributes = {'unit_test_child': 'abcd'}
83 grandchild2.v.unknownAttributes = {'unit_test_grandchild': 'wxyz'}
84 # Test.
85 s = fc.putDescendentVnodeUas(root)
86 assert s.startswith(' descendentVnodeUnknownAttributes='), s
87 #@+node:ekr.20210909194336.39: *4* child
88 #@+node:ekr.20210909194336.40: *5* grandChild
89 #@+node:ekr.20210909194336.41: *3* TestFileCommands.test_putUa
90 def test_putUa(self):
91 c, p = self.c, self.c.p
92 fc = c.fileCommands
93 p.v.unknownAttributes = {'unit_test': 'abcd'}
94 s = fc.putUnknownAttributes(p.v)
95 expected = ' unit_test="58040000006162636471002e"'
96 self.assertEqual(s, expected)
97 #@+node:ekr.20210905052021.32: *3* TestFileCommands.test_fast_readWithElementTree
98 def test_fast_readWithElementTree(self):
99 # Test that readWithElementTree strips all control characters except '\t\r\n'.
100 c = self.c
101 s = chr(0) + 'a' + chr(12) + 'b' + '\t\r\n' + 'c'
102 self.assertEqual(len(s), 8)
103 d = leoFileCommands.FastRead(c, {}).translate_dict
104 s2 = s.translate(d)
105 self.assertEqual(s2, 'ab\t\r\nc')
106 #@-others
107#@-others
108#@-leo