from python_on_rails.either import as_either, Failure, Success


@as_either(TypeError)
def add_one(x):
    return x + 1


@as_either()
def times_five(x):
    return x * 5


def test_success_executes_bindings():
    result = Success(1).bind(add_one).bind(times_five)
    assert isinstance(result, Success)
    assert result.value == 10


def test_a_failure_stops_the_execution_of_later_bindings():
    result = Success("NaN").bind(add_one).bind(times_five)
    assert isinstance(result, Failure)
    assert type(result.value) == TypeError
    assert repr(result.value) == "TypeError('can only concatenate str (not \"int\") to str')"

[((4, 1, 4, 21), 'python_on_rails.either.as_either', 'as_either', ({(4, 11, 4, 20): 'TypeError'}, {}), '(TypeError)', False, 'from python_on_rails.either import as_either, Failure, Success\n'), ((9, 1, 9, 12), 'python_on_rails.either.as_either', 'as_either', ({}, {}), '()', False, 'from python_on_rails.either import as_either, Failure, Success\n'), ((15, 13, 15, 23), 'python_on_rails.either.Success', 'Success', ({(15, 21, 15, 22): '1'}, {}), '(1)', False, 'from python_on_rails.either import as_either, Failure, Success\n'), ((21, 13, 21, 27), 'python_on_rails.either.Success', 'Success', ({(21, 21, 21, 26): '"""NaN"""'}, {}), "('NaN')", False, 'from python_on_rails.either import as_either, Failure, Success\n')]