def test_pattern_str(): assert str(pattern( conc(mult(charclass("a"), one)), conc(mult(charclass("b"), one)), )) == "a|b" assert str(pattern( conc(mult(charclass("a"), one)), conc(mult(charclass("a"), one)), )) == "a" assert str(pattern( conc( mult(charclass("a"), one), mult(charclass("b"), one), mult(charclass("c"), one), ), conc( mult(charclass("d"), one), mult(charclass("e"), one), mult(charclass("f"), one), mult( pattern( conc( mult(charclass("g"), one), mult(charclass("h"), one), mult(charclass("i"), one), ), conc( mult(charclass("j"), one), mult(charclass("k"), one), mult(charclass("l"), one), ), ), one ), ), )) == "abc|def(ghi|jkl)"
def test_pattern_parsing(): assert pattern.parse("abc|def(ghi|jkl)") == pattern( conc( mult(charclass("a"), one), mult(charclass("b"), one), mult(charclass("c"), one), ), conc( mult(charclass("d"), one), mult(charclass("e"), one), mult(charclass("f"), one), mult( pattern( conc( mult(charclass("g"), one), mult(charclass("h"), one), mult(charclass("i"), one), ), conc( mult(charclass("j"), one), mult(charclass("k"), one), mult(charclass("l"), one), ), ), one ), ) )
def test_empty_conc_suppression(): assert pattern.parse("[]0\d").reduce() == charclass.parse("[]") assert pattern( conc( mult(pattern(), one), # this mult can never actually match anything mult(charclass("0"), one), mult(charclass("0123456789"), one), ) # so neither can this conc ).reduce() == charclass.parse("[]")
def test_empty_conc_suppression(): assert pattern.parse("[]0\\d").reduce() == charclass.parse("[]") assert pattern( conc( mult(pattern(), one), # this mult can never actually match anything mult(charclass("0"), one), mult(charclass("0123456789"), one), ) # so neither can this conc ).reduce() == charclass.parse("[]")
def test_mult_reduction_easy(): assert mult.parse("a").reduce() == charclass.parse("a") assert mult.parse("a").reduce() == charclass("a") assert mult.parse("a?").reduce() == mult(charclass("a"), qm) assert mult.parse("a{0}").reduce() == emptystring assert mult.parse("[]").reduce() == nothing assert mult.parse("[]?").reduce() == emptystring assert mult.parse("[]{0}").reduce() == emptystring assert mult.parse("[]{0,5}").reduce() == emptystring assert mult(pattern(), one).reduce() == nothing assert mult(pattern(), qm).reduce() == emptystring assert mult(pattern(), zero).reduce() == emptystring assert mult(pattern(), multiplier.parse("{0,5}")).reduce() == emptystring
def test_pattern_equality(): assert pattern( conc(mult(charclass("a"), one)), conc(mult(charclass("b"), one)), ) == pattern( conc(mult(charclass("b"), one)), conc(mult(charclass("a"), one)), ) assert pattern( conc(mult(charclass("a"), one)), conc(mult(charclass("a"), one)), ) == pattern( conc(mult(charclass("a"), one)), )
def test_mult_reduction_easy(): # mult -> mult # mult -> charclass assert mult(charclass("a"), one).reduce() == charclass("a") assert mult(charclass("a"), qm).reduce() == mult(charclass("a"), qm) assert mult(charclass("a"), zero).reduce() == emptystring assert mult(nothing, one).reduce() == nothing assert mult(nothing, qm).reduce() == emptystring assert mult(nothing, zero).reduce() == emptystring assert mult(nothing, multiplier(bound(0), bound(5))).reduce() == emptystring assert mult(pattern(), one).reduce() == nothing assert mult(pattern(), qm).reduce() == emptystring assert mult(pattern(), zero).reduce() == emptystring assert mult(pattern(), multiplier(bound(0), bound(5))).reduce() == emptystring
def test_empty(): assert nothing.empty() assert charclass().empty() assert not dot.empty() assert not mult.parse("a{0}").empty() assert mult.parse("[]").empty() assert not mult.parse("[]?").empty() assert conc.parse("a[]").empty() assert not conc.parse("a[]?").empty() assert pattern().empty() assert not pattern.parse("a{0}").empty() assert not pattern.parse("[]?").empty()
def test_pattern_parsing(): assert pattern.parse("abc|def(ghi|jkl)") == pattern( conc( mult(charclass("a"), one), mult(charclass("b"), one), mult(charclass("c"), one), ), conc( mult(charclass("d"), one), mult(charclass("e"), one), mult(charclass("f"), one), mult( pattern( conc( mult(charclass("g"), one), mult(charclass("h"), one), mult(charclass("i"), one), ), conc( mult(charclass("j"), one), mult(charclass("k"), one), mult(charclass("l"), one), ), ), one ), ) ) # Accept the "non-capturing group" syntax, "(?: ... )" but give it no # special significance assert parse("(?:)") == parse("()") assert parse("(?:abc|def)") == parse("(abc|def)") parse("(:abc)") # should give no problems # Named groups assert pattern.parse("(?P<ng1>abc)") == parse("(abc)")
def test_empty_pattern_suppression(): assert mult.parse("[]?").reduce() == conc.parse("") assert mult(pattern(), qm).reduce() == conc.parse("")
def test_empty_pattern_reduction(): assert pattern().reduce() == charclass()