Beispiel #1
0
def test_lazy_index_range():
    """
    Test that lazy evaluation of indexing works with range lookups.
    """
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    plist = as_pp([-1, 0, 1, partial(dont_eval)])
    assert [-1, 0, 1] == evaluate(plist[:3])

    plist = as_pp((-1, 0, 1, partial(dont_eval)))
    assert (-1, 0, 1) == evaluate(plist[:3])
Beispiel #2
0
def test_dict():
    """Test that dicts are correctly traversed/converted."""
    def mod(x, y):
        return x % y

    x = as_pp({5: partial(mod, 5, 3), 3: (7, 9), 4: [partial(mod, 9, 4)]})
    y = evaluate(x)
    assert y == {5: 2, 3: (7, 9), 4: [1]}
Beispiel #3
0
def test_list():
    """Test that lists are correctly traversed/converted."""
    def sub(x, y):
        return x - y

    x = as_pp([[3, partial(sub, 2, 3)], partial(sub, 5, 7), partial(float, 9)])
    y = evaluate(x)
    assert y == [[3, -1], -2, 9.0]
    assert isinstance(y[2], float)
Beispiel #4
0
def test_tuple():
    """Test that tuples are correctly traversed/converted."""
    def add(x, y):
        return x + y

    x = as_pp(((3, partial(add, 2, 3)), partial(add, 5, 7), partial(float, 9)))
    y = evaluate(x)
    assert y == ((3, 5), 12, 9.0)
    assert isinstance(y[2], float)
Beispiel #5
0
def test_switch_ordered_dict():
    """Test that lazy evaluation works with OrderedDicts."""
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    r = evaluate(as_pp(OrderedDict({'a': partial(dont_eval), 'b': 3}))['b'])
    assert r == 3
Beispiel #6
0
def test_variable_substitution():
    """Test that variable substitution works correctly."""
    x = variable(name='x', value_type=int)
    y = variable(name='y', value_type=float)
    p = as_pp({3: x, x: [y, [y]], y: 4})
    # Currently no type-checking. This will fail when we add it and need to be
    # updated.
    e = evaluate(p, x='hey', y=5)
    assert e[3] == 'hey'
    assert e['hey'] == [5, [5]]
    assert e[5] == 4
Beispiel #7
0
def test_lazy_index_dict():
    """
    Test that lazy evaluation of indexing works with dict lookups.
    """
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    r = evaluate(as_pp({'a': partial(dont_eval), 'b': 3})['b'])
    assert r == 3
Beispiel #8
0
def test_two_objects():
    """
    Test that identical expression in different parts of graph evaluates
    to an identical object.
    """
    class Foo(object):
        pass

    p = partial(Foo)
    q = as_pp([p, [0, 1, p], [(p, )]])
    r = evaluate(q)
    assert r[0] is r[1][-1]
    assert r[0] is r[2][0][0]
Beispiel #9
0
def test_lazy_index_tuple():
    """
    Test that lazy evaluation of indexing works with element lookups on
    lists.
    """
    def dont_eval():
        # -- This function body should never be evaluated
        #    because we only need the 0'th element of `plist`
        assert 0, 'Evaluate does not need this, should not eval'

    # TODO: James: I opted for this behaviour rather than list(f, el1, el2...)
    # is there a compelling reason to do that? It kind of breaks with the
    # model.
    plist = as_pp((-1, partial(dont_eval)))
    assert -1 == evaluate(plist[0])
Beispiel #10
0
 def check(a, b):
     assert evaluate(as_pp(partial(int, a)) +
                     as_pp(partial(int, b))) == a + b
     assert evaluate(as_pp(partial(int, a)) -
                     as_pp(partial(int, b))) == a - b
     assert evaluate(as_pp(partial(int, a)) *
                     as_pp(partial(int, b))) == a * b
     assert evaluate(as_pp(partial(int, a)) /
                     as_pp(partial(int, b))) == a / b
     assert evaluate(as_pp(partial(int, a)) %
                     as_pp(partial(int, b))) == a % b
     assert evaluate(as_pp(partial(int, a))
                     | as_pp(partial(int, b))) == a | b
     assert evaluate(as_pp(partial(int, a))
                     ^ as_pp(partial(int, b))) == a ^ b
     assert evaluate(as_pp(partial(int, a))
                     & as_pp(partial(int, b))) == a & b
Beispiel #11
0
def test_getitem_dict():
    """Test that indexing works on a PartialPlus'd dict."""
    v = evaluate(as_pp({'a': partial(float, '3'), 'b': 3})['a'])
    assert v == 3.0