Beispiel #1
0
def test_args_via_sequence():
    class Dummy(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    p = lib.sequence(lib.tag("foo"),
                     lib.tag("bar")).map(lambda args: Dummy(*args))
    r = p("foobar")
    assert (r is not None)
    assert (r.x == "foo")
    assert (r.y == "bar")
Beispiel #2
0
def test_function_decorator():
    @lib.parser(lib.tag("foo"))
    def plen(s):
        return len(s)

    assert (plen("foo") == 3)
    with pytest.raises(lib.ParseError):
        plen("boo")
Beispiel #3
0
def test_class_decorator():
    @lib.DeriveParser(lib.tag("foo"), lib.tag("bar"))
    class Dummy(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

    p = Dummy.parser()
    r = p("foobar")

    # Successful cases
    assert (r is not None)
    assert (r.x == "foo")
    assert (r.y == "bar")
    # Failure case
    with pytest.raises(lib.ParseError):
        p("foobaz")
    with pytest.raises(lib.ParseError):
        p("foobarr")
Beispiel #4
0
def test_match_tag():
    p = lib.tag("foo")
    assert (p("foo") == "foo")
Beispiel #5
0
def test_bind_initial_failue():
    p = lib.char('a').bind(lambda _: lib.tag("foo"))
    with pytest.raises(lib.ParseError):
        p("bfoo")
Beispiel #6
0
def test_bind():
    p = lib.anychar().bind(lambda c: lib.tag("foo")
                           if c == 't' else lib.tag("bar"))
    assert (p("tfoo") == "foo")
    assert (p("fbar") == "bar")
Beispiel #7
0
def test_map():
    p = lib.tag("foo").map(len)
    assert (p("foo") == 3)
Beispiel #8
0
def test_sequence_fail():
    p = lib.sequence(lib.tag("foo"), lib.tag("baz"))
    with pytest.raises(lib.ParseError):
        p("foobar")
Beispiel #9
0
def test_peek_fail():
    p = lib.peek(lib.tag("fo"))
    with pytest.raises(lib.ParseError):
        p._run_parser("fro")
    with pytest.raises(lib.ParseError):
        p._run_parser("")
Beispiel #10
0
def test_alternative():
    parser = lib.alternative(lib.tag("foo"), lib.tag("bar"), lib.tag("baz"))
    assert (parser("baz") == "baz")
Beispiel #11
0
def test_match_init():
    p = lib.tag("foo")
    assert (p._run_parser("foot") == ("foo", "t"))
Beispiel #12
0
def test_lift():
    p = lib.tag("foot")
    q = lib.tag("bar")
    combiner = lib.lift(lambda x, y: len(x) - len(y))
    parser = combiner(p, q)
    assert (parser("footbar") == 1)
Beispiel #13
0
def test_right():
    p = lib.tag("foo")
    q = lib.tag("bar")
    parser = lib.right(p, q)
    assert (parser("foobar") == "bar")
Beispiel #14
0
def test_left():
    p = lib.tag("foo")
    q = lib.tag("bar")
    parser = lib.left(p, q)
    assert (parser("foobar") == "foo")
Beispiel #15
0
def test_bind_continuation_failure():
    p = lib.anychar().bind(lambda c: lib.tag("foo")
                           if c == 't' else lib.tag("bar"))
    with pytest.raises(lib.ParseError):
        p("tbar")
Beispiel #16
0
def test_peek():
    p = lib.peek(lib.tag("fo"))
    assert (p._run_parser("foo") == ("fo", "foo"))
Beispiel #17
0
def test_alternative_failure_case():
    parser = lib.alternative(lib.tag("foo"), lib.tag("bar"), lib.tag("baz"))
    with pytest.raises(lib.ParseError):
        parser("boz")
Beispiel #18
0
def test_sequence():
    p = lib.sequence(lib.tag("foo"), lib.tag("bar"))
    assert (p("foobar") == ("foo", "bar"))
Beispiel #19
0
def test_match_init_complete():
    p = lib.tag("foo")
    with pytest.raises(lib.ParseError):
        p("foot")
Beispiel #20
0
def test_derived_partial():
    p = lib.tag("foo").partial()
    assert (p("foot") == ("foo", "t"))
Beispiel #21
0
def test_match_tag_fail():
    p = lib.tag("foo")
    with pytest.raises(lib.ParseError):
        p("flo")