Add test cases for dataclasses. (#17909)
* Add test cases for dataclasses. * Add test for repr output of field. * Add test for ValueError to be raised when both default and default_factory are passed.
This commit is contained in:
parent
6c5d661342
commit
eef1b027ab
|
@ -45,6 +45,25 @@ class TestCase(unittest.TestCase):
|
|||
o = C(42)
|
||||
self.assertEqual(o.x, 42)
|
||||
|
||||
def test_field_default_default_factory_error(self):
|
||||
msg = "cannot specify both default and default_factory"
|
||||
with self.assertRaisesRegex(ValueError, msg):
|
||||
@dataclass
|
||||
class C:
|
||||
x: int = field(default=1, default_factory=int)
|
||||
|
||||
def test_field_repr(self):
|
||||
int_field = field(default=1, init=True, repr=False)
|
||||
int_field.name = "id"
|
||||
repr_output = repr(int_field)
|
||||
expected_output = "Field(name='id',type=None," \
|
||||
f"default=1,default_factory={MISSING!r}," \
|
||||
"init=True,repr=False,hash=None," \
|
||||
"compare=True,metadata=mappingproxy({})," \
|
||||
"_field_type=None)"
|
||||
|
||||
self.assertEqual(repr_output, expected_output)
|
||||
|
||||
def test_named_init_params(self):
|
||||
@dataclass
|
||||
class C:
|
||||
|
|
Loading…
Reference in New Issue