1 | n/a | # Argument Clinic |
---|
2 | n/a | # Copyright 2012-2013 by Larry Hastings. |
---|
3 | n/a | # Licensed to the PSF under a contributor agreement. |
---|
4 | n/a | # |
---|
5 | n/a | |
---|
6 | n/a | import clinic |
---|
7 | n/a | from clinic import DSLParser |
---|
8 | n/a | import collections |
---|
9 | n/a | import inspect |
---|
10 | n/a | from test import support |
---|
11 | n/a | import sys |
---|
12 | n/a | import unittest |
---|
13 | n/a | from unittest import TestCase |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | class FakeConverter: |
---|
17 | n/a | def __init__(self, name, args): |
---|
18 | n/a | self.name = name |
---|
19 | n/a | self.args = args |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | class FakeConverterFactory: |
---|
23 | n/a | def __init__(self, name): |
---|
24 | n/a | self.name = name |
---|
25 | n/a | |
---|
26 | n/a | def __call__(self, name, default, **kwargs): |
---|
27 | n/a | return FakeConverter(self.name, kwargs) |
---|
28 | n/a | |
---|
29 | n/a | |
---|
30 | n/a | class FakeConvertersDict: |
---|
31 | n/a | def __init__(self): |
---|
32 | n/a | self.used_converters = {} |
---|
33 | n/a | |
---|
34 | n/a | def get(self, name, default): |
---|
35 | n/a | return self.used_converters.setdefault(name, FakeConverterFactory(name)) |
---|
36 | n/a | |
---|
37 | n/a | clinic.Clinic.presets_text = '' |
---|
38 | n/a | c = clinic.Clinic(language='C') |
---|
39 | n/a | |
---|
40 | n/a | class FakeClinic: |
---|
41 | n/a | def __init__(self): |
---|
42 | n/a | self.converters = FakeConvertersDict() |
---|
43 | n/a | self.legacy_converters = FakeConvertersDict() |
---|
44 | n/a | self.language = clinic.CLanguage(None) |
---|
45 | n/a | self.filename = None |
---|
46 | n/a | self.block_parser = clinic.BlockParser('', self.language) |
---|
47 | n/a | self.modules = collections.OrderedDict() |
---|
48 | n/a | self.classes = collections.OrderedDict() |
---|
49 | n/a | clinic.clinic = self |
---|
50 | n/a | self.name = "FakeClinic" |
---|
51 | n/a | self.line_prefix = self.line_suffix = '' |
---|
52 | n/a | self.destinations = {} |
---|
53 | n/a | self.add_destination("block", "buffer") |
---|
54 | n/a | self.add_destination("file", "buffer") |
---|
55 | n/a | self.add_destination("suppress", "suppress") |
---|
56 | n/a | d = self.destinations.get |
---|
57 | n/a | self.field_destinations = collections.OrderedDict(( |
---|
58 | n/a | ('docstring_prototype', d('suppress')), |
---|
59 | n/a | ('docstring_definition', d('block')), |
---|
60 | n/a | ('methoddef_define', d('block')), |
---|
61 | n/a | ('impl_prototype', d('block')), |
---|
62 | n/a | ('parser_prototype', d('suppress')), |
---|
63 | n/a | ('parser_definition', d('block')), |
---|
64 | n/a | ('impl_definition', d('block')), |
---|
65 | n/a | )) |
---|
66 | n/a | |
---|
67 | n/a | def get_destination(self, name): |
---|
68 | n/a | d = self.destinations.get(name) |
---|
69 | n/a | if not d: |
---|
70 | n/a | sys.exit("Destination does not exist: " + repr(name)) |
---|
71 | n/a | return d |
---|
72 | n/a | |
---|
73 | n/a | def add_destination(self, name, type, *args): |
---|
74 | n/a | if name in self.destinations: |
---|
75 | n/a | sys.exit("Destination already exists: " + repr(name)) |
---|
76 | n/a | self.destinations[name] = clinic.Destination(name, type, self, *args) |
---|
77 | n/a | |
---|
78 | n/a | def is_directive(self, name): |
---|
79 | n/a | return name == "module" |
---|
80 | n/a | |
---|
81 | n/a | def directive(self, name, args): |
---|
82 | n/a | self.called_directives[name] = args |
---|
83 | n/a | |
---|
84 | n/a | _module_and_class = clinic.Clinic._module_and_class |
---|
85 | n/a | |
---|
86 | n/a | class ClinicWholeFileTest(TestCase): |
---|
87 | n/a | def test_eol(self): |
---|
88 | n/a | # regression test: |
---|
89 | n/a | # clinic's block parser didn't recognize |
---|
90 | n/a | # the "end line" for the block if it |
---|
91 | n/a | # didn't end in "\n" (as in, the last) |
---|
92 | n/a | # byte of the file was '/'. |
---|
93 | n/a | # so it would spit out an end line for you. |
---|
94 | n/a | # and since you really already had one, |
---|
95 | n/a | # the last line of the block got corrupted. |
---|
96 | n/a | c = clinic.Clinic(clinic.CLanguage(None)) |
---|
97 | n/a | raw = "/*[clinic]\nfoo\n[clinic]*/" |
---|
98 | n/a | cooked = c.parse(raw).splitlines() |
---|
99 | n/a | end_line = cooked[2].rstrip() |
---|
100 | n/a | # this test is redundant, it's just here explicitly to catch |
---|
101 | n/a | # the regression test so we don't forget what it looked like |
---|
102 | n/a | self.assertNotEqual(end_line, "[clinic]*/[clinic]*/") |
---|
103 | n/a | self.assertEqual(end_line, "[clinic]*/") |
---|
104 | n/a | |
---|
105 | n/a | |
---|
106 | n/a | |
---|
107 | n/a | class ClinicGroupPermuterTest(TestCase): |
---|
108 | n/a | def _test(self, l, m, r, output): |
---|
109 | n/a | computed = clinic.permute_optional_groups(l, m, r) |
---|
110 | n/a | self.assertEqual(output, computed) |
---|
111 | n/a | |
---|
112 | n/a | def test_range(self): |
---|
113 | n/a | self._test([['start']], ['stop'], [['step']], |
---|
114 | n/a | ( |
---|
115 | n/a | ('stop',), |
---|
116 | n/a | ('start', 'stop',), |
---|
117 | n/a | ('start', 'stop', 'step',), |
---|
118 | n/a | )) |
---|
119 | n/a | |
---|
120 | n/a | def test_add_window(self): |
---|
121 | n/a | self._test([['x', 'y']], ['ch'], [['attr']], |
---|
122 | n/a | ( |
---|
123 | n/a | ('ch',), |
---|
124 | n/a | ('ch', 'attr'), |
---|
125 | n/a | ('x', 'y', 'ch',), |
---|
126 | n/a | ('x', 'y', 'ch', 'attr'), |
---|
127 | n/a | )) |
---|
128 | n/a | |
---|
129 | n/a | def test_ludicrous(self): |
---|
130 | n/a | self._test([['a1', 'a2', 'a3'], ['b1', 'b2']], ['c1'], [['d1', 'd2'], ['e1', 'e2', 'e3']], |
---|
131 | n/a | ( |
---|
132 | n/a | ('c1',), |
---|
133 | n/a | ('b1', 'b2', 'c1'), |
---|
134 | n/a | ('b1', 'b2', 'c1', 'd1', 'd2'), |
---|
135 | n/a | ('a1', 'a2', 'a3', 'b1', 'b2', 'c1'), |
---|
136 | n/a | ('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2'), |
---|
137 | n/a | ('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2', 'e1', 'e2', 'e3'), |
---|
138 | n/a | )) |
---|
139 | n/a | |
---|
140 | n/a | def test_right_only(self): |
---|
141 | n/a | self._test([], [], [['a'],['b'],['c']], |
---|
142 | n/a | ( |
---|
143 | n/a | (), |
---|
144 | n/a | ('a',), |
---|
145 | n/a | ('a', 'b'), |
---|
146 | n/a | ('a', 'b', 'c') |
---|
147 | n/a | )) |
---|
148 | n/a | |
---|
149 | n/a | def test_have_left_options_but_required_is_empty(self): |
---|
150 | n/a | def fn(): |
---|
151 | n/a | clinic.permute_optional_groups(['a'], [], []) |
---|
152 | n/a | self.assertRaises(AssertionError, fn) |
---|
153 | n/a | |
---|
154 | n/a | |
---|
155 | n/a | class ClinicLinearFormatTest(TestCase): |
---|
156 | n/a | def _test(self, input, output, **kwargs): |
---|
157 | n/a | computed = clinic.linear_format(input, **kwargs) |
---|
158 | n/a | self.assertEqual(output, computed) |
---|
159 | n/a | |
---|
160 | n/a | def test_empty_strings(self): |
---|
161 | n/a | self._test('', '') |
---|
162 | n/a | |
---|
163 | n/a | def test_solo_newline(self): |
---|
164 | n/a | self._test('\n', '\n') |
---|
165 | n/a | |
---|
166 | n/a | def test_no_substitution(self): |
---|
167 | n/a | self._test(""" |
---|
168 | n/a | abc |
---|
169 | n/a | """, """ |
---|
170 | n/a | abc |
---|
171 | n/a | """) |
---|
172 | n/a | |
---|
173 | n/a | def test_empty_substitution(self): |
---|
174 | n/a | self._test(""" |
---|
175 | n/a | abc |
---|
176 | n/a | {name} |
---|
177 | n/a | def |
---|
178 | n/a | """, """ |
---|
179 | n/a | abc |
---|
180 | n/a | def |
---|
181 | n/a | """, name='') |
---|
182 | n/a | |
---|
183 | n/a | def test_single_line_substitution(self): |
---|
184 | n/a | self._test(""" |
---|
185 | n/a | abc |
---|
186 | n/a | {name} |
---|
187 | n/a | def |
---|
188 | n/a | """, """ |
---|
189 | n/a | abc |
---|
190 | n/a | GARGLE |
---|
191 | n/a | def |
---|
192 | n/a | """, name='GARGLE') |
---|
193 | n/a | |
---|
194 | n/a | def test_multiline_substitution(self): |
---|
195 | n/a | self._test(""" |
---|
196 | n/a | abc |
---|
197 | n/a | {name} |
---|
198 | n/a | def |
---|
199 | n/a | """, """ |
---|
200 | n/a | abc |
---|
201 | n/a | bingle |
---|
202 | n/a | bungle |
---|
203 | n/a | |
---|
204 | n/a | def |
---|
205 | n/a | """, name='bingle\nbungle\n') |
---|
206 | n/a | |
---|
207 | n/a | class InertParser: |
---|
208 | n/a | def __init__(self, clinic): |
---|
209 | n/a | pass |
---|
210 | n/a | |
---|
211 | n/a | def parse(self, block): |
---|
212 | n/a | pass |
---|
213 | n/a | |
---|
214 | n/a | class CopyParser: |
---|
215 | n/a | def __init__(self, clinic): |
---|
216 | n/a | pass |
---|
217 | n/a | |
---|
218 | n/a | def parse(self, block): |
---|
219 | n/a | block.output = block.input |
---|
220 | n/a | |
---|
221 | n/a | |
---|
222 | n/a | class ClinicBlockParserTest(TestCase): |
---|
223 | n/a | def _test(self, input, output): |
---|
224 | n/a | language = clinic.CLanguage(None) |
---|
225 | n/a | |
---|
226 | n/a | blocks = list(clinic.BlockParser(input, language)) |
---|
227 | n/a | writer = clinic.BlockPrinter(language) |
---|
228 | n/a | for block in blocks: |
---|
229 | n/a | writer.print_block(block) |
---|
230 | n/a | output = writer.f.getvalue() |
---|
231 | n/a | assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input) |
---|
232 | n/a | |
---|
233 | n/a | def round_trip(self, input): |
---|
234 | n/a | return self._test(input, input) |
---|
235 | n/a | |
---|
236 | n/a | def test_round_trip_1(self): |
---|
237 | n/a | self.round_trip(""" |
---|
238 | n/a | verbatim text here |
---|
239 | n/a | lah dee dah |
---|
240 | n/a | """) |
---|
241 | n/a | def test_round_trip_2(self): |
---|
242 | n/a | self.round_trip(""" |
---|
243 | n/a | verbatim text here |
---|
244 | n/a | lah dee dah |
---|
245 | n/a | /*[inert] |
---|
246 | n/a | abc |
---|
247 | n/a | [inert]*/ |
---|
248 | n/a | def |
---|
249 | n/a | /*[inert checksum: 7b18d017f89f61cf17d47f92749ea6930a3f1deb]*/ |
---|
250 | n/a | xyz |
---|
251 | n/a | """) |
---|
252 | n/a | |
---|
253 | n/a | def _test_clinic(self, input, output): |
---|
254 | n/a | language = clinic.CLanguage(None) |
---|
255 | n/a | c = clinic.Clinic(language) |
---|
256 | n/a | c.parsers['inert'] = InertParser(c) |
---|
257 | n/a | c.parsers['copy'] = CopyParser(c) |
---|
258 | n/a | computed = c.parse(input) |
---|
259 | n/a | self.assertEqual(output, computed) |
---|
260 | n/a | |
---|
261 | n/a | def test_clinic_1(self): |
---|
262 | n/a | self._test_clinic(""" |
---|
263 | n/a | verbatim text here |
---|
264 | n/a | lah dee dah |
---|
265 | n/a | /*[copy input] |
---|
266 | n/a | def |
---|
267 | n/a | [copy start generated code]*/ |
---|
268 | n/a | abc |
---|
269 | n/a | /*[copy end generated code: output=03cfd743661f0797 input=7b18d017f89f61cf]*/ |
---|
270 | n/a | xyz |
---|
271 | n/a | """, """ |
---|
272 | n/a | verbatim text here |
---|
273 | n/a | lah dee dah |
---|
274 | n/a | /*[copy input] |
---|
275 | n/a | def |
---|
276 | n/a | [copy start generated code]*/ |
---|
277 | n/a | def |
---|
278 | n/a | /*[copy end generated code: output=7b18d017f89f61cf input=7b18d017f89f61cf]*/ |
---|
279 | n/a | xyz |
---|
280 | n/a | """) |
---|
281 | n/a | |
---|
282 | n/a | |
---|
283 | n/a | class ClinicParserTest(TestCase): |
---|
284 | n/a | def test_trivial(self): |
---|
285 | n/a | parser = DSLParser(FakeClinic()) |
---|
286 | n/a | block = clinic.Block("module os\nos.access") |
---|
287 | n/a | parser.parse(block) |
---|
288 | n/a | module, function = block.signatures |
---|
289 | n/a | self.assertEqual("access", function.name) |
---|
290 | n/a | self.assertEqual("os", module.name) |
---|
291 | n/a | |
---|
292 | n/a | def test_ignore_line(self): |
---|
293 | n/a | block = self.parse("#\nmodule os\nos.access") |
---|
294 | n/a | module, function = block.signatures |
---|
295 | n/a | self.assertEqual("access", function.name) |
---|
296 | n/a | self.assertEqual("os", module.name) |
---|
297 | n/a | |
---|
298 | n/a | def test_param(self): |
---|
299 | n/a | function = self.parse_function("module os\nos.access\n path: int") |
---|
300 | n/a | self.assertEqual("access", function.name) |
---|
301 | n/a | self.assertEqual(2, len(function.parameters)) |
---|
302 | n/a | p = function.parameters['path'] |
---|
303 | n/a | self.assertEqual('path', p.name) |
---|
304 | n/a | self.assertIsInstance(p.converter, clinic.int_converter) |
---|
305 | n/a | |
---|
306 | n/a | def test_param_default(self): |
---|
307 | n/a | function = self.parse_function("module os\nos.access\n follow_symlinks: bool = True") |
---|
308 | n/a | p = function.parameters['follow_symlinks'] |
---|
309 | n/a | self.assertEqual(True, p.default) |
---|
310 | n/a | |
---|
311 | n/a | def test_param_with_continuations(self): |
---|
312 | n/a | function = self.parse_function("module os\nos.access\n follow_symlinks: \\\n bool \\\n =\\\n True") |
---|
313 | n/a | p = function.parameters['follow_symlinks'] |
---|
314 | n/a | self.assertEqual(True, p.default) |
---|
315 | n/a | |
---|
316 | n/a | def test_param_default_expression(self): |
---|
317 | n/a | function = self.parse_function("module os\nos.access\n follow_symlinks: int(c_default='MAXSIZE') = sys.maxsize") |
---|
318 | n/a | p = function.parameters['follow_symlinks'] |
---|
319 | n/a | self.assertEqual(sys.maxsize, p.default) |
---|
320 | n/a | self.assertEqual("MAXSIZE", p.converter.c_default) |
---|
321 | n/a | |
---|
322 | n/a | s = self.parse_function_should_fail("module os\nos.access\n follow_symlinks: int = sys.maxsize") |
---|
323 | n/a | self.assertEqual(s, "Error on line 0:\nWhen you specify a named constant ('sys.maxsize') as your default value,\nyou MUST specify a valid c_default.\n") |
---|
324 | n/a | |
---|
325 | n/a | def test_param_no_docstring(self): |
---|
326 | n/a | function = self.parse_function(""" |
---|
327 | n/a | module os |
---|
328 | n/a | os.access |
---|
329 | n/a | follow_symlinks: bool = True |
---|
330 | n/a | something_else: str = ''""") |
---|
331 | n/a | p = function.parameters['follow_symlinks'] |
---|
332 | n/a | self.assertEqual(3, len(function.parameters)) |
---|
333 | n/a | self.assertIsInstance(function.parameters['something_else'].converter, clinic.str_converter) |
---|
334 | n/a | |
---|
335 | n/a | def test_param_default_parameters_out_of_order(self): |
---|
336 | n/a | s = self.parse_function_should_fail(""" |
---|
337 | n/a | module os |
---|
338 | n/a | os.access |
---|
339 | n/a | follow_symlinks: bool = True |
---|
340 | n/a | something_else: str""") |
---|
341 | n/a | self.assertEqual(s, """Error on line 0: |
---|
342 | n/a | Can't have a parameter without a default ('something_else') |
---|
343 | n/a | after a parameter with a default! |
---|
344 | n/a | """) |
---|
345 | n/a | |
---|
346 | n/a | def disabled_test_converter_arguments(self): |
---|
347 | n/a | function = self.parse_function("module os\nos.access\n path: path_t(allow_fd=1)") |
---|
348 | n/a | p = function.parameters['path'] |
---|
349 | n/a | self.assertEqual(1, p.converter.args['allow_fd']) |
---|
350 | n/a | |
---|
351 | n/a | def test_function_docstring(self): |
---|
352 | n/a | function = self.parse_function(""" |
---|
353 | n/a | module os |
---|
354 | n/a | os.stat as os_stat_fn |
---|
355 | n/a | |
---|
356 | n/a | path: str |
---|
357 | n/a | Path to be examined |
---|
358 | n/a | |
---|
359 | n/a | Perform a stat system call on the given path.""") |
---|
360 | n/a | self.assertEqual(""" |
---|
361 | n/a | stat($module, /, path) |
---|
362 | n/a | -- |
---|
363 | n/a | |
---|
364 | n/a | Perform a stat system call on the given path. |
---|
365 | n/a | |
---|
366 | n/a | path |
---|
367 | n/a | Path to be examined |
---|
368 | n/a | """.strip(), function.docstring) |
---|
369 | n/a | |
---|
370 | n/a | def test_explicit_parameters_in_docstring(self): |
---|
371 | n/a | function = self.parse_function(""" |
---|
372 | n/a | module foo |
---|
373 | n/a | foo.bar |
---|
374 | n/a | x: int |
---|
375 | n/a | Documentation for x. |
---|
376 | n/a | y: int |
---|
377 | n/a | |
---|
378 | n/a | This is the documentation for foo. |
---|
379 | n/a | |
---|
380 | n/a | Okay, we're done here. |
---|
381 | n/a | """) |
---|
382 | n/a | self.assertEqual(""" |
---|
383 | n/a | bar($module, /, x, y) |
---|
384 | n/a | -- |
---|
385 | n/a | |
---|
386 | n/a | This is the documentation for foo. |
---|
387 | n/a | |
---|
388 | n/a | x |
---|
389 | n/a | Documentation for x. |
---|
390 | n/a | |
---|
391 | n/a | Okay, we're done here. |
---|
392 | n/a | """.strip(), function.docstring) |
---|
393 | n/a | |
---|
394 | n/a | def test_parser_regression_special_character_in_parameter_column_of_docstring_first_line(self): |
---|
395 | n/a | function = self.parse_function(""" |
---|
396 | n/a | module os |
---|
397 | n/a | os.stat |
---|
398 | n/a | path: str |
---|
399 | n/a | This/used to break Clinic! |
---|
400 | n/a | """) |
---|
401 | n/a | self.assertEqual("stat($module, /, path)\n--\n\nThis/used to break Clinic!", function.docstring) |
---|
402 | n/a | |
---|
403 | n/a | def test_c_name(self): |
---|
404 | n/a | function = self.parse_function("module os\nos.stat as os_stat_fn") |
---|
405 | n/a | self.assertEqual("os_stat_fn", function.c_basename) |
---|
406 | n/a | |
---|
407 | n/a | def test_return_converter(self): |
---|
408 | n/a | function = self.parse_function("module os\nos.stat -> int") |
---|
409 | n/a | self.assertIsInstance(function.return_converter, clinic.int_return_converter) |
---|
410 | n/a | |
---|
411 | n/a | def test_star(self): |
---|
412 | n/a | function = self.parse_function("module os\nos.access\n *\n follow_symlinks: bool = True") |
---|
413 | n/a | p = function.parameters['follow_symlinks'] |
---|
414 | n/a | self.assertEqual(inspect.Parameter.KEYWORD_ONLY, p.kind) |
---|
415 | n/a | self.assertEqual(0, p.group) |
---|
416 | n/a | |
---|
417 | n/a | def test_group(self): |
---|
418 | n/a | function = self.parse_function("module window\nwindow.border\n [\n ls : int\n ]\n /\n") |
---|
419 | n/a | p = function.parameters['ls'] |
---|
420 | n/a | self.assertEqual(1, p.group) |
---|
421 | n/a | |
---|
422 | n/a | def test_left_group(self): |
---|
423 | n/a | function = self.parse_function(""" |
---|
424 | n/a | module curses |
---|
425 | n/a | curses.addch |
---|
426 | n/a | [ |
---|
427 | n/a | y: int |
---|
428 | n/a | Y-coordinate. |
---|
429 | n/a | x: int |
---|
430 | n/a | X-coordinate. |
---|
431 | n/a | ] |
---|
432 | n/a | ch: char |
---|
433 | n/a | Character to add. |
---|
434 | n/a | [ |
---|
435 | n/a | attr: long |
---|
436 | n/a | Attributes for the character. |
---|
437 | n/a | ] |
---|
438 | n/a | / |
---|
439 | n/a | """) |
---|
440 | n/a | for name, group in ( |
---|
441 | n/a | ('y', -1), ('x', -1), |
---|
442 | n/a | ('ch', 0), |
---|
443 | n/a | ('attr', 1), |
---|
444 | n/a | ): |
---|
445 | n/a | p = function.parameters[name] |
---|
446 | n/a | self.assertEqual(p.group, group) |
---|
447 | n/a | self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) |
---|
448 | n/a | self.assertEqual(function.docstring.strip(), """ |
---|
449 | n/a | addch([y, x,] ch, [attr]) |
---|
450 | n/a | |
---|
451 | n/a | |
---|
452 | n/a | y |
---|
453 | n/a | Y-coordinate. |
---|
454 | n/a | x |
---|
455 | n/a | X-coordinate. |
---|
456 | n/a | ch |
---|
457 | n/a | Character to add. |
---|
458 | n/a | attr |
---|
459 | n/a | Attributes for the character. |
---|
460 | n/a | """.strip()) |
---|
461 | n/a | |
---|
462 | n/a | def test_nested_groups(self): |
---|
463 | n/a | function = self.parse_function(""" |
---|
464 | n/a | module curses |
---|
465 | n/a | curses.imaginary |
---|
466 | n/a | [ |
---|
467 | n/a | [ |
---|
468 | n/a | y1: int |
---|
469 | n/a | Y-coordinate. |
---|
470 | n/a | y2: int |
---|
471 | n/a | Y-coordinate. |
---|
472 | n/a | ] |
---|
473 | n/a | x1: int |
---|
474 | n/a | X-coordinate. |
---|
475 | n/a | x2: int |
---|
476 | n/a | X-coordinate. |
---|
477 | n/a | ] |
---|
478 | n/a | ch: char |
---|
479 | n/a | Character to add. |
---|
480 | n/a | [ |
---|
481 | n/a | attr1: long |
---|
482 | n/a | Attributes for the character. |
---|
483 | n/a | attr2: long |
---|
484 | n/a | Attributes for the character. |
---|
485 | n/a | attr3: long |
---|
486 | n/a | Attributes for the character. |
---|
487 | n/a | [ |
---|
488 | n/a | attr4: long |
---|
489 | n/a | Attributes for the character. |
---|
490 | n/a | attr5: long |
---|
491 | n/a | Attributes for the character. |
---|
492 | n/a | attr6: long |
---|
493 | n/a | Attributes for the character. |
---|
494 | n/a | ] |
---|
495 | n/a | ] |
---|
496 | n/a | / |
---|
497 | n/a | """) |
---|
498 | n/a | for name, group in ( |
---|
499 | n/a | ('y1', -2), ('y2', -2), |
---|
500 | n/a | ('x1', -1), ('x2', -1), |
---|
501 | n/a | ('ch', 0), |
---|
502 | n/a | ('attr1', 1), ('attr2', 1), ('attr3', 1), |
---|
503 | n/a | ('attr4', 2), ('attr5', 2), ('attr6', 2), |
---|
504 | n/a | ): |
---|
505 | n/a | p = function.parameters[name] |
---|
506 | n/a | self.assertEqual(p.group, group) |
---|
507 | n/a | self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) |
---|
508 | n/a | |
---|
509 | n/a | self.assertEqual(function.docstring.strip(), """ |
---|
510 | n/a | imaginary([[y1, y2,] x1, x2,] ch, [attr1, attr2, attr3, [attr4, attr5, |
---|
511 | n/a | attr6]]) |
---|
512 | n/a | |
---|
513 | n/a | |
---|
514 | n/a | y1 |
---|
515 | n/a | Y-coordinate. |
---|
516 | n/a | y2 |
---|
517 | n/a | Y-coordinate. |
---|
518 | n/a | x1 |
---|
519 | n/a | X-coordinate. |
---|
520 | n/a | x2 |
---|
521 | n/a | X-coordinate. |
---|
522 | n/a | ch |
---|
523 | n/a | Character to add. |
---|
524 | n/a | attr1 |
---|
525 | n/a | Attributes for the character. |
---|
526 | n/a | attr2 |
---|
527 | n/a | Attributes for the character. |
---|
528 | n/a | attr3 |
---|
529 | n/a | Attributes for the character. |
---|
530 | n/a | attr4 |
---|
531 | n/a | Attributes for the character. |
---|
532 | n/a | attr5 |
---|
533 | n/a | Attributes for the character. |
---|
534 | n/a | attr6 |
---|
535 | n/a | Attributes for the character. |
---|
536 | n/a | """.strip()) |
---|
537 | n/a | |
---|
538 | n/a | def parse_function_should_fail(self, s): |
---|
539 | n/a | with support.captured_stdout() as stdout: |
---|
540 | n/a | with self.assertRaises(SystemExit): |
---|
541 | n/a | self.parse_function(s) |
---|
542 | n/a | return stdout.getvalue() |
---|
543 | n/a | |
---|
544 | n/a | def test_disallowed_grouping__two_top_groups_on_left(self): |
---|
545 | n/a | s = self.parse_function_should_fail(""" |
---|
546 | n/a | module foo |
---|
547 | n/a | foo.two_top_groups_on_left |
---|
548 | n/a | [ |
---|
549 | n/a | group1 : int |
---|
550 | n/a | ] |
---|
551 | n/a | [ |
---|
552 | n/a | group2 : int |
---|
553 | n/a | ] |
---|
554 | n/a | param: int |
---|
555 | n/a | """) |
---|
556 | n/a | self.assertEqual(s, |
---|
557 | n/a | ('Error on line 0:\n' |
---|
558 | n/a | 'Function two_top_groups_on_left has an unsupported group configuration. (Unexpected state 2.b)\n')) |
---|
559 | n/a | |
---|
560 | n/a | def test_disallowed_grouping__two_top_groups_on_right(self): |
---|
561 | n/a | self.parse_function_should_fail(""" |
---|
562 | n/a | module foo |
---|
563 | n/a | foo.two_top_groups_on_right |
---|
564 | n/a | param: int |
---|
565 | n/a | [ |
---|
566 | n/a | group1 : int |
---|
567 | n/a | ] |
---|
568 | n/a | [ |
---|
569 | n/a | group2 : int |
---|
570 | n/a | ] |
---|
571 | n/a | """) |
---|
572 | n/a | |
---|
573 | n/a | def test_disallowed_grouping__parameter_after_group_on_right(self): |
---|
574 | n/a | self.parse_function_should_fail(""" |
---|
575 | n/a | module foo |
---|
576 | n/a | foo.parameter_after_group_on_right |
---|
577 | n/a | param: int |
---|
578 | n/a | [ |
---|
579 | n/a | [ |
---|
580 | n/a | group1 : int |
---|
581 | n/a | ] |
---|
582 | n/a | group2 : int |
---|
583 | n/a | ] |
---|
584 | n/a | """) |
---|
585 | n/a | |
---|
586 | n/a | def test_disallowed_grouping__group_after_parameter_on_left(self): |
---|
587 | n/a | self.parse_function_should_fail(""" |
---|
588 | n/a | module foo |
---|
589 | n/a | foo.group_after_parameter_on_left |
---|
590 | n/a | [ |
---|
591 | n/a | group2 : int |
---|
592 | n/a | [ |
---|
593 | n/a | group1 : int |
---|
594 | n/a | ] |
---|
595 | n/a | ] |
---|
596 | n/a | param: int |
---|
597 | n/a | """) |
---|
598 | n/a | |
---|
599 | n/a | def test_disallowed_grouping__empty_group_on_left(self): |
---|
600 | n/a | self.parse_function_should_fail(""" |
---|
601 | n/a | module foo |
---|
602 | n/a | foo.empty_group |
---|
603 | n/a | [ |
---|
604 | n/a | [ |
---|
605 | n/a | ] |
---|
606 | n/a | group2 : int |
---|
607 | n/a | ] |
---|
608 | n/a | param: int |
---|
609 | n/a | """) |
---|
610 | n/a | |
---|
611 | n/a | def test_disallowed_grouping__empty_group_on_right(self): |
---|
612 | n/a | self.parse_function_should_fail(""" |
---|
613 | n/a | module foo |
---|
614 | n/a | foo.empty_group |
---|
615 | n/a | param: int |
---|
616 | n/a | [ |
---|
617 | n/a | [ |
---|
618 | n/a | ] |
---|
619 | n/a | group2 : int |
---|
620 | n/a | ] |
---|
621 | n/a | """) |
---|
622 | n/a | |
---|
623 | n/a | def test_no_parameters(self): |
---|
624 | n/a | function = self.parse_function(""" |
---|
625 | n/a | module foo |
---|
626 | n/a | foo.bar |
---|
627 | n/a | |
---|
628 | n/a | Docstring |
---|
629 | n/a | |
---|
630 | n/a | """) |
---|
631 | n/a | self.assertEqual("bar($module, /)\n--\n\nDocstring", function.docstring) |
---|
632 | n/a | self.assertEqual(1, len(function.parameters)) # self! |
---|
633 | n/a | |
---|
634 | n/a | def test_init_with_no_parameters(self): |
---|
635 | n/a | function = self.parse_function(""" |
---|
636 | n/a | module foo |
---|
637 | n/a | class foo.Bar "unused" "notneeded" |
---|
638 | n/a | foo.Bar.__init__ |
---|
639 | n/a | |
---|
640 | n/a | Docstring |
---|
641 | n/a | |
---|
642 | n/a | """, signatures_in_block=3, function_index=2) |
---|
643 | n/a | # self is not in the signature |
---|
644 | n/a | self.assertEqual("Bar()\n--\n\nDocstring", function.docstring) |
---|
645 | n/a | # but it *is* a parameter |
---|
646 | n/a | self.assertEqual(1, len(function.parameters)) |
---|
647 | n/a | |
---|
648 | n/a | def test_illegal_module_line(self): |
---|
649 | n/a | self.parse_function_should_fail(""" |
---|
650 | n/a | module foo |
---|
651 | n/a | foo.bar => int |
---|
652 | n/a | / |
---|
653 | n/a | """) |
---|
654 | n/a | |
---|
655 | n/a | def test_illegal_c_basename(self): |
---|
656 | n/a | self.parse_function_should_fail(""" |
---|
657 | n/a | module foo |
---|
658 | n/a | foo.bar as 935 |
---|
659 | n/a | / |
---|
660 | n/a | """) |
---|
661 | n/a | |
---|
662 | n/a | def test_single_star(self): |
---|
663 | n/a | self.parse_function_should_fail(""" |
---|
664 | n/a | module foo |
---|
665 | n/a | foo.bar |
---|
666 | n/a | * |
---|
667 | n/a | * |
---|
668 | n/a | """) |
---|
669 | n/a | |
---|
670 | n/a | def test_parameters_required_after_star_without_initial_parameters_or_docstring(self): |
---|
671 | n/a | self.parse_function_should_fail(""" |
---|
672 | n/a | module foo |
---|
673 | n/a | foo.bar |
---|
674 | n/a | * |
---|
675 | n/a | """) |
---|
676 | n/a | |
---|
677 | n/a | def test_parameters_required_after_star_without_initial_parameters_with_docstring(self): |
---|
678 | n/a | self.parse_function_should_fail(""" |
---|
679 | n/a | module foo |
---|
680 | n/a | foo.bar |
---|
681 | n/a | * |
---|
682 | n/a | Docstring here. |
---|
683 | n/a | """) |
---|
684 | n/a | |
---|
685 | n/a | def test_parameters_required_after_star_with_initial_parameters_without_docstring(self): |
---|
686 | n/a | self.parse_function_should_fail(""" |
---|
687 | n/a | module foo |
---|
688 | n/a | foo.bar |
---|
689 | n/a | this: int |
---|
690 | n/a | * |
---|
691 | n/a | """) |
---|
692 | n/a | |
---|
693 | n/a | def test_parameters_required_after_star_with_initial_parameters_and_docstring(self): |
---|
694 | n/a | self.parse_function_should_fail(""" |
---|
695 | n/a | module foo |
---|
696 | n/a | foo.bar |
---|
697 | n/a | this: int |
---|
698 | n/a | * |
---|
699 | n/a | Docstring. |
---|
700 | n/a | """) |
---|
701 | n/a | |
---|
702 | n/a | def test_single_slash(self): |
---|
703 | n/a | self.parse_function_should_fail(""" |
---|
704 | n/a | module foo |
---|
705 | n/a | foo.bar |
---|
706 | n/a | / |
---|
707 | n/a | / |
---|
708 | n/a | """) |
---|
709 | n/a | |
---|
710 | n/a | def test_mix_star_and_slash(self): |
---|
711 | n/a | self.parse_function_should_fail(""" |
---|
712 | n/a | module foo |
---|
713 | n/a | foo.bar |
---|
714 | n/a | x: int |
---|
715 | n/a | y: int |
---|
716 | n/a | * |
---|
717 | n/a | z: int |
---|
718 | n/a | / |
---|
719 | n/a | """) |
---|
720 | n/a | |
---|
721 | n/a | def test_parameters_not_permitted_after_slash_for_now(self): |
---|
722 | n/a | self.parse_function_should_fail(""" |
---|
723 | n/a | module foo |
---|
724 | n/a | foo.bar |
---|
725 | n/a | / |
---|
726 | n/a | x: int |
---|
727 | n/a | """) |
---|
728 | n/a | |
---|
729 | n/a | def test_function_not_at_column_0(self): |
---|
730 | n/a | function = self.parse_function(""" |
---|
731 | n/a | module foo |
---|
732 | n/a | foo.bar |
---|
733 | n/a | x: int |
---|
734 | n/a | Nested docstring here, goeth. |
---|
735 | n/a | * |
---|
736 | n/a | y: str |
---|
737 | n/a | Not at column 0! |
---|
738 | n/a | """) |
---|
739 | n/a | self.assertEqual(""" |
---|
740 | n/a | bar($module, /, x, *, y) |
---|
741 | n/a | -- |
---|
742 | n/a | |
---|
743 | n/a | Not at column 0! |
---|
744 | n/a | |
---|
745 | n/a | x |
---|
746 | n/a | Nested docstring here, goeth. |
---|
747 | n/a | """.strip(), function.docstring) |
---|
748 | n/a | |
---|
749 | n/a | def test_directive(self): |
---|
750 | n/a | c = FakeClinic() |
---|
751 | n/a | parser = DSLParser(c) |
---|
752 | n/a | parser.flag = False |
---|
753 | n/a | parser.directives['setflag'] = lambda : setattr(parser, 'flag', True) |
---|
754 | n/a | block = clinic.Block("setflag") |
---|
755 | n/a | parser.parse(block) |
---|
756 | n/a | self.assertTrue(parser.flag) |
---|
757 | n/a | |
---|
758 | n/a | def test_legacy_converters(self): |
---|
759 | n/a | block = self.parse('module os\nos.access\n path: "s"') |
---|
760 | n/a | module, function = block.signatures |
---|
761 | n/a | self.assertIsInstance((function.parameters['path']).converter, clinic.str_converter) |
---|
762 | n/a | |
---|
763 | n/a | def parse(self, text): |
---|
764 | n/a | c = FakeClinic() |
---|
765 | n/a | parser = DSLParser(c) |
---|
766 | n/a | block = clinic.Block(text) |
---|
767 | n/a | parser.parse(block) |
---|
768 | n/a | return block |
---|
769 | n/a | |
---|
770 | n/a | def parse_function(self, text, signatures_in_block=2, function_index=1): |
---|
771 | n/a | block = self.parse(text) |
---|
772 | n/a | s = block.signatures |
---|
773 | n/a | self.assertEqual(len(s), signatures_in_block) |
---|
774 | n/a | assert isinstance(s[0], clinic.Module) |
---|
775 | n/a | assert isinstance(s[function_index], clinic.Function) |
---|
776 | n/a | return s[function_index] |
---|
777 | n/a | |
---|
778 | n/a | def test_scaffolding(self): |
---|
779 | n/a | # test repr on special values |
---|
780 | n/a | self.assertEqual(repr(clinic.unspecified), '<Unspecified>') |
---|
781 | n/a | self.assertEqual(repr(clinic.NULL), '<Null>') |
---|
782 | n/a | |
---|
783 | n/a | # test that fail fails |
---|
784 | n/a | with support.captured_stdout() as stdout: |
---|
785 | n/a | with self.assertRaises(SystemExit): |
---|
786 | n/a | clinic.fail('The igloos are melting!', filename='clown.txt', line_number=69) |
---|
787 | n/a | self.assertEqual(stdout.getvalue(), 'Error in file "clown.txt" on line 69:\nThe igloos are melting!\n') |
---|
788 | n/a | |
---|
789 | n/a | |
---|
790 | n/a | if __name__ == "__main__": |
---|
791 | n/a | unittest.main() |
---|