def test_AnyOfObject():
class Foo(jst.JSONHasTraits):
intval = T.Integer()
flag = T.Bool()
class Bar(jst.JSONHasTraits):
strval = T.Unicode()
flag = T.Bool()
class FooBar(jst.AnyOfObject):
_classes = [Foo, Bar]
FooBar(strval='hello', flag=True)
FooBar(intval=5, flag=True)
with pytest.raises(T.TraitError):
FooBar(strval=666, flag=False)
with pytest.raises(T.TraitError):
FooBar(strval='hello', flag='bad arg')
with pytest.raises(T.TraitError):
FooBar(intval='bad arg', flag=False)
with pytest.raises(T.TraitError):
FooBar(intval=42, flag='bad arg')
# Test from_dict
FooBar.from_dict({'strval': 'hello', 'flag': True})
FooBar.from_dict({'intval': 42, 'flag': False})
评论列表
文章目录