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:
Karthikeyan Singaravelan 2020-01-09 19:11:46 +05:30 committed by Eric V. Smith
parent 6c5d661342
commit eef1b027ab
1 changed files with 19 additions and 0 deletions

View File

@ -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: