Beispiel #1
0
def test_compose():
    f = restrict_arity(Mock(), 1)
    g = restrict_arity(Mock(), 3)
    h = compose(f, g)

    result = h(1, 2, 3)

    g.assert_called_once_with(1, 2, 3)
    f.assert_called_once_with(g.return_value)
    assert result is f.return_value
Beispiel #2
0
def test_parallel_combine():
    h = restrict_arity(Mock(), 2)
    f = restrict_arity(Mock(), 3)
    g = restrict_arity(Mock(), 3)
    c = parallel_combine(h, f, g)

    result = c(1, 2, 3)

    f.assert_called_once_with(1, 2, 3)
    g.assert_called_once_with(1, 2, 3)
    h.assert_called_once_with(f.return_value, g.return_value)
    assert result is h.return_value
Beispiel #3
0
def test_spread_combine_with_g_vararg():
    h, f, g = Mock(), Mock(), Mock()
    restrict_arity(f, 2)
    restrict_arity(g, Arity(0, False))
    c = spread_combine(h, f, g)

    result = c(1, 2, 3, 4, 5)

    f.assert_called_once_with(1, 2)
    g.assert_called_once_with(3, 4, 5)
    h.assert_called_once_with(f.return_value, g.return_value)
    assert result == h.return_value
    assert get_arity(c) == Arity(2, False)
Beispiel #4
0
def test_spread_combine():
    h, f, g = Mock(), Mock(), Mock()
    restrict_arity(f, 2)
    restrict_arity(g, 3)
    c = spread_combine(h, f, g)

    result = c(1, 2, 3, 4, 5)

    f.assert_called_once_with(1, 2)
    g.assert_called_once_with(3, 4, 5)
    h.assert_called_once_with(f.return_value, g.return_value)
    assert result is h.return_value
    assert get_arity(c) == Arity(5)
Beispiel #5
0
def test_iterate_feeds_output_back_to_function():
    func = restrict_arity(Mock(), 1)
    iterate(3)(func)("input")
    func.assert_has_calls(
        [call("input"),
         call(func.return_value),
         call(func.return_value)])
Beispiel #6
0
def test_permute_arguments():
    func = restrict_arity(Mock(), 4)
    f1 = permute_arguments(1, 2, 0, 3)
    f2 = f1(func)
    r = f2('a', 'b', 'c', 'd')

    func.assert_called_once_with('b', 'c', 'a', 'd')
    assert r is func.return_value
Beispiel #7
0
def test_curry_multple_arguments():
    func = restrict_arity(Mock(), 5)
    f1 = curry_arguments(1, 3)
    f2 = f1('a', 'b', 'c')
    f3 = f2(func)
    r = f3('x', 'y')

    func.assert_called_once_with('a', 'x', 'b', 'y', 'c')
    assert r is func.return_value
Beispiel #8
0
def test_discard_one_argument():
    dc = discard_arguments(1)
    func = restrict_arity(Mock(), 2)
    f = dc(func)

    r = f(1, 2, 3)

    func.assert_called_once_with(1, 3)
    assert r is func.return_value
Beispiel #9
0
def test_curry_argument():
    func = restrict_arity(Mock(), 4)
    f1 = curry_arguments(2)
    f2 = f1('a', 'b', 'c')
    f3 = f2(func)
    r = f3('d')

    func.assert_called_once_with('a', 'b', 'd', 'c')
    assert r is func.return_value
Beispiel #10
0
def test_discard_keyword_argument():
    dc = discard_arguments('b')
    func = restrict_arity(Mock(), 2)
    f = dc(func)

    r = f(1, b=2, c=3)

    func.assert_called_once_with(1, c=3)
    assert r is func.return_value
Beispiel #11
0
def test_discard_multiple_arguments():
    dc = discard_arguments(0, 2, 4)
    func = restrict_arity(Mock(), 2)
    f = dc(func)

    r = f(1, 2, 3, 4, 5)

    func.assert_called_once_with(2, 4)
    assert r is func.return_value
Beispiel #12
0
def test_parallel_combine_arity_equals_arity_of_f_and_g():
    h = restrict_arity(Mock(), 2)
    p = parallel_combine(h, lambda x, y, z: 0, lambda x, y, z: 0)
    assert get_arity(p) == Arity(3)
Beispiel #13
0
def test_iterate_calls_function_n_times():
    func = restrict_arity(Mock(), 1)
    iterate(3)(func)("input")
    assert func.call_count == 3
Beispiel #14
0
def test_iterate_once_calls_the_function():
    func = restrict_arity(Mock(), 1)
    result = iterate(1)(func)(42)
    func.assert_called_once_with(42)
    assert result is func.return_value
Beispiel #15
0
def test_composition_checks_arity():
    f = restrict_arity(Mock(), 1)
    h = compose(f, lambda x, y: 0)
    with pytest.raises(TypeError, match="argument"):
        compose(f, h)()
Beispiel #16
0
def test_spread_combine_f_and_g_cant_both_be_vararg():
    h, f, g = Mock(), Mock(), Mock()
    restrict_arity(f, Arity(2, 3))
    restrict_arity(g, Arity(2, 3))
    with pytest.raises(AssertionError):
        spread_combine(h, f, g)
Beispiel #17
0
def test_composition_arity_equals_arity_of_g():
    f = restrict_arity(Mock(), 1)
    h = compose(f, lambda x, y: 0)
    assert get_arity(h) == Arity(2)