コード例 #1
0
def test_parse_one_of():
    emphasis = pg.NamedPattern(
        'emphasis',
        pg.AllOf(
            pg.Ignore("*"),
            pg.Words(),
            pg.Ignore("*")))

    words = pg.NamedPattern(
        'words',
        pg.Words())

    phrase = pg.NamedPattern(
        'phrase',
        pg.OneOf(
            words,
            emphasis))

    expected = ['phrase', ['emphasis', "bold words"]]
    result = pg.parse_string("*bold words*", phrase)
    assert result == expected

    expected = ['phrase', ['words', "normal words"]]
    result = pg.parse_string("normal words", phrase)
    assert result == expected
コード例 #2
0
def test_parse_many():
    emphasis = pg.NamedPattern(
        'emphasis',
        pg.AllOf(
            pg.Ignore("*"),
            pg.Words(),
            pg.Ignore("*")))

    words = pg.NamedPattern(
        'words',
        pg.Words())

    phrase = pg.NamedPattern(
        'phrase',
        pg.OneOf(
            words,
            emphasis))

    body = pg.NamedPattern(
        'body',
        pg.Many(phrase))

    expected = ['body',
                ['phrase',
                 ['words', 'a phrase with ']],
                ['phrase',
                 ['emphasis', "bold words"]],
                ['phrase',
                 ['words', " in it"]]]

    result = pg.parse_string("a phrase with *bold words* in it", body)
    assert result == expected

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("123", body)
コード例 #3
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_many():
    def emphasis():
        return pg.AllOf(
            lambda: pg.Ignore("*"),
            lambda: pg.Words(),
            lambda: pg.Ignore("*"))

    def words():
        return pg.Words()

    def phrase():
        return pg.OneOf(
            words,
            emphasis)

    def body():
        return pg.Many(phrase)

    expected = ['body',
                ['phrase',
                 ['words', 'a phrase with ']],
                ['phrase',
                 ['emphasis', "bold words"]],
                ['phrase',
                 ['words', " in it"]]]

    result = pg.parse_string("a phrase with *bold words* in it", body)
    assert result == expected

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("123", body)
コード例 #4
0
def test_parse_many():
    def emphasis():
        return pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(),
                        lambda: pg.Ignore("*"))

    def words():
        return pg.Words()

    def phrase():
        return pg.OneOf(words, emphasis)

    def body():
        return pg.Many(phrase)

    expected = [
        'body', ['phrase', ['words', 'a phrase with ']],
        ['phrase', ['emphasis', "bold words"]],
        ['phrase', ['words', " in it"]]
    ]

    result = pg.parse_string("a phrase with *bold words* in it", body)
    assert result == expected

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("123", body)
コード例 #5
0
def test_escaped():
    html_text = pg.NamedPattern(
        'html_text',
        pg.Many(
            pg.Words(
                pg.Words.letters+"</>")))

    escaped_text = pg.NamedPattern(
        'escaped_text',
        pg.Escaped(html_text))

    data = """<p>Some Text</p>"""

    expected = [
        'escaped_text',
        ['html_text',
         "&lt;p&gt;Some Text&lt;/p&gt;"]]

    result = pg.parse_string(data, escaped_text)
    assert expected == result

    html_text = pg.Many(
        pg.Words(
            pg.Words.letters+"</>"))

    escaped_text = pg.NamedPattern(
        'escaped_text',
        pg.Escaped(html_text))

    expected = [
        'escaped_text',
        "&lt;p&gt;Some Text&lt;/p&gt;"]

    result = pg.parse_string(data, escaped_text)
    assert expected == result
コード例 #6
0
def test_parse_string_b():
    letter_b = pg.NamedPattern("letter_b", "b")

    result = pg.parse_string("b", letter_b)
    assert result == ['letter_b', "b"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("c", letter_b)
コード例 #7
0
def test_not():
    def not_a():
        return pg.Not("a")

    result = pg.parse_string("bc", not_a)
    assert result == ['not_a', 'b']

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("a", not_a)
コード例 #8
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_not():
    def not_a():
        return pg.Not("a")

    result = pg.parse_string("bc", not_a)
    assert result == ['not_a', 'b']

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("a", not_a)
コード例 #9
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_string_b():
    def letter_b():
        return "b"

    result = pg.parse_string("b", letter_b)
    assert result == ['letter_b', "b"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("c", letter_b)
コード例 #10
0
def test_parse_string_b():
    def letter_b():
        return "b"

    result = pg.parse_string("b", letter_b)
    assert result == ['letter_b', "b"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("c", letter_b)
コード例 #11
0
def test_not():
    not_a = pg.NamedPattern(
        'not_a',
        pg.Not("a"))

    result = pg.parse_string("bc", not_a)
    assert result == ['not_a', 'b']

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("a", not_a)
コード例 #12
0
def test_parse_string_a():
    letter_a = pg.NamedPattern("letter_a", "a")

    result = pg.parse_string("a", letter_a)
    assert result == ['letter_a', "a"]

    result = pg.parse_string("ab", letter_a)
    assert result == ['letter_a', "a"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("c", letter_a)
コード例 #13
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_string_some_a():
    def word_a():
        return pg.Some('a')

    result = pg.parse_string("aa", word_a)
    assert result == ['word_a', "aa"]

    result = pg.parse_string("aaa", word_a)
    assert result == ['word_a', "aaa"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("caa", word_a)
コード例 #14
0
def test_parse_string_some_a():
    def word_a():
        return pg.Some('a')

    result = pg.parse_string("aa", word_a)
    assert result == ['word_a', "aa"]

    result = pg.parse_string("aaa", word_a)
    assert result == ['word_a', "aaa"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("caa", word_a)
コード例 #15
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_string_a():
    def letter_a():
        return "a"

    result = pg.parse_string("a", letter_a)
    assert result == ['letter_a', "a"]

    result = pg.parse_string("ab", letter_a)
    assert result == ['letter_a', "a"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("c", letter_a)
コード例 #16
0
def test_parse_string_a():
    def letter_a():
        return "a"

    result = pg.parse_string("a", letter_a)
    assert result == ['letter_a', "a"]

    result = pg.parse_string("ab", letter_a)
    assert result == ['letter_a', "a"]

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("c", letter_a)
コード例 #17
0
def test_indented():
    def list_item():
        return pg.AllOf(pg.Ignore(pg.Optional(pg.Many("\n"))), pg.Ignore("* "),
                        pg.Words())

    def nested_list():
        return pg.AllOf(
            pg.Ignore(pg.Optional(pg.Many("\n"))),
            pg.Indented(pg.AllOf(list_item,
                                 pg.Optional(pg.Many(list_item, nested_list))),
                        optional=True))

    data = """
* A bullet
"""

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

    result = pg.parse_string(data, nested_list)
    assert expected == result

    data = """
* A bullet
  * A bullet in a sublist
"""

    expected = [
        'nested_list', ['list_item', "A bullet"],
        ['nested_list', ['list_item', "A bullet in a sublist"]]
    ]

    result = pg.parse_string(data, nested_list)
    assert expected == result

    data = """
* A bullet
  * A bullet in a sublist
  * Another bullet in a sublist
* Another bullet in the first list
"""

    expected = [
        'nested_list', ['list_item', "A bullet"],
        [
            'nested_list', ['list_item', "A bullet in a sublist"],
            ['list_item', "Another bullet in a sublist"]
        ], ['list_item', "Another bullet in the first list"]
    ]

    result = pg.parse_string(data, nested_list)
    assert expected == result
コード例 #18
0
def test_parse_words():
    body = pg.NamedPattern(
        'body',
        pg.Words())

    result = pg.parse_string("The confused dog jumped over the fox", body)
    assert result == ['body', "The confused dog jumped over the fox"]
コード例 #19
0
def test_parse_ignore():
    def emphasis():
        return pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(),
                        lambda: pg.Ignore("*"))

    result = pg.parse_string("*bold words*", emphasis)
    assert result == ['emphasis', "bold words"]
コード例 #20
0
def test_numbered_bullet():
    def numbered_bullet():
        return pg.AllOf(pg.Ignore(pg.Optional(pg.Many("\n"))),
                        pg.Ignore(pg.Words(letters="1234567890")),
                        pg.Ignore(". "), pg.Words())

    def ordered_list():
        return pg.AllOf(
            pg.Ignore(pg.Optional(pg.Many("\n"))),
            pg.Indented(pg.AllOf(
                numbered_bullet,
                pg.Optional(pg.Many(numbered_bullet, ordered_list))),
                        optional=True))

    data = """
1. A numbered bullet
  2. A bullet in a sublist
  3. Another bullet in a sublist
4. Another bullet in the first list
"""

    expected = [
        'ordered_list', ['numbered_bullet', "A numbered bullet"],
        [
            'ordered_list', ['numbered_bullet', "A bullet in a sublist"],
            ['numbered_bullet', "Another bullet in a sublist"]
        ], ['numbered_bullet', "Another bullet in the first list"]
    ]

    result = pg.parse_string(data, ordered_list)
    assert expected == result
コード例 #21
0
def test_parse_string_some_aab():
    word_a = pg.NamedPattern("word_a", pg.Some('a'))
    letter_b = pg.NamedPattern("letter_b", "b")
    word_aab = pg.NamedPattern(
        'word_aab',
        pg.AllOf(word_a, letter_b))

    expected = ['word_aab',
                ['word_a', "aa"],
                ['letter_b', "b"]]

    result = pg.parse_string("aab", word_aab)
    assert result == expected

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("caab", word_aab)
コード例 #22
0
def test_parse_string_some_aab():
    def word_a():
        return pg.Some('a')

    def letter_b():
        return "b"

    def word_aab():
        return pg.AllOf(word_a, letter_b)

    expected = ['word_aab', ['word_a', "aa"], ['letter_b', "b"]]

    result = pg.parse_string("aab", word_aab)
    assert result == expected

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("caab", word_aab)
コード例 #23
0
def test_parse_one_of():
    def emphasis():
        return pg.AllOf(lambda: pg.Ignore("*"), lambda: pg.Words(),
                        lambda: pg.Ignore("*"))

    def words():
        return pg.Words()

    def phrase():
        return pg.OneOf(words, emphasis)

    expected = ['phrase', ['emphasis', "bold words"]]
    result = pg.parse_string("*bold words*", phrase)
    assert result == expected

    expected = ['phrase', ['words', "normal words"]]
    result = pg.parse_string("normal words", phrase)
    assert result == expected
コード例 #24
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_ignore():
    def emphasis():
        return pg.AllOf(
            lambda: pg.Ignore("*"),
            lambda: pg.Words(),
            lambda: pg.Ignore("*"))

    result = pg.parse_string("*bold words*", emphasis)
    assert result == ['emphasis', "bold words"]
コード例 #25
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_string_some_aab():
    def word_a():
        return pg.Some('a')

    def letter_b():
        return "b"

    def word_aab():
        return pg.AllOf(word_a, letter_b)

    expected = ['word_aab',
                ['word_a', "aa"],
                ['letter_b', "b"]]

    result = pg.parse_string("aab", word_aab)
    assert result == expected

    with py.test.raises(pg.NoPatternFound):
        result = pg.parse_string("caab", word_aab)
コード例 #26
0
def test_parse_ignore():
    emphasis = pg.NamedPattern(
        'emphasis',
        pg.AllOf(
            pg.Ignore("*"),
            pg.Words(),
            pg.Ignore("*")))

    result = pg.parse_string("*bold words*", emphasis)
    assert result == ['emphasis', "bold words"]
コード例 #27
0
def test_optional():
    def optional_a():
        return pg.Optional("a")

    result = pg.parse_string("a", optional_a)
    expected = ['', "a"]
    assert expected == result

    def optional_a():
        return pg.Optional("a")

    def letters():
        return pg.Words()

    def body():
        return pg.AllOf(optional_a, letters)

    result = pg.parse_string("abc", body)
    expected = ['body', "a", ['letters', "bc"]]
    assert expected == result
コード例 #28
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_one_of():
    def emphasis():
        return pg.AllOf(
            lambda: pg.Ignore("*"),
            lambda: pg.Words(),
            lambda: pg.Ignore("*"))

    def words():
        return pg.Words()

    def phrase():
        return pg.OneOf(
            words,
            emphasis)

    expected = ['phrase', ['emphasis', "bold words"]]
    result = pg.parse_string("*bold words*", phrase)
    assert result == expected

    expected = ['phrase', ['words', "normal words"]]
    result = pg.parse_string("normal words", phrase)
    assert result == expected
コード例 #29
0
def test_optional():
    optional_a = pg.NamedPattern(
        'optional_a',
        pg.Optional("a"))

    result = pg.parse_string("a", optional_a)
    expected = ['', "a"]
    assert expected == result

    letters = pg.NamedPattern(
        'letters',
        pg.Words())

    body = pg.NamedPattern(
        'body',
        pg.AllOf(optional_a, letters))

    result = pg.parse_string("abc", body)
    expected = [
        'body',
        "a",
        ['letters', "bc"]]
    assert expected == result
コード例 #30
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_optional():
    def optional_a():
        return pg.Optional("a")

    result = pg.parse_string("a", optional_a)
    expected = ['', "a"]
    assert expected == result


    def optional_a():
        return pg.Optional("a")

    def letters():
        return pg.Words()

    def body():
        return pg.AllOf(optional_a, letters)

    result = pg.parse_string("abc", body)
    expected = [
        'body',
        "a",
        ['letters', "bc"]]
    assert expected == result
コード例 #31
0
ファイル: test_htmlise.py プロジェクト: Singletoned/markdown3
def test_numbered_bullet():
    def numbered_bullet():
        return pg.AllOf(
        pg.Ignore(
            pg.Optional(
                pg.Many("\n"))),
            pg.Ignore(pg.Words(letters="1234567890")),
            pg.Ignore(". "),
            pg.Words())

    def ordered_list():
        return pg.AllOf(
            pg.Ignore(
                pg.Optional(
                    pg.Many("\n"))),
            pg.Indented(
                pg.AllOf(
                    numbered_bullet,
                    pg.Optional(
                        pg.Many(
                            numbered_bullet,
                            ordered_list))),
                optional=True))

    data = """
1. A numbered bullet
  2. A bullet in a sublist
  3. Another bullet in a sublist
4. Another bullet in the first list
"""

    expected = [
        'ordered_list',
        ['numbered_bullet',
         "A numbered bullet"],
        ['ordered_list',
         ['numbered_bullet',
          "A bullet in a sublist"],
         ['numbered_bullet',
          "Another bullet in a sublist"]],
        ['numbered_bullet',
         "Another bullet in the first list"]]

    result = pg.parse_string(data, ordered_list)
    assert expected == result
コード例 #32
0
def test_unknown_matcher():
    def unknown():
        return 1

    with py.test.raises(pg.UnknownMatcherType):
        result = pg.parse_string("", unknown)
コード例 #33
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_parse_words():
    def body():
        return pg.Words()

    result = pg.parse_string("The confused dog jumped over the fox", body)
    assert result == ['body', "The confused dog jumped over the fox"]
コード例 #34
0
ファイル: test_pegger.py プロジェクト: bwhmather/pegger
def test_unknown_matcher():
    def unknown():
        return 1

    with py.test.raises(pg.UnknownMatcherType):
        result = pg.parse_string("", unknown)
コード例 #35
0
ファイル: test_pegger.py プロジェクト: Singletoned/pegger
def test_indented():
    list_item = pg.NamedPattern(
        'list_item',
        pg.AllOf(
            pg.Ignore(
                pg.Optional(
                    pg.Many("\n"))),
            pg.Ignore("* "),
            pg.Words()))

    @pg.lazy
    def nested_list():
        return pg.AllOf(
            pg.Ignore(
                pg.Optional(
                    pg.Many("\n"))),
            pg.Indented(
                pg.AllOf(
                    list_item,
                pg.Optional(
                    pg.Many(
                        list_item,
                        nested_list))),
                optional=True))

    data = """
* A bullet
"""

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

    result = pg.parse_string(data, nested_list)
    assert expected == result

    data = """
* A bullet
  * A bullet in a sublist
"""

    expected = [
        'nested_list',
        ['list_item', "A bullet"],
        ['nested_list',
         ['list_item', "A bullet in a sublist"]]]

    result = pg.parse_string(data, nested_list)
    assert expected == result

    data = """
* A bullet
  * A bullet in a sublist
  * Another bullet in a sublist
* Another bullet in the first list
"""

    expected = [
        'nested_list',
        ['list_item', "A bullet"],
        ['nested_list',
         ['list_item', "A bullet in a sublist"],
         ['list_item', "Another bullet in a sublist"]],
        ['list_item', "Another bullet in the first list"]]

    result = pg.parse_string(data, nested_list)
    assert expected == result
コード例 #36
0
def parse(text, pattern=body):
    if not text.endswith("\n\n"):
        text = text + "\n\n"
    return pg.parse_string(text, pattern)
コード例 #37
0
def to_html(text, pattern=body):
    if not text.endswith("\n\n"):
        text = text + "\n\n"
    return htmlise(pg.parse_string(text, pattern)).strip()
コード例 #38
0
def test_parse_words():
    def body():
        return pg.Words()

    result = pg.parse_string("The confused dog jumped over the fox", body)
    assert result == ['body', "The confused dog jumped over the fox"]