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"
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"
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 == ""
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
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"
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 == ""
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
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 == ""
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')
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 == ""
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 == ""
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')
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
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 == ""
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"
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