# Path: paco/whilst.py
# @asyncio.coroutine
# def whilst(coro, coro_test, assert_coro=None, *args, **kw):
#     """
#     Repeatedly call `coro` coroutine function while `coro_test` returns `True`.
# 
#     This function is the inverse of `paco.until()`.
# 
#     This function is a coroutine.
# 
#     Arguments:
#         coro (coroutinefunction): coroutine function to execute.
#         coro_test (coroutinefunction): coroutine function to test.
#         assert_coro (coroutinefunction): optional assertion coroutine used
#             to determine if the test passed or not.
#         *args (mixed): optional variadic arguments to pass to `coro` function.
# 
#     Raises:
#         TypeError: if input arguments are invalid.
# 
#     Returns:
#         list: result values returned by `coro`.
# 
#     Usage::
# 
#         calls = 0
# 
#         async def task():
#             nonlocal calls
#             calls += 1
#             return calls
# 
#         async def calls_lt_4():
#             return calls > 4
# 
#         await paco.until(task, calls_lt_4)
#         # => [1, 2, 3, 4, 5]
# 
#     """
#     assert_corofunction(coro=coro, coro_test=coro_test)
# 
#     # Store yielded values by coroutine
#     results = []
# 
#     # Set assertion coroutine
#     assert_coro = assert_coro or assert_true
# 
#     # Execute coroutine until a certain
#     while (yield from assert_coro((yield from coro_test()))):
#         results.append((yield from coro(*args, **kw)))
# 
#     return results
# 
# 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 whilst
from .helpers import run_in_loop
# -*- coding: utf-8 -*-


def test_whilst():
    calls = 0

    @asyncio.coroutine
    def coro_test():
        return calls < 5

    @asyncio.coroutine
    def coro():
        nonlocal calls
        calls += 1
        return calls

    task = whilst(coro, coro_test)
    assert run_in_loop(task) == [1, 2, 3, 4, 5]


def test_whilst_invalid_input():
    with pytest.raises(TypeError):

# Path: paco/whilst.py
# @asyncio.coroutine
# def whilst(coro, coro_test, assert_coro=None, *args, **kw):
#     """
#     Repeatedly call `coro` coroutine function while `coro_test` returns `True`.
# 
#     This function is the inverse of `paco.until()`.
# 
#     This function is a coroutine.
# 
#     Arguments:
#         coro (coroutinefunction): coroutine function to execute.
#         coro_test (coroutinefunction): coroutine function to test.
#         assert_coro (coroutinefunction): optional assertion coroutine used
#             to determine if the test passed or not.
#         *args (mixed): optional variadic arguments to pass to `coro` function.
# 
#     Raises:
#         TypeError: if input arguments are invalid.
# 
#     Returns:
#         list: result values returned by `coro`.
# 
#     Usage::
# 
#         calls = 0
# 
#         async def task():
#             nonlocal calls
#             calls += 1
#             return calls
# 
#         async def calls_lt_4():
#             return calls > 4
# 
#         await paco.until(task, calls_lt_4)
#         # => [1, 2, 3, 4, 5]
# 
#     """
#     assert_corofunction(coro=coro, coro_test=coro_test)
# 
#     # Store yielded values by coroutine
#     results = []
# 
#     # Set assertion coroutine
#     assert_coro = assert_coro or assert_true
# 
#     # Execute coroutine until a certain
#     while (yield from assert_coro((yield from coro_test()))):
#         results.append((yield from coro(*args, **kw)))
# 
#     return results
# 
# 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/whilst_test.py
import pytest
import asyncio
from paco import whilst
from .helpers import run_in_loop

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


def test_whilst():
    calls = 0

    @asyncio.coroutine
    def coro_test():
        return calls < 5

    @asyncio.coroutine
    def coro():
        nonlocal calls
        calls += 1
        return calls

    task = whilst(coro, coro_test)
    assert run_in_loop(task) == [1, 2, 3, 4, 5]


def test_whilst_invalid_input():
    with pytest.raises(TypeError):

        run_in_loop(whilst(None, None))