<s> from flatland import ( <EOL> Element , <EOL> Skip , <EOL> SkipAll , <EOL> SkipAllFalse , <EOL> Unevaluated , <EOL> ) <EOL> from tests . _util import assert_raises , eq_ , requires_unicode_coercion <EOL> def test_cloning ( ) : <EOL> new_element = Element . named ( u'<STR_LIT:x>' ) <EOL> assert isinstance ( new_element , type ) <EOL> assert new_element . __module__ != Element . __module__ <EOL> assert '<STR_LIT>' in new_element . __module__ <EOL> @ requires_unicode_coercion <EOL> def test_naming ( ) : <EOL> for arg in ( u'<STR_LIT>' , '<STR_LIT>' , None ) : <EOL> schema = Element . named ( arg ) <EOL> eq_ ( schema . name , arg ) <EOL> eq_ ( schema . label , arg ) <EOL> for arg in ( u'<STR_LIT>' , '<STR_LIT>' , None ) : <EOL> schema = Element . named ( arg ) . using ( label = u'<STR_LIT>' ) <EOL> eq_ ( schema . name , arg ) <EOL> eq_ ( schema . label , u'<STR_LIT>' ) <EOL> def test_validators ( ) : <EOL> el = Element ( ) <EOL> assert not el . validators <EOL> el = Element ( validators = ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> eq_ ( el . validators , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> el = Element ( validators = xrange ( <NUM_LIT:3> ) ) <EOL> eq_ ( el . validators , list ( xrange ( <NUM_LIT:3> ) ) ) <EOL> schema = Element . using ( validators = xrange ( <NUM_LIT:3> ) ) <EOL> eq_ ( schema . validators , list ( xrange ( <NUM_LIT:3> ) ) ) <EOL> def test_dsl_validated_by ( ) : <EOL> s = Element . using ( validators = ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> eq_ ( s . validators , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> s = Element . validated_by ( <NUM_LIT> , <NUM_LIT> ) <EOL> eq_ ( s . validators , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> s = Element . using ( validators = ( <NUM_LIT> , <NUM_LIT> ) ) . validated_by ( <NUM_LIT> ) <EOL> eq_ ( s . validators , [ <NUM_LIT> ] ) <EOL> assert_raises ( TypeError , Element . validated_by , int ) <EOL> def test_dsl_including_validators ( ) : <EOL> base = Element . validated_by ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> eq_ ( base . validators , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> s = base . including_validators ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> eq_ ( s . validators , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> s = base . including_validators ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , position = <NUM_LIT:0> ) <EOL> eq_ ( s . validators , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> s = base . including_validators ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , position = <NUM_LIT:1> ) <EOL> eq_ ( s . validators , [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> s = base . including_validators ( <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , position = - <NUM_LIT:2> ) <EOL> eq_ ( s . validators , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:3> ] ) <EOL> s = Element . including_validators ( <NUM_LIT:1> ) <EOL> eq_ ( s . validators , [ <NUM_LIT:1> ] ) <EOL> def test_optional ( ) : <EOL> el = Element ( ) <EOL> assert not el . optional <EOL> el = Element ( optional = True ) <EOL> assert el . optional <EOL> el = Element ( optional = False ) <EOL> assert not el . optional <EOL> def test_label ( ) : <EOL> for item in Element . named ( u'<STR_LIT:x>' ) , Element . named ( u'<STR_LIT:x>' ) ( ) , Element ( name = u'<STR_LIT:x>' ) : <EOL> assert item . label == u'<STR_LIT:x>' <EOL> for item in ( Element . using ( name = u'<STR_LIT:x>' , label = u'<STR_LIT:L>' ) , <EOL> Element . using ( name = u'<STR_LIT:x>' , label = u'<STR_LIT:L>' ) ( ) , <EOL> Element ( name = u'<STR_LIT:x>' , label = u'<STR_LIT:L>' ) ) : <EOL> assert item . label == u'<STR_LIT:L>' <EOL> def test_instance_defaults ( ) : <EOL> el = Element ( ) <EOL> eq_ ( el . name , None ) <EOL> eq_ ( el . label , None ) <EOL> eq_ ( el . optional , False ) <EOL> eq_ ( el . default , None ) <EOL> eq_ ( el . default_factory , None ) <EOL> eq_ ( el . default_value , None ) <EOL> eq_ ( el . validators , ( ) ) <EOL> eq_ ( el . valid , Unevaluated ) <EOL> eq_ ( el . errors , [ ] ) <EOL> eq_ ( el . warnings , [ ] ) <EOL> eq_ ( tuple ( _ . name for _ in el . path ) , ( None , ) ) <EOL> eq_ ( el . parent , None ) <EOL> eq_ ( el . root , el ) <EOL> eq_ ( el . flattened_name ( ) , u'<STR_LIT>' ) <EOL> eq_ ( el . value , None ) <EOL> eq_ ( el . u , u'<STR_LIT>' ) <EOL> def test_abstract ( ) : <EOL> element = Element ( ) <EOL> assert_raises ( NotImplementedError , element . set , None ) <EOL> assert_raises ( NotImplementedError , element . set_flat , ( ) ) <EOL> assert_raises ( NotImplementedError , element . el , u'<STR_LIT:foo>' ) <EOL> def test_message_buckets ( ) : <EOL> el = Element ( ) <EOL> el . add_error ( '<STR_LIT:error>' ) <EOL> eq_ ( el . errors , [ '<STR_LIT:error>' ] ) <EOL> el . add_error ( '<STR_LIT:error>' ) <EOL> eq_ ( el . errors , [ '<STR_LIT:error>' ] ) <EOL> el . add_error ( '<STR_LIT>' ) <EOL> eq_ ( el . errors , [ '<STR_LIT:error>' , '<STR_LIT>' ] ) <EOL> el . add_warning ( '<STR_LIT>' ) <EOL> eq_ ( el . warnings , [ '<STR_LIT>' ] ) <EOL> el . add_warning ( '<STR_LIT>' ) <EOL> eq_ ( el . warnings , [ '<STR_LIT>' ] ) <EOL> def test_validation ( ) : <EOL> ok = lambda item , data : True <EOL> not_ok = lambda item , data : False <EOL> none = lambda item , data : None <EOL> skip = lambda item , data : Skip <EOL> all_ok = lambda item , data : SkipAll <EOL> all_not_ok = lambda item , data : SkipAllFalse <EOL> for res , validators in ( ( True , ( ok , ) ) , <EOL> ( True , ( ok , ok ) ) , <EOL> ( True , ( skip , ) ) , <EOL> ( True , ( skip , not_ok ) ) , <EOL> ( True , ( ok , skip , not_ok ) ) , <EOL> ( True , ( all_ok , ) ) , <EOL> ( True , ( all_ok , not_ok ) ) , <EOL> ( False , ( none , ) ) , <EOL> ( False , ( ok , none ) ) , <EOL> ( False , ( not_ok , ) ) , <EOL> ( False , ( ok , not_ok , ) ) , <EOL> ( False , ( ok , not_ok , ok , ) ) , <EOL> ( False , ( all_not_ok , ) ) , <EOL> ( False , ( ok , all_not_ok , ok ) ) ) : <EOL> el = Element ( validators = validators , validates_down = '<STR_LIT>' ) <EOL> valid = el . validate ( ) <EOL> assert valid == res <EOL> assert bool ( valid ) is res <EOL> assert el . valid is res <EOL> assert el . all_valid is res <EOL> element = None <EOL> def got_element ( item , data ) : <EOL> assert item is element , repr ( item ) <EOL> return True <EOL> element = Element ( validators = ( got_element , ) , validates_down = '<STR_LIT>' ) <EOL> assert element . validate ( ) <EOL> def test_validator_return ( ) : <EOL> class Bool ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , val ) : <EOL> self . val = val <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . val ) <EOL> Validatable = Element . using ( validates_down = '<STR_LIT>' ) <EOL> true = lambda * a : True <EOL> skip = lambda * a : Skip <EOL> skipall = lambda * a : SkipAll <EOL> one = lambda * a : <NUM_LIT:1> <EOL> false = lambda * a : False <EOL> skipallfalse = lambda * a : SkipAllFalse <EOL> zero = lambda * a : <NUM_LIT:0> <EOL> none = lambda * a : None <EOL> no = lambda * a : Bool ( False ) <EOL> for validator in true , one , skip , skipall : <EOL> el = Validatable ( validators = ( validator , ) ) <EOL> assert el . validate ( ) <EOL> for validator in false , zero , none , skipallfalse : <EOL> el = Validatable ( validators = ( validator , ) ) <EOL> assert not el . validate ( ) <EOL> for validator in [ no ] : <EOL> el = Validatable ( validators = ( validator , ) ) <EOL> assert_raises ( TypeError , el . validate ) <EOL> def test_default_value ( ) : <EOL> el = Element ( ) <EOL> assert el . default_value is None <EOL> el = Element ( default = '<STR_LIT:abc>' ) <EOL> assert el . default_value == '<STR_LIT:abc>' <EOL> el = Element ( default_factory = lambda x : '<STR_LIT>' ) <EOL> assert el . default_value == '<STR_LIT>' <EOL> el = Element ( default = '<STR_LIT>' , default_factory = lambda x : '<STR_LIT>' ) <EOL> assert el . default_value == '<STR_LIT>' <EOL> el = Element ( default = '<STR_LIT>' , default_factory = lambda x : x . default ) <EOL> assert el . default_value == '<STR_LIT>' </s>
