Exemple #1
0
def test_match_indented_nested_bullets():
    def bullet():
        return pg.AllOf(
            pg.Ignore(
                pg.Optional(
                    pg.Many("\n"))),
            pg.Ignore("* "),
            pg.Words())

    def indented_bullets():
        return pg.Indented(
            pg.AllOf(
                bullet,
                pg.Optional(
                    indented_bullets,
                    )),
            optional=True)

    data = """
* Line One
* Line Two
"""

    expected = [
        'indented_bullets',
        ['bullet', "Line One"],
        ['indented_bullets',
         ['bullet', "Line Two"]]]

    match, rest = pg.match_indented(data, indented_bullets(), 'indented_bullets')
    assert match == expected
    assert rest == "\n"
Exemple #2
0
    def test_retaining_linebreaks(self):
        paragraph = (pg.Words())

        indented_text = pg.Indented(paragraph)

        data = "  Some text\n"

        expected = ['indented_text', "Some text"]

        match, rest = pg.match_indented(data, indented_text, 'indented_text')
        assert match == expected
        assert rest == "\n"
Exemple #3
0
    def test_indented_with_anonymous_pattern_and_subpattern(self):
        paragraph = (pg.Words())

        indented_text = pg.Indented(paragraph)

        data = "  Some text"

        expected = [None, "Some text"]

        match, rest = pg.match_indented(data, indented_text, None)
        assert match == expected
        assert rest == ""
Exemple #4
0
    def test_reindenting_indented_rest(self):
        paragraph = (pg.Words())

        indented_text = pg.Indented(paragraph)

        data = "  Some text\n  Unmatched text\n  More unmatched text"

        expected = ['indented_text', "Some text"]
        expected_rest = "\n  Unmatched text\n  More unmatched text"

        match, rest = pg.match_indented(data, indented_text, 'indented_text')
        assert match == expected
        assert rest == expected_rest
Exemple #5
0
    def test_retaining_linebreaks(self):
        paragraph = (
            pg.Words())

        indented_text = pg.Indented(paragraph)

        data = "  Some text\n"

        expected = ['indented_text', "Some text"]

        match, rest = pg.match_indented(data, indented_text, 'indented_text')
        assert match == expected
        assert rest == "\n"
Exemple #6
0
    def test_indented_with_anonymous_pattern_and_subpattern(self):
        paragraph = (
            pg.Words())

        indented_text = pg.Indented(paragraph)

        data = "  Some text"

        expected = [None, "Some text"]

        match, rest = pg.match_indented(data, indented_text, None)
        assert match == expected
        assert rest == ""
Exemple #7
0
    def test_reindenting_indented_rest(self):
        paragraph = (
            pg.Words())

        indented_text = pg.Indented(paragraph)

        data = "  Some text\n  Unmatched text\n  More unmatched text"

        expected = ['indented_text', "Some text"]
        expected_rest = "\n  Unmatched text\n  More unmatched text"

        match, rest = pg.match_indented(data, indented_text, 'indented_text')
        assert match == expected
        assert rest == expected_rest
Exemple #8
0
    def test_with_optional(self):
        """Test that optional allows you to match without an indent"""
        def list_item():
            return pg.AllOf(pg.Ignore("* "), pg.Words())

        indented_bullets = pg.Indented(pg.Many(list_item), optional=True)

        data = """* A bullet"""

        expected = ['indented_bullets', ['list_item', "A bullet"]]

        match, rest = pg.match_indented(data, indented_bullets,
                                        'indented_bullets')
        assert match == expected
        assert rest == ""
Exemple #9
0
    def test_without_optional(self):
        """Test that without optional, no match is made"""
        def list_item():
            return (pg.Ignore("\n* "), pg.Words())

        indented_bullets = pg.Indented(pg.Many(list_item))

        data = """
    * A bullet"""

        expected = ['list_item', "A bullet"]

        with py.test.raises(pg.NoPatternFound):
            match, rest = pg.match_indented(data, indented_bullets,
                                            'indented_bullets')
Exemple #10
0
    def test_indented_with_named_subpattern(self):
        def paragraph():
            return (pg.Words())

        indented_text = pg.Indented(paragraph)

        data_with_spaces = """  Some text"""
        data_with_tabs = """\tSome text"""

        expected = ['indented_text', ['paragraph', "Some text"]]

        for data in [data_with_spaces, data_with_tabs]:
            match, rest = pg.match_indented(data, indented_text,
                                            'indented_text')
            assert match == expected
            assert rest == ""
Exemple #11
0
    def test_indented_with_named_subpattern(self):
        def paragraph():
            return (
                pg.Words())

        indented_text = pg.Indented(paragraph)

        data_with_spaces = """  Some text"""
        data_with_tabs = """\tSome text"""

        expected = [
            'indented_text',
            ['paragraph', "Some text"]]

        for data in [data_with_spaces, data_with_tabs]:
            match, rest = pg.match_indented(data, indented_text, 'indented_text')
            assert match == expected
            assert rest == ""
Exemple #12
0
    def test_without_optional(self):
        """Test that without optional, no match is made"""
        def list_item():
            return (
                pg.Ignore("\n* "),
                pg.Words())

        indented_bullets = pg.Indented(
            pg.Many(list_item))

        data = """
    * A bullet"""

        expected = [
            'list_item', "A bullet"]

        with py.test.raises(pg.NoPatternFound):
            match, rest = pg.match_indented(data, indented_bullets, 'indented_bullets')
Exemple #13
0
def test_indented_bullet():
    def paragraph():
        return pg.AllOf(pg.Ignore(pg.Optional("\n")), pg.Words())

    indented_paragraphs = pg.Indented(pg.Many(paragraph),
                                      initial_indent="*   ")

    data = """
*   Paragraph One
    Paragraph Two
""".strip()

    expected = [
        'indented_paragraphs', ['paragraph', "Paragraph One"],
        ['paragraph', "Paragraph Two"]
    ]

    match, rest = pg.match_indented(data, indented_paragraphs,
                                    "indented_paragraphs")
    assert match == expected
Exemple #14
0
    def test_with_optional(self):
        """Test that optional allows you to match without an indent"""
        def list_item():
            return pg.AllOf(
                pg.Ignore("* "),
                pg.Words())

        indented_bullets = pg.Indented(
            pg.Many(list_item),
            optional=True)

        data = """* A bullet"""

        expected = [
            'indented_bullets',
            ['list_item', "A bullet"]]

        match, rest = pg.match_indented(data, indented_bullets, 'indented_bullets')
        assert match == expected
        assert rest == ""
Exemple #15
0
def test_match_indented_nested_bullets():
    def bullet():
        return pg.AllOf(pg.Ignore(pg.Optional(pg.Many("\n"))), pg.Ignore("* "),
                        pg.Words())

    def indented_bullets():
        return pg.Indented(pg.AllOf(bullet, pg.Optional(indented_bullets, )),
                           optional=True)

    data = """
* Line One
* Line Two
"""

    expected = [
        'indented_bullets', ['bullet', "Line One"],
        ['indented_bullets', ['bullet', "Line Two"]]
    ]

    match, rest = pg.match_indented(data, indented_bullets(),
                                    'indented_bullets')
    assert match == expected
    assert rest == "\n"
Exemple #16
0
def test_indented_bullet():
    def paragraph():
        return pg.AllOf(
            pg.Ignore(pg.Optional("\n")),
            pg.Words())

    indented_paragraphs =  pg.Indented(
        pg.Many(paragraph),
        initial_indent="*   ")

    data = """
*   Paragraph One
    Paragraph Two
""".strip()

    expected = [
        'indented_paragraphs',
        ['paragraph',
         "Paragraph One"],
        ['paragraph',
         "Paragraph Two"]]

    match, rest = pg.match_indented(data, indented_paragraphs, "indented_paragraphs")
    assert match == expected