def test_reprs(): # Pattern matchers assert repr(pg.Some("a")) == "<Some pattern='a'>" assert repr(pg.Ignore("#")) == "<Ignore pattern=<Text pattern='#'>>" assert repr(pg.Not("#")) == "<Not pattern=<Text pattern='#'>>" assert repr(pg.Optional("#")) == "<Optional pattern=<Text pattern='#'>>" # Option matchers assert repr(pg.OneOf("abc", pg.Not("#"))) == "<OneOf options=(<Text pattern='abc'>, <Not pattern=<Text pattern='#'>>)>" assert repr(pg.Many("abc", pg.Not("#"))) == "<Many options=(<Text pattern='abc'>, <Not pattern=<Text pattern='#'>>)>" assert repr(pg.Words()) == "<Words letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,'>"
def test_match_some(): word_a = pg.Some('a') match, rest = pg.match_some("a", word_a, "word_a") assert match == ['word_a', "a"] assert rest == "" match, rest = pg.match_some("aab", word_a, "word_a") assert match == ['word_a', "aa"] assert rest == "b" with py.test.raises(pg.NoPatternFound): match, rest = pg.match_some("ccc", word_a, "word_a")
def test_parse_string_some_a(): word_a = pg.NamedPattern( 'word_a', 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)
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)
def word_a(): return pg.Some('a')