コード例 #1
0
ファイル: test_parser.py プロジェクト: kmisterios/Hack1
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()
コード例 #2
0
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),
        ]
    )
コード例 #3
0
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)],
    )
コード例 #4
0
ファイル: test_parser.py プロジェクト: kmisterios/Hack1
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()
コード例 #5
0
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)
コード例 #6
0
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,
    )
コード例 #7
0
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)