# Path: paco/thunk.py
# def thunk(coro):
#     """
#     A thunk is a subroutine that is created, often automatically, to assist
#     a call to another subroutine.
# 
#     Creates a thunk coroutine which returns coroutine function that accepts no
#     arguments and when invoked it schedules the wrapper coroutine and
#     returns the final result.
# 
#     See Wikipedia page for more information about Thunk subroutines:
#     https://en.wikipedia.org/wiki/Thunk
# 
#     Arguments:
#         value (coroutinefunction): wrapped coroutine function to invoke.
# 
#     Returns:
#         coroutinefunction
# 
#     Usage::
# 
#         async def task():
#             return 'foo'
# 
#         coro = paco.thunk(task)
# 
#         await coro()
#         # => 'foo'
#         await coro()
#         # => 'foo'
# 
#     """
#     assert_corofunction(coro=coro)
# 
#     @asyncio.coroutine
#     def wrapper():
#         return (yield from coro())
# 
#     return wrapper
# 
# Path: tests/helpers.py
# def run_in_loop(coro, *args, **kw):
#     loop = asyncio.get_event_loop()
#     if asyncio.iscoroutinefunction(coro) or isfunction(coro):
#         coro = coro(*args, **kw)
#     return loop.run_until_complete(coro)


import pytest
import asyncio
from paco import thunk
from .helpers import run_in_loop
# -*- coding: utf-8 -*-


@asyncio.coroutine
def task():
    return 'foo'


def test_thunk():
    coro = thunk(task)
    assert run_in_loop(coro()) == 'foo'
    assert run_in_loop(coro()) == 'foo'
    assert run_in_loop(coro()) == 'foo'


def test_thunk_error():
    with pytest.raises(TypeError):
        run_in_loop(None)

    with pytest.raises(TypeError):
        run_in_loop(1)

    with pytest.raises(TypeError):

# Path: paco/thunk.py
# def thunk(coro):
#     """
#     A thunk is a subroutine that is created, often automatically, to assist
#     a call to another subroutine.
# 
#     Creates a thunk coroutine which returns coroutine function that accepts no
#     arguments and when invoked it schedules the wrapper coroutine and
#     returns the final result.
# 
#     See Wikipedia page for more information about Thunk subroutines:
#     https://en.wikipedia.org/wiki/Thunk
# 
#     Arguments:
#         value (coroutinefunction): wrapped coroutine function to invoke.
# 
#     Returns:
#         coroutinefunction
# 
#     Usage::
# 
#         async def task():
#             return 'foo'
# 
#         coro = paco.thunk(task)
# 
#         await coro()
#         # => 'foo'
#         await coro()
#         # => 'foo'
# 
#     """
#     assert_corofunction(coro=coro)
# 
#     @asyncio.coroutine
#     def wrapper():
#         return (yield from coro())
# 
#     return wrapper
# 
# Path: tests/helpers.py
# def run_in_loop(coro, *args, **kw):
#     loop = asyncio.get_event_loop()
#     if asyncio.iscoroutinefunction(coro) or isfunction(coro):
#         coro = coro(*args, **kw)
#     return loop.run_until_complete(coro)


# Path: tests/thunk_test.py
import pytest
import asyncio
from paco import thunk
from .helpers import run_in_loop

# -*- coding: utf-8 -*-


@asyncio.coroutine
def task():
    return 'foo'


def test_thunk():
    coro = thunk(task)
    assert run_in_loop(coro()) == 'foo'
    assert run_in_loop(coro()) == 'foo'
    assert run_in_loop(coro()) == 'foo'


def test_thunk_error():
    with pytest.raises(TypeError):
        run_in_loop(None)

    with pytest.raises(TypeError):
        run_in_loop(1)

    with pytest.raises(TypeError):

        run_in_loop('foo')