Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc6a566e

Browse files
[3.13]gh-71339: Add additional assertion methods in test.support (GH-128707) (GH-128815)
Add a mix-in class ExtraAssertions containing the following methods:* assertHasAttr() and assertNotHasAttr()* assertIsSubclass() and assertNotIsSubclass()* assertStartsWith() and assertNotStartsWith()* assertEndsWith() and assertNotEndsWith()(cherry picked from commit06cad77)
1 parent59b919b commitc6a566e

File tree

7 files changed

+70
-56
lines changed

7 files changed

+70
-56
lines changed

‎Lib/test/support/testcase.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
11
frommathimportcopysign,isnan
22

33

4+
classExtraAssertions:
5+
6+
defassertIsSubclass(self,cls,superclass,msg=None):
7+
ifissubclass(cls,superclass):
8+
return
9+
standardMsg=f'{cls!r} is not a subclass of{superclass!r}'
10+
self.fail(self._formatMessage(msg,standardMsg))
11+
12+
defassertNotIsSubclass(self,cls,superclass,msg=None):
13+
ifnotissubclass(cls,superclass):
14+
return
15+
standardMsg=f'{cls!r} is a subclass of{superclass!r}'
16+
self.fail(self._formatMessage(msg,standardMsg))
17+
18+
defassertHasAttr(self,obj,name,msg=None):
19+
ifnothasattr(obj,name):
20+
ifisinstance(obj,types.ModuleType):
21+
standardMsg=f'module{obj.__name__!r} has no attribute{name!r}'
22+
elifisinstance(obj,type):
23+
standardMsg=f'type object{obj.__name__!r} has no attribute{name!r}'
24+
else:
25+
standardMsg=f'{type(obj).__name__!r} object has no attribute{name!r}'
26+
self.fail(self._formatMessage(msg,standardMsg))
27+
28+
defassertNotHasAttr(self,obj,name,msg=None):
29+
ifhasattr(obj,name):
30+
ifisinstance(obj,types.ModuleType):
31+
standardMsg=f'module{obj.__name__!r} has unexpected attribute{name!r}'
32+
elifisinstance(obj,type):
33+
standardMsg=f'type object{obj.__name__!r} has unexpected attribute{name!r}'
34+
else:
35+
standardMsg=f'{type(obj).__name__!r} object has unexpected attribute{name!r}'
36+
self.fail(self._formatMessage(msg,standardMsg))
37+
38+
defassertStartsWith(self,s,prefix,msg=None):
39+
ifs.startswith(prefix):
40+
return
41+
standardMsg=f"{s!r} doesn't start with{prefix!r}"
42+
self.fail(self._formatMessage(msg,standardMsg))
43+
44+
defassertNotStartsWith(self,s,prefix,msg=None):
45+
ifnots.startswith(prefix):
46+
return
47+
self.fail(self._formatMessage(msg,f"{s!r} starts with{prefix!r}"))
48+
49+
defassertEndsWith(self,s,suffix,msg=None):
50+
ifs.endswith(suffix):
51+
return
52+
standardMsg=f"{s!r} doesn't end with{suffix!r}"
53+
self.fail(self._formatMessage(msg,standardMsg))
54+
55+
defassertNotEndsWith(self,s,suffix,msg=None):
56+
ifnots.endswith(suffix):
57+
return
58+
self.fail(self._formatMessage(msg,f"{s!r} ends with{suffix!r}"))
59+
60+
461
classExceptionIsLikeMixin:
562
defassertExceptionIsLike(self,exc,template):
663
"""

‎Lib/test/test_descr.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
fromcopyimportdeepcopy
1616
fromcontextlibimportredirect_stdout
1717
fromtestimportsupport
18+
fromtest.support.testcaseimportExtraAssertions
1819

1920
try:
2021
import_testcapi
@@ -403,15 +404,7 @@ def test_wrap_lenfunc_bad_cast(self):
403404
self.assertEqual(range(sys.maxsize).__len__(),sys.maxsize)
404405

405406

406-
classClassPropertiesAndMethods(unittest.TestCase):
407-
408-
defassertHasAttr(self,obj,name):
409-
self.assertTrue(hasattr(obj,name),
410-
'%r has no attribute %r'% (obj,name))
411-
412-
defassertNotHasAttr(self,obj,name):
413-
self.assertFalse(hasattr(obj,name),
414-
'%r has unexpected attribute %r'% (obj,name))
407+
classClassPropertiesAndMethods(unittest.TestCase,ExtraAssertions):
415408

416409
deftest_python_dicts(self):
417410
# Testing Python subclass of dict...

‎Lib/test/test_gdb/util.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
importsysconfig
88
importunittest
99
fromtestimportsupport
10+
fromtest.support.testcaseimportExtraAssertions
1011

1112

1213
GDB_PROGRAM=shutil.which('gdb')or'gdb'
@@ -152,7 +153,7 @@ def setup_module():
152153
print()
153154

154155

155-
classDebuggerTests(unittest.TestCase):
156+
classDebuggerTests(unittest.TestCase,ExtraAssertions):
156157

157158
"""Test that the debugger can debug Python."""
158159

@@ -280,11 +281,6 @@ def get_stack_trace(self, source=None, script=None,
280281

281282
returnout
282283

283-
defassertEndsWith(self,actual,exp_end):
284-
'''Ensure that the given "actual" string ends with "exp_end"'''
285-
self.assertTrue(actual.endswith(exp_end),
286-
msg='%r did not end with %r'% (actual,exp_end))
287-
288284
defassertMultilineMatches(self,actual,pattern):
289285
m=re.match(pattern,actual,re.DOTALL)
290286
ifnotm:

‎Lib/test/test_importlib/resources/test_functional.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importimportlib
44

55
fromtest.supportimportwarnings_helper
6+
fromtest.support.testcaseimportExtraAssertions
67

78
fromimportlibimportresources
89

@@ -28,7 +29,7 @@ def anchor02(self):
2829
returnimportlib.import_module('data02')
2930

3031

31-
classFunctionalAPIBase(util.DiskSetup):
32+
classFunctionalAPIBase(util.DiskSetup,ExtraAssertions):
3233
defsetUp(self):
3334
super().setUp()
3435
self.load_fixture('data02')
@@ -43,12 +44,6 @@ def _gen_resourcetxt_path_parts(self):
4344
withself.subTest(path_parts=path_parts):
4445
yieldpath_parts
4546

46-
defassertEndsWith(self,string,suffix):
47-
"""Assert that `string` ends with `suffix`.
48-
49-
Used to ignore an architecture-specific UTF-16 byte-order mark."""
50-
self.assertEqual(string[-len(suffix) :],suffix)
51-
5247
deftest_read_text(self):
5348
self.assertEqual(
5449
resources.read_text(self.anchor01,'utf-8.file'),

‎Lib/test/test_pyclbr.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
fromunittestimportTestCase,mainasunittest_main
1111
fromtest.test_importlibimportutilastest_importlib_util
1212
importwarnings
13+
fromtest.support.testcaseimportExtraAssertions
1314

1415

1516
StaticMethodType=type(staticmethod(lambda:None))
@@ -22,7 +23,7 @@
2223
# is imperfect (as designed), testModule is called with a set of
2324
# members to ignore.
2425

25-
classPyclbrTest(TestCase):
26+
classPyclbrTest(TestCase,ExtraAssertions):
2627

2728
defassertListEq(self,l1,l2,ignore):
2829
''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
@@ -31,14 +32,6 @@ def assertListEq(self, l1, l2, ignore):
3132
print("l1=%r\nl2=%r\nignore=%r"% (l1,l2,ignore),file=sys.stderr)
3233
self.fail("%r missing"%missing.pop())
3334

34-
defassertHasattr(self,obj,attr,ignore):
35-
''' succeed iff hasattr(obj,attr) or attr in ignore. '''
36-
ifattrinignore:return
37-
ifnothasattr(obj,attr):print("???",attr)
38-
self.assertTrue(hasattr(obj,attr),
39-
'expected hasattr(%r, %r)'% (obj,attr))
40-
41-
4235
defassertHaskey(self,obj,key,ignore):
4336
''' succeed iff key in obj or key in ignore. '''
4437
ifkeyinignore:return
@@ -86,7 +79,7 @@ def ismethod(oclass, obj, name):
8679
forname,valueindict.items():
8780
ifnameinignore:
8881
continue
89-
self.assertHasattr(module,name,ignore)
82+
self.assertHasAttr(module,name,ignore)
9083
py_item=getattr(module,name)
9184
ifisinstance(value,pyclbr.Function):
9285
self.assertIsInstance(py_item, (FunctionType,BuiltinFunctionType))

‎Lib/test/test_typing.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
importtypes
4747

4848
fromtest.supportimportcaptured_stderr,cpython_only,infinite_recursion,requires_docstrings,import_helper
49+
fromtest.support.testcaseimportExtraAssertions
4950
fromtest.typinganndataimportann_module695,mod_generics_cache,_typed_dict_helper
5051

5152

@@ -54,21 +55,7 @@
5455
CANNOT_SUBCLASS_INSTANCE='Cannot subclass an instance of %s'
5556

5657

57-
classBaseTestCase(TestCase):
58-
59-
defassertIsSubclass(self,cls,class_or_tuple,msg=None):
60-
ifnotissubclass(cls,class_or_tuple):
61-
message='%r is not a subclass of %r'% (cls,class_or_tuple)
62-
ifmsgisnotNone:
63-
message+=' : %s'%msg
64-
raiseself.failureException(message)
65-
66-
defassertNotIsSubclass(self,cls,class_or_tuple,msg=None):
67-
ifissubclass(cls,class_or_tuple):
68-
message='%r is a subclass of %r'% (cls,class_or_tuple)
69-
ifmsgisnotNone:
70-
message+=' : %s'%msg
71-
raiseself.failureException(message)
58+
classBaseTestCase(TestCase,ExtraAssertions):
7259

7360
defclear_caches(self):
7461
forfintyping._cleanups:
@@ -1249,10 +1236,6 @@ class Gen[*Ts]: ...
12491236

12501237
classTypeVarTupleTests(BaseTestCase):
12511238

1252-
defassertEndsWith(self,string,tail):
1253-
ifnotstring.endswith(tail):
1254-
self.fail(f"String{string!r} does not end with{tail!r}")
1255-
12561239
deftest_name(self):
12571240
Ts=TypeVarTuple('Ts')
12581241
self.assertEqual(Ts.__name__,'Ts')

‎Lib/test/test_venv.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
requires_resource,copy_python_src_ignore)
2727
fromtest.support.os_helperimport (can_symlink,EnvironmentVarGuard,rmtree,
2828
TESTFN,FakePath)
29+
fromtest.support.testcaseimportExtraAssertions
2930
importunittest
3031
importvenv
3132
fromunittest.mockimportpatch,Mock
@@ -64,7 +65,7 @@ def check_output(cmd, encoding=None):
6465
)
6566
returnout,err
6667

67-
classBaseTest(unittest.TestCase):
68+
classBaseTest(unittest.TestCase,ExtraAssertions):
6869
"""Base class for venv tests."""
6970
maxDiff=80*50
7071

@@ -111,10 +112,6 @@ def get_text_file_contents(self, *args, encoding='utf-8'):
111112
result=f.read()
112113
returnresult
113114

114-
defassertEndsWith(self,string,tail):
115-
ifnotstring.endswith(tail):
116-
self.fail(f"String{string!r} does not end with{tail!r}")
117-
118115
classBasicTest(BaseTest):
119116
"""Test venv module functionality."""
120117

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp