<s> from __future__ import unicode_literals <EOL> import copy <EOL> import pickle <EOL> import sys <EOL> from django . contrib . auth . models import User <EOL> from django . test import TestCase as DjangoTestCase <EOL> from django . utils import six <EOL> from django . utils . unittest import TestCase <EOL> from django . utils . functional import SimpleLazyObject , empty <EOL> class _ComplexObject ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __eq__ ( self , other ) : <EOL> return self . name == other . name <EOL> def __hash__ ( self ) : <EOL> return hash ( self . name ) <EOL> if six . PY3 : <EOL> def __bytes__ ( self ) : <EOL> return ( "<STR_LIT>" % self . name ) . encode ( "<STR_LIT:utf-8>" ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> else : <EOL> def __str__ ( self ) : <EOL> return b"<STR_LIT>" % str ( self . name ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> complex_object = lambda : _ComplexObject ( "<STR_LIT>" ) <EOL> class TestUtilsSimpleLazyObject ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_equality ( self ) : <EOL> self . assertEqual ( complex_object ( ) , SimpleLazyObject ( complex_object ) ) <EOL> self . assertEqual ( SimpleLazyObject ( complex_object ) , complex_object ( ) ) <EOL> def test_hash ( self ) : <EOL> self . assertEqual ( hash ( complex_object ( ) ) , <EOL> hash ( SimpleLazyObject ( complex_object ) ) ) <EOL> def test_repr ( self ) : <EOL> x = SimpleLazyObject ( complex_object ) <EOL> self . assertEqual ( "<STR_LIT>" % complex_object , repr ( x ) ) <EOL> self . assertEqual ( empty , x . _wrapped ) <EOL> name = x . name <EOL> self . assertIsInstance ( x . _wrapped , _ComplexObject ) <EOL> self . assertEqual ( "<STR_LIT>" % x . _wrapped , repr ( x ) ) <EOL> def test_bytes ( self ) : <EOL> self . assertEqual ( b"<STR_LIT>" , <EOL> bytes ( SimpleLazyObject ( complex_object ) ) ) <EOL> def test_text ( self ) : <EOL> self . assertEqual ( "<STR_LIT>" , six . text_type ( SimpleLazyObject ( complex_object ) ) ) <EOL> def test_class ( self ) : <EOL> self . assertEqual ( _ComplexObject , SimpleLazyObject ( complex_object ) . __class__ ) <EOL> def test_deepcopy ( self ) : <EOL> s = SimpleLazyObject ( complex_object ) <EOL> self . assertIs ( s . _wrapped , empty ) <EOL> s2 = copy . deepcopy ( s ) <EOL> self . assertIs ( s . _wrapped , empty ) <EOL> self . assertEqual ( s2 , complex_object ( ) ) <EOL> name = s . name <EOL> self . assertIsNot ( s . _wrapped , empty ) <EOL> s3 = copy . deepcopy ( s ) <EOL> self . assertEqual ( s3 , complex_object ( ) ) <EOL> def test_none ( self ) : <EOL> i = [ <NUM_LIT:0> ] <EOL> def f ( ) : <EOL> i [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> return None <EOL> x = SimpleLazyObject ( f ) <EOL> self . assertEqual ( str ( x ) , "<STR_LIT:None>" ) <EOL> self . assertEqual ( i , [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( str ( x ) , "<STR_LIT:None>" ) <EOL> self . assertEqual ( i , [ <NUM_LIT:1> ] ) <EOL> def test_bool ( self ) : <EOL> x = SimpleLazyObject ( lambda : <NUM_LIT:3> ) <EOL> self . assertTrue ( x ) <EOL> x = SimpleLazyObject ( lambda : <NUM_LIT:0> ) <EOL> self . assertFalse ( x ) <EOL> def test_pickle_complex ( self ) : <EOL> x = SimpleLazyObject ( complex_object ) <EOL> pickled = pickle . dumps ( x ) <EOL> unpickled = pickle . loads ( pickled ) <EOL> self . assertEqual ( unpickled , x ) <EOL> self . assertEqual ( six . text_type ( unpickled ) , six . text_type ( x ) ) <EOL> self . assertEqual ( unpickled . name , x . name ) <EOL> def test_dict ( self ) : <EOL> lazydict = SimpleLazyObject ( lambda : { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( lazydict [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> lazydict [ '<STR_LIT>' ] = - <NUM_LIT:1> <EOL> self . assertEqual ( lazydict [ '<STR_LIT>' ] , - <NUM_LIT:1> ) <EOL> del lazydict [ '<STR_LIT>' ] <EOL> with self . assertRaises ( KeyError ) : <EOL> lazydict [ '<STR_LIT>' ] <EOL> def test_trace ( self ) : <EOL> old_trace_func = sys . gettrace ( ) <EOL> try : <EOL> def trace_func ( frame , event , arg ) : <EOL> frame . f_locals [ '<STR_LIT>' ] . __class__ <EOL> if old_trace_func is not None : <EOL> old_trace_func ( frame , event , arg ) <EOL> sys . settrace ( trace_func ) <EOL> SimpleLazyObject ( None ) <EOL> finally : <EOL> sys . settrace ( old_trace_func ) <EOL> def test_not_equal ( self ) : <EOL> lazy1 = SimpleLazyObject ( lambda : <NUM_LIT:2> ) <EOL> lazy2 = SimpleLazyObject ( lambda : <NUM_LIT:2> ) <EOL> lazy3 = SimpleLazyObject ( lambda : <NUM_LIT:3> ) <EOL> self . assertEqual ( lazy1 , lazy2 ) <EOL> self . assertNotEqual ( lazy1 , lazy3 ) <EOL> self . assertTrue ( lazy1 != lazy3 ) <EOL> self . assertFalse ( lazy1 != lazy2 ) <EOL> class TestUtilsSimpleLazyObjectDjangoTestCase ( DjangoTestCase ) : <EOL> def test_pickle_py2_regression ( self ) : <EOL> user = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> x = SimpleLazyObject ( lambda : user ) <EOL> pickled = pickle . dumps ( x ) <EOL> pickled = pickle . dumps ( x , <NUM_LIT:0> ) <EOL> pickled = pickle . dumps ( x , <NUM_LIT:1> ) <EOL> pickled = pickle . dumps ( x , <NUM_LIT:2> ) <EOL> if six . PY2 : <EOL> import cPickle <EOL> pickled = cPickle . dumps ( x ) </s>
