def test_unknown_operator(name, position): parser = DSLParser( [Token(position=position, value=name, type=TokenType.IDENTIFIER)], operators={}, ) message = u'Unknown operator "{}"'.format(name) with assert_raises_dsl_error(message, position): parser.parse()
def test_many(): _assert_tokens_match( source=u'first one two|second 10|third none', expected=[ Token(position=0, value=u'first', type=TokenType.IDENTIFIER), Token(position=6, value=u'one', type=TokenType.IDENTIFIER), Token(position=10, value=u'two', type=TokenType.IDENTIFIER), Token(position=13, value=u'|', type=TokenType.PIPE), Token(position=14, value=u'second', type=TokenType.IDENTIFIER), Token(position=21, value=10, type=TokenType.INTEGER), Token(position=23, value=u'|', type=TokenType.PIPE), Token(position=24, value=u'third', type=TokenType.IDENTIFIER), Token(position=30, value=None, type=TokenType.NULL), ] )
def _assert_token_matches(source, value, token_type): """ :type source: six.text_type :type value: object :type token_type: TokenType """ _assert_tokens_match( source=source, expected=[Token(position=0, value=value, type=token_type)], )
def test_expected_dsl_operator_name(value, position, token_type): hypothesis.assume(token_type is not TokenType.IDENTIFIER) parser = DSLParser( tokens=[Token(position=position, value=value, type=token_type)], operators={}, ) message = u'Expected DSL operator name' with assert_raises_dsl_error(message, position): parser.parse()
def test_position(left, right, whitespace): source = left + whitespace + right position, tokens = scan(source=source, position=len(left)) assert tokens == [ Token( position=len(left) + len(whitespace), value=right, type=TokenType.IDENTIFIER, ), ] assert position == len(source)
def _get_token(match): """ :type match: re.Match :rtype: Token """ token_type = TokenType(match.lastgroup) value = match.group(token_type) converter = _CONVERTERS.get(token_type) if converter is not None: value = converter(value) return Token( position=match.start(), type=token_type, value=value, )
def test_breakpoint(left, right, whitespace, breakpoint): source = left + whitespace + breakpoint + whitespace + right position, tokens = scan(source=source, breakpoint=re.escape(breakpoint)) assert tokens == [Token(position=0, value=left, type=TokenType.IDENTIFIER)] assert position == len(left) + len(whitespace) + len(breakpoint)