Example #1
0
def test_parse_simple_tagkey(inp_str, expected):
    stream = io.StringIO(inp_str)
    tokenizer = TextRoffBodyTokenizer(stream)
    tokens = tokenizer.tokenize_simple_tagkey()
    parser = roffparse.RoffParser(tokens, stream)
    tagkey_parser = roffparse.RoffTagKeyParser(tokens, stream, parser)
    assert next(tagkey_parser.parse_tagkey()) == expected
Example #2
0
def test_parse_byte_array_values():
    stream = io.StringIO("array byte x 2 255 0")
    tokenizer = TextRoffBodyTokenizer(stream)
    tokens = tokenizer.tokenize_array_tagkey()
    parser = roffparse.RoffParser(tokens, stream)
    parser.is_binary_file = False

    parser = roffparse.RoffTagKeyParser(tokens, stream, parser)
    assert next(iter(parser)) == ("x", b"\xff\x00")
Example #3
0
def test_parse_byten_values():
    stream = io.StringIO("byte x 1")
    tokenizer = TextRoffBodyTokenizer(stream)
    tokens = tokenizer.tokenize_simple_tagkey()
    parser = roffparse.RoffParser(tokens, stream)
    parser.is_binary_file = False

    parser = roffparse.RoffTagKeyParser(tokens, stream, parser)
    assert next(iter(parser)) == ("x", b"\x01")
Example #4
0
def test_parse_tagkey_binary_array_types(input_str, expected):
    stream = io.BytesIO(input_str)
    tokens = BinaryRoffBodyTokenizer(stream).tokenize_array_tagkey()
    parser = roffparse.RoffParser(tokens, stream)
    parser.is_binary_file = True
    parser = roffparse.RoffTagKeyParser(tokens, stream, parser)
    val = next(iter(parser))
    assert val[0] == expected[0]
    assert np.array_equal(val[1], expected[1])
Example #5
0
def test_parse_tagkey_ascii_types(input_str, expected, expected_type):
    stream = io.StringIO(input_str)
    tokenizer = TextRoffBodyTokenizer(stream)
    tokens = tokenizer.tokenize_tagkey()
    parser = roffparse.RoffTagKeyParser(tokens, stream,
                                        roffparse.RoffParser(stream, tokens))
    val = next(iter(parser))
    assert val == expected
    assert isinstance(val[1], expected_type)
Example #6
0
def test_parse_boolean_values_typing():
    stream = io.StringIO("bool x 2")
    tokenizer = TextRoffBodyTokenizer(stream)
    tokens = tokenizer.tokenize_simple_tagkey()
    parser = roffparse.RoffParser(tokens, stream)
    parser.is_binary_file = False

    parser = roffparse.RoffTagKeyParser(tokens, stream, parser)
    with pytest.raises(roffparse.RoffTypeError, match="must be either 1 or 0"):
        next(iter(parser))
Example #7
0
def test_parse_tagkey_binary_types(input_str, expected, expected_type):
    stream = io.BytesIO(input_str)
    tokenizer = BinaryRoffBodyTokenizer(stream)
    tokens = tokenizer.tokenize_simple_tagkey()
    parser = roffparse.RoffParser(tokens, stream)
    parser.is_binary_file = True
    parser = roffparse.RoffTagKeyParser(tokens, stream, parser)
    val = next(iter(parser))
    assert val == expected
    assert isinstance(val[1], expected_type)
Example #8
0
def test_parse_array_tagkey(inp_str, expected):
    stream = io.StringIO(inp_str)
    tokenizer = TextRoffBodyTokenizer(stream)
    tokens = tokenizer.tokenize_array_tagkey()
    parser = roffparse.RoffParser(tokens, stream)
    tagkey_parser = roffparse.RoffTagKeyParser(tokens, stream, parser)
    varname, array = next(tagkey_parser.parse_tagkey())

    assert expected[0] == varname
    assert np.array_equal(expected[1], array)
Example #9
0
def test_parse_ascii_string_literal():
    tokens = iter([Token(TokenKind.STRING_LITERAL, 1, 6)])
    stream = io.StringIO('"hello"')
    parser = roffparse.RoffTagKeyParser(tokens, stream, None)
    assert next(parser.parse_string_literal()) == "hello"
Example #10
0
def test_parse_binary_string_Literal():
    tokens = iter([Token(TokenKind.STRING_LITERAL, 0, 5)])
    stream = io.BytesIO(b"hello\0")
    parser = roffparse.RoffTagKeyParser(tokens, stream, None)
    assert next(parser.parse_string_literal()) == "hello"
Example #11
0
def test_parse_numeric_binary_value(dtype, bytevalue, expected):
    tokens = iter([Token(TokenKind.BINARY_NUMERIC_VALUE, 0, len(bytevalue))])
    stream = io.BytesIO(bytevalue)
    parser = roffparse.RoffTagKeyParser(tokens, stream, None)
    assert next(parser.parse_numeric_value(dtype)) == expected
Example #12
0
def test_parse_numeric_ascii_value(valuestr, valuetype, expected):
    tokens = iter([Token(TokenKind.NUMERIC_VALUE, 0, len(valuestr))])
    stream = io.StringIO(valuestr)
    parser = roffparse.RoffTagKeyParser(tokens, stream, None)
    assert next(parser.parse_numeric_value(valuetype)) == expected
Example #13
0
def test_parse_numeric_value_syntax_error():
    tokens = iter([Token(TokenKind.NUMERIC_VALUE, 0, 0)])
    stream = io.StringIO("ERR")
    parser = roffparse.RoffTagKeyParser(tokens, stream, None)
    with pytest.raises(roffparse.RoffSyntaxError):
        next(parser.parse_numeric_value(np.int32))
Example #14
0
def test_parse_numeric_value_type_error():
    tokens = iter([Token(TokenKind.STRING_LITERAL, 0, 0)])
    stream = io.StringIO()
    parser = roffparse.RoffTagKeyParser(tokens, stream, None)
    with pytest.raises(roffparse.RoffTypeError):
        next(parser.parse_numeric_value(np.int32))