def test_conc_parsing(): assert conc.parse("abcde[^fg]*h{5}[a-z]+") == conc( mult(charclass("a"), one), mult(charclass("b"), one), mult(charclass("c"), one), mult(charclass("d"), one), mult(charclass("e"), one), mult(~charclass("fg"), star), mult(charclass("h"), multiplier(bound(5), bound(5))), mult(charclass("abcdefghijklmnopqrstuvwxyz"), plus), ) assert conc.parse("[bc]*[ab]*") == conc( mult(charclass("bc"), star), mult(charclass("ab"), star), ) assert conc.parse("abc...") == conc( mult(charclass("a"), one), mult(charclass("b"), one), mult(charclass("c"), one), mult(dot, one), mult(dot, one), mult(dot, one), ) assert conc.parse("\\d{4}-\\d{2}-\\d{2}") == conc( mult(charclass("0123456789"), multiplier(bound(4), bound(4))), mult(charclass("-"), one), mult(charclass("0123456789"), multiplier(bound(2), bound(2))), mult(charclass("-"), one), mult(charclass("0123456789"), multiplier(bound(2), bound(2))), )
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_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_empty_mult_suppression(): assert conc.parse("[]0\\d").reduce() == charclass.parse("[]") assert conc( mult(pattern(), one), # this mult can never actually match anything mult(charclass("0"), one), mult(charclass("0123456789"), one), ).reduce() == charclass.parse("[]")
def test_empty_mult_suppression(): assert conc.parse("[]0\d").reduce() == charclass.parse("[]") assert conc( mult(pattern(), one), # this mult can never actually match anything mult(charclass("0"), one), mult(charclass("0123456789"), one), ).reduce() == charclass.parse("[]")
def test_conc_str(): assert str(conc( mult(charclass("a"), one), mult(charclass("b"), one), mult(charclass("c"), one), mult(charclass("d"), one), mult(charclass("e"), one), mult(~charclass("fg"), star), mult(charclass("h"), multiplier(bound(5), bound(5))), mult(charclass("abcdefghijklmnopqrstuvwxyz"), plus), )) == "abcde[^fg]*h{5}[a-z]+"
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_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_conc_equality(): assert conc(mult(charclass("a"), one)) == conc(mult(charclass("a"), one)) assert conc(mult(charclass("a"), one)) != conc(mult(charclass("b"), one)) assert conc(mult(charclass("a"), one)) != conc(mult(charclass("a"), qm)) assert conc(mult(charclass("a"), one)) != conc(mult(charclass("a"), multiplier(bound(1), bound(2)))) assert conc(mult(charclass("a"), one)) != emptystring