ยปCore Development>Code coverage>Lib/tkinter/test/test_tkinter/test_variables.py

Python code coverage for Lib/tkinter/test/test_tkinter/test_variables.py

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