Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43324a4

Browse files
committed
Move test for copy.replace(structseq) to test_structseq.py
1 parent e8e6be4 commit 43324a4

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

Lib/test/test_copy.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import copyreg
55
import weakref
66
import abc
7-
import os
8-
import time
97
from operator import le, lt, ge, gt, eq, ne, attrgetter
108

119
import unittest
@@ -947,28 +945,6 @@ def test_namedtuple(self):
947945
with self.assertRaisesRegex(ValueError, 'unexpected field name'):
948946
copy.replace(p, x=1, error=2)
949947

950-
def test_structseq(self):
951-
t = time.gmtime(0)
952-
self.assertEqual(copy.replace(t), (1970, 1, 1, 0, 0, 0, 3, 1, 0))
953-
self.assertEqual(copy.replace(t, tm_year=2000),
954-
(2000, 1, 1, 0, 0, 0, 3, 1, 0))
955-
self.assertEqual(copy.replace(t, tm_mon=2),
956-
(1970, 2, 1, 0, 0, 0, 3, 1, 0))
957-
self.assertEqual(copy.replace(t, tm_year=2000, tm_mon=2),
958-
(2000, 2, 1, 0, 0, 0, 3, 1, 0))
959-
with self.assertRaisesRegex(ValueError, 'unexpected field name'):
960-
copy.replace(t, tm_year=2000, error=2)
961-
962-
assert os.stat_result.n_unnamed_fields > 0
963-
r = os.stat_result(range(os.stat_result.n_sequence_fields), {'st_atime_ns': -1})
964-
self.assertHasAttr(r, 'st_atime_ns')
965-
self.assertEqual(r.st_atime_ns, -1)
966-
with self.assertRaisesRegex(AttributeError, 'readonly attribute'):
967-
r.st_atime_ns = -2
968-
r2 = copy.replace(r, st_atime_ns=-3)
969-
self.assertHasAttr(r2, 'st_atime_ns')
970-
self.assertEqual(r2.st_atime_ns, -3)
971-
972948
def test_dataclass(self):
973949
from dataclasses import dataclass
974950
@dataclass

Lib/test/test_structseq.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import os
23
import time
34
import unittest
@@ -133,6 +134,31 @@ def test_match_args_with_unnamed_fields(self):
133134
self.assertEqual(os.stat_result.n_unnamed_fields, 3)
134135
self.assertEqual(os.stat_result.__match_args__, expected_args)
135136

137+
def test_copy_replace_all_fields_visible(self):
138+
t = time.gmtime(0)
139+
assert t.n_unnamed_fields == 0 and t.n_sequence_fields == t.n_fields
140+
141+
self.assertEqual(copy.replace(t), (1970, 1, 1, 0, 0, 0, 3, 1, 0))
142+
self.assertEqual(copy.replace(t, tm_year=2000),
143+
(2000, 1, 1, 0, 0, 0, 3, 1, 0))
144+
self.assertEqual(copy.replace(t, tm_mon=2),
145+
(1970, 2, 1, 0, 0, 0, 3, 1, 0))
146+
self.assertEqual(copy.replace(t, tm_year=2000, tm_mon=2),
147+
(2000, 2, 1, 0, 0, 0, 3, 1, 0))
148+
with self.assertRaisesRegex(ValueError, 'unexpected field name'):
149+
copy.replace(t, tm_year=2000, error=2)
150+
151+
def test_copy_replace_has_invisible_fields(self):
152+
assert os.stat_result.n_sequence_fields + os.stat_result.n_unnamed_fields < os.stat_result.n_fields
153+
r = os.stat_result(range(os.stat_result.n_sequence_fields), {'st_atime_ns': -1})
154+
self.assertHasAttr(r, 'st_atime_ns')
155+
self.assertEqual(r.st_atime_ns, -1)
156+
with self.assertRaisesRegex(AttributeError, 'readonly attribute'):
157+
r.st_atime_ns = -2
158+
r2 = copy.replace(r, st_atime_ns=-3)
159+
self.assertHasAttr(r2, 'st_atime_ns')
160+
self.assertEqual(r2.st_atime_ns, -3)
161+
136162

137163
if __name__ == "__main__":
138164
unittest.main()

0 commit comments

Comments
 (0)