<s> import contextlib <EOL> from itertools import islice <EOL> import re <EOL> import warnings <EOL> import six <EOL> from . utils import stringdiffer <EOL> __testify = <NUM_LIT:1> <EOL> def _val_subtract ( val1 , val2 , dict_subtractor , list_subtractor ) : <EOL> """<STR_LIT>""" <EOL> if val1 == val2 : <EOL> return type ( val1 ) ( ) <EOL> if isinstance ( val1 , dict ) and isinstance ( val2 , dict ) : <EOL> val_diff = dict_subtractor ( val1 , val2 ) <EOL> elif isinstance ( val1 , ( list , tuple ) ) and isinstance ( val2 , ( list , tuple ) ) : <EOL> val_diff = list_subtractor ( val1 , val2 ) <EOL> else : <EOL> val_diff = val1 <EOL> return val_diff <EOL> def _dict_subtract ( dict1 , dict2 ) : <EOL> """<STR_LIT>""" <EOL> result = dict ( dict1 ) <EOL> common_keys = set ( dict1 . keys ( ) ) & set ( dict2 . keys ( ) ) <EOL> for key in common_keys : <EOL> val1 , val2 = dict1 [ key ] , dict2 [ key ] <EOL> if val1 == val2 : <EOL> del result [ key ] <EOL> else : <EOL> result [ key ] = _val_subtract ( val1 , val2 , _dict_subtract , _list_subtract ) <EOL> return result <EOL> def _list_subtract ( list1 , list2 ) : <EOL> """<STR_LIT>""" <EOL> res_list = [ _val_subtract ( val1 , val2 , _dict_subtract , _list_subtract ) <EOL> for val1 , val2 in zip ( list1 , list2 ) if val1 != val2 ] <EOL> res_list += list1 [ len ( list2 ) : ] <EOL> if isinstance ( list1 , tuple ) : <EOL> return tuple ( res_list ) <EOL> else : <EOL> return res_list <EOL> def assert_raises ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if ( len ( args ) == <NUM_LIT:1> ) and not kwargs : <EOL> return _assert_raises_context_manager ( args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return _assert_raises ( * args , ** kwargs ) <EOL> def assert_raises_such_that ( exception_class , exception_test = lambda e : e , callable_obj = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if callable_obj is None : <EOL> return _assert_raises_context_manager ( exception_class , exception_test ) <EOL> else : <EOL> with _assert_raises_context_manager ( exception_class , exception_test ) : <EOL> callable_obj ( * args , ** kwargs ) <EOL> def assert_raises_exactly ( exception_class , * args ) : <EOL> """<STR_LIT>""" <EOL> def test_exception ( exception ) : <EOL> assert_is ( type ( exception ) , exception_class ) <EOL> assert_equal ( exception . args , args ) <EOL> return assert_raises_such_that ( exception_class , test_exception ) <EOL> def assert_raises_and_contains ( expected_exception_class , strings , callable_obj , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> callable_obj ( * args , ** kwargs ) <EOL> except expected_exception_class as e : <EOL> message = str ( e ) . lower ( ) <EOL> try : <EOL> is_string = isinstance ( strings , basestring ) <EOL> except NameError : <EOL> is_string = isinstance ( strings , str ) <EOL> if is_string : <EOL> strings = [ strings ] <EOL> for string in strings : <EOL> assert_in ( string . lower ( ) , message ) <EOL> else : <EOL> assert_not_reached ( "<STR_LIT>" % expected_exception_class ) <EOL> @ contextlib . contextmanager <EOL> def _assert_raises_context_manager ( exception_class , exception_test = lambda e : e ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> yield <EOL> except exception_class as e : <EOL> exception_test ( e ) <EOL> else : <EOL> assert_not_reached ( "<STR_LIT>" % <EOL> exception_class ) <EOL> def _assert_raises ( exception_class , callable , * args , ** kwargs ) : <EOL> with _assert_raises_context_manager ( exception_class ) : <EOL> callable ( * args , ** kwargs ) <EOL> def _diff_message ( lhs , rhs ) : <EOL> """<STR_LIT>""" <EOL> lhs = _to_characters ( lhs ) <EOL> rhs = _to_characters ( rhs ) <EOL> message = u'<STR_LIT>' % stringdiffer . highlight ( lhs , rhs ) <EOL> if six . PY2 : <EOL> return message . encode ( '<STR_LIT>' ) <EOL> else : <EOL> return message <EOL> def assert_equal ( lval , rval , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> assert lval == rval , message <EOL> else : <EOL> assert lval == rval , "<STR_LIT>" % ( lval , rval , _diff_message ( lval , rval ) ) <EOL> assert_equals = assert_equal <EOL> def _get_msg ( args , kwargs , suggestion ) : <EOL> if args : <EOL> raise TypeError ( <EOL> '<STR_LIT>' . format ( <EOL> suggestion , <EOL> ) , <EOL> ) <EOL> message = kwargs . pop ( '<STR_LIT:message>' , None ) <EOL> if kwargs : <EOL> raise TypeError ( '<STR_LIT>' . format ( kwargs ) ) <EOL> return message <EOL> def assert_truthy ( lval , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> message = _get_msg ( args , kwargs , '<STR_LIT>' ) <EOL> if message : <EOL> assert lval , message <EOL> else : <EOL> assert lval , "<STR_LIT>" % ( <EOL> lval , True , _diff_message ( lval , True ) , <EOL> ) <EOL> def assert_falsey ( lval , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> message = _get_msg ( args , kwargs , '<STR_LIT>' ) <EOL> if message : <EOL> assert not lval , message <EOL> else : <EOL> assert not lval , "<STR_LIT>" % ( <EOL> lval , False , _diff_message ( lval , False ) , <EOL> ) <EOL> def assert_almost_equal ( lval , rval , digits , message = None ) : <EOL> """<STR_LIT>""" <EOL> real_message = message or "<STR_LIT>" % ( lval , rval ) <EOL> assert round ( lval , digits ) == round ( rval , digits ) , real_message <EOL> def assert_within_tolerance ( lval , rval , tolerance , message = None ) : <EOL> """<STR_LIT>""" <EOL> real_message = message or "<STR_LIT>" % ( lval , rval ) <EOL> assert abs ( float ( lval ) - float ( rval ) ) / float ( lval ) < tolerance , real_message <EOL> def assert_not_equal ( lval , rval , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> assert lval != rval , message <EOL> else : <EOL> assert lval != rval , '<STR_LIT>' % ( lval , rval ) <EOL> def assert_lt ( lval , rval , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> assert lval < rval , message <EOL> else : <EOL> assert lval < rval , '<STR_LIT>' % ( lval , rval ) <EOL> def assert_lte ( lval , rval , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> assert lval <= rval , message <EOL> else : <EOL> assert lval <= rval , '<STR_LIT>' % ( lval , rval ) <EOL> def assert_gt ( lval , rval , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> assert lval > rval , message <EOL> else : <EOL> assert lval > rval , '<STR_LIT>' % ( lval , rval ) <EOL> def assert_gte ( lval , rval , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> assert lval >= rval , message <EOL> else : <EOL> assert lval >= rval , '<STR_LIT>' % ( lval , rval ) <EOL> def assert_in_range ( val , start , end , message = None , inclusive = False ) : <EOL> """<STR_LIT>""" <EOL> if inclusive : <EOL> real_message = message or "<STR_LIT>" % ( start , val , end ) <EOL> assert start <= val <= end , real_message <EOL> else : <EOL> real_message = message or "<STR_LIT>" % ( start , val , end ) <EOL> assert start < val < end , real_message <EOL> def assert_between ( a , b , c ) : <EOL> """<STR_LIT>""" <EOL> assert_in_range ( b , a , c , inclusive = True ) <EOL> def assert_in ( item , sequence , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> assert item in sequence , message % { '<STR_LIT>' : item , '<STR_LIT>' : sequence } <EOL> def assert_not_in ( item , sequence , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> assert item not in sequence , message % { '<STR_LIT>' : item , '<STR_LIT>' : sequence } <EOL> def assert_all_in ( left , right ) : <EOL> """<STR_LIT>""" <EOL> unmatching = [ ] <EOL> for item in left : <EOL> if item not in right : <EOL> unmatching . append ( item ) <EOL> if unmatching : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' % ( right , unmatching ) <EOL> ) <EOL> def assert_starts_with ( val , prefix ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" % locals ( ) <EOL> assert val . startswith ( prefix ) , message <EOL> def assert_not_reached ( message = None ) : <EOL> """<STR_LIT>""" <EOL> if message : <EOL> assert False , message <EOL> else : <EOL> assert False , '<STR_LIT>' <EOL> def assert_rows_equal ( rows1 , rows2 ) : <EOL> """<STR_LIT>""" <EOL> def norm_row ( row ) : <EOL> if isinstance ( row , dict ) : <EOL> return tuple ( ( k , row [ k ] ) for k in sorted ( row ) ) <EOL> else : <EOL> return tuple ( sorted ( row ) ) <EOL> def norm_rows ( rows ) : <EOL> return tuple ( sorted ( norm_row ( row ) for row in rows ) ) <EOL> assert_equal ( norm_rows ( rows1 ) , norm_rows ( rows2 ) ) <EOL> def assert_empty ( iterable , max_elements_to_print = None , message = None ) : <EOL> """<STR_LIT>""" <EOL> if max_elements_to_print is None : <EOL> try : <EOL> max_elements_to_print = len ( iterable ) <EOL> except TypeError : <EOL> max_elements_to_print = <NUM_LIT:10> <EOL> message = message or "<STR_LIT>" . format ( iterable ) <EOL> if max_elements_to_print == <NUM_LIT:0> : <EOL> sample = list ( islice ( iterable , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> else : <EOL> sample_plus_extra = list ( islice ( iterable , <NUM_LIT:0> , max_elements_to_print + <NUM_LIT:1> ) ) <EOL> sample_is_whole_iterable = len ( sample_plus_extra ) <= max_elements_to_print <EOL> sample = sample_plus_extra [ : max_elements_to_print ] <EOL> if sample_is_whole_iterable : <EOL> message += '<STR_LIT>' % sample <EOL> else : <EOL> message += '<STR_LIT>' % ( len ( sample ) , sample ) <EOL> assert len ( sample ) == <NUM_LIT:0> , message <EOL> def assert_not_empty ( iterable , message = None ) : <EOL> """<STR_LIT>""" <EOL> for value in iterable : <EOL> break <EOL> else : <EOL> raise AssertionError ( message if message else <EOL> "<STR_LIT>" . format ( iterable ) <EOL> ) <EOL> def assert_length ( sequence , expected , message = None ) : <EOL> """<STR_LIT>""" <EOL> message = message or "<STR_LIT>" <EOL> length = len ( list ( sequence ) ) <EOL> assert length == expected , message % locals ( ) <EOL> def assert_call ( turtle , call_idx , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> actual = turtle . calls [ call_idx ] if turtle . calls else None <EOL> message = "<STR_LIT>" % ( call_idx , ( args , kwargs ) , actual ) <EOL> assert actual == ( args , kwargs ) , message <EOL> def assert_is ( left , right , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> assert left is right , message % { '<STR_LIT:left>' : left , '<STR_LIT:right>' : right } <EOL> def assert_is_not ( left , right , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> assert left is not right , message % { '<STR_LIT:left>' : left , '<STR_LIT:right>' : right } <EOL> def assert_all_match_regex ( pattern , values , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> for value in values : <EOL> assert re . match ( pattern , value ) , message % { '<STR_LIT:value>' : value , '<STR_LIT>' : pattern } <EOL> def assert_match_regex ( pattern , value , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> assert_all_match_regex ( pattern , [ value ] , * args , ** kwargs ) <EOL> def assert_any_match_regex ( pattern , values , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> for value in values : <EOL> if re . match ( pattern , value ) is not None : <EOL> return <EOL> raise AssertionError ( message % { '<STR_LIT>' : values , '<STR_LIT>' : pattern } ) <EOL> def assert_all_not_match_regex ( pattern , values , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> for value in values : <EOL> assert not re . match ( pattern , value ) , message % { '<STR_LIT:value>' : value , '<STR_LIT>' : pattern } <EOL> def assert_sets_equal ( <EOL> left , <EOL> right , <EOL> message = "<STR_LIT>" , <EOL> msg = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> if left != right : <EOL> extra_left = left - right <EOL> extra_right = right - left <EOL> raise AssertionError ( message % { <EOL> '<STR_LIT:left>' : left , <EOL> '<STR_LIT:right>' : right , <EOL> '<STR_LIT>' : extra_left , <EOL> '<STR_LIT>' : extra_right , <EOL> } ) <EOL> def assert_dicts_equal ( <EOL> left , <EOL> right , <EOL> ignore_keys = None , <EOL> message = "<STR_LIT>" , <EOL> msg = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> if ignore_keys is not None : <EOL> left = dict ( ( k , left [ k ] ) for k in left if k not in ignore_keys ) <EOL> right = dict ( ( k , right [ k ] ) for k in right if k not in ignore_keys ) <EOL> if left == right : <EOL> return <EOL> extra_left = _dict_subtract ( left , right ) <EOL> extra_right = _dict_subtract ( right , left ) <EOL> raise AssertionError ( message % { <EOL> '<STR_LIT:left>' : left , <EOL> '<STR_LIT:right>' : right , <EOL> '<STR_LIT>' : extra_left , <EOL> '<STR_LIT>' : extra_right , <EOL> } ) <EOL> def assert_dict_subset ( left , right , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> difference_dict = _dict_subtract ( left , right ) <EOL> if not difference_dict : <EOL> return <EOL> extra_left = difference_dict <EOL> small_right = dict ( ( k , right [ k ] ) for k in right if k in left . keys ( ) ) <EOL> extra_right = _dict_subtract ( small_right , left ) <EOL> raise AssertionError ( message % { <EOL> '<STR_LIT:left>' : left , <EOL> '<STR_LIT:right>' : right , <EOL> '<STR_LIT>' : extra_left , <EOL> '<STR_LIT>' : extra_right , <EOL> } ) <EOL> def assert_subset ( left , right , message = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if msg : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> message = msg <EOL> set_left = set ( left ) <EOL> set_right = set ( right ) <EOL> if not ( set_left <= set_right ) : <EOL> extra = set_left - set_right <EOL> raise AssertionError ( message % { <EOL> '<STR_LIT:left>' : left , <EOL> '<STR_LIT:right>' : right , <EOL> '<STR_LIT>' : set_left , <EOL> '<STR_LIT>' : set_right , <EOL> '<STR_LIT>' : extra , <EOL> } ) <EOL> def assert_list_prefix ( left , right ) : <EOL> """<STR_LIT>""" <EOL> assert_equal ( left , right [ : len ( left ) ] ) <EOL> def assert_sorted_equal ( left , right , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> assert_equal ( sorted ( left ) , sorted ( right ) , ** kwargs ) <EOL> def assert_isinstance ( object_ , type_ ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( object_ , type_ ) , "<STR_LIT>" % ( type_ , type ( object_ ) ) <EOL> def assert_datetimes_equal ( a , b ) : <EOL> """<STR_LIT>""" <EOL> assert_equal ( a . utctimetuple ( ) [ : - <NUM_LIT:3> ] , b . utctimetuple ( ) [ : - <NUM_LIT:3> ] , "<STR_LIT>" % ( a , b ) ) <EOL> def assert_exactly_one ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> truthy_fxn = kwargs . pop ( '<STR_LIT>' , lambda x : x is not None ) <EOL> assert not kwargs , "<STR_LIT>" % kwargs <EOL> true_args = [ arg for arg in args if truthy_fxn ( arg ) ] <EOL> if len ( true_args ) != <NUM_LIT:1> : <EOL> raise AssertionError ( "<STR_LIT>" % ( len ( true_args ) , args ) ) <EOL> return true_args [ <NUM_LIT:0> ] <EOL> @ contextlib . contextmanager <EOL> def _assert_warns_context_manager ( warning_class = None , warnings_test = None ) : <EOL> """<STR_LIT>""" <EOL> with warnings . catch_warnings ( record = True ) as caught : <EOL> warnings . resetwarnings ( ) <EOL> if warning_class : <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' ) <EOL> warnings . simplefilter ( '<STR_LIT>' , category = warning_class ) <EOL> else : <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> yield <EOL> assert_gt ( len ( caught ) , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> if warnings_test : <EOL> warnings_test ( caught ) <EOL> def assert_warns ( warning_class = None , callable = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if callable is None : <EOL> return _assert_warns_context_manager ( warning_class = warning_class ) <EOL> else : <EOL> with _assert_warns_context_manager ( warning_class = warning_class ) : <EOL> callable ( * args , ** kwargs ) <EOL> def assert_warns_such_that ( warnings_test , callable = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if callable is None : <EOL> return _assert_warns_context_manager ( warnings_test = warnings_test ) <EOL> else : <EOL> with _assert_warns_context_manager ( warnings_test = warnings_test ) : <EOL> callable ( * args , ** kwargs ) <EOL> def _to_characters ( x ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x , six . text_type ) : <EOL> return x <EOL> try : <EOL> return six . text_type ( x , '<STR_LIT>' ) <EOL> except UnicodeDecodeError : <EOL> return six . text_type ( x , '<STR_LIT>' ) <EOL> except TypeError : <EOL> try : <EOL> return six . text_type ( x ) <EOL> except UnicodeDecodeError : <EOL> try : <EOL> return bytes ( x ) . decode ( '<STR_LIT>' ) <EOL> except UnicodeDecodeError : <EOL> return bytes ( x ) . decode ( '<STR_LIT>' ) </s>
