Example #1
0
def test_fold_effect_empty():
    """
    Returns an Effect resulting in the initial value when there are no effects.
    """
    eff = fold_effect(operator.add, 0, [])
    result = sync_perform(base_dispatcher, eff)
    assert result == 0
Example #2
0
def test_fold_effect_empty():
    """
    Returns an Effect resulting in the initial value when there are no effects.
    """
    eff = fold_effect(operator.add, 0, [])
    result = sync_perform(base_dispatcher, eff)
    assert result == 0
Example #3
0
def test_fold_effect_str():
    """str()ing a FoldError returns useful traceback/exception info."""
    effs = [Effect("a"), Effect(Error(ZeroDivisionError("foo"))), Effect("c")]
    dispatcher = [("a", lambda i: "Ei")]

    eff = fold_effect(operator.add, "Nil", effs)
    with raises(FoldError) as excinfo:
        perform_sequence(dispatcher, eff)
    assert str(excinfo.value).startswith("<FoldError after accumulating 'NilEi'> Original traceback follows:\n")
    assert str(excinfo.value).endswith("ZeroDivisionError: foo")
Example #4
0
def test_fold_effect():
    """
    :func:`fold_effect` folds the given function over the results of the
    effects.
    """
    effs = [Effect("a"), Effect("b"), Effect("c")]

    dispatcher = [("a", lambda i: "Ei"), ("b", lambda i: "Bee"), ("c", lambda i: "Cee")]
    eff = fold_effect(operator.add, "Nil", effs)
    result = perform_sequence(dispatcher, eff)
    assert result == "NilEiBeeCee"
Example #5
0
def test_fold_effect_str():
    """str()ing a FoldError returns useful traceback/exception info."""
    effs = [Effect("a"), Effect(Error(ZeroDivisionError("foo"))), Effect("c")]
    dispatcher = [("a", lambda i: "Ei")]

    eff = fold_effect(operator.add, "Nil", effs)
    with raises(FoldError) as excinfo:
        perform_sequence(dispatcher, eff)
    assert str(excinfo.value).startswith(
        "<FoldError after accumulating 'NilEi'> Original traceback follows:\n")
    assert str(excinfo.value).endswith("ZeroDivisionError: foo")
Example #6
0
def test_fold_effect():
    """
    :func:`fold_effect` folds the given function over the results of the
    effects.
    """
    effs = [Effect("a"), Effect("b"), Effect("c")]

    dispatcher = [
        ("a", lambda i: "Ei"),
        ("b", lambda i: "Bee"),
        ("c", lambda i: "Cee"),
    ]
    eff = fold_effect(operator.add, "Nil", effs)
    result = perform_sequence(dispatcher, eff)
    assert result == "NilEiBeeCee"
Example #7
0
def test_fold_effect():
    """
    :func:`fold_effect` folds the given function over the results of the
    effects.
    """
    effs = [Effect('a'), Effect('b'), Effect('c')]

    dispatcher = [
        ('a', lambda i: 'Ei'),
        ('b', lambda i: 'Bee'),
        ('c', lambda i: 'Cee'),
    ]
    eff = fold_effect(operator.add, 'Nil', effs)
    result = perform_sequence(dispatcher, eff)
    assert result == 'NilEiBeeCee'
Example #8
0
def test_fold_effect_errors():
    """
    When one of the effects in the folding list fails, a FoldError is raised
    with the accumulator so far.
    """
    effs = [Effect("a"), Effect(Error(ZeroDivisionError("foo"))), Effect("c")]

    dispatcher = [("a", lambda i: "Ei")]

    eff = fold_effect(operator.add, "Nil", effs)

    with raises(FoldError) as excinfo:
        perform_sequence(dispatcher, eff)
    assert excinfo.value.accumulator == "NilEi"
    assert excinfo.value.wrapped_exception[0] is ZeroDivisionError
    assert str(excinfo.value.wrapped_exception[1]) == "foo"
Example #9
0
def test_fold_effect_errors():
    """
    When one of the effects in the folding list fails, a FoldError is raised
    with the accumulator so far.
    """
    effs = [Effect("a"), Effect(Error(ZeroDivisionError("foo"))), Effect("c")]

    dispatcher = [("a", lambda i: "Ei")]

    eff = fold_effect(operator.add, "Nil", effs)

    with raises(FoldError) as excinfo:
        perform_sequence(dispatcher, eff)
    assert excinfo.value.accumulator == "NilEi"
    assert_that(excinfo.value.wrapped_exception,
                MatchesException(ZeroDivisionError("foo")))
Example #10
0
def test_fold_effect_errors():
    """
    When one of the effects in the folding list fails, a FoldError is raised
    with the accumulator so far.
    """
    effs = [Effect('a'), Effect(Error(ZeroDivisionError('foo'))), Effect('c')]

    dispatcher = [('a', lambda i: 'Ei')]

    eff = fold_effect(operator.add, 'Nil', effs)

    with raises(FoldError) as excinfo:
        perform_sequence(dispatcher, eff)
    assert excinfo.value.accumulator == 'NilEi'
    assert excinfo.value.wrapped_exception[0] is ZeroDivisionError
    assert str(excinfo.value.wrapped_exception[1]) == 'foo'
Example #11
0
def test_fold_effect():
    """
    :func:`fold_effect` folds the given function over the results of the
    effects.
    """
    effs = [Effect('a'), Effect('b'), Effect('c')]

    dispatcher = SequenceDispatcher([
        ('a', lambda i: 'Ei'),
        ('b', lambda i: 'Bee'),
        ('c', lambda i: 'Cee'),
    ])
    eff = fold_effect(operator.add, 'Nil', effs)

    with dispatcher.consume():
        result = sync_perform(_base_and(dispatcher), eff)
    assert result == 'NilEiBeeCee'