PyXR

c:\python24\lib \ test \ test_tcl.py



0001 #!/usr/bin/env python
0002 
0003 import unittest
0004 import os
0005 from test import test_support
0006 from Tkinter import Tcl
0007 from _tkinter import TclError
0008 
0009 class TclTest(unittest.TestCase):
0010 
0011     def setUp(self):
0012         self.interp = Tcl()
0013 
0014     def testEval(self):
0015         tcl = self.interp
0016         tcl.eval('set a 1')
0017         self.assertEqual(tcl.eval('set a'),'1')
0018 
0019     def testEvalException(self):
0020         tcl = self.interp
0021         self.assertRaises(TclError,tcl.eval,'set a')
0022 
0023     def testEvalException2(self):
0024         tcl = self.interp
0025         self.assertRaises(TclError,tcl.eval,'this is wrong')
0026 
0027     def testCall(self):
0028         tcl = self.interp
0029         tcl.call('set','a','1')
0030         self.assertEqual(tcl.call('set','a'),'1')
0031 
0032     def testCallException(self):
0033         tcl = self.interp
0034         self.assertRaises(TclError,tcl.call,'set','a')
0035 
0036     def testCallException2(self):
0037         tcl = self.interp
0038         self.assertRaises(TclError,tcl.call,'this','is','wrong')
0039 
0040     def testSetVar(self):
0041         tcl = self.interp
0042         tcl.setvar('a','1')
0043         self.assertEqual(tcl.eval('set a'),'1')
0044 
0045     def testSetVarArray(self):
0046         tcl = self.interp
0047         tcl.setvar('a(1)','1')
0048         self.assertEqual(tcl.eval('set a(1)'),'1')
0049 
0050     def testGetVar(self):
0051         tcl = self.interp
0052         tcl.eval('set a 1')
0053         self.assertEqual(tcl.getvar('a'),'1')
0054 
0055     def testGetVarArray(self):
0056         tcl = self.interp
0057         tcl.eval('set a(1) 1')
0058         self.assertEqual(tcl.getvar('a(1)'),'1')
0059 
0060     def testGetVarException(self):
0061         tcl = self.interp
0062         self.assertRaises(TclError,tcl.getvar,'a')
0063 
0064     def testGetVarArrayException(self):
0065         tcl = self.interp
0066         self.assertRaises(TclError,tcl.getvar,'a(1)')
0067 
0068     def testUnsetVar(self):
0069         tcl = self.interp
0070         tcl.setvar('a',1)
0071         self.assertEqual(tcl.eval('info exists a'),'1')
0072         tcl.unsetvar('a')
0073         self.assertEqual(tcl.eval('info exists a'),'0')
0074 
0075     def testUnsetVarArray(self):
0076         tcl = self.interp
0077         tcl.setvar('a(1)',1)
0078         tcl.setvar('a(2)',2)
0079         self.assertEqual(tcl.eval('info exists a(1)'),'1')
0080         self.assertEqual(tcl.eval('info exists a(2)'),'1')
0081         tcl.unsetvar('a(1)')
0082         self.assertEqual(tcl.eval('info exists a(1)'),'0')
0083         self.assertEqual(tcl.eval('info exists a(2)'),'1')
0084 
0085     def testUnsetVarException(self):
0086         tcl = self.interp
0087         self.assertRaises(TclError,tcl.unsetvar,'a')
0088 
0089     def testEvalFile(self):
0090         tcl = self.interp
0091         filename = "testEvalFile.tcl"
0092         fd = open(filename,'w')
0093         script = """set a 1
0094         set b 2
0095         set c [ expr $a + $b ]
0096         """
0097         fd.write(script)
0098         fd.close()
0099         tcl.evalfile(filename)
0100         os.remove(filename)
0101         self.assertEqual(tcl.eval('set a'),'1')
0102         self.assertEqual(tcl.eval('set b'),'2')
0103         self.assertEqual(tcl.eval('set c'),'3')
0104 
0105     def testEvalFileException(self):
0106         tcl = self.interp
0107         filename = "doesnotexists"
0108         try:
0109             os.remove(filename)
0110         except Exception,e:
0111             pass
0112         self.assertRaises(TclError,tcl.evalfile,filename)
0113 
0114     def testPackageRequireException(self):
0115         tcl = self.interp
0116         self.assertRaises(TclError,tcl.eval,'package require DNE')
0117 
0118     def testLoadTk(self):
0119         import os
0120         if 'DISPLAY' not in os.environ:
0121             # skipping test of clean upgradeability
0122             return
0123         tcl = Tcl()
0124         self.assertRaises(TclError,tcl.winfo_geometry)
0125         tcl.loadtk()
0126         self.assertEqual('1x1+0+0', tcl.winfo_geometry())
0127 
0128     def testLoadTkFailure(self):
0129         import os
0130         old_display = None
0131         import sys
0132         if sys.platform.startswith('win') or sys.platform.startswith('darwin'):
0133             return # no failure possible on windows?
0134         if 'DISPLAY' in os.environ:
0135             old_display = os.environ['DISPLAY']
0136             del os.environ['DISPLAY']
0137             # on some platforms, deleting environment variables
0138             # doesn't actually carry through to the process level
0139             # because they don't support unsetenv
0140             # If that's the case, abort.
0141             display = os.popen('echo $DISPLAY').read().strip()
0142             if display:
0143                 return
0144         try:
0145             tcl = Tcl()
0146             self.assertRaises(TclError, tcl.winfo_geometry)
0147             self.assertRaises(TclError, tcl.loadtk)
0148         finally:
0149             if old_display is not None:
0150                 os.environ['DISPLAY'] = old_display
0151 
0152 def test_main():
0153     test_support.run_unittest(TclTest)
0154 
0155 if __name__ == "__main__":
0156     test_main()
0157 

Generated by PyXR 0.9.4
SourceForge.net Logo