<s> from __future__ import absolute_import , with_statement <EOL> from mock import Mock , patch <EOL> from nose . tools import * <EOL> import parse <EOL> from behave . matchers import Match , Matcher , ParseMatcher , RegexMatcher <EOL> from behave import matchers , runner <EOL> class DummyMatcher ( Matcher ) : <EOL> desired_result = None <EOL> def check_match ( self , step ) : <EOL> return DummyMatcher . desired_result <EOL> class TestMatcher ( object ) : <EOL> def setUp ( self ) : <EOL> DummyMatcher . desired_result = None <EOL> def test_returns_none_if_check_match_returns_none ( self ) : <EOL> matcher = DummyMatcher ( None , None ) <EOL> assert matcher . match ( '<STR_LIT>' ) is None <EOL> def test_returns_match_object_if_check_match_returns_arguments ( self ) : <EOL> arguments = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> func = lambda x : - x <EOL> DummyMatcher . desired_result = arguments <EOL> matcher = DummyMatcher ( func , None ) <EOL> match = matcher . match ( '<STR_LIT>' ) <EOL> assert isinstance ( match , Match ) <EOL> assert match . func is func <EOL> assert match . arguments == arguments <EOL> class TestParseMatcher ( object ) : <EOL> def setUp ( self ) : <EOL> self . recorded_args = None <EOL> def record_args ( self , * args , ** kwargs ) : <EOL> self . recorded_args = ( args , kwargs ) <EOL> def test_returns_none_if_parser_does_not_match ( self ) : <EOL> matcher = ParseMatcher ( None , '<STR_LIT>' ) <EOL> with patch . object ( matcher . parser , '<STR_LIT>' ) as parse : <EOL> parse . return_value = None <EOL> assert matcher . match ( '<STR_LIT>' ) is None <EOL> def test_returns_arguments_based_on_matches ( self ) : <EOL> func = lambda x : - x <EOL> matcher = ParseMatcher ( func , '<STR_LIT:foo>' ) <EOL> results = parse . Result ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : - <NUM_LIT> } , <EOL> { <EOL> <NUM_LIT:0> : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> <NUM_LIT:1> : ( <NUM_LIT:16> , <NUM_LIT> ) , <EOL> <NUM_LIT:2> : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT:foo>' : ( <NUM_LIT:32> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> } ) <EOL> expected = [ <EOL> ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:1>' , <NUM_LIT:1> , None ) , <EOL> ( <NUM_LIT:16> , <NUM_LIT> , '<STR_LIT:2>' , <NUM_LIT:2> , None ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:3>' , <NUM_LIT:3> , None ) , <EOL> ( <NUM_LIT:32> , <NUM_LIT> , '<STR_LIT:bar>' , '<STR_LIT:bar>' , '<STR_LIT:foo>' ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT>' , - <NUM_LIT> , '<STR_LIT>' ) , <EOL> ] <EOL> with patch . object ( matcher . parser , '<STR_LIT>' ) as p : <EOL> p . return_value = results <EOL> m = matcher . match ( '<STR_LIT>' ) <EOL> assert m . func is func <EOL> args = m . arguments <EOL> have = [ ( a . start , a . end , a . original , a . value , a . name ) for a in args ] <EOL> eq_ ( have , expected ) <EOL> def test_named_arguments ( self ) : <EOL> text = "<STR_LIT>" <EOL> matcher = ParseMatcher ( self . record_args , text ) <EOL> context = runner . Context ( Mock ( ) ) <EOL> m = matcher . match ( "<STR_LIT>" ) <EOL> m . run ( context ) <EOL> eq_ ( self . recorded_args , ( ( context , ) , { <EOL> '<STR_LIT:string>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : <NUM_LIT:11> , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } ) ) <EOL> def test_positional_arguments ( self ) : <EOL> text = "<STR_LIT>" <EOL> matcher = ParseMatcher ( self . record_args , text ) <EOL> context = runner . Context ( Mock ( ) ) <EOL> m = matcher . match ( "<STR_LIT>" ) <EOL> m . run ( context ) <EOL> eq_ ( self . recorded_args , ( ( context , '<STR_LIT:foo>' , <NUM_LIT:11> , <NUM_LIT> ) , { } ) ) <EOL> class TestRegexMatcher ( object ) : <EOL> def test_returns_none_if_regex_does_not_match ( self ) : <EOL> matcher = RegexMatcher ( None , '<STR_LIT>' ) <EOL> regex = Mock ( ) <EOL> regex . match . return_value = None <EOL> matcher . regex = regex <EOL> assert matcher . match ( '<STR_LIT>' ) is None <EOL> def test_returns_arguments_based_on_groups ( self ) : <EOL> func = lambda x : - x <EOL> matcher = RegexMatcher ( func , '<STR_LIT:foo>' ) <EOL> regex = Mock ( ) <EOL> regex . groupindex = { '<STR_LIT:foo>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:5> } <EOL> match = Mock ( ) <EOL> match . groups . return_value = ( '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' , '<STR_LIT:bar>' , '<STR_LIT>' ) <EOL> positions = { <EOL> <NUM_LIT:1> : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> <NUM_LIT:2> : ( <NUM_LIT:16> , <NUM_LIT> ) , <EOL> <NUM_LIT:3> : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> <NUM_LIT:4> : ( <NUM_LIT:32> , <NUM_LIT> ) , <EOL> <NUM_LIT:5> : ( <NUM_LIT> , <NUM_LIT> ) , <EOL> } <EOL> match . start . side_effect = lambda idx : positions [ idx ] [ <NUM_LIT:0> ] <EOL> match . end . side_effect = lambda idx : positions [ idx ] [ <NUM_LIT:1> ] <EOL> regex . match . return_value = match <EOL> matcher . regex = regex <EOL> expected = [ <EOL> ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:1>' , '<STR_LIT:1>' , None ) , <EOL> ( <NUM_LIT:16> , <NUM_LIT> , '<STR_LIT:2>' , '<STR_LIT:2>' , None ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:3>' , '<STR_LIT:3>' , None ) , <EOL> ( <NUM_LIT:32> , <NUM_LIT> , '<STR_LIT:bar>' , '<STR_LIT:bar>' , '<STR_LIT:foo>' ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> m = matcher . match ( '<STR_LIT>' ) <EOL> assert m . func is func <EOL> args = m . arguments <EOL> have = [ ( a . start , a . end , a . original , a . value , a . name ) for a in args ] <EOL> eq_ ( have , expected ) <EOL> def test_steps_with_same_prefix_are_not_ordering_sensitive ( self ) : <EOL> def step_func1 ( context ) : pass <EOL> def step_func2 ( context ) : pass <EOL> matcher1 = RegexMatcher ( step_func1 , "<STR_LIT>" ) <EOL> matcher2 = RegexMatcher ( step_func2 , "<STR_LIT>" ) <EOL> matched1 = matcher1 . match ( matcher2 . string ) <EOL> matched2 = matcher2 . match ( matcher1 . string ) <EOL> assert matched1 is None <EOL> assert matched2 is None <EOL> matched1 = matcher1 . match ( matcher1 . string ) <EOL> matched2 = matcher2 . match ( matcher2 . string ) <EOL> assert isinstance ( matched1 , Match ) <EOL> assert isinstance ( matched2 , Match ) <EOL> @ raises ( AssertionError ) <EOL> def test_step_should_not_use_regex_begin_marker ( self ) : <EOL> RegexMatcher ( None , "<STR_LIT>" ) <EOL> @ raises ( AssertionError ) <EOL> def test_step_should_not_use_regex_end_marker ( self ) : <EOL> RegexMatcher ( None , "<STR_LIT>" ) <EOL> @ raises ( AssertionError ) <EOL> def test_step_should_not_use_regex_begin_and_end_marker ( self ) : <EOL> RegexMatcher ( None , "<STR_LIT>" ) <EOL> def test_step_matcher_current_matcher ( ) : <EOL> current_matcher = matchers . current_matcher <EOL> for name , klass in list ( matchers . matcher_mapping . items ( ) ) : <EOL> matchers . use_step_matcher ( name ) <EOL> matcher = matchers . get_matcher ( lambda x : - x , '<STR_LIT:foo>' ) <EOL> assert isinstance ( matcher , klass ) <EOL> matchers . current_matcher = current_matcher </s>
