<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from behave import given , then , register_type <EOL> from behave . tag_expression import TagExpression <EOL> from behave_model_util import convert_comma_list , convert_model_element_tags <EOL> from hamcrest import assert_that , equal_to <EOL> class ModelElement ( object ) : <EOL> def __init__ ( self , name , tags = None ) : <EOL> self . name = name <EOL> self . tags = tags or [ ] <EOL> def convert_tag_expression ( text ) : <EOL> parts = text . strip ( ) . split ( ) <EOL> return TagExpression ( parts ) <EOL> register_type ( TagExpression = convert_tag_expression ) <EOL> def convert_yesno ( text ) : <EOL> text = text . strip ( ) . lower ( ) <EOL> assert text in convert_yesno . choices <EOL> return text in convert_yesno . true_choices <EOL> convert_yesno . choices = ( "<STR_LIT:yes>" , "<STR_LIT>" , "<STR_LIT:true>" , "<STR_LIT:false>" ) <EOL> convert_yesno . true_choices = ( "<STR_LIT:yes>" , "<STR_LIT:true>" ) <EOL> @ given ( '<STR_LIT>' ) <EOL> def step_given_the_tag_expression ( context , tag_expression ) : <EOL> """<STR_LIT>""" <EOL> context . tag_expression = tag_expression <EOL> @ given ( '<STR_LIT>' ) <EOL> def step_given_the_tag_expression ( context , default_tags ) : <EOL> """<STR_LIT>""" <EOL> context . default_tags = default_tags <EOL> tag_expression = getattr ( context , "<STR_LIT>" , None ) <EOL> if tag_expression is None : <EOL> context . tag_expression = default_tags <EOL> @ then ( '<STR_LIT>' ) <EOL> def step_then_tag_expression_selects_elements_with_tags ( context ) : <EOL> """<STR_LIT>""" <EOL> assert context . tag_expression , "<STR_LIT>" <EOL> context . table . require_columns ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> tag_expression = context . tag_expression <EOL> expected = [ ] <EOL> actual = [ ] <EOL> for row in context . table . rows : <EOL> element_tags = convert_model_element_tags ( row [ "<STR_LIT>" ] ) <EOL> expected_element_selected = convert_yesno ( row [ "<STR_LIT>" ] ) <EOL> actual_element_selected = tag_expression . check ( element_tags ) <EOL> expected . append ( ( element_tags , expected_element_selected ) ) <EOL> actual . append ( ( element_tags , actual_element_selected ) ) <EOL> assert_that ( actual , equal_to ( expected ) ) <EOL> @ given ( '<STR_LIT>' ) <EOL> def step_given_named_model_elements_with_tags ( context ) : <EOL> """<STR_LIT>""" <EOL> assert context . table , "<STR_LIT>" <EOL> context . table . require_columns ( [ "<STR_LIT:name>" , "<STR_LIT>" ] ) <EOL> model_element_names = set ( ) <EOL> model_elements = [ ] <EOL> for row in context . table . rows : <EOL> name = row [ "<STR_LIT:name>" ] . strip ( ) <EOL> tags = convert_model_element_tags ( row [ "<STR_LIT>" ] ) <EOL> assert name not in model_element_names , "<STR_LIT>" % name <EOL> model_elements . append ( ModelElement ( name , tags = tags ) ) <EOL> model_element_names . add ( name ) <EOL> context . model_elements = model_elements <EOL> @ then ( '<STR_LIT>' ) <EOL> def step_given_named_model_elements_with_tags ( context ) : <EOL> """<STR_LIT>""" <EOL> assert context . model_elements , "<STR_LIT>" <EOL> assert context . table , "<STR_LIT>" <EOL> context . table . require_columns ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> for row_index , row in enumerate ( context . table . rows ) : <EOL> tag_expression_text = row [ "<STR_LIT>" ] <EOL> tag_expression = convert_tag_expression ( tag_expression_text ) <EOL> expected_selected_names = convert_comma_list ( row [ "<STR_LIT>" ] ) <EOL> actual_selected = [ ] <EOL> for model_element in context . model_elements : <EOL> if tag_expression . check ( model_element . tags ) : <EOL> actual_selected . append ( model_element . name ) <EOL> assert_that ( actual_selected , equal_to ( expected_selected_names ) , <EOL> "<STR_LIT>" % ( tag_expression_text , row_index ) ) </s>
