예제 #1
0
def test_asserting_everything_was_touched_on_an_empty_document_produces_the_expected_result(
):
    input = ("")

    enolib.parse(input).assert_all_touched()

    assert bool('it passes') is True
예제 #2
0
def test_querying_a_list_with_an_empty_item_for_required_values_raises_the_expected_validationerror():
    error = None

    input = ("list:\n"
             "- item\n"
             "-")

    try:
        enolib.parse(input).list('list').required_string_values()
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError
    
    text = ("The list 'list' may not contain empty items.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 "      1 | list:\n"
                 "      2 | - item\n"
                 " >    3 | -")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 2
    assert error.selection['from']['column'] == 1
    assert error.selection['to']['line'] == 2
    assert error.selection['to']['column'] == 1
def test_querying_a_fieldset_with_two_entries_for_a_required_but_missing_entry_raises_the_expected_validationerror(
):
    error = None

    input = ("fieldset:\n" "entry = value\n" "entry = value")

    try:
        enolib.parse(input).fieldset('fieldset').required_entry('missing')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = (
        "The fieldset entry 'missing' is missing - in case it has been specified look for typos and also check for correct capitalization."
    )

    assert error.text == text

    snippet = ("   Line | Content\n"
               " *    1 | fieldset:\n"
               " ?    2 | entry = value\n"
               " ?    3 | entry = value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 9
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 9
예제 #4
0
def test_three_empty_elements_copying_each_other_two_of_them_cyclically_raises_the_expected_parseerror():
    error = None

    input = ("copy < empty\n"
             "empty < cyclic\n"
             "cyclic < empty")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError
    
    text = ("In line 3 'empty' is copied into itself.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 "      1 | copy < empty\n"
                 " *    2 | empty < cyclic\n"
                 " >    3 | cyclic < empty")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 2
    assert error.selection['from']['column'] == 9
    assert error.selection['to']['line'] == 2
    assert error.selection['to']['column'] == 14
예제 #5
0
def test_querying_a_fieldset_entry_for_a_required_but_missing_value_raises_the_expected_validationerror():
    error = None

    input = ("fieldset:\n"
             "entry =")

    try:
        enolib.parse(input).fieldset('fieldset').entry('entry').required_string_value()
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError
    
    text = ("The fieldset entry 'entry' must contain a value.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 "      1 | fieldset:\n"
                 " >    2 | entry =")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 1
    assert error.selection['from']['column'] == 7
    assert error.selection['to']['line'] == 1
    assert error.selection['to']['column'] == 7
예제 #6
0
def test_querying_a_section_for_a_required_but_missing_section_raises_the_expected_validationerror(
):
    error = None

    input = ("# section")

    try:
        enolib.parse(input).section('section').required_section('section')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = (
        "The section 'section' is missing - in case it has been specified look for typos and also check for correct capitalization."
    )

    assert error.text == text

    snippet = ("   Line | Content\n" " *    1 | # section")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 9
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 9
def test_expecting_a_section_but_getting_a_field_with_continuations_raises_the_expected_validationerror():
    error = None

    input = ("field:\n"
             "| continuation\n"
             "| continuation")

    try:
        enolib.parse(input).section('field')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError
    
    text = ("A section with the key 'field' was expected.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 " >    1 | field:\n"
                 " *    2 | | continuation\n"
                 " *    3 | | continuation")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 2
    assert error.selection['to']['column'] == 14
예제 #8
0
def test_fuzz(text):
    '''Only allow the parse method to raise ParseErrors.'''

    try:
        enolib.parse(text)
    except Exception as e:
        assert isinstance(e, ParseError)
예제 #9
0
def test_querying_a_section_for_a_required_but_missing_comment_raises_the_expected_validationerror():
    error = None

    input = ("# section")

    try:
        enolib.parse(input).section('section').required_string_comment()
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError
    
    text = ("A required comment for this element is missing.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 " >    1 | # section")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 0
예제 #10
0
def test_a_line_without_operators_raises_the_expected_parseerror():
    error = None

    input = ("list:\n" "- item\n" "- item\n" "illegal")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError

    text = ("Line 4 does not follow any specified pattern.")

    assert error.text == text

    snippet = ("   Line | Content\n"
               "   ...\n"
               "      2 | - item\n"
               "      3 | - item\n"
               " >    4 | illegal")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 3
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 3
    assert error.selection['to']['column'] == 7
예제 #11
0
def test_parse_with_invalid_syntax():
    with pytest.raises(ParseError) as excinfo:
        enolib.parse('language eno')

    assert str(excinfo.value) == snapshot(
        str(excinfo.value),
        'tests/snapshots/parse_with_invalid_syntax.snap.txt')
예제 #12
0
def test_parsing_a_fieldset_entry_preceded_by_a_field_raises_the_expected_parseerror(
):
    error = None

    input = ("field: value\n" "entry = value")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError

    text = (
        "Line 2 contains a fieldset entry without a fieldset being specified before."
    )

    assert error.text == text

    snippet = ("   Line | Content\n"
               "      1 | field: value\n"
               " >    2 | entry = value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 1
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 1
    assert error.selection['to']['column'] == 13
예제 #13
0
def test_parsing_a_fieldset_entry_preceded_by_a_copied_list_raises_the_expected_parseerror(
):
    error = None

    input = ("list:\n" "- item\n" "\n" "copy < list\n" "entry = value")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError

    text = (
        "Line 5 contains a fieldset entry without a fieldset being specified before."
    )

    assert error.text == text

    snippet = ("   Line | Content\n"
               "   ...\n"
               "      3 | \n"
               "      4 | copy < list\n"
               " >    5 | entry = value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 4
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 4
    assert error.selection['to']['column'] == 13
예제 #14
0
def test_asserting_everything_was_touched_on_an_untouched_document_containing_a_single_field_with_a_custom_message_function_raises_the_expected_validationerror(
):
    error = None

    input = ("field: value")

    try:
        enolib.parse(input).assert_all_touched(
            lambda element:
            f"my generated message for unexpected element '{element.string_key()}'"
        )
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("my generated message for unexpected element 'field'")

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | field: value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 12
def test_expecting_sections_but_getting_a_field_raises_the_expected_validationerror(
):
    error = None

    input = ("field: value")

    try:
        enolib.parse(input).sections('field')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("Only sections with the key 'field' were expected.")

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | field: value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 12
예제 #16
0
def test_triggering_an_error_inside_a_custom_loader_when_querying_the_key_of_a_field_raises_the_expected_validationerror(
):
    error = None

    input = ("field: value")

    try:

        def loader(value):
            raise ValueError('my error')

        enolib.parse(input).field('field').key(loader)
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("There is a problem with the key of this element: my error")

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | field: value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 5
예제 #17
0
def test_expecting_a_list_but_getting_two_lists_raises_the_expected_validationerror(
):
    error = None

    input = ("list:\n" "- item\n" "list:\n" "- item")

    try:
        enolib.parse(input).list('list')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("Only a single list with the key 'list' was expected.")

    assert error.text == text

    snippet = ("   Line | Content\n"
               " >    1 | list:\n"
               " *    2 | - item\n"
               " >    3 | list:\n"
               " *    4 | - item")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 1
    assert error.selection['to']['column'] == 6
예제 #18
0
def test_expecting_a_section_but_getting_an_empty_element_raises_the_expected_validationerror(
):
    error = None

    input = ("element:")

    try:
        enolib.parse(input).section('element')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("A section with the key 'element' was expected.")

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | element:")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 8
def test_a_multiline_field_with_an_edge_case_key_and_missing_space_in_the_ending_line_raises_the_expected_parseerror(
):
    error = None

    input = ("-- -\n" "value\n" "value\n" "value\n" "---")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError

    text = (
        "The multiline field '-' starting in line 1 is not terminated until the end of the document."
    )

    assert error.text == text

    snippet = ("   Line | Content\n"
               " >    1 | -- -\n"
               " *    2 | value\n"
               " *    3 | value\n"
               " *    4 | value\n"
               " *    5 | ---")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 4
예제 #20
0
def test_copying_a_section_that_does_not_exist_raises_the_expected_parseerror(
):
    error = None

    input = ("# copy < section")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError

    text = (
        "In line 1 the section 'section' should be copied, but it was not found."
    )

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | # copy < section")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 16
예제 #21
0
def test_asserting_everything_was_touched_when_the_only_present_empty_element_was_not_touched_raises_the_expected_validationerror(
):
    error = None

    input = ("element:")

    try:
        enolib.parse(input).assert_all_touched()
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = (
        "This element was not expected, make sure it is at the right place in the document and that its key is not mis-typed."
    )

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | element:")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 8
예제 #22
0
def test_copying_a_section_whose_key_only_exists_on_a_fieldset_raises_the_expected_parseerror(
):
    error = None

    input = ("fieldset:\n" "entry = value\n" "\n" "# copy < fieldset")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError

    text = (
        "In line 4 the section 'fieldset' should be copied, but it was not found."
    )

    assert error.text == text

    snippet = ("   Line | Content\n"
               "   ...\n"
               "      2 | entry = value\n"
               "      3 | \n"
               " >    4 | # copy < fieldset")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 3
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 3
    assert error.selection['to']['column'] == 17
예제 #23
0
def test_three_sections_with_one_being_copied_into_its_own_subsection_raises_the_expected_parseerror():
    error = None

    input = ("# section\n"
             "## copied_subsection < section\n"
             "# copied_section < section")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError
    
    text = ("In line 2 'section' is copied into itself.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 " *    1 | # section\n"
                 " >    2 | ## copied_subsection < section\n"
                 "      3 | # copied_section < section")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 1
    assert error.selection['from']['column'] == 23
    assert error.selection['to']['line'] == 1
    assert error.selection['to']['column'] == 30
예제 #24
0
def test_asserting_everything_was_touched_on_an_untouched_document_with_a_custom_message_raises_the_expected_validationerror(
):
    error = None

    input = ("field: value")

    try:
        enolib.parse(input).assert_all_touched('my custom message')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("my custom message")

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | field: value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 12
예제 #25
0
def test_querying_a_field_with_empty_line_continuations_for_a_required_but_missing_value_raises_the_expected_validationerror():
    error = None

    input = ("field:\n"
             "|\n"
             "\n"
             "|")

    try:
        enolib.parse(input).field('field').required_string_value()
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError
    
    text = ("The field 'field' must contain a value.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 " >    1 | field:\n"
                 " *    2 | |\n"
                 " *    3 | \n"
                 " *    4 | |")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 6
    assert error.selection['to']['line'] == 3
    assert error.selection['to']['column'] == 1
def test_expecting_lists_but_getting_an_empty_section_raises_the_expected_validationerror(
):
    error = None

    input = ("# section")

    try:
        enolib.parse(input).lists('section')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("Only lists with the key 'section' were expected.")

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | # section")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 9
예제 #27
0
def test_expecting_a_section_but_getting_a_list_with_two_items_with_comments_raises_the_expected_validationerror(
):
    error = None

    input = ("list:\n" "> comment\n" "- item\n" "\n" "> comment\n" "- item")

    try:
        enolib.parse(input).section('list')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("A section with the key 'list' was expected.")

    assert error.text == text

    snippet = ("   Line | Content\n"
               " >    1 | list:\n"
               " *    2 | > comment\n"
               " *    3 | - item\n"
               " *    4 | \n"
               " *    5 | > comment\n"
               " *    6 | - item")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 5
    assert error.selection['to']['column'] == 6
def test_starting_the_first_section_in_the_document_at_a_deep_level_raises_the_expected_parseerror(
):
    error = None

    input = ("### section")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError

    text = (
        "Line 1 starts a section that is more than one level deeper than the current one."
    )

    assert error.text == text

    snippet = ("   Line | Content\n" " >    1 | ### section")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 0
    assert error.selection['to']['column'] == 11
예제 #29
0
def test_expecting_a_fieldset_but_getting_two_fieldsets_raises_the_expected_validationerror(
):
    error = None

    input = ("fieldset:\n" "entry = value\n" "fieldset:\n" "entry = value")

    try:
        enolib.parse(input).fieldset('fieldset')
    except enolib.ValidationError as _error:
        if isinstance(_error, enolib.ValidationError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ValidationError

    text = ("Only a single fieldset with the key 'fieldset' was expected.")

    assert error.text == text

    snippet = ("   Line | Content\n"
               " >    1 | fieldset:\n"
               " *    2 | entry = value\n"
               " >    3 | fieldset:\n"
               " *    4 | entry = value")

    assert error.snippet == snippet

    assert error.selection['from']['line'] == 0
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 1
    assert error.selection['to']['column'] == 13
예제 #30
0
def test_parsing_a_line_continuation_preceded_by_a_copied_field_raises_the_expected_parseerror():
    error = None

    input = ("field: value\n"
             "\n"
             "copy < field\n"
             "| illegal_continuation")

    try:
        enolib.parse(input)
    except enolib.ParseError as _error:
        if isinstance(_error, enolib.ParseError):
            error = _error
        else:
            raise _error

    assert type(error) is enolib.ParseError
    
    text = ("Line 4 contains a line continuation without a continuable element being specified before.")
    
    assert error.text == text
    
    snippet   = ("   Line | Content\n"
                 "   ...\n"
                 "      2 | \n"
                 "      3 | copy < field\n"
                 " >    4 | | illegal_continuation")
    
    assert error.snippet == snippet
    
    assert error.selection['from']['line'] == 3
    assert error.selection['from']['column'] == 0
    assert error.selection['to']['line'] == 3
    assert error.selection['to']['column'] == 22