| 1 |
from unittest import TestCase |
| 2 |
from types import SimpleNamespace |
| 3 |
import gc |
| 4 |
import weakref |
| 5 |
|
| 6 |
from pgzero.animation import animate |
| 7 |
from pgzero import clock |
| 8 |
|
| 9 |
|
| 10 |
class TestObject: |
| 11 |
"""A simple object subclass""" |
| 12 |
|
| 13 |
|
| 14 |
class SuggestionTest(TestCase): |
| 15 |
def test_basic_animation(self): |
| 16 |
"""Test that animation works""" |
| 17 |
obj = SimpleNamespace() |
| 18 |
obj.attribute = 0 |
| 19 |
animate(obj, attribute=2, duration=2) |
| 20 |
self.assertEqual(obj.attribute, 0) |
| 21 |
clock.tick(1) |
| 22 |
self.assertEqual(obj.attribute, 1) |
| 23 |
clock.tick(1) |
| 24 |
self.assertEqual(obj.attribute, 2) |
| 25 |
clock.tick(1) |
| 26 |
self.assertEqual(obj.attribute, 2) |
| 27 |
|
| 28 |
def test_some_tween(self): |
| 29 |
"""Test that tweening does something""" |
| 30 |
obj = SimpleNamespace() |
| 31 |
obj.attribute = 0 |
| 32 |
animate(obj, attribute=2, tween='accelerate', duration=2) |
| 33 |
self.assertEqual(obj.attribute, 0) |
| 34 |
clock.tick(1) |
| 35 |
assert 0 < obj.attribute < 1 |
| 36 |
clock.tick(1) |
| 37 |
self.assertEqual(obj.attribute, 2) |
| 38 |
clock.tick(1) |
| 39 |
self.assertEqual(obj.attribute, 2) |
| 40 |
|
| 41 |
def test_tuple_animation(self): |
| 42 |
"""Test that you can animate a tuple""" |
| 43 |
obj = SimpleNamespace() |
| 44 |
obj.attribute = 0, 2 |
| 45 |
animate(obj, attribute=(2, 0), duration=2) |
| 46 |
self.assertEqual(obj.attribute, (0, 2)) |
| 47 |
clock.tick(1) |
| 48 |
self.assertEqual(obj.attribute, (1, 1)) |
| 49 |
clock.tick(1) |
| 50 |
self.assertEqual(obj.attribute, (2, 0)) |
| 51 |
clock.tick(1) |
| 52 |
self.assertEqual(obj.attribute, (2, 0)) |
| 53 |
|
| 54 |
def test_list_animation(self): |
| 55 |
"""Test that you can animate a list""" |
| 56 |
obj = SimpleNamespace() |
| 57 |
obj.attribute = [0, 2] |
| 58 |
animate(obj, attribute=[2, 0], duration=2) |
| 59 |
self.assertEqual(obj.attribute, [0, 2]) |
| 60 |
clock.tick(1) |
| 61 |
self.assertEqual(obj.attribute, [1, 1]) |
| 62 |
clock.tick(1) |
| 63 |
self.assertEqual(obj.attribute, [2, 0]) |
| 64 |
clock.tick(1) |
| 65 |
self.assertEqual(obj.attribute, [2, 0]) |
| 66 |
|
| 67 |
def test_on_finished(self): |
| 68 |
"""Test the on_finished callback works""" |
| 69 |
obj = SimpleNamespace() |
| 70 |
obj.attribute = 0 |
| 71 |
endlist = ['in progress'] |
| 72 |
animate(obj, attribute=2, duration=2, on_finished=endlist.clear) |
| 73 |
self.assertEqual(obj.attribute, 0) |
| 74 |
self.assertEqual(endlist, ['in progress']) |
| 75 |
clock.tick(1) |
| 76 |
self.assertEqual(obj.attribute, 1) |
| 77 |
self.assertEqual(endlist, ['in progress']) |
| 78 |
clock.tick(1) |
| 79 |
self.assertEqual(obj.attribute, 2) |
| 80 |
clock.tick(1) |
| 81 |
self.assertEqual(obj.attribute, 2) |
| 82 |
self.assertEqual(endlist, []) |
| 83 |
|
| 84 |
def test_cancel_old(self): |
| 85 |
"""Test an old animation is cancelled when overwritten""" |
| 86 |
obj = SimpleNamespace() |
| 87 |
obj.attribute = 0 |
| 88 |
animate(obj, attribute=-4, duration=4) |
| 89 |
animate(obj, attribute=2, duration=2) |
| 90 |
self.assertEqual(obj.attribute, 0) |
| 91 |
clock.tick(1) |
| 92 |
self.assertEqual(obj.attribute, 1) |
| 93 |
clock.tick(1) |
| 94 |
self.assertEqual(obj.attribute, 2) |
| 95 |
clock.tick(1) |
| 96 |
self.assertEqual(obj.attribute, 2) |
| 97 |
|
| 98 |
def test_cancel_some(self): |
| 99 |
"""Test only the overwritten target of an old animation is cancelled""" |
| 100 |
obj = SimpleNamespace() |
| 101 |
obj.attr1 = 0 |
| 102 |
obj.attr2 = 0 |
| 103 |
animate(obj, attr1=-4, attr2=-4, duration=4) |
| 104 |
animate(obj, attr1=2, duration=2) |
| 105 |
self.assertEqual(obj.attr1, 0) |
| 106 |
self.assertEqual(obj.attr2, 0) |
| 107 |
clock.tick(1) |
| 108 |
self.assertEqual(obj.attr1, 1) |
| 109 |
self.assertEqual(obj.attr2, -1) |
| 110 |
clock.tick(1) |
| 111 |
self.assertEqual(obj.attr1, 2) |
| 112 |
self.assertEqual(obj.attr2, -2) |
| 113 |
clock.tick(1) |
| 114 |
self.assertEqual(obj.attr1, 2) |
| 115 |
self.assertEqual(obj.attr2, -3) |
| 116 |
|
| 117 |
def test_no_linger(self): |
| 118 |
"""Test that deleted animations are garbage-collected""" |
| 119 |
anim1_alivelist = ['animation 1 alive'] |
| 120 |
anim2_alivelist = ['animation 2 alive'] |
| 121 |
obj_alivelist = ['object alive'] |
| 122 |
|
| 123 |
obj = TestObject() |
| 124 |
obj.attribute = 0 |
| 125 |
anim1 = animate(obj, attribute=1, duration=5) |
| 126 |
anim2 = animate(obj, attribute=2, duration=1) |
| 127 |
weakref.finalize(anim1, anim1_alivelist.clear) |
| 128 |
weakref.finalize(anim2, anim2_alivelist.clear) |
| 129 |
weakref.finalize(obj, obj_alivelist.clear) |
| 130 |
|
| 131 |
del anim1 |
| 132 |
del anim2 |
| 133 |
del obj |
| 134 |
gc.collect() |
| 135 |
self.assertEqual(anim1_alivelist, []) |
| 136 |
|
| 137 |
clock.tick(3) |
| 138 |
gc.collect() |
| 139 |
self.assertEqual(anim2_alivelist, []) |
| 140 |
self.assertEqual(obj_alivelist, []) |