import asyncio
import pytest

def test_hello_world(workspace):
    workspace.src('main.py', r"""
    import asyncio

    async def main():
        print('Hello, ', end='')
        await asyncio.sleep(1)
        print('World!')

    # Python 3.7+
    asyncio.run(main())
    """)

    r = workspace.run('python main.py')
    assert r.out == 'Hello, World!'

@pytest.mark.asyncio
async def test_hello_world__pytest_asyncio(workspace):
    result = await async_task('World')
    assert result == 'Hello, World!'

async def async_task(input):
    await asyncio.sleep(1)
    return 'Hello, %s!' % input

def test_import_asyncio_not_needed_for_using_async_await_keywords(workspace):
    workspace.src('main.py', r"""
    async def main():
        pass

    print(type(main()))
    """)

    r = workspace.run('python main.py')
    assert r.out == "<class 'coroutine'>"
    assert r.err == "main.py:4: RuntimeWarning: coroutine 'main' was never awaited\n  print(type(main()))"


import asyncio
import pytest

def test_hello_world(workspace):
    workspace.src('main.py', r"""
    import asyncio

    async def do_something_else():
        print('...', end='')
        await asyncio.sleep(1)
        print('!', end='')

    async def say_hello_async(who):
        print('Hello, ', end='')
        await asyncio.sleep(1)
        print(who, end='')

    async def main():
        await asyncio.gather(say_hello_async('World'), do_something_else())

    asyncio.run(main())
    """)

    r = workspace.run('python main.py')
    assert r.out == 'Hello, ...World!'

@pytest.mark.asyncio
async def test_hello_world__pytest_asyncio(capsys):
    async def do_something_else():
        print('...', end='')
        await asyncio.sleep(1)
        print('!', end='')

    async def say_hello_async(who):
        print('Hello, ', end='')
        await asyncio.sleep(1)
        print(who, end='')

    await asyncio.gather(say_hello_async('World'), do_something_else())

    out, _ = capsys.readouterr()
    assert out == 'Hello, ...World!'

def test_import_asyncio_not_needed_for_using_async_await_keywords(workspace):
    workspace.src('main.py', r"""
    async def main():
        pass

    print(type(main()))
    """)

    r = workspace.run('python main.py')
    assert r.out == "<class 'coroutine'>"
    assert r.err == "main.py:4: RuntimeWarning: coroutine 'main' was never awaited\n  print(type(main()))"


Make hello world (asyncio) more involved
[python] Make hello world (asyncio) more involved

  import asyncio
  import pytest
  
  def test_hello_world(workspace):
      workspace.src('main.py', r"""
      import asyncio
  
-     async def main():
+     async def do_something_else():
+         print('...', end='')
+         await asyncio.sleep(1)
+         print('!', end='')
+ 
+     async def say_hello_async(who):
          print('Hello, ', end='')
          await asyncio.sleep(1)
-         print('World!')
+         print(who, end='')
  
-     # Python 3.7+
+     async def main():
+         await asyncio.gather(say_hello_async('World'), do_something_else())
+ 
      asyncio.run(main())
      """)
  
      r = workspace.run('python main.py')
-     assert r.out == 'Hello, World!'
+     assert r.out == 'Hello, ...World!'
  
  @pytest.mark.asyncio
- async def test_hello_world__pytest_asyncio(workspace):
+ async def test_hello_world__pytest_asyncio(capsys):
-     result = await async_task('World')
-     assert result == 'Hello, World!'
+     async def do_something_else():
+         print('...', end='')
+         await asyncio.sleep(1)
+         print('!', end='')
  
- async def async_task(input):
+     async def say_hello_async(who):
+         print('Hello, ', end='')
-     await asyncio.sleep(1)
+         await asyncio.sleep(1)
-     return 'Hello, %s!' % input
+         print(who, end='')
+ 
+     await asyncio.gather(say_hello_async('World'), do_something_else())
+ 
+     out, _ = capsys.readouterr()
+     assert out == 'Hello, ...World!'
  
  def test_import_asyncio_not_needed_for_using_async_await_keywords(workspace):
      workspace.src('main.py', r"""
      async def main():
          pass
  
      print(type(main()))
      """)
  
      r = workspace.run('python main.py')
      assert r.out == "<class 'coroutine'>"
      assert r.err == "main.py:4: RuntimeWarning: coroutine 'main' was never awaited\n  print(type(main()))"
  
  
Make hello world (asyncio) more involved
## Code Before:
import asyncio
import pytest

def test_hello_world(workspace):
    workspace.src('main.py', r"""
    import asyncio

    async def main():
        print('Hello, ', end='')
        await asyncio.sleep(1)
        print('World!')

    # Python 3.7+
    asyncio.run(main())
    """)

    r = workspace.run('python main.py')
    assert r.out == 'Hello, World!'

@pytest.mark.asyncio
async def test_hello_world__pytest_asyncio(workspace):
    result = await async_task('World')
    assert result == 'Hello, World!'

async def async_task(input):
    await asyncio.sleep(1)
    return 'Hello, %s!' % input

def test_import_asyncio_not_needed_for_using_async_await_keywords(workspace):
    workspace.src('main.py', r"""
    async def main():
        pass

    print(type(main()))
    """)

    r = workspace.run('python main.py')
    assert r.out == "<class 'coroutine'>"
    assert r.err == "main.py:4: RuntimeWarning: coroutine 'main' was never awaited\n  print(type(main()))"


## Instruction:
Make hello world (asyncio) more involved
## Code After:
import asyncio
import pytest

def test_hello_world(workspace):
    workspace.src('main.py', r"""
    import asyncio

    async def do_something_else():
        print('...', end='')
        await asyncio.sleep(1)
        print('!', end='')

    async def say_hello_async(who):
        print('Hello, ', end='')
        await asyncio.sleep(1)
        print(who, end='')

    async def main():
        await asyncio.gather(say_hello_async('World'), do_something_else())

    asyncio.run(main())
    """)

    r = workspace.run('python main.py')
    assert r.out == 'Hello, ...World!'

@pytest.mark.asyncio
async def test_hello_world__pytest_asyncio(capsys):
    async def do_something_else():
        print('...', end='')
        await asyncio.sleep(1)
        print('!', end='')

    async def say_hello_async(who):
        print('Hello, ', end='')
        await asyncio.sleep(1)
        print(who, end='')

    await asyncio.gather(say_hello_async('World'), do_something_else())

    out, _ = capsys.readouterr()
    assert out == 'Hello, ...World!'

def test_import_asyncio_not_needed_for_using_async_await_keywords(workspace):
    workspace.src('main.py', r"""
    async def main():
        pass

    print(type(main()))
    """)

    r = workspace.run('python main.py')
    assert r.out == "<class 'coroutine'>"
    assert r.err == "main.py:4: RuntimeWarning: coroutine 'main' was never awaited\n  print(type(main()))"

