<s> from tests . _util import eq_ , assert_raises <EOL> from flatland import util <EOL> def test_lazy_property ( ) : <EOL> poison = False <EOL> class Foo ( object ) : <EOL> @ util . lazy_property <EOL> def squiznart ( self ) : <EOL> assert not poison <EOL> return '<STR_LIT:abc>' <EOL> assert Foo . squiznart != '<STR_LIT:abc>' <EOL> assert hasattr ( Foo . squiznart , '<STR_LIT>' ) <EOL> f = Foo ( ) <EOL> assert '<STR_LIT>' not in f . __dict__ <EOL> assert f . squiznart == '<STR_LIT:abc>' <EOL> assert f . __dict__ [ '<STR_LIT>' ] == '<STR_LIT:abc>' <EOL> poison = True <EOL> assert f . squiznart == '<STR_LIT:abc>' <EOL> new_foo = Foo ( ) <EOL> assert_raises ( AssertionError , getattr , new_foo , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' not in new_foo . __dict__ <EOL> def test_as_mapping ( ) : <EOL> class Foo ( object ) : <EOL> clazz = '<STR_LIT:c>' <EOL> def __init__ ( self ) : <EOL> self . inzt = '<STR_LIT:i>' <EOL> m = util . as_mapping ( Foo ) <EOL> assert '<STR_LIT>' in m <EOL> assert m [ '<STR_LIT>' ] == '<STR_LIT:c>' <EOL> assert sorted ( dir ( Foo ) ) == sorted ( m ) <EOL> assert_raises ( KeyError , m . __getitem__ , '<STR_LIT>' ) <EOL> mi = util . as_mapping ( Foo ( ) ) <EOL> assert '<STR_LIT>' in mi <EOL> assert mi [ '<STR_LIT>' ] == '<STR_LIT:c>' <EOL> assert '<STR_LIT>' in mi <EOL> assert mi [ '<STR_LIT>' ] == '<STR_LIT:i>' <EOL> assert sorted ( dir ( Foo ( ) ) ) == sorted ( mi ) <EOL> def test_luhn10 ( ) : <EOL> assert util . luhn10 ( <NUM_LIT:0> ) is True <EOL> assert util . luhn10 ( <NUM_LIT> ) is True <EOL> assert util . luhn10 ( <NUM_LIT> ) is False <EOL> def test_to_pairs ( ) : <EOL> to_pairs = util . to_pairs <EOL> wanted = [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] <EOL> assert list ( to_pairs ( wanted ) ) == wanted <EOL> assert list ( to_pairs ( iter ( wanted ) ) ) == wanted <EOL> assert sorted ( to_pairs ( dict ( wanted ) ) ) == wanted <EOL> class Duck ( object ) : <EOL> def keys ( self ) : <EOL> return dict ( wanted ) . keys ( ) <EOL> def __getitem__ ( self , key ) : <EOL> return dict ( wanted ) [ key ] <EOL> assert sorted ( to_pairs ( Duck ( ) ) ) == wanted <EOL> PAIRS = [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) , ( '<STR_LIT:c>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT:d>' , <NUM_LIT:4> ) , ( '<STR_LIT:d>' , <NUM_LIT:4> ) , ( '<STR_LIT:d>' , <NUM_LIT:5> ) ] <EOL> def test_keyslice_conflict ( ) : <EOL> generator = util . keyslice_pairs ( ( ) , include = [ <NUM_LIT:1> ] , omit = [ <NUM_LIT:2> ] ) <EOL> assert_raises ( TypeError , list , generator ) <EOL> def test_keyslice_pairs ( ) : <EOL> assert list ( util . keyslice_pairs ( PAIRS ) ) == PAIRS <EOL> assert list ( util . keyslice_pairs ( tuple ( PAIRS ) ) ) == PAIRS <EOL> assert list ( util . keyslice_pairs ( iter ( PAIRS ) ) ) == PAIRS <EOL> def _keyslice_eq_ ( wanted , kw = { } ) : <EOL> got = list ( util . keyslice_pairs ( PAIRS , ** kw ) ) <EOL> eq_ ( wanted , got ) <EOL> def test_keyslice_include ( ) : <EOL> yield _keyslice_eq_ , PAIRS , dict ( include = [ ] ) <EOL> yield _keyslice_eq_ , [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) ] , dict ( include = [ '<STR_LIT:a>' ] ) <EOL> yield _keyslice_eq_ , [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] , dict ( include = [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> yield _keyslice_eq_ , [ ( '<STR_LIT:d>' , <NUM_LIT:4> ) , ( '<STR_LIT:d>' , <NUM_LIT:4> ) , ( '<STR_LIT:d>' , <NUM_LIT:5> ) ] , dict ( include = [ '<STR_LIT:d>' ] ) <EOL> yield _keyslice_eq_ , [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) ] , dict ( include = [ '<STR_LIT:a>' , '<STR_LIT:e>' ] ) <EOL> def test_keyslice_omit ( ) : <EOL> yield _keyslice_eq_ , PAIRS , dict ( omit = [ ] ) <EOL> yield _keyslice_eq_ , [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) , ( '<STR_LIT:c>' , <NUM_LIT:3> ) ] , dict ( omit = [ '<STR_LIT:d>' ] ) <EOL> yield _keyslice_eq_ , [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] , dict ( omit = [ '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> yield _keyslice_eq_ , [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ] , dict ( omit = [ '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' ] ) <EOL> yield _keyslice_eq_ , [ ] , dict ( omit = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> def test_keyslice_rename ( ) : <EOL> wanted = PAIRS [ : <NUM_LIT:3> ] + [ ( '<STR_LIT>' , <NUM_LIT:4> ) , ( '<STR_LIT>' , <NUM_LIT:4> ) , ( '<STR_LIT>' , <NUM_LIT:5> ) ] <EOL> yield _keyslice_eq_ , wanted , dict ( rename = { '<STR_LIT:d>' : '<STR_LIT>' } ) <EOL> yield _keyslice_eq_ , wanted , dict ( rename = [ ( '<STR_LIT:d>' , '<STR_LIT>' ) ] ) <EOL> yield _keyslice_eq_ , wanted , dict ( rename = { '<STR_LIT:d>' : '<STR_LIT>' , '<STR_LIT:e>' : '<STR_LIT:Y>' } ) <EOL> wanted = [ ( '<STR_LIT:d>' , <NUM_LIT:1> ) , ( '<STR_LIT:c>' , <NUM_LIT:2> ) , ( '<STR_LIT:b>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT:a>' , <NUM_LIT:4> ) , ( '<STR_LIT:a>' , <NUM_LIT:4> ) , ( '<STR_LIT:a>' , <NUM_LIT:5> ) ] <EOL> yield _keyslice_eq_ , wanted , dict ( rename = zip ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_keyslice_key ( ) : <EOL> wanted = [ ( int ( k , <NUM_LIT:16> ) , v ) for k , v in PAIRS ] <EOL> keyfunc = lambda v : int ( v , <NUM_LIT:16> ) <EOL> yield _keyslice_eq_ , wanted , dict ( key = keyfunc ) <EOL> wanted = wanted [ : <NUM_LIT:3> ] + [ ( <NUM_LIT:0> , <NUM_LIT:4> ) , ( <NUM_LIT:0> , <NUM_LIT:4> ) , ( <NUM_LIT:0> , <NUM_LIT:5> ) ] <EOL> yield _keyslice_eq_ , wanted , dict ( key = keyfunc , rename = { <NUM_LIT> : <NUM_LIT:0> } ) <EOL> def test_keyslice_mixed ( ) : <EOL> wanted = [ ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:X>' , <NUM_LIT:2> ) ] <EOL> yield _keyslice_eq_ , wanted , dict ( rename = { '<STR_LIT:b>' : '<STR_LIT:X>' } , include = [ '<STR_LIT:a>' ] ) <EOL> yield _keyslice_eq_ , wanted , dict ( rename = { '<STR_LIT:b>' : '<STR_LIT:X>' } , omit = [ '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> def test_symbols ( ) : <EOL> sym1 = util . symbol ( '<STR_LIT:foo>' ) <EOL> assert sym1 . name == '<STR_LIT:foo>' <EOL> sym2 = util . symbol ( '<STR_LIT:foo>' ) <EOL> assert sym1 is sym2 <EOL> assert sym1 == sym2 <EOL> sym3 = util . symbol ( '<STR_LIT:bar>' ) <EOL> assert sym1 is not sym3 <EOL> assert sym1 != sym3 <EOL> assert repr ( sym3 ) == '<STR_LIT:bar>' <EOL> def test_symbol_pickle ( ) : <EOL> import pickle <EOL> try : <EOL> import cPickle <EOL> except ImportError : <EOL> cPickle = pickle <EOL> for mod in pickle , cPickle : <EOL> sym1 = util . symbol ( '<STR_LIT:foo>' ) <EOL> sym2 = util . symbol ( '<STR_LIT:foo>' ) <EOL> assert sym1 is sym2 <EOL> s = pickle . dumps ( sym1 ) <EOL> sym3 = pickle . loads ( s ) <EOL> for protocol in <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> : <EOL> serial = pickle . dumps ( sym1 ) <EOL> rt = pickle . loads ( serial ) <EOL> assert rt is sym1 <EOL> assert rt is sym2 </s>
