예제 #1
0
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")
예제 #2
0
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')
예제 #3
0
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')
예제 #4
0
def test_multiple_alternatives():
    assert r.alt("foo", "bar", "baz") == "(foo|bar|baz)"
예제 #5
0
def test_single_alternative():
    assert r.alt("foo") == "(foo)"
예제 #6
0
def test_empty_alternative():
    assert r.alt() == "()"