File: sdl2ext_particles_test.py

package info (click to toggle)
pysdl2 0.9.9%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,276 kB
  • sloc: python: 18,592; makefile: 148; sh: 40
file content (130 lines) | stat: -rw-r--r-- 3,976 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import sys
import pytest
from sdl2.ext import particles


class TestSDL2ExtParticles(object):
    __tags__ = ["sdl2ext"]

    def test_Particle(self):
        p = particles.Particle(0, 0, 0)
        assert isinstance(p, particles.Particle)
        assert p.x == p.y == p.life == 0
        p = particles.Particle(1, 2, 3)
        assert isinstance(p, particles.Particle)
        assert p.x == 1
        assert p.y == 2
        assert p.life == 3

    def test_Particle_xy_position(self):
        for x in range(-100, 100):
            for y in range(-100, 100):
                p = particles.Particle(x, y, 1)
                assert p.position == (x, y)
                assert p.x == x
                assert p.y == y
                p.position = x + 1, y + 1
                assert p.position == (x + 1, y + 1)
                assert p.x == x + 1
                assert p.y == y + 1
                p.x = x
                assert p.position == (x, y + 1)
                assert p.x == x
                assert p.y == y + 1
                p.y = y
                assert p.position == (x, y)
                assert p.x == x
                assert p.y == y

    def test_Particle_life(self):
        for life in range(-100, 100):
            p = particles.Particle(0, 0, life)
            assert p.life == life

    def test_ParticleEngine(self):
        engine = particles.ParticleEngine()
        assert isinstance(engine, particles.ParticleEngine)
        assert particles.Particle in engine.componenttypes
        assert engine.createfunc is None
        assert engine.deletefunc is None
        assert engine.updatefunc is None

    def test_ParticleEngine_createfunc(self):
        def func(w, c):
            pass
        engine = particles.ParticleEngine()
        assert engine.createfunc is None
        engine.createfunc = func
        assert engine.createfunc == func

        def setf(x, f):
            x.createfunc = f
        with pytest.raises(TypeError):
            setf(engine, None)
        with pytest.raises(TypeError):
            setf(engine, "Test")
        with pytest.raises(TypeError):
            setf(engine, 1234)

    def test_ParticleEngine_deletefunc(self):
        def func(w, c):
            pass
        engine = particles.ParticleEngine()
        assert engine.deletefunc is None
        engine.deletefunc = func
        assert engine.deletefunc == func

        def setf(x, f):
            x.deletefunc = f
        with pytest.raises(TypeError):
            setf(engine, None)
        with pytest.raises(TypeError):
            setf(engine, "Test")
        with pytest.raises(TypeError):
            setf(engine, 1234)

    def test_ParticleEngine_updatefunc(self):
        def func(w, c):
            pass
        engine = particles.ParticleEngine()
        assert engine.updatefunc is None
        engine.updatefunc = func
        assert engine.updatefunc == func

        def setf(x, f):
            x.updatefunc = f
        with pytest.raises(TypeError):
            setf(engine, None)
        with pytest.raises(TypeError):
            setf(engine, "Test")
        with pytest.raises(TypeError):
            setf(engine, 1234)

    def test_ParticleEngine_process(self):
        def cfunc(w, c):
            assert len(c) == w["runs"]
            for p in c:
                assert p.life <= 0

        def ufunc(w, c):
            assert len(c) == 100 - w["runs"]
            for p in c:
                assert p.life >= 1

        def dfunc(w, c):
            assert len(c) == w["runs"]
            for p in c:
                assert p.life <= 0

        plist = []
        for x in range(2, 102):
            plist.append(particles.Particle(x, x, x - 1))

        engine = particles.ParticleEngine()
        engine.createfunc = cfunc
        engine.updatefunc = ufunc
        engine.deletefunc = dfunc
        world = {"runs": 1}
        engine.process(world, plist)
        world["runs"] = 2
        engine.process(world, plist)