blob: 6cab2e85e1af9bd576e7318054e51356015e4d53 [file] [log] [blame]
David Aschere2b4b322004-02-18 05:59:53 +00001#!/usr/bin/env python
2
3import unittest
4import os
Neal Norwitz63dfece2004-02-19 02:37:29 +00005from test import test_support
David Aschere2b4b322004-02-18 05:59:53 +00006from Tkinter import Tcl
7from _tkinter import TclError
8
9class TclTest(unittest.TestCase):
10
11 def setUp(self):
12 self.interp = Tcl()
13
14 def testEval(self):
15 tcl = self.interp
16 tcl.eval('set a 1')
17 self.assertEqual(tcl.eval('set a'),'1')
18
19 def testEvalException(self):
20 tcl = self.interp
21 self.assertRaises(TclError,tcl.eval,'set a')
22
23 def testEvalException2(self):
24 tcl = self.interp
25 self.assertRaises(TclError,tcl.eval,'this is wrong')
26
27 def testCall(self):
28 tcl = self.interp
29 tcl.call('set','a','1')
30 self.assertEqual(tcl.call('set','a'),'1')
31
32 def testCallException(self):
33 tcl = self.interp
34 self.assertRaises(TclError,tcl.call,'set','a')
35
36 def testCallException2(self):
37 tcl = self.interp
38 self.assertRaises(TclError,tcl.call,'this','is','wrong')
39
40 def testSetVar(self):
41 tcl = self.interp
42 tcl.setvar('a','1')
43 self.assertEqual(tcl.eval('set a'),'1')
44
45 def testSetVarArray(self):
46 tcl = self.interp
47 tcl.setvar('a(1)','1')
48 self.assertEqual(tcl.eval('set a(1)'),'1')
49
50 def testGetVar(self):
51 tcl = self.interp
52 tcl.eval('set a 1')
53 self.assertEqual(tcl.getvar('a'),'1')
54
55 def testGetVarArray(self):
56 tcl = self.interp
57 tcl.eval('set a(1) 1')
58 self.assertEqual(tcl.getvar('a(1)'),'1')
59
60 def testGetVarException(self):
61 tcl = self.interp
62 self.assertRaises(TclError,tcl.getvar,'a')
63
64 def testGetVarArrayException(self):
65 tcl = self.interp
66 self.assertRaises(TclError,tcl.getvar,'a(1)')
67
68 def testUnsetVar(self):
69 tcl = self.interp
70 tcl.setvar('a',1)
71 self.assertEqual(tcl.eval('info exists a'),'1')
72 tcl.unsetvar('a')
73 self.assertEqual(tcl.eval('info exists a'),'0')
74
75 def testUnsetVarArray(self):
76 tcl = self.interp
77 tcl.setvar('a(1)',1)
78 tcl.setvar('a(2)',2)
79 self.assertEqual(tcl.eval('info exists a(1)'),'1')
80 self.assertEqual(tcl.eval('info exists a(2)'),'1')
81 tcl.unsetvar('a(1)')
82 self.assertEqual(tcl.eval('info exists a(1)'),'0')
83 self.assertEqual(tcl.eval('info exists a(2)'),'1')
84
85 def testUnsetVarException(self):
86 tcl = self.interp
87 self.assertRaises(TclError,tcl.unsetvar,'a')
88
89 def testEvalFile(self):
90 tcl = self.interp
91 filename = "testEvalFile.tcl"
92 fd = open(filename,'w')
93 script = """set a 1
94 set b 2
95 set c [ expr $a + $b ]
96 """
97 fd.write(script)
98 fd.close()
99 tcl.evalfile(filename)
100 self.assertEqual(tcl.eval('set a'),'1')
101 self.assertEqual(tcl.eval('set b'),'2')
102 self.assertEqual(tcl.eval('set c'),'3')
103
104 def testEvalFileException(self):
105 tcl = self.interp
106 filename = "doesnotexists"
107 try:
108 os.remove(filename)
109 except Exception,e:
110 pass
111 self.assertRaises(TclError,tcl.evalfile,filename)
112
113 def testPackageRequire(self):
114 tcl = self.interp
115 tcl.eval('package require Tclx')
116 tcl.eval('keylset a b.c 1')
117 self.assertEqual(tcl.eval('keylget a b.c'),'1')
118
119 def testPackageRequireException(self):
120 tcl = self.interp
121 self.assertRaises(TclError,tcl.eval,'package require DNE')
122
123 def testLoadTk(self):
124 import os
125 if 'DISPLAY' not in os.environ:
126 # skipping test of clean upgradeability
127 return
128 tcl = Tcl()
129 self.assertRaises(TclError,tcl.winfo_geometry)
130 tcl.loadtk()
131 self.assertEqual('1x1+0+0', tcl.winfo_geometry())
132
133 def testLoadTkFailure(self):
134 import os
135 old_display = None
136 import sys
137 if sys.platform.startswith('win'):
138 return # no failure possible on windows?
139 if 'DISPLAY' in os.environ:
140 old_display = os.environ['DISPLAY']
141 del os.environ['DISPLAY']
142 # on some platforms, deleting environment variables
143 # doesn't actually carry through to the process level
144 # because they don't support unsetenv
145 # If that's the case, abort.
146 display = os.popen('echo $DISPLAY').read().strip()
147 if display:
148 return
149 try:
150 tcl = Tcl()
151 self.assertRaises(TclError, tcl.winfo_geometry)
152 self.assertRaises(TclError, tcl.loadtk)
153 finally:
154 if old_display is not None:
155 os.environ['DISPLAY'] = old_display
Neal Norwitz63dfece2004-02-19 02:37:29 +0000156
157def test_main():
158 test_support.run_unittest(TclTest)
159
David Aschere2b4b322004-02-18 05:59:53 +0000160if __name__ == "__main__":
Neal Norwitz63dfece2004-02-19 02:37:29 +0000161 test_main()