def test_empty(self): toks = [] delims = {(perfile.TOK_CHAR, ',')} result = list(perfile.split_toks(toks, delims)) assert result == [[]]
def test_multi(self): toks = [ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), (perfile.TOK_CHAR, '('), (perfile.TOK_WORD, 'arg1'), (perfile.TOK_CHAR, ','), (perfile.TOK_WORD, 'arg2'), (perfile.TOK_CHAR, ')'), ] delims = { (perfile.TOK_CHAR, '('), (perfile.TOK_CHAR, ','), (perfile.TOK_CHAR, ')'), } result = list(perfile.split_toks(toks, delims)) assert result == [ [ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), ], [ (perfile.TOK_WORD, 'arg1'), ], [ (perfile.TOK_WORD, 'arg2'), ], [], ]
def test_empty_include(self): toks = [] delims = {(perfile.TOK_CHAR, ',')} result = list(perfile.split_toks(toks, delims, True)) assert result == [([], None)]
def test_multi_include(self): toks = [ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), (perfile.TOK_CHAR, '('), (perfile.TOK_WORD, 'arg1'), (perfile.TOK_CHAR, ','), (perfile.TOK_WORD, 'arg2'), (perfile.TOK_CHAR, ')'), ] delims = { (perfile.TOK_CHAR, '('), (perfile.TOK_CHAR, ','), (perfile.TOK_CHAR, ')'), } result = list(perfile.split_toks(toks, delims, True)) assert result == [ ([ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), ], (perfile.TOK_CHAR, '(')), ([ (perfile.TOK_WORD, 'arg1'), ], (perfile.TOK_CHAR, ',')), ([ (perfile.TOK_WORD, 'arg2'), ], (perfile.TOK_CHAR, ')')), ([], None), ]
def __init__(self, values, start_coord, toks): """ Initialize a ``TestDirective`` instance. :param dict values: The values dictionary that the directive's return value may be placed in. :param start_coord: The coordinates the directive started at. :type start_coord: ``hypocrite.location.Coordinate`` :param list toks: A list of tokens. :raises hypocrite.perfile.ParseException: An error occurred while parsing the directive. """ # Make sure the token list is correct if (len(toks) < 2 or toks[0].type_ != perfile.TOK_WORD or not toks[0].value or toks[-1] != (perfile.TOK_CHAR, '{')): raise perfile.ParseException('Invalid %%test directive at %s' % start_coord) # Save the gunk we need for __call__() self.name = toks[0].value self.fixtures = [] self.values = values self.start_coord = start_coord # Extract the optional fixtures if len(toks) > 2: if (toks[1] != (perfile.TOK_CHAR, '(') or toks[-2] != (perfile.TOK_CHAR, ')')): raise perfile.ParseException('Invalid %%test directive at %s' % start_coord) for fix_toks in perfile.split_toks(toks[2:-2], {(perfile.TOK_CHAR, ',')}): # Sanity-check the tokens if len(fix_toks) < 1 or len(fix_toks) > 2: raise perfile.ParseException( 'Invalid fixture specification in %%test directive ' 'at %s' % start_coord) # Determine if it's an injectable inject = True if fix_toks[0] == (perfile.TOK_CHAR, '!'): inject = False fix_toks.pop(0) # Determine the fixture name if (len(fix_toks) != 1 or fix_toks[0].type_ != perfile.TOK_WORD or not fix_toks[0].value): raise perfile.ParseException( 'Invalid fixture specification in %%test directive ' 'at %s' % start_coord) # Add the fixture injection self.fixtures.append( HypoFixtureInjection(fix_toks[0].value, inject))
def test_one_include(self): toks = [ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), ] delims = {(perfile.TOK_CHAR, ',')} result = list(perfile.split_toks(toks, delims, True)) assert result == [ ([ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), ], None), ]
def test_one_comma(self): toks = [ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), (perfile.TOK_CHAR, ','), ] delims = {(perfile.TOK_CHAR, ',')} result = list(perfile.split_toks(toks, delims)) assert result == [ [ (perfile.TOK_CHAR, '!'), (perfile.TOK_WORD, 'something'), ], [], ]
def __init__(self, values, start_coord, toks): """ Initialize a ``SectionDirective`` instance. :param dict values: The values dictionary that the directive's return value may be placed in. :param start_coord: The coordinates the directive started at. :type start_coord: ``hypocrite.location.Coordinate`` :param list toks: A list of tokens. :raises hypocrite.perfile.ParseException: An error occurred while parsing the directive. """ # Make sure the token list is correct if (len(toks) < 2 or toks[0].type_ != perfile.TOK_WORD or not toks[0].value or toks[-1] != (perfile.TOK_CHAR, '{')): raise perfile.ParseException('Invalid %%section directive at %s' % start_coord) # Check for requirements requires = set() if len(toks) > 2: if (len(toks) < 4 or toks[1] != (perfile.TOK_CHAR, '(') or toks[-2] != (perfile.TOK_CHAR, ')')): raise perfile.ParseException( 'Invalid %%section directive at %s' % start_coord) for requirement in perfile.split_toks(toks[2:-2], {(perfile.TOK_CHAR, ',')}): if (len(requirement) != 1 or requirement[0].type_ != perfile.TOK_WORD or not requirement[0].value): raise perfile.ParseException( 'Invalid %%section directive at %s' % start_coord) requires.add(requirement[0].value) # Save the gunk we need for __call__() self.name = toks[0].value self.requires = requires self.values = values self.start_coord = start_coord
def _extract_type(toks, delims): """ Helper to extract type and name information from a mock declaration. Note that this version can only handle simple type declarations; function pointers should be ``typedef``'d. :param list toks: The list of tokens from which to extract the type. :param set delims: The delimiters to use when extracting the types. :returns: An iterator of 3-element tuples. The first element will be a list of tokens forming the type; the second element will be a token identified as the argument or function name (which may be ``None`` if two delimiters followed each other); and the third element will be the delimiter. At the end of iteration, a final element is returned with the delimiter set to ``None``. """ # Thin wrapper around split_toks() for stack, delim in perfile.split_toks(toks, delims, True): yield stack[:-1], stack[-1] if stack else None, delim