ch25: new example with __init_subclas__

This commit is contained in:
Luciano Ramalho
2021-04-17 21:02:51 -03:00
parent 4ff0a59608
commit 177d914c9f
3 changed files with 201 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
import pytest
from checkedlib import Checked
def test_field_descriptor_validation_type_error():
class Cat(Checked):
name: str
weight: float
with pytest.raises(TypeError) as e:
felix = Cat(name='Felix', weight=None)
assert str(e.value) == 'None is not compatible with weight:float'
def test_field_descriptor_validation_value_error():
class Cat(Checked):
name: str
weight: float
with pytest.raises(TypeError) as e:
felix = Cat(name='Felix', weight='half stone')
assert str(e.value) == "'half stone' is not compatible with weight:float"
def test_constructor_attribute_error():
class Cat(Checked):
name: str
weight: float
with pytest.raises(AttributeError) as e:
felix = Cat(name='Felix', weight=3.2, age=7)
assert str(e.value) == "'Cat' has no attribute 'age'"