Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(api-break) durationが0の時にも値を生むように anim_with_ratio と anim_with_dt_et_r… #129

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/asynckivy/_anim_with_xxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'anim_with_dt', 'anim_with_et', 'anim_with_ratio', 'anim_with_dt_et', 'anim_with_dt_et_ratio',
)

from asynckivy import repeat_sleeping
from ._sleep import repeat_sleeping


async def anim_with_dt(*, step=0):
Expand Down Expand Up @@ -108,8 +108,12 @@ async def anim_with_ratio(*, duration=1., step=0):

.. versionadded:: 0.6.1
'''
et = 0.
async with repeat_sleeping(step=step) as sleep:
if not duration:
await sleep()
yield 1.0
return
et = 0.
while et < duration:
et += await sleep()
yield et / duration
Expand All @@ -126,8 +130,12 @@ async def anim_with_dt_et_ratio(*, duration=1., step=0):

.. versionadded:: 0.6.1
'''
et = 0.
async with repeat_sleeping(step=step) as sleep:
if not duration:
dt = await sleep()
yield dt, dt, 1.0
return
et = 0.
while et < duration:
dt = await sleep()
et += dt
Expand Down
27 changes: 27 additions & 0 deletions tests/test_anim_with_xxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ async def async_fn():
assert task.finished


def test_ratio_zero_duration(approx, sleep_then_tick):
import asynckivy as ak

async def async_fn():
l = [p async for p in ak.anim_with_ratio(duration=0)]
assert l == approx([1.0, ])

task = ak.start(async_fn())
sleep_then_tick(.1)
assert task.finished


def test_dt_et_ratio(approx, sleep_then_tick):
import asynckivy as ak

Expand All @@ -93,3 +105,18 @@ async def async_fn():
for __ in range(3):
sleep_then_tick(.2)
assert task.finished


def test_dt_et_ratio_zero_duration(approx, sleep_then_tick):
import asynckivy as ak

async def async_fn():
l = [v async for v in ak.anim_with_dt_et_ratio(duration=0)]
# assert l == approx([(0.2, 0.2, 1.0, ), ]) # This doesn't work for some reason.
assert l[0][0] == approx(0.2)
assert l[0][1] == approx(0.2)
assert l[0][2] == approx(1.0)

task = ak.start(async_fn())
sleep_then_tick(.2)
assert task.finished