Ejemplo n.º 1
0
def test_items_can_be_appended_to_and_removed_from_an_inline_table():
    string = """table = {}
"""

    parser = Parser(string)
    _, table = parser._parse_item()

    assert isinstance(table, InlineTable)
    assert "{}" == table.as_string()

    table.append(Key("foo"),
                 String(StringType.SLB, "bar", "bar", Trivia(trail="")))

    assert '{foo = "bar"}' == table.as_string()

    table.append(Key("baz"), Integer(34, Trivia(trail=""), "34"))

    assert '{foo = "bar", baz = 34}' == table.as_string()

    table.remove(Key("baz"))

    assert '{foo = "bar"}' == table.as_string()

    table.remove(Key("foo"))

    assert "{}" == table.as_string()

    with pytest.raises(NonExistentKey):
        table.remove(Key("foo"))
Ejemplo n.º 2
0
def test_items_can_be_appended_to_and_removed_from_a_table():
    string = """[table]
"""

    parser = Parser(string)
    _, table = parser._parse_table()

    assert isinstance(table, Table)
    assert "\n" == table.as_string()

    table.append(Key("foo"),
                 String(StringType.SLB, "bar", "bar", Trivia(trail="\n")))

    assert '\nfoo = "bar"\n' == table.as_string()

    table.append(
        Key("baz"),
        Integer(34, Trivia(comment_ws="   ", comment="# Integer", trail=""),
                "34"),
    )

    assert '\nfoo = "bar"\nbaz = 34   # Integer' == table.as_string()

    table.remove(Key("baz"))

    assert '\nfoo = "bar"\n' == table.as_string()

    table.remove(Key("foo"))

    assert "\n" == table.as_string()

    with pytest.raises(NonExistentKey):
        table.remove(Key("foo"))
Ejemplo n.º 3
0
def test_parser_should_raise_an_error_if_equal_not_found():
    content = """[foo]
a {c = 1, d = 2}
"""
    parser = Parser(content)
    with pytest.raises(UnexpectedCharError):
        parser.parse()
Ejemplo n.º 4
0
def test_parser_should_raise_an_internal_error_if_parsing_wrong_type_of_string():
    parser = Parser('"foo"')

    with pytest.raises(InternalParserError) as e:
        parser._parse_string(StringType.SLL)

    assert e.value.line == 1
    assert e.value.col == 0
Ejemplo n.º 5
0
def test_parser_should_raise_an_error_for_empty_tables():
    content = """
[one]
[]
"""

    parser = Parser(content)

    with pytest.raises(EmptyTableNameError) as e:
        parser.parse()

    assert e.value.line == 3
    assert e.value.col == 1