1#!/usr/bin/env python 2 3import unittest 4import os 5from test import test_support 6 7# Skip this test if the _tkinter module wasn't built. 8_tkinter = test_support.import_module('_tkinter') 9 10from Tkinter import Tcl 11from _tkinter import TclError 12 13 14class TkinterTest(unittest.TestCase): 15 16 def testFlattenLen(self): 17 # flatten(<object with no length>) 18 self.assertRaises(TypeError, _tkinter._flatten, True) 19 20 21class 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') 100 101 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) 112 os.remove(filename) 113 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) 122 except Exception,e: 123 pass 124 self.assertRaises(TclError,tcl.evalfile,filename) 125 126 def testPackageRequireException(self): 127 tcl = self.interp 128 self.assertRaises(TclError,tcl.eval,'package require DNE') 129 130 def testLoadWithUNC(self): 131 import sys 132 if sys.platform != 'win32': 133 return 134 135 # Build a UNC path from the regular path. 136 # Something like 137 # \\%COMPUTERNAME%\c$\python27\python.exe 138 139 fullname = os.path.abspath(sys.executable) 140 if fullname[1] != ':': 141 return 142 unc_name = r'\\%s\%s$\%s' % (os.environ['COMPUTERNAME'], 143 fullname[0], 144 fullname[3:]) 145 146 with test_support.EnvironmentVarGuard() as env: 147 env.unset("TCL_LIBRARY") 148 f = os.popen('%s -c "import Tkinter; print Tkinter"' % (unc_name,)) 149 150 self.assertTrue('Tkinter.py' in f.read()) 151 # exit code must be zero 152 self.assertEqual(f.close(), None) 153 154 155 156def test_main(): 157 test_support.run_unittest(TclTest, TkinterTest) 158 159if __name__ == "__main__": 160 test_main() 161