<s> from pypred import predicate , merge , ast <EOL> try : <EOL> from mock import patch <EOL> except ImportError : <EOL> from unittest . mock import patch <EOL> DEEP = merge . RefactorSettings . deep ( ) <EOL> class TestMerge ( object ) : <EOL> def test_names ( self ) : <EOL> n1 = ast . Literal ( "<STR_LIT:foo>" ) <EOL> assert ( "<STR_LIT>" , "<STR_LIT:foo>" ) == merge . node_name ( n1 ) <EOL> n2 = ast . Number ( <NUM_LIT:12> ) <EOL> assert ( "<STR_LIT>" , <NUM_LIT:12> ) == merge . node_name ( n2 ) <EOL> n3 = ast . Constant ( True ) <EOL> assert ( "<STR_LIT>" , True ) == merge . node_name ( n3 ) <EOL> n4 = ast . Regex ( "<STR_LIT>" ) <EOL> assert ( "<STR_LIT>" , "<STR_LIT>" ) == merge . node_name ( n4 ) <EOL> n5 = ast . Undefined ( ) <EOL> assert "<STR_LIT>" == merge . node_name ( n5 ) <EOL> n6 = ast . Empty ( ) <EOL> assert "<STR_LIT>" == merge . node_name ( n6 ) <EOL> n7 = ast . NegateOperator ( n3 ) <EOL> assert ( "<STR_LIT>" , True ) == merge . node_name ( n7 ) <EOL> n8 = ast . CompareOperator ( '<STR_LIT:=>' , n1 , n2 ) <EOL> n8_name = merge . node_name ( n8 ) <EOL> assert ( "<STR_LIT>" , "<STR_LIT:=>" , ( "<STR_LIT>" , "<STR_LIT:foo>" ) , ( "<STR_LIT>" , <NUM_LIT:12> ) ) == n8_name <EOL> n8_static = merge . node_name ( n8 , True ) <EOL> assert ( "<STR_LIT>" , "<STR_LIT>" , ( "<STR_LIT>" , "<STR_LIT:foo>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ) == n8_static <EOL> n9 = ast . MatchOperator ( n1 , n4 ) <EOL> n9_name = merge . node_name ( n9 ) <EOL> assert ( "<STR_LIT>" , ( "<STR_LIT>" , "<STR_LIT:foo>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ) == n9_name <EOL> n10 = ast . ContainsOperator ( n1 , n2 ) <EOL> n10_name = merge . node_name ( n10 ) <EOL> assert ( "<STR_LIT>" , ( "<STR_LIT>" , "<STR_LIT:foo>" ) , ( "<STR_LIT>" , <NUM_LIT> ) ) == n10_name <EOL> n11 = ast . LogicalOperator ( '<STR_LIT>' , n1 , n3 ) <EOL> n11_name = merge . node_name ( n11 ) <EOL> assert ( "<STR_LIT>" , "<STR_LIT:foo>" ) == n11_name <EOL> n12 = ast . LiteralSet ( [ n1 , n2 ] ) <EOL> n12_name = merge . node_name ( n12 ) <EOL> assert "<STR_LIT>" == n12_name <EOL> def test_count ( self ) : <EOL> pred_str = """<STR_LIT>""" <EOL> p1 = predicate . Predicate ( pred_str ) <EOL> count , names = merge . count_expressions ( p1 . ast ) <EOL> assert len ( count ) == <NUM_LIT:5> <EOL> assert ( "<STR_LIT>" , "<STR_LIT:test>" ) in names <EOL> k = ( "<STR_LIT>" , "<STR_LIT>" , ( "<STR_LIT>" , "<STR_LIT:foo>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> assert k in names <EOL> assert count [ k ] == <NUM_LIT:2> <EOL> def test_rewrite_norm ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . Literal ( '<STR_LIT:foo>' ) <EOL> r = ast . Literal ( '<STR_LIT:bar>' ) <EOL> c = ast . LogicalOperator ( '<STR_LIT>' , l , r ) <EOL> name = merge . node_name ( c ) <EOL> merge . rewrite_ast ( c , name , l , True ) <EOL> assert isinstance ( c . left , ast . Constant ) <EOL> def test_rewrite_compare ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . Literal ( '<STR_LIT:foo>' ) <EOL> r = ast . Literal ( '<STR_LIT:bar>' ) <EOL> n = ast . CompareOperator ( '<STR_LIT:>>' , l , r ) <EOL> name = merge . node_name ( n ) <EOL> with patch ( '<STR_LIT>' ) as c : <EOL> merge . rewrite_ast ( n , name , n , True ) <EOL> assert c . called <EOL> def test_rewrite_contains ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . LiteralSet ( [ ast . Number ( <NUM_LIT:1> ) , ast . Number ( <NUM_LIT:2> ) ] ) <EOL> r = ast . Literal ( '<STR_LIT:bar>' ) <EOL> c = ast . ContainsOperator ( l , r ) <EOL> name = merge . node_name ( c ) <EOL> with patch ( '<STR_LIT>' ) as cr : <EOL> merge . rewrite_ast ( c , name , c , True ) <EOL> assert cr . called <EOL> def test_select_expr_negate ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . Literal ( '<STR_LIT:foo>' ) <EOL> n = ast . NegateOperator ( l ) <EOL> name = merge . node_name ( n ) <EOL> expr = merge . select_rewrite_expression ( DEEP , name , [ n ] ) <EOL> assert expr == l <EOL> def test_select_expr_logical ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . Literal ( '<STR_LIT:foo>' ) <EOL> r = ast . Literal ( '<STR_LIT:bar>' ) <EOL> n = ast . LogicalOperator ( '<STR_LIT>' , l , r ) <EOL> name = merge . node_name ( n ) <EOL> expr = merge . select_rewrite_expression ( DEEP , name , [ n ] ) <EOL> assert expr == l <EOL> n . left = None <EOL> expr = merge . select_rewrite_expression ( DEEP , name , [ n ] ) <EOL> assert expr == r <EOL> def test_select_expr_first ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . Literal ( '<STR_LIT:foo>' ) <EOL> r = ast . Literal ( '<STR_LIT:bar>' ) <EOL> name = merge . node_name ( l ) <EOL> expr = merge . select_rewrite_expression ( DEEP , name , [ l , r ] ) <EOL> assert expr == l <EOL> def test_select_expr_compare ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . Literal ( '<STR_LIT:foo>' ) <EOL> r = ast . Literal ( '<STR_LIT:bar>' ) <EOL> n = ast . CompareOperator ( '<STR_LIT:>>' , l , r ) <EOL> name = merge . node_name ( n ) <EOL> with patch ( '<STR_LIT>' ) as c : <EOL> merge . select_rewrite_expression ( DEEP , name , [ n ] ) <EOL> assert c . called <EOL> def test_select_expr_contains ( self ) : <EOL> "<STR_LIT>" <EOL> l = ast . LiteralSet ( [ ast . Number ( <NUM_LIT:1> ) , ast . Number ( <NUM_LIT:2> ) ] ) <EOL> r = ast . Literal ( '<STR_LIT:bar>' ) <EOL> c = ast . ContainsOperator ( l , r ) <EOL> name = merge . node_name ( c ) <EOL> with patch ( '<STR_LIT>' ) as cr : <EOL> merge . select_rewrite_expression ( DEEP , name , [ c ] ) <EOL> assert cr . called <EOL> def test_merge ( self ) : <EOL> "<STR_LIT>" <EOL> p1 = predicate . Predicate ( '<STR_LIT:foo>' ) <EOL> p2 = predicate . Predicate ( '<STR_LIT:bar>' ) <EOL> p3 = predicate . Predicate ( '<STR_LIT>' ) <EOL> p4 = predicate . Predicate ( '<STR_LIT>' ) <EOL> m = merge . merge ( [ p1 , p2 , p3 , p4 ] ) <EOL> assert isinstance ( m , ast . Both ) <EOL> assert isinstance ( m . left , ast . Both ) <EOL> assert isinstance ( m . right , ast . Both ) <EOL> assert m . left . left . pred == p1 <EOL> assert m . left . right . pred == p2 <EOL> assert m . right . left . pred == p3 <EOL> assert m . right . right . pred == p4 </s>
