gh-122255: Add black box tests in test_warnings (GH-122227)

They are similar to white box tests for gh-86298 in test_importlib.
This commit is contained in:
Serhiy Storchaka 2024-08-08 10:35:47 +03:00 committed by GitHub
parent 6094c6fc2f
commit fe13c9baf4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 91 additions and 0 deletions

View File

@ -640,6 +640,97 @@ class WarnTests(BaseTest):
self.module.warn('good warning category', MyWarningClass)
self.assertIsInstance(cm.warning, Warning)
def check_module_globals(self, module_globals):
with original_warnings.catch_warnings(module=self.module, record=True) as w:
self.module.filterwarnings('default')
self.module.warn_explicit(
'eggs', UserWarning, 'bar', 1,
module_globals=module_globals)
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, UserWarning)
self.assertEqual(str(w[0].message), 'eggs')
def check_module_globals_error(self, module_globals, errmsg, errtype=ValueError):
if self.module is py_warnings:
self.check_module_globals(module_globals)
return
with original_warnings.catch_warnings(module=self.module, record=True) as w:
self.module.filterwarnings('always')
with self.assertRaisesRegex(errtype, re.escape(errmsg)):
self.module.warn_explicit(
'eggs', UserWarning, 'bar', 1,
module_globals=module_globals)
self.assertEqual(len(w), 0)
def check_module_globals_deprecated(self, module_globals, msg):
if self.module is py_warnings:
self.check_module_globals(module_globals)
return
with original_warnings.catch_warnings(module=self.module, record=True) as w:
self.module.filterwarnings('always')
self.module.warn_explicit(
'eggs', UserWarning, 'bar', 1,
module_globals=module_globals)
self.assertEqual(len(w), 2)
self.assertEqual(w[0].category, DeprecationWarning)
self.assertEqual(str(w[0].message), msg)
self.assertEqual(w[1].category, UserWarning)
self.assertEqual(str(w[1].message), 'eggs')
def test_gh86298_no_loader_and_no_spec(self):
self.check_module_globals({'__name__': 'bar'})
def test_gh86298_loader_is_none_and_no_spec(self):
self.check_module_globals({'__name__': 'bar', '__loader__': None})
def test_gh86298_no_loader_and_spec_is_none(self):
self.check_module_globals_error(
{'__name__': 'bar', '__spec__': None},
'Module globals is missing a __spec__.loader')
def test_gh86298_loader_is_none_and_spec_is_none(self):
self.check_module_globals_error(
{'__name__': 'bar', '__loader__': None, '__spec__': None},
'Module globals is missing a __spec__.loader')
def test_gh86298_loader_is_none_and_spec_loader_is_none(self):
self.check_module_globals_error(
{'__name__': 'bar', '__loader__': None,
'__spec__': types.SimpleNamespace(loader=None)},
'Module globals is missing a __spec__.loader')
def test_gh86298_no_spec(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object()},
'Module globals is missing a __spec__.loader')
def test_gh86298_spec_is_none(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object(), '__spec__': None},
'Module globals is missing a __spec__.loader')
def test_gh86298_no_spec_loader(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object(),
'__spec__': types.SimpleNamespace()},
'Module globals is missing a __spec__.loader')
def test_gh86298_loader_and_spec_loader_disagree(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object(),
'__spec__': types.SimpleNamespace(loader=object())},
'Module globals; __loader__ != __spec__.loader')
def test_gh86298_no_loader_and_no_spec_loader(self):
self.check_module_globals_error(
{'__name__': 'bar', '__spec__': types.SimpleNamespace()},
'Module globals is missing a __spec__.loader', AttributeError)
def test_gh86298_no_loader_with_spec_loader_okay(self):
self.check_module_globals(
{'__name__': 'bar',
'__spec__': types.SimpleNamespace(loader=object())})
class CWarnTests(WarnTests, unittest.TestCase):
module = c_warnings