mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-106368: Increase test coverage for Argument Clinic (#107514)
As per this commit, we've got approx. ~91% test coverage for clinic.py.
This commit is contained in:
parent
9ff7b4af13
commit
b9c9a36c2f
2 changed files with 350 additions and 15 deletions
|
@ -8,6 +8,7 @@ from test.support.os_helper import TESTFN, unlink
|
|||
from textwrap import dedent
|
||||
from unittest import TestCase
|
||||
import collections
|
||||
import contextlib
|
||||
import inspect
|
||||
import os.path
|
||||
import sys
|
||||
|
@ -335,6 +336,70 @@ class ClinicWholeFileTest(_ParserBase):
|
|||
)
|
||||
self.assertIn(msg, out)
|
||||
|
||||
@staticmethod
|
||||
@contextlib.contextmanager
|
||||
def _clinic_version(new_version):
|
||||
"""Helper for test_version_*() tests"""
|
||||
_saved = clinic.version
|
||||
clinic.version = new_version
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
clinic.version = _saved
|
||||
|
||||
def test_version_directive(self):
|
||||
dataset = (
|
||||
# (clinic version, required version)
|
||||
('3', '2'), # required version < clinic version
|
||||
('3.1', '3.0'), # required version < clinic version
|
||||
('1.2b0', '1.2a7'), # required version < clinic version
|
||||
('5', '5'), # required version == clinic version
|
||||
('6.1', '6.1'), # required version == clinic version
|
||||
('1.2b3', '1.2b3'), # required version == clinic version
|
||||
)
|
||||
for clinic_version, required_version in dataset:
|
||||
with self.subTest(clinic_version=clinic_version,
|
||||
required_version=required_version):
|
||||
with self._clinic_version(clinic_version):
|
||||
block = dedent(f"""
|
||||
/*[clinic input]
|
||||
version {required_version}
|
||||
[clinic start generated code]*/
|
||||
""")
|
||||
self.clinic.parse(block)
|
||||
|
||||
def test_version_directive_insufficient_version(self):
|
||||
with self._clinic_version('4'):
|
||||
err = (
|
||||
"Insufficient Clinic version!\n"
|
||||
" Version: 4\n"
|
||||
" Required: 5"
|
||||
)
|
||||
out = self.expect_failure("""
|
||||
/*[clinic input]
|
||||
version 5
|
||||
[clinic start generated code]*/
|
||||
""")
|
||||
self.assertIn(err, out)
|
||||
|
||||
def test_version_directive_illegal_char(self):
|
||||
err = "Illegal character 'v' in version string 'v5'"
|
||||
out = self.expect_failure("""
|
||||
/*[clinic input]
|
||||
version v5
|
||||
[clinic start generated code]*/
|
||||
""")
|
||||
self.assertIn(err, out)
|
||||
|
||||
def test_version_directive_unsupported_string(self):
|
||||
err = "Unsupported version string: '.-'"
|
||||
out = self.expect_failure("""
|
||||
/*[clinic input]
|
||||
version .-
|
||||
[clinic start generated code]*/
|
||||
""")
|
||||
self.assertIn(err, out)
|
||||
|
||||
|
||||
class ClinicGroupPermuterTest(TestCase):
|
||||
def _test(self, l, m, r, output):
|
||||
|
@ -513,6 +578,22 @@ xyz
|
|||
|
||||
|
||||
class ClinicParserTest(_ParserBase):
|
||||
|
||||
def parse(self, text):
|
||||
c = FakeClinic()
|
||||
parser = DSLParser(c)
|
||||
block = clinic.Block(text)
|
||||
parser.parse(block)
|
||||
return block
|
||||
|
||||
def parse_function(self, text, signatures_in_block=2, function_index=1):
|
||||
block = self.parse(text)
|
||||
s = block.signatures
|
||||
self.assertEqual(len(s), signatures_in_block)
|
||||
assert isinstance(s[0], clinic.Module)
|
||||
assert isinstance(s[function_index], clinic.Function)
|
||||
return s[function_index]
|
||||
|
||||
def checkDocstring(self, fn, expected):
|
||||
self.assertTrue(hasattr(fn, "docstring"))
|
||||
self.assertEqual(fn.docstring.strip(),
|
||||
|
@ -1395,21 +1476,6 @@ Couldn't find existing function 'fooooooooooooooooooooooo'!
|
|||
parser_decl = p.simple_declaration(in_parser=True)
|
||||
self.assertNotIn("Py_UNUSED", parser_decl)
|
||||
|
||||
def parse(self, text):
|
||||
c = FakeClinic()
|
||||
parser = DSLParser(c)
|
||||
block = clinic.Block(text)
|
||||
parser.parse(block)
|
||||
return block
|
||||
|
||||
def parse_function(self, text, signatures_in_block=2, function_index=1):
|
||||
block = self.parse(text)
|
||||
s = block.signatures
|
||||
self.assertEqual(len(s), signatures_in_block)
|
||||
assert isinstance(s[0], clinic.Module)
|
||||
assert isinstance(s[function_index], clinic.Function)
|
||||
return s[function_index]
|
||||
|
||||
def test_scaffolding(self):
|
||||
# test repr on special values
|
||||
self.assertEqual(repr(clinic.unspecified), '<Unspecified>')
|
||||
|
@ -1446,6 +1512,15 @@ Couldn't find existing function 'fooooooooooooooooooooooo'!
|
|||
""")
|
||||
self.assertEqual(stdout.getvalue(), expected)
|
||||
|
||||
def test_illegal_c_identifier(self):
|
||||
err = "Illegal C identifier: 17a"
|
||||
out = self.parse_function_should_fail("""
|
||||
module test
|
||||
test.fn
|
||||
a as 17a: int
|
||||
""")
|
||||
self.assertIn(err, out)
|
||||
|
||||
|
||||
class ClinicExternalTest(TestCase):
|
||||
maxDiff = None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue