<s> import unittest <EOL> import sys <EOL> from chai . comparators import * <EOL> if sys . version_info . major == <NUM_LIT:2> : <EOL> from comparator_py2 import * <EOL> class ComparatorsTest ( unittest . TestCase ) : <EOL> def test_build_comparators_builds_equals ( self ) : <EOL> comp = build_comparators ( "<STR_LIT:str>" ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( comp , Equals ) ) <EOL> comp = build_comparators ( <NUM_LIT:12> ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( comp , Equals ) ) <EOL> comp = build_comparators ( <NUM_LIT> ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( comp , Equals ) ) <EOL> comp = build_comparators ( [ ] ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( comp , Equals ) ) <EOL> comp = build_comparators ( { } ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( comp , Equals ) ) <EOL> comp = build_comparators ( tuple ( ) ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( comp , Equals ) ) <EOL> def test_build_comparators_is_a ( self ) : <EOL> class CustomObject ( object ) : pass <EOL> comp = build_comparators ( CustomObject ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( comp , Any ) ) <EOL> self . assertTrue ( comp . test ( CustomObject ) ) <EOL> self . assertTrue ( comp . test ( CustomObject ( ) ) ) <EOL> def test_build_comparators_passes_comparators ( self ) : <EOL> any_comp = Any ( ) <EOL> comp = build_comparators ( any_comp ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( comp is any_comp ) <EOL> def test_equals ( self ) : <EOL> comp = Equals ( <NUM_LIT:3> ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT:3> ) ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT> ) ) <EOL> self . assertFalse ( comp . test ( '<STR_LIT:3>' ) ) <EOL> def test_equals_repr ( self ) : <EOL> comp = Equals ( <NUM_LIT:3> ) <EOL> self . assertEquals ( str ( comp ) , "<STR_LIT:3>" ) <EOL> def test_eq ( self ) : <EOL> comp = Equals ( <NUM_LIT:3> ) <EOL> self . assertEquals ( comp , <NUM_LIT:3> ) <EOL> def test_is_a ( self ) : <EOL> comp = IsA ( str ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT:foo>' ) ) <EOL> if sys . version_info . major == <NUM_LIT:2> : <EOL> self . assertFalse ( comp . test ( bytearray ( '<STR_LIT:foo>' ) ) ) <EOL> else : <EOL> self . assertFalse ( comp . test ( bytearray ( '<STR_LIT:foo>' . encode ( '<STR_LIT:ascii>' ) ) ) ) <EOL> comp = IsA ( ( str , int ) ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT> ) ) <EOL> self . assertFalse ( comp . test ( <NUM_LIT> ) ) <EOL> def test_is_a_repr ( self ) : <EOL> comp = IsA ( str ) <EOL> self . assertEquals ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_is_a_format_name ( self ) : <EOL> comp = IsA ( str ) <EOL> self . assertEquals ( comp . _format_name ( ) , "<STR_LIT:str>" ) <EOL> comp = IsA ( ( str , list ) ) <EOL> self . assertEquals ( comp . _format_name ( ) , "<STR_LIT>" ) <EOL> def test_is ( self ) : <EOL> class Test ( object ) : <EOL> def __eq__ ( self , other ) : return True <EOL> obj1 = Test ( ) <EOL> obj2 = Test ( ) <EOL> comp = Is ( obj1 ) <EOL> self . assertEquals ( obj1 , obj2 ) <EOL> self . assertTrue ( comp . test ( obj1 ) ) <EOL> self . assertFalse ( comp . test ( obj2 ) ) <EOL> def test_is_repr ( self ) : <EOL> class TestObj ( object ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> obj = TestObj ( ) <EOL> self . assertEquals ( repr ( Is ( obj ) ) , "<STR_LIT>" ) <EOL> def test_almost_equal ( self ) : <EOL> comp = AlmostEqual ( <NUM_LIT> , <NUM_LIT:3> ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT> ) ) <EOL> self . assertFalse ( comp . test ( <NUM_LIT> ) ) <EOL> def test_almost_equal_repr ( self ) : <EOL> comp = AlmostEqual ( <NUM_LIT> , <NUM_LIT:3> ) <EOL> self . assertEquals ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_regex ( self ) : <EOL> comp = Regex ( '<STR_LIT>' ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( comp . test ( '<STR_LIT>' ) ) <EOL> def test_regex_repr ( self ) : <EOL> comp = Regex ( '<STR_LIT>' ) <EOL> self . assertEquals ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_any ( self ) : <EOL> comp = Any ( <NUM_LIT:1> , <NUM_LIT> , str ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT:1> ) ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT> ) ) <EOL> self . assertFalse ( comp . test ( <NUM_LIT:4> ) ) <EOL> def test_any_repr ( self ) : <EOL> comp = Any ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , str ) <EOL> if sys . version_info . major == <NUM_LIT:2> : <EOL> self . assertEquals ( repr ( comp ) , "<STR_LIT>" ) <EOL> else : <EOL> self . assertEquals ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_in ( self ) : <EOL> comp = In ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT:foo>' ) ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT:bar>' ) ) <EOL> self . assertFalse ( comp . test ( '<STR_LIT:none>' ) ) <EOL> def test_in_repr ( self ) : <EOL> comp = In ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> self . assertEqual ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_contains ( self ) : <EOL> comp = Contains ( '<STR_LIT:foo>' ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( comp . test ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) ) <EOL> self . assertTrue ( comp . test ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) ) <EOL> self . assertFalse ( comp . test ( '<STR_LIT>' ) ) <EOL> def test_contains_repr ( self ) : <EOL> comp = Contains ( "<STR_LIT:foo>" ) <EOL> self . assertEqual ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_all ( self ) : <EOL> comp = All ( IsA ( bytearray ) , Equals ( '<STR_LIT:foo>' . encode ( '<STR_LIT:ascii>' ) ) ) <EOL> self . assertTrue ( comp . test ( bytearray ( '<STR_LIT:foo>' . encode ( '<STR_LIT:ascii>' ) ) ) ) <EOL> self . assertFalse ( comp . test ( '<STR_LIT:foo>' ) ) <EOL> self . assertEquals ( '<STR_LIT:foo>' . encode ( '<STR_LIT:ascii>' ) , bytearray ( '<STR_LIT:foo>' . encode ( '<STR_LIT:ascii>' ) ) ) <EOL> def test_all_repr ( self ) : <EOL> comp = All ( IsA ( bytearray ) , Equals ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_not ( self ) : <EOL> comp = Not ( Any ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT:2> ) ) <EOL> self . assertFalse ( comp . test ( <NUM_LIT:1> ) ) <EOL> self . assertFalse ( comp . test ( <NUM_LIT:3> ) ) <EOL> def test_no_repr ( self ) : <EOL> comp = Not ( Any ( <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> self . assertEqual ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_function ( self ) : <EOL> r = [ True , False ] <EOL> comp = Function ( lambda arg : r [ arg ] ) <EOL> self . assertTrue ( comp . test ( <NUM_LIT:0> ) ) <EOL> self . assertFalse ( comp . test ( <NUM_LIT:1> ) ) <EOL> def test_function_repr ( self ) : <EOL> func = lambda arg : True <EOL> comp = Function ( func ) <EOL> self . assertEqual ( repr ( comp ) , "<STR_LIT>" % str ( func ) ) <EOL> def test_ignore ( self ) : <EOL> comp = Ignore ( ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT>' ) ) <EOL> def test_ignore_repr ( self ) : <EOL> comp = Ignore ( ) <EOL> self . assertEqual ( repr ( comp ) , "<STR_LIT>" ) <EOL> def test_variable ( self ) : <EOL> comp = Variable ( '<STR_LIT:foo>' ) <EOL> self . assertEquals ( <NUM_LIT:0> , len ( Variable . _cache ) ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT:bar>' ) ) <EOL> self . assertEquals ( <NUM_LIT:1> , len ( Variable . _cache ) ) <EOL> self . assertTrue ( comp . test ( '<STR_LIT:bar>' ) ) <EOL> self . assertFalse ( comp . test ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( Variable ( '<STR_LIT:foo>' ) . test ( '<STR_LIT:bar>' ) ) <EOL> self . assertFalse ( Variable ( '<STR_LIT:foo>' ) . test ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( <NUM_LIT:1> , len ( Variable . _cache ) ) <EOL> self . assertEquals ( '<STR_LIT:bar>' , comp . value ) <EOL> self . assertEquals ( '<STR_LIT:bar>' , Variable ( '<STR_LIT:foo>' ) . value ) <EOL> v = Variable ( '<STR_LIT>' ) <EOL> self . assertEquals ( <NUM_LIT:1> , len ( Variable . _cache ) ) <EOL> v . test ( '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , v . value ) <EOL> self . assertEquals ( <NUM_LIT:2> , len ( Variable . _cache ) ) <EOL> Variable . clear ( ) <EOL> self . assertEquals ( <NUM_LIT:0> , len ( Variable . _cache ) ) <EOL> def test_variable_repr ( self ) : <EOL> v = Variable ( '<STR_LIT:foo>' ) <EOL> self . assertEquals ( repr ( v ) , "<STR_LIT>" ) <EOL> def test_like_init ( self ) : <EOL> c = Like ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> self . assertEquals ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , c . _src ) <EOL> c = Like ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> self . assertEquals ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] , c . _src ) <EOL> def test_like_test ( self ) : <EOL> c = Like ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> self . assertTrue ( c . test ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) ) <EOL> self . assertTrue ( c . test ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> self . assertFalse ( c . test ( { '<STR_LIT:foo>' : '<STR_LIT>' } ) ) <EOL> c = Like ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> self . assertTrue ( c . test ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) ) <EOL> self . assertTrue ( c . test ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertFalse ( c . test ( [ '<STR_LIT:foo>' , '<STR_LIT>' ] ) ) <EOL> def test_like_repr ( self ) : <EOL> c = Like ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> self . assertEquals ( repr ( c ) , "<STR_LIT>" ) </s>
