blob: eebdd255b82616eb66b7bd4e0f83114fbf7cdc61 [file] [log] [blame]
David Aschere2b4b322004-02-18 05:59:53 +00001#!/usr/bin/env python
2
3import unittest
4import os
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
R. David Murraya21e4ca2009-03-31 23:16:50 +00006
7# Skip this test if the _tkinter module wasn't built.
8_tkinter = support.import_module('_tkinter')
9
Guilherme Polofa8fba92009-02-07 02:33:47 +000010from tkinter import Tcl
David Aschere2b4b322004-02-18 05:59:53 +000011from _tkinter import TclError
12
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +000013
14class TkinterTest(unittest.TestCase):
15
16 def testFlattenLen(self):
17 # flatten(<object with no length>)
18 self.assertRaises(TypeError, _tkinter._flatten, True)
19
20
David Aschere2b4b322004-02-18 05:59:53 +000021class TclTest(unittest.TestCase):
22
23 def setUp(self):
24 self.interp = Tcl()
25
26 def testEval(self):
27 tcl = self.interp
28 tcl.eval('set a 1')
29 self.assertEqual(tcl.eval('set a'),'1')
30
31 def testEvalException(self):
32 tcl = self.interp
33 self.assertRaises(TclError,tcl.eval,'set a')
34
35 def testEvalException2(self):
36 tcl = self.interp
37 self.assertRaises(TclError,tcl.eval,'this is wrong')
38
39 def testCall(self):
40 tcl = self.interp
41 tcl.call('set','a','1')
42 self.assertEqual(tcl.call('set','a'),'1')
43
44 def testCallException(self):
45 tcl = self.interp
46 self.assertRaises(TclError,tcl.call,'set','a')
47
48 def testCallException2(self):
49 tcl = self.interp
50 self.assertRaises(TclError,tcl.call,'this','is','wrong')
51
52 def testSetVar(self):
53 tcl = self.interp
54 tcl.setvar('a','1')
55 self.assertEqual(tcl.eval('set a'),'1')
56
57 def testSetVarArray(self):
58 tcl = self.interp
59 tcl.setvar('a(1)','1')
60 self.assertEqual(tcl.eval('set a(1)'),'1')
61
62 def testGetVar(self):
63 tcl = self.interp
64 tcl.eval('set a 1')
65 self.assertEqual(tcl.getvar('a'),'1')
66
67 def testGetVarArray(self):
68 tcl = self.interp
69 tcl.eval('set a(1) 1')
70 self.assertEqual(tcl.getvar('a(1)'),'1')
71
72 def testGetVarException(self):
73 tcl = self.interp
74 self.assertRaises(TclError,tcl.getvar,'a')
75
76 def testGetVarArrayException(self):
77 tcl = self.interp
78 self.assertRaises(TclError,tcl.getvar,'a(1)')
79
80 def testUnsetVar(self):
81 tcl = self.interp
82 tcl.setvar('a',1)
83 self.assertEqual(tcl.eval('info exists a'),'1')
84 tcl.unsetvar('a')
85 self.assertEqual(tcl.eval('info exists a'),'0')
86
87 def testUnsetVarArray(self):
88 tcl = self.interp
89 tcl.setvar('a(1)',1)
90 tcl.setvar('a(2)',2)
91 self.assertEqual(tcl.eval('info exists a(1)'),'1')
92 self.assertEqual(tcl.eval('info exists a(2)'),'1')
93 tcl.unsetvar('a(1)')
94 self.assertEqual(tcl.eval('info exists a(1)'),'0')
95 self.assertEqual(tcl.eval('info exists a(2)'),'1')
96
97 def testUnsetVarException(self):
98 tcl = self.interp
99 self.assertRaises(TclError,tcl.unsetvar,'a')
Tim Peters27f88362004-07-08 04:22:35 +0000100
David Aschere2b4b322004-02-18 05:59:53 +0000101 def testEvalFile(self):
102 tcl = self.interp
103 filename = "testEvalFile.tcl"
104 fd = open(filename,'w')
105 script = """set a 1
106 set b 2
107 set c [ expr $a + $b ]
108 """
109 fd.write(script)
110 fd.close()
111 tcl.evalfile(filename)
Neal Norwitz9a8d55e2004-02-29 15:37:50 +0000112 os.remove(filename)
David Aschere2b4b322004-02-18 05:59:53 +0000113 self.assertEqual(tcl.eval('set a'),'1')
114 self.assertEqual(tcl.eval('set b'),'2')
115 self.assertEqual(tcl.eval('set c'),'3')
116
117 def testEvalFileException(self):
118 tcl = self.interp
119 filename = "doesnotexists"
120 try:
121 os.remove(filename)
Guido van Rossumb940e112007-01-10 16:19:56 +0000122 except Exception as e:
David Aschere2b4b322004-02-18 05:59:53 +0000123 pass
124 self.assertRaises(TclError,tcl.evalfile,filename)
125
David Aschere2b4b322004-02-18 05:59:53 +0000126 def testPackageRequireException(self):
127 tcl = self.interp
128 self.assertRaises(TclError,tcl.eval,'package require DNE')
129
Neal Norwitz63dfece2004-02-19 02:37:29 +0000130
131def test_main():
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000132 support.run_unittest(TclTest, TkinterTest)
Neal Norwitz63dfece2004-02-19 02:37:29 +0000133
David Aschere2b4b322004-02-18 05:59:53 +0000134if __name__ == "__main__":
Neal Norwitz63dfece2004-02-19 02:37:29 +0000135 test_main()