<s> import re <EOL> from twisted . trial import unittest <EOL> from nagcat import nagios_objects , _object_parser_py <EOL> try : <EOL> from nagcat import _object_parser_c <EOL> except ImportError : <EOL> _object_parser_c = None <EOL> class ModuleTestcase ( unittest . TestCase ) : <EOL> def testObjectParser ( self ) : <EOL> expect = [ _object_parser_py . ObjectParser ] <EOL> if _object_parser_c : <EOL> expect . append ( _object_parser_c . ObjectParser ) <EOL> self . assertIn ( nagios_objects . ObjectParser , expect ) <EOL> class ObjectsPyTestCase ( unittest . TestCase ) : <EOL> parser = _object_parser_py . ObjectParser <EOL> status = False <EOL> objects = { <EOL> '<STR_LIT:host>' : [ <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> ] , <EOL> } <EOL> def escape ( self , string ) : <EOL> def cb ( match ) : <EOL> esc = match . group ( <NUM_LIT:1> ) <EOL> if esc == '<STR_LIT:\\>' : <EOL> return '<STR_LIT>' <EOL> elif esc == '<STR_LIT:\n>' : <EOL> return '<STR_LIT>' <EOL> elif esc == '<STR_LIT:|>' : <EOL> return '<STR_LIT>' <EOL> else : <EOL> assert <NUM_LIT:0> <EOL> return re . sub ( r'<STR_LIT>' , cb , string ) <EOL> def mkfile ( self , objects ) : <EOL> file_path = self . mktemp ( ) <EOL> file_obj = open ( file_path , '<STR_LIT:w>' ) <EOL> for obj_type , seq in objects . iteritems ( ) : <EOL> for obj in seq : <EOL> if self . status : <EOL> file_obj . write ( "<STR_LIT>" % obj_type ) <EOL> else : <EOL> file_obj . write ( "<STR_LIT>" % obj_type ) <EOL> for attr , value in obj . iteritems ( ) : <EOL> value = self . escape ( value ) <EOL> if self . status : <EOL> file_obj . write ( "<STR_LIT>" % ( attr , value ) ) <EOL> else : <EOL> file_obj . write ( "<STR_LIT>" % ( attr , value ) ) <EOL> file_obj . write ( "<STR_LIT>" ) <EOL> file_obj . close ( ) <EOL> return file_path <EOL> def todict ( self , parser ) : <EOL> return dict ( ( k , parser [ k ] ) for k in parser . types ( ) ) <EOL> def testSimple ( self ) : <EOL> parser = self . parser ( self . mkfile ( self . objects ) ) <EOL> parsed = self . todict ( parser ) <EOL> self . assertEquals ( parsed , self . objects ) <EOL> def testEscape ( self ) : <EOL> objects = { '<STR_LIT:host>' : [ { '<STR_LIT>' : "<STR_LIT>" } ] , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : "<STR_LIT>" } ] } <EOL> parser = self . parser ( self . mkfile ( objects ) ) <EOL> parsed = self . todict ( parser ) <EOL> self . assertEquals ( parsed , objects ) <EOL> def testFilterTypes ( self ) : <EOL> parser = self . parser ( self . mkfile ( self . objects ) , <EOL> object_types = ( '<STR_LIT:host>' , ) ) <EOL> parsed = self . todict ( parser ) <EOL> expect = { '<STR_LIT:host>' : self . objects [ '<STR_LIT:host>' ] } <EOL> self . assertEquals ( parsed , expect ) <EOL> def testFilterValues ( self ) : <EOL> parser = self . parser ( self . mkfile ( self . objects ) , <EOL> object_select = { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> parsed = self . todict ( parser ) <EOL> expect = { '<STR_LIT:host>' : self . objects [ '<STR_LIT:host>' ] [ : <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : self . objects [ '<STR_LIT>' ] [ : <NUM_LIT:1> ] } <EOL> self . assertEquals ( parsed , expect ) <EOL> class StatusPyTestCase ( ObjectsPyTestCase ) : <EOL> status = True <EOL> class ObjectsCTestCase ( ObjectsPyTestCase ) : <EOL> if _object_parser_c : <EOL> parser = _object_parser_c . ObjectParser <EOL> else : <EOL> skip = "<STR_LIT>" <EOL> class StatusCTestCase ( StatusPyTestCase ) : <EOL> if _object_parser_c : <EOL> parser = _object_parser_c . ObjectParser <EOL> else : <EOL> skip = "<STR_LIT>" </s>
