1 | n/a | import unittest |
---|
2 | n/a | import gc |
---|
3 | n/a | from tkinter import (Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl, |
---|
4 | n/a | TclError) |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class Var(Variable): |
---|
8 | n/a | |
---|
9 | n/a | _default = "default" |
---|
10 | n/a | side_effect = False |
---|
11 | n/a | |
---|
12 | n/a | def set(self, value): |
---|
13 | n/a | self.side_effect = True |
---|
14 | n/a | super().set(value) |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class TestBase(unittest.TestCase): |
---|
18 | n/a | |
---|
19 | n/a | def setUp(self): |
---|
20 | n/a | self.root = Tcl() |
---|
21 | n/a | |
---|
22 | n/a | def tearDown(self): |
---|
23 | n/a | del self.root |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | class TestVariable(TestBase): |
---|
27 | n/a | |
---|
28 | n/a | def info_exists(self, *args): |
---|
29 | n/a | return self.root.getboolean(self.root.call("info", "exists", *args)) |
---|
30 | n/a | |
---|
31 | n/a | def test_default(self): |
---|
32 | n/a | v = Variable(self.root) |
---|
33 | n/a | self.assertEqual("", v.get()) |
---|
34 | n/a | self.assertRegex(str(v), r"^PY_VAR(\d+)$") |
---|
35 | n/a | |
---|
36 | n/a | def test_name_and_value(self): |
---|
37 | n/a | v = Variable(self.root, "sample string", "varname") |
---|
38 | n/a | self.assertEqual("sample string", v.get()) |
---|
39 | n/a | self.assertEqual("varname", str(v)) |
---|
40 | n/a | |
---|
41 | n/a | def test___del__(self): |
---|
42 | n/a | self.assertFalse(self.info_exists("varname")) |
---|
43 | n/a | v = Variable(self.root, "sample string", "varname") |
---|
44 | n/a | self.assertTrue(self.info_exists("varname")) |
---|
45 | n/a | del v |
---|
46 | n/a | self.assertFalse(self.info_exists("varname")) |
---|
47 | n/a | |
---|
48 | n/a | def test_dont_unset_not_existing(self): |
---|
49 | n/a | self.assertFalse(self.info_exists("varname")) |
---|
50 | n/a | v1 = Variable(self.root, name="name") |
---|
51 | n/a | v2 = Variable(self.root, name="name") |
---|
52 | n/a | del v1 |
---|
53 | n/a | self.assertFalse(self.info_exists("name")) |
---|
54 | n/a | # shouldn't raise exception |
---|
55 | n/a | del v2 |
---|
56 | n/a | self.assertFalse(self.info_exists("name")) |
---|
57 | n/a | |
---|
58 | n/a | def test___eq__(self): |
---|
59 | n/a | # values doesn't matter, only class and name are checked |
---|
60 | n/a | v1 = Variable(self.root, name="abc") |
---|
61 | n/a | v2 = Variable(self.root, name="abc") |
---|
62 | n/a | self.assertEqual(v1, v2) |
---|
63 | n/a | |
---|
64 | n/a | v3 = Variable(self.root, name="abc") |
---|
65 | n/a | v4 = StringVar(self.root, name="abc") |
---|
66 | n/a | self.assertNotEqual(v3, v4) |
---|
67 | n/a | |
---|
68 | n/a | def test_invalid_name(self): |
---|
69 | n/a | with self.assertRaises(TypeError): |
---|
70 | n/a | Variable(self.root, name=123) |
---|
71 | n/a | |
---|
72 | n/a | def test_null_in_name(self): |
---|
73 | n/a | with self.assertRaises(ValueError): |
---|
74 | n/a | Variable(self.root, name='var\x00name') |
---|
75 | n/a | with self.assertRaises(ValueError): |
---|
76 | n/a | self.root.globalsetvar('var\x00name', "value") |
---|
77 | n/a | with self.assertRaises(ValueError): |
---|
78 | n/a | self.root.globalsetvar(b'var\x00name', "value") |
---|
79 | n/a | with self.assertRaises(ValueError): |
---|
80 | n/a | self.root.setvar('var\x00name', "value") |
---|
81 | n/a | with self.assertRaises(ValueError): |
---|
82 | n/a | self.root.setvar(b'var\x00name', "value") |
---|
83 | n/a | |
---|
84 | n/a | def test_initialize(self): |
---|
85 | n/a | v = Var(self.root) |
---|
86 | n/a | self.assertFalse(v.side_effect) |
---|
87 | n/a | v.set("value") |
---|
88 | n/a | self.assertTrue(v.side_effect) |
---|
89 | n/a | |
---|
90 | n/a | def test_trace_old(self): |
---|
91 | n/a | # Old interface |
---|
92 | n/a | v = Variable(self.root) |
---|
93 | n/a | vname = str(v) |
---|
94 | n/a | trace = [] |
---|
95 | n/a | def read_tracer(*args): |
---|
96 | n/a | trace.append(('read',) + args) |
---|
97 | n/a | def write_tracer(*args): |
---|
98 | n/a | trace.append(('write',) + args) |
---|
99 | n/a | cb1 = v.trace_variable('r', read_tracer) |
---|
100 | n/a | cb2 = v.trace_variable('wu', write_tracer) |
---|
101 | n/a | self.assertEqual(sorted(v.trace_vinfo()), [('r', cb1), ('wu', cb2)]) |
---|
102 | n/a | self.assertEqual(trace, []) |
---|
103 | n/a | |
---|
104 | n/a | v.set('spam') |
---|
105 | n/a | self.assertEqual(trace, [('write', vname, '', 'w')]) |
---|
106 | n/a | |
---|
107 | n/a | trace = [] |
---|
108 | n/a | v.get() |
---|
109 | n/a | self.assertEqual(trace, [('read', vname, '', 'r')]) |
---|
110 | n/a | |
---|
111 | n/a | trace = [] |
---|
112 | n/a | info = sorted(v.trace_vinfo()) |
---|
113 | n/a | v.trace_vdelete('w', cb1) # Wrong mode |
---|
114 | n/a | self.assertEqual(sorted(v.trace_vinfo()), info) |
---|
115 | n/a | with self.assertRaises(TclError): |
---|
116 | n/a | v.trace_vdelete('r', 'spam') # Wrong command name |
---|
117 | n/a | self.assertEqual(sorted(v.trace_vinfo()), info) |
---|
118 | n/a | v.trace_vdelete('r', (cb1, 43)) # Wrong arguments |
---|
119 | n/a | self.assertEqual(sorted(v.trace_vinfo()), info) |
---|
120 | n/a | v.get() |
---|
121 | n/a | self.assertEqual(trace, [('read', vname, '', 'r')]) |
---|
122 | n/a | |
---|
123 | n/a | trace = [] |
---|
124 | n/a | v.trace_vdelete('r', cb1) |
---|
125 | n/a | self.assertEqual(v.trace_vinfo(), [('wu', cb2)]) |
---|
126 | n/a | v.get() |
---|
127 | n/a | self.assertEqual(trace, []) |
---|
128 | n/a | |
---|
129 | n/a | trace = [] |
---|
130 | n/a | del write_tracer |
---|
131 | n/a | gc.collect() |
---|
132 | n/a | v.set('eggs') |
---|
133 | n/a | self.assertEqual(trace, [('write', vname, '', 'w')]) |
---|
134 | n/a | |
---|
135 | n/a | trace = [] |
---|
136 | n/a | del v |
---|
137 | n/a | gc.collect() |
---|
138 | n/a | self.assertEqual(trace, [('write', vname, '', 'u')]) |
---|
139 | n/a | |
---|
140 | n/a | def test_trace(self): |
---|
141 | n/a | v = Variable(self.root) |
---|
142 | n/a | vname = str(v) |
---|
143 | n/a | trace = [] |
---|
144 | n/a | def read_tracer(*args): |
---|
145 | n/a | trace.append(('read',) + args) |
---|
146 | n/a | def write_tracer(*args): |
---|
147 | n/a | trace.append(('write',) + args) |
---|
148 | n/a | tr1 = v.trace_add('read', read_tracer) |
---|
149 | n/a | tr2 = v.trace_add(['write', 'unset'], write_tracer) |
---|
150 | n/a | self.assertEqual(sorted(v.trace_info()), [ |
---|
151 | n/a | (('read',), tr1), |
---|
152 | n/a | (('write', 'unset'), tr2)]) |
---|
153 | n/a | self.assertEqual(trace, []) |
---|
154 | n/a | |
---|
155 | n/a | v.set('spam') |
---|
156 | n/a | self.assertEqual(trace, [('write', vname, '', 'write')]) |
---|
157 | n/a | |
---|
158 | n/a | trace = [] |
---|
159 | n/a | v.get() |
---|
160 | n/a | self.assertEqual(trace, [('read', vname, '', 'read')]) |
---|
161 | n/a | |
---|
162 | n/a | trace = [] |
---|
163 | n/a | info = sorted(v.trace_info()) |
---|
164 | n/a | v.trace_remove('write', tr1) # Wrong mode |
---|
165 | n/a | self.assertEqual(sorted(v.trace_info()), info) |
---|
166 | n/a | with self.assertRaises(TclError): |
---|
167 | n/a | v.trace_remove('read', 'spam') # Wrong command name |
---|
168 | n/a | self.assertEqual(sorted(v.trace_info()), info) |
---|
169 | n/a | v.get() |
---|
170 | n/a | self.assertEqual(trace, [('read', vname, '', 'read')]) |
---|
171 | n/a | |
---|
172 | n/a | trace = [] |
---|
173 | n/a | v.trace_remove('read', tr1) |
---|
174 | n/a | self.assertEqual(v.trace_info(), [(('write', 'unset'), tr2)]) |
---|
175 | n/a | v.get() |
---|
176 | n/a | self.assertEqual(trace, []) |
---|
177 | n/a | |
---|
178 | n/a | trace = [] |
---|
179 | n/a | del write_tracer |
---|
180 | n/a | gc.collect() |
---|
181 | n/a | v.set('eggs') |
---|
182 | n/a | self.assertEqual(trace, [('write', vname, '', 'write')]) |
---|
183 | n/a | |
---|
184 | n/a | trace = [] |
---|
185 | n/a | del v |
---|
186 | n/a | gc.collect() |
---|
187 | n/a | self.assertEqual(trace, [('write', vname, '', 'unset')]) |
---|
188 | n/a | |
---|
189 | n/a | |
---|
190 | n/a | class TestStringVar(TestBase): |
---|
191 | n/a | |
---|
192 | n/a | def test_default(self): |
---|
193 | n/a | v = StringVar(self.root) |
---|
194 | n/a | self.assertEqual("", v.get()) |
---|
195 | n/a | |
---|
196 | n/a | def test_get(self): |
---|
197 | n/a | v = StringVar(self.root, "abc", "name") |
---|
198 | n/a | self.assertEqual("abc", v.get()) |
---|
199 | n/a | self.root.globalsetvar("name", "value") |
---|
200 | n/a | self.assertEqual("value", v.get()) |
---|
201 | n/a | |
---|
202 | n/a | def test_get_null(self): |
---|
203 | n/a | v = StringVar(self.root, "abc\x00def", "name") |
---|
204 | n/a | self.assertEqual("abc\x00def", v.get()) |
---|
205 | n/a | self.root.globalsetvar("name", "val\x00ue") |
---|
206 | n/a | self.assertEqual("val\x00ue", v.get()) |
---|
207 | n/a | |
---|
208 | n/a | |
---|
209 | n/a | class TestIntVar(TestBase): |
---|
210 | n/a | |
---|
211 | n/a | def test_default(self): |
---|
212 | n/a | v = IntVar(self.root) |
---|
213 | n/a | self.assertEqual(0, v.get()) |
---|
214 | n/a | |
---|
215 | n/a | def test_get(self): |
---|
216 | n/a | v = IntVar(self.root, 123, "name") |
---|
217 | n/a | self.assertEqual(123, v.get()) |
---|
218 | n/a | self.root.globalsetvar("name", "345") |
---|
219 | n/a | self.assertEqual(345, v.get()) |
---|
220 | n/a | self.root.globalsetvar("name", "876.5") |
---|
221 | n/a | self.assertEqual(876, v.get()) |
---|
222 | n/a | |
---|
223 | n/a | def test_invalid_value(self): |
---|
224 | n/a | v = IntVar(self.root, name="name") |
---|
225 | n/a | self.root.globalsetvar("name", "value") |
---|
226 | n/a | with self.assertRaises((ValueError, TclError)): |
---|
227 | n/a | v.get() |
---|
228 | n/a | |
---|
229 | n/a | |
---|
230 | n/a | class TestDoubleVar(TestBase): |
---|
231 | n/a | |
---|
232 | n/a | def test_default(self): |
---|
233 | n/a | v = DoubleVar(self.root) |
---|
234 | n/a | self.assertEqual(0.0, v.get()) |
---|
235 | n/a | |
---|
236 | n/a | def test_get(self): |
---|
237 | n/a | v = DoubleVar(self.root, 1.23, "name") |
---|
238 | n/a | self.assertAlmostEqual(1.23, v.get()) |
---|
239 | n/a | self.root.globalsetvar("name", "3.45") |
---|
240 | n/a | self.assertAlmostEqual(3.45, v.get()) |
---|
241 | n/a | |
---|
242 | n/a | def test_get_from_int(self): |
---|
243 | n/a | v = DoubleVar(self.root, 1.23, "name") |
---|
244 | n/a | self.assertAlmostEqual(1.23, v.get()) |
---|
245 | n/a | self.root.globalsetvar("name", "3.45") |
---|
246 | n/a | self.assertAlmostEqual(3.45, v.get()) |
---|
247 | n/a | self.root.globalsetvar("name", "456") |
---|
248 | n/a | self.assertAlmostEqual(456, v.get()) |
---|
249 | n/a | |
---|
250 | n/a | def test_invalid_value(self): |
---|
251 | n/a | v = DoubleVar(self.root, name="name") |
---|
252 | n/a | self.root.globalsetvar("name", "value") |
---|
253 | n/a | with self.assertRaises((ValueError, TclError)): |
---|
254 | n/a | v.get() |
---|
255 | n/a | |
---|
256 | n/a | |
---|
257 | n/a | class TestBooleanVar(TestBase): |
---|
258 | n/a | |
---|
259 | n/a | def test_default(self): |
---|
260 | n/a | v = BooleanVar(self.root) |
---|
261 | n/a | self.assertIs(v.get(), False) |
---|
262 | n/a | |
---|
263 | n/a | def test_get(self): |
---|
264 | n/a | v = BooleanVar(self.root, True, "name") |
---|
265 | n/a | self.assertIs(v.get(), True) |
---|
266 | n/a | self.root.globalsetvar("name", "0") |
---|
267 | n/a | self.assertIs(v.get(), False) |
---|
268 | n/a | self.root.globalsetvar("name", 42 if self.root.wantobjects() else 1) |
---|
269 | n/a | self.assertIs(v.get(), True) |
---|
270 | n/a | self.root.globalsetvar("name", 0) |
---|
271 | n/a | self.assertIs(v.get(), False) |
---|
272 | n/a | self.root.globalsetvar("name", "on") |
---|
273 | n/a | self.assertIs(v.get(), True) |
---|
274 | n/a | |
---|
275 | n/a | def test_set(self): |
---|
276 | n/a | true = 1 if self.root.wantobjects() else "1" |
---|
277 | n/a | false = 0 if self.root.wantobjects() else "0" |
---|
278 | n/a | v = BooleanVar(self.root, name="name") |
---|
279 | n/a | v.set(True) |
---|
280 | n/a | self.assertEqual(self.root.globalgetvar("name"), true) |
---|
281 | n/a | v.set("0") |
---|
282 | n/a | self.assertEqual(self.root.globalgetvar("name"), false) |
---|
283 | n/a | v.set(42) |
---|
284 | n/a | self.assertEqual(self.root.globalgetvar("name"), true) |
---|
285 | n/a | v.set(0) |
---|
286 | n/a | self.assertEqual(self.root.globalgetvar("name"), false) |
---|
287 | n/a | v.set("on") |
---|
288 | n/a | self.assertEqual(self.root.globalgetvar("name"), true) |
---|
289 | n/a | |
---|
290 | n/a | def test_invalid_value_domain(self): |
---|
291 | n/a | false = 0 if self.root.wantobjects() else "0" |
---|
292 | n/a | v = BooleanVar(self.root, name="name") |
---|
293 | n/a | with self.assertRaises(TclError): |
---|
294 | n/a | v.set("value") |
---|
295 | n/a | self.assertEqual(self.root.globalgetvar("name"), false) |
---|
296 | n/a | self.root.globalsetvar("name", "value") |
---|
297 | n/a | with self.assertRaises(ValueError): |
---|
298 | n/a | v.get() |
---|
299 | n/a | self.root.globalsetvar("name", "1.0") |
---|
300 | n/a | with self.assertRaises(ValueError): |
---|
301 | n/a | v.get() |
---|
302 | n/a | |
---|
303 | n/a | |
---|
304 | n/a | tests_gui = (TestVariable, TestStringVar, TestIntVar, |
---|
305 | n/a | TestDoubleVar, TestBooleanVar) |
---|
306 | n/a | |
---|
307 | n/a | |
---|
308 | n/a | if __name__ == "__main__": |
---|
309 | n/a | from test.support import run_unittest |
---|
310 | n/a | run_unittest(*tests_gui) |
---|