Exemplo n.º 1
0
def test_partial():
    add2 = scope.partial('add', 2)
    print(add2)
    assert len(str(add2).split('\n')) == 3

    # add2 evaluates to a scope method
    thing = rec_eval(add2)
    print(thing)
    assert 'SymbolTableEntry' in str(thing)

    # add2() evaluates to a failure because it's only a partial application
    assert_raises(NotImplementedError, rec_eval, add2())

    # add2(3) evaluates to 5 because we've filled in all the blanks
    thing = rec_eval(add2(3))
    print(thing)
    assert thing == 5
Exemplo n.º 2
0
def test_partial():
    add2 = scope.partial("add", 2)
    print(add2)
    assert len(str(add2).split("\n")) == 3

    # add2 evaluates to a scope method
    thing = rec_eval(add2)
    print(thing)
    assert "SymbolTableEntry" in str(thing)

    # add2() evaluates to a failure because it's only a partial application
    assert_raises(NotImplementedError, rec_eval, add2())

    # add2(3) evaluates to 5 because we've filled in all the blanks
    thing = rec_eval(add2(3))
    print(thing)
    assert thing == 5
Exemplo n.º 3
0
def test_recursion():
    scope.define(
        Lambda(
            "Fact",
            [("x", p0)],
            expr=scope.switch(p0 > 1, 1, p0 * base.apply("Fact", p0 - 1)),
        ))
    print(scope.Fact(3))
    assert rec_eval(scope.Fact(3)) == 6
Exemplo n.º 4
0
def test_callpipe():

    # -- set up some 1-variable functions
    a2 = scope.partial("add", 2)
    a3 = scope.partial("add", 3)

    def s9(a):
        return scope.sub(a, 9)

    # x + 2 + 3 - 9 == x - 4
    r = scope.callpipe1([a2, a3, s9], 5)
    thing = rec_eval(r)
    assert thing == 1
Exemplo n.º 5
0
def test_callpipe():

    # -- set up some 1-variable functions
    a2 = scope.partial('add', 2)
    a3 = scope.partial('add', 3)

    def s9(a):
        return scope.sub(a, 9)

    # x + 2 + 3 - 9 == x - 4
    r = scope.callpipe1([a2, a3, s9], 5)
    thing = rec_eval(r)
    assert thing == 1
Exemplo n.º 6
0
def test_kwswitch():
    i = Literal()
    ab = scope.kwswitch(i, k1="a", k2="b", err=scope.Raise(Exception))
    assert rec_eval(ab, memo={i: "k1"}) == "a"
    assert rec_eval(ab, memo={i: "k2"}) == "b"
    assert_raises(Exception, rec_eval, ab, memo={i: "err"})
Exemplo n.º 7
0
def test_switch_and_Raise():
    i = Literal()
    ab = scope.switch(i, "a", "b", scope.Raise(Exception))
    assert rec_eval(ab, memo={i: 0}) == "a"
    assert rec_eval(ab, memo={i: 1}) == "b"
    assert_raises(Exception, rec_eval, ab, memo={i: 2})
Exemplo n.º 8
0
def test_kwswitch():
    i = Literal()
    ab = scope.kwswitch(i, k1='a', k2='b', err=scope.Raise(Exception))
    assert rec_eval(ab, memo={i: 'k1'}) == 'a'
    assert rec_eval(ab, memo={i: 'k2'}) == 'b'
    assert_raises(Exception, rec_eval, ab, memo={i: 'err'})
Exemplo n.º 9
0
def test_switch_and_Raise():
    i = Literal()
    ab = scope.switch(i, 'a', 'b', scope.Raise(Exception))
    assert rec_eval(ab, memo={i: 0}) == 'a'
    assert rec_eval(ab, memo={i: 1}) == 'b'
    assert_raises(Exception, rec_eval, ab, memo={i: 2})
Exemplo n.º 10
0
def test_recursion():
    scope.define(Lambda('Fact', [('x', p0)],
                 expr=scope.switch(p0 > 1, 1, p0 * base.apply('Fact', p0 - 1))))
    print(scope.Fact(3))
    assert rec_eval(scope.Fact(3)) == 6
Exemplo n.º 11
0
def test_kwswitch():
    i = Literal()
    ab = scope.kwswitch(i, k1='a', k2='b', err=scope.Raise(Exception))
    assert rec_eval(ab, memo={i: 'k1'}) == 'a'
    assert rec_eval(ab, memo={i: 'k2'}) == 'b'
    assert_raises(Exception, rec_eval, ab, memo={i: 'err'})
Exemplo n.º 12
0
def test_switch_and_Raise():
    i = Literal()
    ab = scope.switch(i, 'a', 'b', scope.Raise(Exception))
    assert rec_eval(ab, memo={i: 0}) == 'a'
    assert rec_eval(ab, memo={i: 1}) == 'b'
    assert_raises(Exception, rec_eval, ab, memo={i: 2})