-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixin_test.py
More file actions
61 lines (43 loc) · 1.65 KB
/
mixin_test.py
File metadata and controls
61 lines (43 loc) · 1.65 KB
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
"""Mixins."""
class GraphicalEntity:
def __init__(self, size_x: int, size_y: int) -> None:
self.size_x = size_x
self.size_y = size_y
class Button(GraphicalEntity):
def __init__(self, size_x: int, size_y: int) -> None:
super().__init__(size_x, size_y)
self.status = False
def toggle(self) -> bool:
self.status = not self.status
return self.status
class LimitSizeMixin:
"""Mixin that limits the size of a `GraphicalEntity` to 500 x 400."""
def __init__(self, size_x: int, size_y: int) -> None:
super().__init__(min(size_x, 500), min(size_y, 400))
class LimitSizeButton(LimitSizeMixin, Button):
"""Limited size button.
Note: the position of the mixin is important as super follows the MRO. As it is,
the MRO of `LimitSizeButton` is `(LimitSizeButton, LimitSizeMixin, Button,
GraphicalEntity, object)`. When we instantiate it, the `__init__` method is
provided by `LimitSizeMixin`, which in turn calls through `super` the method
`__init__` of `Button`.
"""
def test_normal_button() -> None:
"""Button without mixin to limit its size."""
button = Button(1000, 500)
assert button.toggle()
assert not button.toggle()
assert (button.size_x, button.size_y) == (1000, 500)
def test_limit_button() -> None:
"""Button with mixin to limit its size."""
button = LimitSizeButton(1000, 500)
assert button.toggle()
assert not button.toggle()
assert (button.size_x, button.size_y) == (500, 400)
assert LimitSizeButton.__mro__ == (
LimitSizeButton,
LimitSizeMixin,
Button,
GraphicalEntity,
object,
)