| 1 | import pytest |
| 2 | |
| 3 | import pytermgui as ptg |
| 4 | from pytermgui.animations import Animation, Direction, _remove_flag |
| 5 | |
| 6 | |
| 7 | class MyTarget: |
| 8 | test_attr: int = 5 |
| 9 | |
| 10 | |
| 11 | def _reset(): |
| 12 | ptg.animator._animations = [] |
| 13 | |
| 14 | |
| 15 | def test_flags(): |
| 16 | target = MyTarget() |
| 17 | |
| 18 | with pytest.raises(ValueError): |
| 19 | _remove_flag(target, "nothing") |
| 20 | |
| 21 | animation = ptg.animator.animate_attr(target=target, attr="test_attr", duration=100) |
| 22 | |
| 23 | assert hasattr(target, "__ptg_animated__") |
| 24 | assert "test_attr" in target.__ptg_animated__ |
| 25 | |
| 26 | animation.finish() |
| 27 | assert not hasattr(target, "__ptg_animated__") |
| 28 | |
| 29 | |
| 30 | def test_is_animated(): |
| 31 | target = MyTarget() |
| 32 | |
| 33 | assert not ptg.is_animated(target, "test_attr") |
| 34 | |
| 35 | ptg.animator.animate_attr(target=target, attr="test_attr", duration=100) |
| 36 | |
| 37 | assert ptg.is_animated(target, "test_attr") |
| 38 | |
| 39 | |
| 40 | def test_animate_float(): |
| 41 | _reset() |
| 42 | |
| 43 | elapsed = 0 |
| 44 | animation = ptg.animator.animate_float(duration=1000, direction=-1) |
| 45 | |
| 46 | print(ptg.animator._animations) |
| 47 | for _ in range(10): |
| 48 | elapsed += 0.1 |
| 49 | ptg.animator.step(0.1) |
| 50 | assert round(animation.state, 4) == round(1 - elapsed, 4) |
| 51 | |
| 52 | |
| 53 | def test_animate_attr(): |
| 54 | _reset() |
| 55 | |
| 56 | target = MyTarget() |
| 57 | ptg.animator.animate_attr( |
| 58 | target=target, attr="test_attr", start=0, end=10, duration=1000 |
| 59 | ) |
| 60 | |
| 61 | for i in range(1, 10): |
| 62 | ptg.animator.step(0.1) |
| 63 | assert target.test_attr == i |
| 64 | |
| 65 | |
| 66 | def test_animate_attr_manual_step(): |
| 67 | target = MyTarget() |
| 68 | |
| 69 | animation = ptg.animator.animate_attr( |
| 70 | target=target, attr="test_attr", start=0, end=10, duration=100, direction=-1 |
| 71 | ) |
| 72 | |
| 73 | for i in range(1, 10): |
| 74 | animation.step(0.1) |
| 75 | |
| 76 | |
| 77 | def test_animator_not_active(): |
| 78 | _reset() |
| 79 | |
| 80 | assert not ptg.animator.is_active |
| 81 | |
| 82 | ptg.animator.animate_float(duration=100) |
| 83 | assert ptg.animator.is_active |
| 84 | |
| 85 | |
| 86 | def test_animation_finishes_on_step(): |
| 87 | _reset() |
| 88 | |
| 89 | def _on_step(anim: Animation) -> bool: |
| 90 | return anim.direction == Direction.BACKWARD |
| 91 | |
| 92 | ptg.animator.schedule( |
| 93 | Animation( |
| 94 | duration=100, |
| 95 | direction=Direction.FORWARD, |
| 96 | loop=True, |
| 97 | state=0.0, |
| 98 | _remaining=0, |
| 99 | on_step=_on_step, |
| 100 | on_finish=lambda _: None, |
| 101 | ) |
| 102 | ) |
| 103 | |
| 104 | ptg.animator.animate_attr( |
| 105 | target=MyTarget(), |
| 106 | attr="test_attr", |
| 107 | duration=1000, |
| 108 | loop=True, |
| 109 | on_step=_on_step, |
| 110 | on_finish=lambda _: None, |
| 111 | ) |
| 112 | |
| 113 | ptg.animator.step(1) |
| 114 | assert not ptg.animator.is_active |