-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_callback_event_data.py
More file actions
160 lines (123 loc) · 3.89 KB
/
test_callback_event_data.py
File metadata and controls
160 lines (123 loc) · 3.89 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import sys
import types
class WxStub(types.ModuleType):
def __getattr__(self, name):
value = type(name, (), {})
setattr(self, name, value)
return value
def install_wx_stub():
wx = WxStub("wx")
wx.__path__ = []
for name in (
"Window",
"EvtHandler",
"Event",
"PyEventBinder",
"StaticText",
"Gauge",
"Control",
"ControlWithItems",
"TextCtrl",
"Panel",
"Slider",
"ListCtrl",
"TopLevelWindow",
"Dialog",
"Frame",
"MDIParentFrame",
"MDIChildFrame",
"Notebook",
"MenuBar",
"Menu",
"MenuItem",
):
setattr(wx, name, type(name, (), {}))
for index, name in enumerate(
(
"ID_OK",
"ID_APPLY",
"ID_CANCEL",
"ID_CLOSE",
"ID_FIND",
"ID_YES",
"ID_NO",
"ID_ANY",
),
start=1,
):
setattr(wx, name, index)
wx_lib = types.ModuleType("wx.lib")
intctrl = types.ModuleType("wx.lib.intctrl")
intctrl.IntCtrl = type("IntCtrl", (), {})
sized_controls = types.ModuleType("wx.lib.sized_controls")
sized_controls.SizedDialog = type("SizedDialog", (), {})
sized_controls.SizedPanel = type("SizedPanel", (), {})
sized_controls.SizedFrame = type("SizedFrame", (), {})
calendar = types.ModuleType("wx.lib.calendar")
adv = WxStub("wx.adv")
adv.__path__ = []
wx.adv = adv
sys.modules.setdefault("wx", wx)
sys.modules.setdefault("wx.lib", wx_lib)
sys.modules.setdefault("wx.lib.intctrl", intctrl)
sys.modules.setdefault("wx.lib.sized_controls", sized_controls)
sys.modules.setdefault("wx.lib.calendar", calendar)
sys.modules.setdefault("wx.adv", adv)
try:
import wx # noqa: F401
except ModuleNotFoundError:
install_wx_stub()
from gui_builder.widgets.wx_widgets import callback_wrapper
class LazyEvent:
def __init__(self):
self.calls = []
self.skipped = False
self.stopped = False
def GetSelection(self):
self.calls.append("GetSelection")
return 7
def GetString(self):
self.calls.append("GetString")
raise AssertionError("unrequested getter was called")
def Skip(self):
self.skipped = True
def StopPropagation(self):
self.stopped = True
class WidgetStub:
def find_event_target(self, callback):
raise ValueError("callback is not attached to this widget")
def test_callback_wrapper_only_reads_requested_defaulted_event_fields():
event = LazyEvent()
received = {}
def callback(selection=None):
received["selection"] = selection
callback_wrapper(WidgetStub(), callback)(event)
assert received == {"selection": 7}
assert event.calls == ["GetSelection"]
assert event.skipped is True
assert event.stopped is False
def test_callback_wrapper_event_argument_does_not_read_event_getters():
event = LazyEvent()
received = {}
def callback(event=None):
received["event"] = event
callback_wrapper(WidgetStub(), callback)(event)
assert received == {"event": event}
assert event.calls == []
def test_callback_wrapper_named_event_with_kwargs_does_not_read_event_getters():
event = LazyEvent()
received = {}
def callback(event=None, **kwargs):
received["event"] = event
received["kwargs"] = kwargs
callback_wrapper(WidgetStub(), callback)(event)
assert received == {"event": event, "kwargs": {}}
assert event.calls == []
def test_callback_wrapper_only_reads_requested_keyword_only_event_fields():
event = LazyEvent()
received = {}
def callback(*, selection=None):
received["selection"] = selection
callback_wrapper(WidgetStub(), callback)(event)
assert received == {"selection": 7}
assert event.calls == ["GetSelection"]