PageRenderTime 121ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/tkinter/test/test_tkinter/test_variables.py

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