<s> import os <EOL> import re <EOL> from robot import utils <EOL> from robot . utils . asserts import assert_equal <EOL> from robot . result import ( ExecutionResultBuilder , Keyword , TestCase , TestSuite , <EOL> Result ) <EOL> from robot . libraries . BuiltIn import BuiltIn <EOL> class NoSlotsKeyword ( Keyword ) : <EOL> pass <EOL> class NoSlotsTestCase ( TestCase ) : <EOL> keyword_class = NoSlotsKeyword <EOL> class NoSlotsTestSuite ( TestSuite ) : <EOL> test_class = NoSlotsTestCase <EOL> keyword_class = NoSlotsKeyword <EOL> class TestCheckerLibrary : <EOL> def process_output ( self , path ) : <EOL> set_suite_variable = BuiltIn ( ) . set_suite_variable <EOL> if not path or path . upper ( ) == '<STR_LIT>' : <EOL> set_suite_variable ( '<STR_LIT>' , None ) <EOL> print "<STR_LIT>" <EOL> return <EOL> path = path . replace ( '<STR_LIT:/>' , os . sep ) <EOL> try : <EOL> print "<STR_LIT>" % path <EOL> result = Result ( root_suite = NoSlotsTestSuite ( ) ) <EOL> ExecutionResultBuilder ( path ) . build ( result ) <EOL> except : <EOL> set_suite_variable ( '<STR_LIT>' , None ) <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> % utils . get_error_message ( ) ) <EOL> set_suite_variable ( '<STR_LIT>' , process_suite ( result . suite ) ) <EOL> set_suite_variable ( '<STR_LIT>' , result . statistics ) <EOL> set_suite_variable ( '<STR_LIT>' , process_errors ( result . errors ) ) <EOL> def get_test_case ( self , name ) : <EOL> suite = BuiltIn ( ) . get_variable_value ( '<STR_LIT>' ) <EOL> return self . _get_test_from_suite ( suite , name ) <EOL> def _get_test_from_suite ( self , suite , name ) : <EOL> tests = self . get_tests_from_suite ( suite , name ) <EOL> if len ( tests ) == <NUM_LIT:1> : <EOL> return tests [ <NUM_LIT:0> ] <EOL> err = "<STR_LIT>" if not tests else "<STR_LIT>" <EOL> raise RuntimeError ( err % ( name , suite . name ) ) <EOL> def get_tests_from_suite ( self , suite , name = None ) : <EOL> tests = [ test for test in suite . tests <EOL> if name is None or utils . eq ( test . name , name ) ] <EOL> for subsuite in suite . suites : <EOL> tests . extend ( self . get_tests_from_suite ( subsuite , name ) ) <EOL> return tests <EOL> def get_test_suite ( self , name ) : <EOL> suite = BuiltIn ( ) . get_variable_value ( '<STR_LIT>' ) <EOL> suites = self . _get_suites_from_suite ( suite , name ) <EOL> if len ( suites ) == <NUM_LIT:1> : <EOL> return suites [ <NUM_LIT:0> ] <EOL> err = "<STR_LIT>" if not suites else "<STR_LIT>" <EOL> raise RuntimeError ( err % ( name , suite . name ) ) <EOL> def _get_suites_from_suite ( self , suite , name ) : <EOL> suites = [ suite ] if utils . eq ( suite . name , name ) else [ ] <EOL> for subsuite in suite . suites : <EOL> suites . extend ( self . _get_suites_from_suite ( subsuite , name ) ) <EOL> return suites <EOL> def check_test_case ( self , testname , status = None , message = None ) : <EOL> test = self . _get_test_from_suite ( BuiltIn ( ) . get_variable_value ( '<STR_LIT>' ) , testname ) <EOL> self . _check_test_status ( test , status = status , message = message ) <EOL> return test <EOL> def _check_test_status ( self , test , status = None , message = None ) : <EOL> """<STR_LIT>""" <EOL> if status is not None : <EOL> test . exp_status = status <EOL> if message is not None : <EOL> test . exp_message = message <EOL> if test . exp_status != test . status : <EOL> if test . exp_status == '<STR_LIT>' : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" + test . message <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" + test . exp_message <EOL> raise AssertionError ( msg ) <EOL> if test . exp_message == test . message : <EOL> return <EOL> if test . exp_message . startswith ( '<STR_LIT>' ) : <EOL> pattern = test . exp_message . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) . strip ( ) <EOL> if re . match ( '<STR_LIT>' % pattern , test . message , re . DOTALL ) : <EOL> return <EOL> if test . exp_message . startswith ( '<STR_LIT>' ) : <EOL> pattern = test . exp_message . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) . strip ( ) <EOL> matcher = utils . Matcher ( pattern , caseless = False , spaceless = False ) <EOL> if matcher . match ( test . message ) : <EOL> return <EOL> if test . exp_message . startswith ( '<STR_LIT>' ) : <EOL> start = test . exp_message . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) . strip ( ) <EOL> if not start : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if test . message . startswith ( start ) : <EOL> return <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( test . exp_message , test . message ) ) <EOL> def should_contain_tests ( self , suite , * names , ** names_and_statuses ) : <EOL> """<STR_LIT>""" <EOL> tests = self . get_tests_from_suite ( suite ) <EOL> expected = [ ( n , None ) for n in names if n not in names_and_statuses ] <EOL> expected . extend ( ( n , s ) for n , s in names_and_statuses . items ( ) ) <EOL> tests_msg = "<STR_LIT>" % ( '<STR_LIT:U+002CU+0020>' . join ( sorted ( [ e [ <NUM_LIT:0> ] for e in expected ] , key = lambda s : s . lower ( ) ) ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( sorted ( [ t . name for t in tests ] , key = lambda s : s . lower ( ) ) ) ) <EOL> if len ( tests ) != len ( expected ) : <EOL> raise AssertionError ( "<STR_LIT>" + tests_msg ) <EOL> for test in tests : <EOL> print "<STR_LIT>" % test . name <EOL> try : <EOL> status = self . _find_expected_status ( test . name , expected ) <EOL> except IndexError : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % ( test . name , tests_msg ) ) <EOL> expected . pop ( expected . index ( ( test . name , status ) ) ) <EOL> if status and '<STR_LIT::>' in status : <EOL> status , message = status . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> else : <EOL> message = None <EOL> self . _check_test_status ( test , status , message ) <EOL> assert not expected <EOL> def _find_expected_status ( self , test , expected ) : <EOL> for name , status in expected : <EOL> if name == test : <EOL> return status <EOL> raise IndexError <EOL> def should_not_contain_tests ( self , suite , * test_names ) : <EOL> actual_names = [ t . name for t in suite . tests ] <EOL> for name in test_names : <EOL> if name in actual_names : <EOL> raise AssertionError ( '<STR_LIT>' % name ) <EOL> def should_contain_suites ( self , suite , * expected ) : <EOL> print '<STR_LIT>' , suite . suites <EOL> expected = sorted ( expected ) <EOL> actual = sorted ( s . name for s in suite . suites ) <EOL> if len ( actual ) != len ( expected ) : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( len ( expected ) , '<STR_LIT:U+002CU+0020>' . join ( expected ) , <EOL> len ( actual ) , '<STR_LIT:U+002CU+0020>' . join ( actual ) ) ) <EOL> for name in expected : <EOL> if not utils . Matcher ( name ) . match_any ( actual ) : <EOL> raise AssertionError ( '<STR_LIT>' % name ) <EOL> def should_contain_tags ( self , test , * tags ) : <EOL> print '<STR_LIT>' , test . tags <EOL> assert_equal ( len ( test . tags ) , len ( tags ) , '<STR_LIT>' ) <EOL> tags = sorted ( tags , key = lambda s : s . lower ( ) . replace ( '<STR_LIT:_>' , '<STR_LIT>' ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) ) <EOL> for act , exp in zip ( test . tags , tags ) : <EOL> assert_equal ( act , exp ) <EOL> def should_contain_keywords ( self , item , * kw_names ) : <EOL> actual_names = [ kw . name for kw in item . keywords ] <EOL> assert_equal ( len ( actual_names ) , len ( kw_names ) , '<STR_LIT>' ) <EOL> for act , exp in zip ( actual_names , kw_names ) : <EOL> assert_equal ( act , exp ) <EOL> def test_should_have_correct_keywords ( self , * kw_names , ** config ) : <EOL> get_var = BuiltIn ( ) . get_variable_value <EOL> suite = get_var ( '<STR_LIT>' ) <EOL> name = config . get ( '<STR_LIT:name>' , get_var ( '<STR_LIT>' ) ) <EOL> kw_index = int ( config . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> test = self . _get_test_from_suite ( suite , name ) <EOL> self . _check_test_status ( test ) <EOL> self . should_contain_keywords ( test . keywords [ kw_index ] , * kw_names ) <EOL> return test <EOL> def check_log_message ( self , item , msg , level = '<STR_LIT>' , html = False , pattern = '<STR_LIT>' ) : <EOL> b = BuiltIn ( ) <EOL> matcher = b . should_match if pattern else b . should_be_equal <EOL> matcher ( item . message . rstrip ( ) , msg . rstrip ( ) , '<STR_LIT>' ) <EOL> b . should_be_equal ( item . level , '<STR_LIT>' if level == '<STR_LIT>' else level , '<STR_LIT>' ) <EOL> b . should_be_equal ( str ( item . html ) , str ( html or level == '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def process_suite ( suite ) : <EOL> for subsuite in suite . suites : <EOL> process_suite ( subsuite ) <EOL> for test in suite . tests : <EOL> process_test ( test ) <EOL> for kw in suite . keywords : <EOL> process_keyword ( kw ) <EOL> suite . setup = suite . keywords . setup <EOL> suite . teardown = suite . keywords . teardown <EOL> return suite <EOL> def process_test ( test ) : <EOL> if '<STR_LIT>' in test . doc : <EOL> test . exp_status = '<STR_LIT>' <EOL> test . exp_message = test . doc . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . lstrip ( ) <EOL> else : <EOL> test . exp_status = '<STR_LIT>' <EOL> test . exp_message = '<STR_LIT>' if '<STR_LIT>' not in test . doc else test . doc . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . lstrip ( ) <EOL> for kw in test . keywords : <EOL> process_keyword ( kw ) <EOL> test . setup = test . keywords . setup <EOL> test . teardown = test . keywords . teardown <EOL> test . keywords = test . kws = list ( test . keywords . normal ) <EOL> test . keyword_count = test . kw_count = len ( test . keywords ) <EOL> def process_keyword ( kw ) : <EOL> if kw is None : <EOL> return <EOL> kw . kws = kw . keywords <EOL> kw . msgs = kw . messages <EOL> kw . message_count = kw . msg_count = len ( kw . messages ) <EOL> kw . keyword_count = kw . kw_count = len ( list ( kw . keywords . normal ) ) <EOL> for subkw in kw . keywords : <EOL> process_keyword ( subkw ) <EOL> def process_errors ( errors ) : <EOL> errors . msgs = errors . messages <EOL> errors . message_count = errors . msg_count = len ( errors . messages ) <EOL> return errors </s>
