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