def test_label(): # .label fallback to .name works for instances and classes for item in Element.named(u'x'), Element.named(u'x')(), Element(name=u'x'): assert item.label == u'x' for item in (Element.using(name=u'x', label=u'L'), Element.using(name=u'x', label=u'L')(), Element(name=u'x', label=u'L')): assert item.label == u'L'
def test_dsl_validated_by(): s = Element.using(validators=(123, 456)) eq_(s.validators, [123, 456]) s = Element.validated_by(123, 456) eq_(s.validators, [123, 456]) s = Element.using(validators=(123, 456)).validated_by(789) eq_(s.validators, [789]) assert_raises(TypeError, Element.validated_by, int)
def test_dsl_validated_by(): s = Element.using(validators=(123, 456)) assert s.validators == [123, 456] s = Element.validated_by(123, 456) assert s.validators == [123, 456] s = Element.using(validators=(123, 456)).validated_by(789) assert s.validators == [789] with pytest.raises(TypeError): Element.validated_by(int)
def test_validators(): # Validators may be inherited or supplied at construction. el = Element() assert not el.validators # argument is transformed into a list copy el = Element(validators=(123, 456)) eq_(el.validators, [123, 456]) el = Element(validators=xrange(3)) eq_(el.validators, list(xrange(3))) schema = Element.using(validators=xrange(3)) eq_(schema.validators, list(xrange(3)))
def test_validator_return(): # Validator returns can be bool, int or None. class Bool(object): """A truthy object that does not implement __and__""" def __init__(self, val): self.val = val def __bool__(self): return bool(self.val) __nonzero__ = __bool__ Validatable = Element.using(validates_down='validators') # mostly we care about allowing None for False true = lambda *a: True skip = lambda *a: Skip skipall = lambda *a: SkipAll one = lambda *a: 1 false = lambda *a: False skipallfalse = lambda *a: SkipAllFalse zero = lambda *a: 0 none = lambda *a: None no = lambda *a: Bool(False) for validator in true, one, skip, skipall: el = Validatable(validators=(validator,)) assert el.validate() for validator in false, zero, none, skipallfalse: el = Validatable(validators=(validator,)) assert not el.validate() for validator in [no]: el = Validatable(validators=(validator,)) with pytest.raises(TypeError): el.validate()
def test_validator_return(): # Validator returns can be bool, int or None. class Bool(object): """A truthy object that does not implement __and__""" def __init__(self, val): self.val = val def __bool__(self): return bool(self.val) __nonzero__ = __bool__ Validatable = Element.using(validates_down='validators') # mostly we care about allowing None for False true = lambda *a: True skip = lambda *a: Skip skipall = lambda *a: SkipAll one = lambda *a: 1 false = lambda *a: False skipallfalse = lambda *a: SkipAllFalse zero = lambda *a: 0 none = lambda *a: None no = lambda *a: Bool(False) for validator in true, one, skip, skipall: el = Validatable(validators=(validator, )) assert el.validate() for validator in false, zero, none, skipallfalse: el = Validatable(validators=(validator, )) assert not el.validate() for validator in [no]: el = Validatable(validators=(validator, )) assert_raises(TypeError, el.validate)