async def test_flat_map_monad():
    m = unit(42)

    async def mapper(x):
        return unit(x * 10)

    a = await run(flat_map(mapper, m))
    b = await run(unit(420))
    assert a == b
Exemple #2
0
async def test_unit_happy_resolved_future():
    fut = asyncio.Future()
    xs = unit(fut)
    fut.set_result(42)

    obv = AsyncAnonymousObserver()
    await run(xs, obv)
    assert obv.values == [(0, 42), (0, )]
async def test_unit_happy_resolved_future():
    fut = asyncio.Future()
    xs = unit(fut)
    fut.set_result(42)

    lis = AnonymousAsyncObserver()
    await run(xs, lis)
    assert lis.values == [(0, 42), (0, )]
async def test_unit_happy():
    xs = unit(42)
    result = []

    async def asend(value):
        result.append(value)

    await run(xs, AnonymousAsyncObserver(asend))
    assert result == [42]
async def test_unit_happy_future_resolve():
    fut = asyncio.Future()
    xs = unit(fut)

    lis = AnonymousAsyncObserver()
    sub = await subscribe(xs, lis)
    fut.set_result(42)
    await sub
    assert lis.values == [(0, 42), (0, )]
async def test_unit_future_cancel():
    fut = asyncio.Future()
    xs = unit(fut)

    lis = AnonymousAsyncObserver()
    sub = await subscribe(xs, lis)
    fut.cancel()
    with pytest.raises(asyncio.CancelledError):
        await sub
    assert lis.values == [(0, )]
Exemple #7
0
async def test_unit_happy_future_resolve():
    fut = asyncio.Future()
    xs = unit(fut)

    obv = AsyncAnonymousObserver()
    async with subscribe(xs, obv):
        fut.set_result(42)
        await obv

    assert obv.values == [(0, 42), (0, )]
Exemple #8
0
async def test_unit_future_exception():
    fut = asyncio.Future()
    ex = Exception("ex")
    xs = unit(fut)

    obv = AsyncAnonymousObserver()
    async with subscribe(xs, obv):
        fut.set_exception(ex)
        with pytest.raises(Exception):
            await obv
    assert obv.values == [(0, ex)]
Exemple #9
0
async def test_unit_future_cancel():
    fut = asyncio.Future()
    xs = unit(fut)

    obv = AsyncAnonymousObserver()
    async with subscribe(xs, obv):
        fut.cancel()
        with pytest.raises(asyncio.CancelledError):
            await obv

    assert obv.values == [(0, )]
async def test_unit_future_exception():
    fut = asyncio.Future()
    ex = Exception("ex")
    xs = unit(fut)

    lis = AnonymousAsyncObserver()
    sub = await subscribe(xs, lis)
    fut.set_exception(ex)
    with pytest.raises(Exception):
        await sub
    assert lis.values == [(0, ex)]
async def test_flat_map_monad_law_right_identity():
    # m >>= return is no different than just m.

    m = unit("move on up")

    async def aunit(x):
        return unit(x)

    a = await run(flat_map(aunit, m))
    b = await run(m)

    assert a == b
async def test_flat_map_monad_law_left_identity():
    # return x >>= f is the same thing as f x

    x = 3

    async def f(x):
        return unit(x + 100000)

    a = await run(flat_map(f, unit(x)))
    b = await run(await f(x))

    assert a == b
async def test_unit_observer_throws():
    error = Exception("error")
    xs = unit(42)
    result = []

    def asend(value):
        result.append(value)
        raise error

    sub = await subscribe(xs, AnonymousAsyncObserver(asend))

    try:
        await sub
    except Exception as ex:
        assert ex == error
    assert result == [42]
async def test_flat_map_monad_law_associativity():
    # (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g)

    m = unit(42)

    async def f(x):
        return unit(x + 1000)

    async def g(y):
        return unit(y * 333)

    async def h(x):
        return flat_map(g, await f(x))

    a = await run(flat_map(g, flat_map(f, m)))
    b = await run(flat_map(h, m))

    assert a == b
async def test_unit_close():
    xs = unit(42)
    result = []
    sub = None

    async def asend(value):
        result.append(value)
        sub.cancel()
        await asyncio.sleep(0)

    sub = await subscribe(xs, AnonymousAsyncObserver(asend))

    try:
        await sub
    except asyncio.CancelledError:
        pass

    assert result == [42]
Exemple #16
0
async def test_unit_close():
    xs = unit(42)
    result = []
    sub = None

    async def asend(value):
        result.append(value)
        await sub.adispose()
        await asyncio.sleep(0)

    obv = AsyncAnonymousObserver(asend)
    sub = await subscribe(xs, obv)

    try:
        await obv
    except asyncio.CancelledError:
        pass

    assert result == [42]
Exemple #17
0
 def unit(cls, value: T) -> 'AsyncObservable[T]':
     from aioreactive.operators.unit import unit
     return unit(value)
 async def aunit(x):
     return unit(x)
 async def f(x):
     return unit(x + 1000)
 async def g(y):
     return unit(y * 333)
 async def mapper(x):
     return unit(x * 10)