Coverage for commands\test_convertCommands.py : 100%

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.20211013081056.1: * @file ../unittests/commands/test_convertCommands.py
4#@@first
5"""Tests of leo.commands.leoConvertCommands."""
6import os
7import re
8import textwrap
9from leo.core import leoGlobals as g
10from leo.core.leoTest2 import LeoUnitTest
12#@+others
13#@+node:ekr.20211013081200.1: ** class TestPythonToTypeScript(LeoUnitTest):
14class TestPythonToTypeScript(LeoUnitTest):
15 """Test cases for python-to-typescript command"""
17 #@+others
18 #@+node:ekr.20211013090653.1: *3* test_py2ts.setUp
19 def setUp(self):
20 super().setUp()
21 c = self.c
22 self.x = c.convertCommands.PythonToTypescript(c)
23 self.assertTrue(hasattr(self.x, 'convert'))
24 root = self.root_p
25 # Delete all children
26 root.deleteAllChildren()
27 # Read leo.core.leoNodes into contents.
28 unittest_dir = os.path.dirname(__file__)
29 core_dir = os.path.abspath(os.path.join(unittest_dir, '..', '..', 'core'))
30 path = os.path.join(core_dir, 'leoNodes.py')
31 with open(path) as f:
32 contents = f.read()
33 # Set the gnx of the @file nodes in the contents to root.gnx.
34 # This is necessary because of a check in fast_at.scan_lines.
35 pat = re.compile(r'^\s*#@\+node:([^:]+): \* @file leoNodes\.py$')
36 line3 = g.splitLines(contents)[2]
37 m = pat.match(line3)
38 assert m, "Can not replace gnx"
39 contents = contents.replace(m.group(1), root.gnx)
40 # Replace c's outline with leoNodes.py.
41 gnx2vnode = {}
42 ok = c.atFileCommands.fast_read_into_root(c, contents, gnx2vnode, path, root)
43 self.assertTrue(ok)
44 root.h = 'leoNodes.py'
45 self.p = root
46 c.selectPosition(self.p)
47 #@+node:ekr.20211013081200.2: *3* test_py2ts.test_setup
48 def test_setup(self):
49 c = self.c
50 assert self.x
51 assert self.p
52 if 0:
53 self.dump_tree()
54 if 0:
55 for p in c.all_positions():
56 g.printObj(p.b, tag=p.h)
57 #@+node:ekr.20211013085659.1: *3* test_py2ts.test_convert_position_class
58 def test_convert_position_class(self):
59 # Convert a copy of the Position class
60 self.x.convert(self.p)
61 #@+node:ekr.20211021075411.1: *3* test_py2ts.test_do_f_strings()
62 def test_do_f_strings(self):
64 x = self.x
65 tests = (
66 (
67 'g.es(f"{timestamp}created: {fileName}")\n',
68 'g.es(`${timestamp}created: ${fileName}`)\n',
69 ),
70 (
71 'g.es(f"read {len(files)} files in {t2 - t1:2.2f} seconds")\n',
72 'g.es(`read ${len(files)} files in ${t2 - t1} seconds`)\n',
73 ),
74 (
75 'print(f"s: {s!r}")\n',
76 'print(`s: ${s}`)\n',
77 ),
78 )
79 for test in tests:
80 source, expected = test
81 lines = [source]
82 x.do_f_strings(lines)
83 self.assertEqual(lines[-1], expected)
84 #@-others
85#@+node:ekr.20220108083112.1: ** class TestAddMypyAnnotations(LeoUnitTest):
86class TestAddMypyAnnotations(LeoUnitTest):
87 """Test cases for add-mypy-annotations command"""
89 def setUp(self):
90 super().setUp()
91 # print(self.id())
92 c = self.c
93 self.p = self.root_p
94 self.x = c.convertCommands.Add_Mypy_Annotations(c)
95 self.x.types_d = {
96 'c': 'Cmdr',
97 'ch': 'str',
98 'gnx': 'str',
99 'd': 'Dict[str, str]',
100 'i': 'int',
101 'j': 'int',
102 'k': 'int',
103 'n': 'int',
104 'p': 'Pos',
105 's': 'str',
106 'v': 'VNode',
107 }
109 #@+others
110 #@+node:ekr.20220108083112.4: *3* test_ama.test_plain_args
111 def test_plain_args(self):
112 p = self.p
113 p.b = textwrap.dedent('''\
114 def f1(i, s):
115 pass
116 ''')
117 expected = textwrap.dedent('''\
118 def f1(i: int, s: str) -> Any:
119 pass
120 ''')
121 self.x.convert_body(p)
122 self.assertEqual(p.b, expected)
123 #@+node:ekr.20220108091352.1: *3* test_ama.test_already_annotated
124 def test_already_annotated(self):
125 p = self.p
126 p.b = contents = textwrap.dedent('''\
127 def f1(i: int, s: str) -> str:
128 return s
130 def f2(self, c: Cmdr, g: Any, ivars: List[str]) -> Any:
131 pass
132 ''')
133 self.x.convert_body(p)
134 self.assertEqual(p.b, contents)
135 #@+node:ekr.20220108093044.1: *3* test_ama.test_initializers
136 def test_initializers(self):
137 p = self.p
138 p.b = textwrap.dedent('''\
139 def f3(i = 2, f = 1.1, b = True, s = 'abc', x = None):
140 pass
141 ''')
142 expected = textwrap.dedent('''\
143 def f3(i: int=2, f: float=1.1, b: bool=True, s: str='abc', x: Any=None) -> Any:
144 pass
145 ''')
146 self.x.convert_body(p)
147 self.assertEqual(p.b, expected)
148 #@+node:ekr.20220108093621.1: *3* test_ama.test_multiline_def
149 def test_multiline_def(self):
150 p = self.p
151 p.b = textwrap.dedent('''\
152 def f (
153 self,
154 a,
155 b=1,
156 c = 2 ,
157 *args,
158 **kwargs,
159 ):
160 pass
161 ''')
162 expected = textwrap.dedent('''\
163 def f(
164 self,
165 a: Any,
166 b: int=1,
167 c: int=2,
168 *args: Any,
169 **kwargs: Any,
170 ) -> Any:
171 pass
172 ''')
173 self.x.convert_body(p)
174 self.assertEqual(p.b, expected)
175 #@+node:ekr.20220108153333.1: *3* test_ama.test_multiline_def_with_comments
176 def test_multiline_def_with_comments(self):
177 p = self.p
178 p.b = textwrap.dedent('''\
179 def f (
180 self,# comment 1
181 a, # comment, 2
182 b=1,
183 d=2, # comment with trailing comma,
184 e=3,
185 ):
186 pass
187 ''')
188 # Note: The command insert exactly two spaces before comments.
189 expected = textwrap.dedent('''\
190 def f(
191 self, # comment 1
192 a: Any, # comment, 2
193 b: int=1,
194 d: int=2, # comment with trailing comma,
195 e: int=3,
196 ) -> Any:
197 pass
198 ''')
199 self.x.convert_body(p)
200 # g.printObj(p.b)
201 self.assertEqual(p.b, expected)
202 #@-others
203#@-others
206#@-leo