Coverage for commands\test_convertCommands.py: 100%
94 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.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 'event': 'Event',
101 'i': 'int',
102 'j': 'int',
103 'k': 'int',
104 'n': 'int',
105 'p': 'Pos',
106 's': 'str',
107 'v': 'VNode',
108 }
110 #@+others
111 #@+node:ekr.20220108091352.1: *3* test_ama.test_already_annotated
112 def test_already_annotated(self):
113 p = self.p
114 p.b = contents = textwrap.dedent('''\
115 def f1(i: int, s: str) -> str:
116 return s
118 def f2(self, c: Cmdr, g: Any, ivars: List[str]) -> Any:
119 pass
120 ''')
121 self.x.convert_body(p)
122 self.assertEqual(p.b, contents)
123 #@+node:ekr.20220416053117.1: *3* test_ama.test_bug_2606
124 def test_bug_2606(self):
125 # https://github.com/leo-editor/leo-editor/issues/2606
126 p = self.p
127 # Make sure any adjustment to the args logic doesn't affect following functions.
128 p.b = textwrap.dedent('''\
129 def f1(root=p and p.copy()):
130 pass
132 def f2(n=1, f=0.1):
133 pass
135 def f3(a, self=self):
136 pass
137 ''')
138 expected = textwrap.dedent('''\
139 def f1(root: Any=p and p.copy()) -> None:
140 pass
142 def f2(n: int=1, f: float=0.1) -> None:
143 pass
145 def f3(a: Any, self=self) -> None:
146 pass
147 ''')
148 self.x.convert_body(p)
149 self.assertEqual(p.b, expected)
150 #@+node:ekr.20220108093044.1: *3* test_ama.test_initializers
151 def test_initializers(self):
152 p = self.p
153 p.b = textwrap.dedent('''\
154 def f3(i = 2, f = 1.1, b = True, s = 'abc', x = None):
155 pass
156 ''')
157 expected = textwrap.dedent('''\
158 def f3(i: int=2, f: float=1.1, b: bool=True, s: str='abc', x: Any=None) -> None:
159 pass
160 ''')
161 self.x.convert_body(p)
162 self.assertEqual(p.b, expected)
163 #@+node:ekr.20220108093621.1: *3* test_ama.test_multiline_def
164 def test_multiline_def(self):
165 p = self.p
166 p.b = textwrap.dedent('''\
167 def f (
168 self,
169 a,
170 b=1,
171 c = 2 ,
172 *args,
173 **kwargs,
174 ):
175 pass
176 ''')
177 expected = textwrap.dedent('''\
178 def f(
179 self,
180 a: Any,
181 b: int=1,
182 c: int=2,
183 *args: Any,
184 **kwargs: Any,
185 ) -> None:
186 pass
187 ''')
188 self.x.convert_body(p)
189 self.assertEqual(p.b, expected)
190 #@+node:ekr.20220108153333.1: *3* test_ama.test_multiline_def_with_comments
191 def test_multiline_def_with_comments(self):
192 p = self.p
193 p.b = textwrap.dedent('''\
194 def f (
195 self,# comment 1
196 a, # comment, 2
197 b=1,
198 d=2, # comment with trailing comma,
199 e=3,
200 ):
201 pass
202 ''')
203 # Note: The command insert exactly two spaces before comments.
204 expected = textwrap.dedent('''\
205 def f(
206 self, # comment 1
207 a: Any, # comment, 2
208 b: int=1,
209 d: int=2, # comment with trailing comma,
210 e: int=3,
211 ) -> None:
212 pass
213 ''')
214 self.x.convert_body(p)
215 # g.printObj(p.b)
216 self.assertEqual(p.b, expected)
217 #@+node:ekr.20220108083112.4: *3* test_ama.test_plain_args
218 def test_plain_args(self):
219 p = self.p
220 p.b = textwrap.dedent('''\
221 def f1(event, i, s):
222 pass
223 ''')
224 expected = textwrap.dedent('''\
225 def f1(event: Event, i: int, s: str) -> None:
226 pass
227 ''')
228 self.x.convert_body(p)
229 self.assertEqual(p.b, expected)
230 #@+node:ekr.20220416082758.1: *3* test_ama.test_special_methods
231 def test_special_methods(self):
232 p = self.p
233 p.b = textwrap.dedent('''\
234 def __init__(self):
235 pass
237 def __repr__(self):
238 pass
240 def __str__(self):
241 pass
242 ''')
243 expected = textwrap.dedent('''\
244 def __init__(self) -> None:
245 pass
247 def __repr__(self) -> str:
248 pass
250 def __str__(self) -> str:
251 pass
252 ''')
253 self.x.convert_body(p)
254 self.assertEqual(p.b, expected)
255 #@-others
256#@-others
259#@-leo