def test_number(): int_pattern = r.seq(r.opt(r.alt(r.quote("+"), r.quote("-"))), r.repeat(1, None, r.char_from("0123456789"))) num_pattern = r.seq(int_pattern, r.opt(r.seq(r.quote("."), r.opt(int_pattern)))) number = re.compile(num_pattern) assert not number.fullmatch("foo") assert not number.fullmatch("+") assert number.fullmatch("0") assert number.fullmatch("-42") assert number.fullmatch("3.") assert number.fullmatch("3.14")
def test_unlimited_repetition(): pattern = r.repeat(2, None, r.alt(r.quote("cat"), r.quote("dog"))) assert not re.fullmatch(pattern, 'cat') assert re.fullmatch(pattern, 'catdog') assert re.fullmatch(pattern, 'dogcatcat') assert re.fullmatch(pattern, 'dogcatcatdogdogcat')
def test_match_alternative(): pattern = r.alt(r.quote("cat"), r.quote("dog")) assert not re.fullmatch(pattern, 'foo') assert re.fullmatch(pattern, 'cat') assert re.fullmatch(pattern, 'dog') assert not re.fullmatch(pattern, 'catdog')
def test_multiple_alternatives(): assert r.alt("foo", "bar", "baz") == "(foo|bar|baz)"
def test_single_alternative(): assert r.alt("foo") == "(foo)"
def test_empty_alternative(): assert r.alt() == "()"