File: test_transform_anchor.py

package info (click to toggle)
pgzero 1.2.post4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,980 kB
  • sloc: python: 4,273; makefile: 166
file content (41 lines) | stat: -rw-r--r-- 1,080 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from math import sin, cos, radians
from unittest import TestCase
from pgzero.actor import transform_anchor

root2 = 2 ** 0.5


def assertVecEqual(a, b, decimal_places=7):
    for t in (a, b):
        if not isinstance(t, tuple):
            raise AssertionError('%r is not a tuple' % t)
        if len(t) != 2:
            raise AssertionError('Expected 2-tuple, not %r' % t)

    ax, ay = a
    bx, by = b
    epsilon = 10 ** -decimal_places
    if abs(ax - bx) > epsilon or abs(ay - by) > epsilon:
        raise AssertionError('%r != %r (to %d decimal places)' % (
            a, b, decimal_places
        ))


class TransformAnchorTest(TestCase):
    def test_identity(self):
        assertVecEqual(
            transform_anchor(5, 5, 10, 10, 0),
            (5, 5)
        )

    def test_45deg(self):
        assertVecEqual(
            transform_anchor(5, 5, 10, 10, 45),
            (5 * root2, 5 * root2)
        )

    def test_45deg_offset(self):
        assertVecEqual(
            transform_anchor(0, 0.5, 1, 1, 45),
            (0.25 * root2, 0.75 * root2)
        )