<s> from test import support , mapping_tests <EOL> import collections <EOL> d0 = { } <EOL> d1 = { "<STR_LIT>" : <NUM_LIT:1> } <EOL> d2 = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:2> } <EOL> d3 = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:3> , "<STR_LIT>" : <NUM_LIT:5> } <EOL> d4 = { "<STR_LIT>" : None , "<STR_LIT>" : None } <EOL> d5 = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:1> } <EOL> class UserDictTest ( mapping_tests . TestHashMappingProtocol ) : <EOL> type2test = collections . UserDict <EOL> def test_all ( self ) : <EOL> u = collections . UserDict ( ) <EOL> u0 = collections . UserDict ( d0 ) <EOL> u1 = collections . UserDict ( d1 ) <EOL> u2 = collections . UserDict ( d2 ) <EOL> uu = collections . UserDict ( u ) <EOL> uu0 = collections . UserDict ( u0 ) <EOL> uu1 = collections . UserDict ( u1 ) <EOL> uu2 = collections . UserDict ( u2 ) <EOL> self . assertEqual ( collections . UserDict ( one = <NUM_LIT:1> , two = <NUM_LIT:2> ) , d2 ) <EOL> self . assertEqual ( collections . UserDict ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) ] ) , d2 ) <EOL> self . assertEqual ( collections . UserDict ( dict = [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) ] ) , d2 ) <EOL> self . assertEqual ( collections . UserDict ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) ] , two = <NUM_LIT:3> , three = <NUM_LIT:5> ) , d3 ) <EOL> self . assertEqual ( collections . UserDict . fromkeys ( '<STR_LIT>' . split ( ) ) , d4 ) <EOL> self . assertEqual ( collections . UserDict ( ) . fromkeys ( '<STR_LIT>' . split ( ) ) , d4 ) <EOL> self . assertEqual ( collections . UserDict . fromkeys ( '<STR_LIT>' . split ( ) , <NUM_LIT:1> ) , d5 ) <EOL> self . assertEqual ( collections . UserDict ( ) . fromkeys ( '<STR_LIT>' . split ( ) , <NUM_LIT:1> ) , d5 ) <EOL> self . assertTrue ( u1 . fromkeys ( '<STR_LIT>' . split ( ) ) is not u1 ) <EOL> self . assertIsInstance ( u1 . fromkeys ( '<STR_LIT>' . split ( ) ) , collections . UserDict ) <EOL> self . assertIsInstance ( u2 . fromkeys ( '<STR_LIT>' . split ( ) ) , collections . UserDict ) <EOL> self . assertEqual ( str ( u0 ) , str ( d0 ) ) <EOL> self . assertEqual ( repr ( u1 ) , repr ( d1 ) ) <EOL> self . assertEqual ( repr ( u2 ) , repr ( d2 ) ) <EOL> all = [ d0 , d1 , d2 , u , u0 , u1 , u2 , uu , uu0 , uu1 , uu2 ] <EOL> for a in all : <EOL> for b in all : <EOL> self . assertEqual ( a == b , len ( a ) == len ( b ) ) <EOL> self . assertEqual ( u2 [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> self . assertRaises ( KeyError , u1 . __getitem__ , "<STR_LIT>" ) <EOL> u3 = collections . UserDict ( u2 ) <EOL> u3 [ "<STR_LIT>" ] = <NUM_LIT:2> <EOL> u3 [ "<STR_LIT>" ] = <NUM_LIT:3> <EOL> del u3 [ "<STR_LIT>" ] <EOL> self . assertRaises ( KeyError , u3 . __delitem__ , "<STR_LIT>" ) <EOL> u3 . clear ( ) <EOL> self . assertEqual ( u3 , { } ) <EOL> u2a = u2 . copy ( ) <EOL> self . assertEqual ( u2a , u2 ) <EOL> u2b = collections . UserDict ( x = <NUM_LIT> , y = <NUM_LIT> ) <EOL> u2c = u2b . copy ( ) <EOL> self . assertEqual ( u2b , u2c ) <EOL> class MyUserDict ( collections . UserDict ) : <EOL> def display ( self ) : print ( self ) <EOL> m2 = MyUserDict ( u2 ) <EOL> m2a = m2 . copy ( ) <EOL> self . assertEqual ( m2a , m2 ) <EOL> m2 [ '<STR_LIT:foo>' ] = '<STR_LIT:bar>' <EOL> self . assertNotEqual ( m2a , m2 ) <EOL> self . assertEqual ( u2 . keys ( ) , d2 . keys ( ) ) <EOL> self . assertEqual ( u2 . items ( ) , d2 . items ( ) ) <EOL> self . assertEqual ( list ( u2 . values ( ) ) , list ( d2 . values ( ) ) ) <EOL> for i in u2 . keys ( ) : <EOL> self . assertIn ( i , u2 ) <EOL> self . assertEqual ( i in u1 , i in d1 ) <EOL> self . assertEqual ( i in u0 , i in d0 ) <EOL> t = collections . UserDict ( ) <EOL> t . update ( u2 ) <EOL> self . assertEqual ( t , u2 ) <EOL> for i in u2 . keys ( ) : <EOL> self . assertEqual ( u2 . get ( i ) , u2 [ i ] ) <EOL> self . assertEqual ( u1 . get ( i ) , d1 . get ( i ) ) <EOL> self . assertEqual ( u0 . get ( i ) , d0 . get ( i ) ) <EOL> for i in range ( <NUM_LIT:20> ) : <EOL> u2 [ i ] = str ( i ) <EOL> ikeys = [ ] <EOL> for k in u2 : <EOL> ikeys . append ( k ) <EOL> keys = u2 . keys ( ) <EOL> self . assertEqual ( set ( ikeys ) , set ( keys ) ) <EOL> t = collections . UserDict ( ) <EOL> self . assertEqual ( t . setdefault ( "<STR_LIT:x>" , <NUM_LIT> ) , <NUM_LIT> ) <EOL> self . assertIn ( "<STR_LIT:x>" , t ) <EOL> self . assertEqual ( t . setdefault ( "<STR_LIT:x>" , <NUM_LIT> ) , <NUM_LIT> ) <EOL> t = collections . UserDict ( x = <NUM_LIT> ) <EOL> self . assertEqual ( t . pop ( "<STR_LIT:x>" ) , <NUM_LIT> ) <EOL> self . assertRaises ( KeyError , t . pop , "<STR_LIT:x>" ) <EOL> self . assertEqual ( t . pop ( "<STR_LIT:x>" , <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> t [ "<STR_LIT:x>" ] = <NUM_LIT> <EOL> self . assertEqual ( t . pop ( "<STR_LIT:x>" , <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> t = collections . UserDict ( x = <NUM_LIT> ) <EOL> self . assertEqual ( t . popitem ( ) , ( "<STR_LIT:x>" , <NUM_LIT> ) ) <EOL> self . assertRaises ( KeyError , t . popitem ) <EOL> def test_missing ( self ) : <EOL> self . assertEqual ( hasattr ( collections . UserDict , "<STR_LIT>" ) , False ) <EOL> class D ( collections . UserDict ) : <EOL> def __missing__ ( self , key ) : <EOL> return <NUM_LIT> <EOL> d = D ( { <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:4> } ) <EOL> self . assertEqual ( d [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( d [ <NUM_LIT:3> ] , <NUM_LIT:4> ) <EOL> self . assertNotIn ( <NUM_LIT:2> , d ) <EOL> self . assertNotIn ( <NUM_LIT:2> , d . keys ( ) ) <EOL> self . assertEqual ( d [ <NUM_LIT:2> ] , <NUM_LIT> ) <EOL> class E ( collections . UserDict ) : <EOL> def __missing__ ( self , key ) : <EOL> raise RuntimeError ( key ) <EOL> e = E ( ) <EOL> try : <EOL> e [ <NUM_LIT> ] <EOL> except RuntimeError as err : <EOL> self . assertEqual ( err . args , ( <NUM_LIT> , ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> class F ( collections . UserDict ) : <EOL> def __init__ ( self ) : <EOL> self . __missing__ = lambda key : None <EOL> collections . UserDict . __init__ ( self ) <EOL> f = F ( ) <EOL> try : <EOL> f [ <NUM_LIT> ] <EOL> except KeyError as err : <EOL> self . assertEqual ( err . args , ( <NUM_LIT> , ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> class G ( collections . UserDict ) : <EOL> pass <EOL> g = G ( ) <EOL> try : <EOL> g [ <NUM_LIT> ] <EOL> except KeyError as err : <EOL> self . assertEqual ( err . args , ( <NUM_LIT> , ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_main ( ) : <EOL> support . run_unittest ( <EOL> UserDictTest , <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
