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
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")
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")
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])
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)
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))
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)
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)
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"
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"
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
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
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))
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))