2004-02-18 01:59:53 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
2004-02-18 22:37:29 -04:00
|
|
|
from test import test_support
|
2009-03-31 15:32:17 -03:00
|
|
|
|
|
|
|
# Skip this test if the _tkinter module wasn't built.
|
|
|
|
_tkinter = test_support.import_module('_tkinter')
|
|
|
|
|
2009-02-06 22:20:29 -04:00
|
|
|
from Tkinter import Tcl
|
2004-02-18 01:59:53 -04:00
|
|
|
from _tkinter import TclError
|
|
|
|
|
2009-01-29 22:24:39 -04:00
|
|
|
|
|
|
|
class TkinterTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def testFlattenLen(self):
|
|
|
|
# flatten(<object with no length>)
|
|
|
|
self.assertRaises(TypeError, _tkinter._flatten, True)
|
|
|
|
|
|
|
|
|
2004-02-18 01:59:53 -04:00
|
|
|
class TclTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.interp = Tcl()
|
|
|
|
|
|
|
|
def testEval(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.eval('set a 1')
|
|
|
|
self.assertEqual(tcl.eval('set a'),'1')
|
|
|
|
|
|
|
|
def testEvalException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.eval,'set a')
|
|
|
|
|
|
|
|
def testEvalException2(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.eval,'this is wrong')
|
|
|
|
|
|
|
|
def testCall(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.call('set','a','1')
|
|
|
|
self.assertEqual(tcl.call('set','a'),'1')
|
|
|
|
|
|
|
|
def testCallException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.call,'set','a')
|
|
|
|
|
|
|
|
def testCallException2(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.call,'this','is','wrong')
|
|
|
|
|
|
|
|
def testSetVar(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a','1')
|
|
|
|
self.assertEqual(tcl.eval('set a'),'1')
|
|
|
|
|
|
|
|
def testSetVarArray(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a(1)','1')
|
|
|
|
self.assertEqual(tcl.eval('set a(1)'),'1')
|
|
|
|
|
|
|
|
def testGetVar(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.eval('set a 1')
|
|
|
|
self.assertEqual(tcl.getvar('a'),'1')
|
|
|
|
|
|
|
|
def testGetVarArray(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.eval('set a(1) 1')
|
|
|
|
self.assertEqual(tcl.getvar('a(1)'),'1')
|
|
|
|
|
|
|
|
def testGetVarException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.getvar,'a')
|
|
|
|
|
|
|
|
def testGetVarArrayException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.getvar,'a(1)')
|
|
|
|
|
|
|
|
def testUnsetVar(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a',1)
|
|
|
|
self.assertEqual(tcl.eval('info exists a'),'1')
|
|
|
|
tcl.unsetvar('a')
|
|
|
|
self.assertEqual(tcl.eval('info exists a'),'0')
|
|
|
|
|
|
|
|
def testUnsetVarArray(self):
|
|
|
|
tcl = self.interp
|
|
|
|
tcl.setvar('a(1)',1)
|
|
|
|
tcl.setvar('a(2)',2)
|
|
|
|
self.assertEqual(tcl.eval('info exists a(1)'),'1')
|
|
|
|
self.assertEqual(tcl.eval('info exists a(2)'),'1')
|
|
|
|
tcl.unsetvar('a(1)')
|
|
|
|
self.assertEqual(tcl.eval('info exists a(1)'),'0')
|
|
|
|
self.assertEqual(tcl.eval('info exists a(2)'),'1')
|
|
|
|
|
|
|
|
def testUnsetVarException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.unsetvar,'a')
|
2004-07-08 01:22:35 -03:00
|
|
|
|
2004-02-18 01:59:53 -04:00
|
|
|
def testEvalFile(self):
|
|
|
|
tcl = self.interp
|
|
|
|
filename = "testEvalFile.tcl"
|
|
|
|
fd = open(filename,'w')
|
|
|
|
script = """set a 1
|
|
|
|
set b 2
|
|
|
|
set c [ expr $a + $b ]
|
|
|
|
"""
|
|
|
|
fd.write(script)
|
|
|
|
fd.close()
|
|
|
|
tcl.evalfile(filename)
|
2004-02-29 11:37:50 -04:00
|
|
|
os.remove(filename)
|
2004-02-18 01:59:53 -04:00
|
|
|
self.assertEqual(tcl.eval('set a'),'1')
|
|
|
|
self.assertEqual(tcl.eval('set b'),'2')
|
|
|
|
self.assertEqual(tcl.eval('set c'),'3')
|
|
|
|
|
|
|
|
def testEvalFileException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
filename = "doesnotexists"
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except Exception,e:
|
|
|
|
pass
|
|
|
|
self.assertRaises(TclError,tcl.evalfile,filename)
|
|
|
|
|
|
|
|
def testPackageRequireException(self):
|
|
|
|
tcl = self.interp
|
|
|
|
self.assertRaises(TclError,tcl.eval,'package require DNE')
|
|
|
|
|
2010-06-04 16:39:07 -03:00
|
|
|
def testLoadWithUNC(self):
|
|
|
|
import sys
|
|
|
|
if sys.platform != 'win32':
|
|
|
|
return
|
|
|
|
|
|
|
|
# Build a UNC path from the regular path.
|
|
|
|
# Something like
|
|
|
|
# \\%COMPUTERNAME%\c$\python27\python.exe
|
|
|
|
|
|
|
|
fullname = os.path.abspath(sys.executable)
|
|
|
|
if fullname[1] != ':':
|
|
|
|
return
|
|
|
|
unc_name = r'\\%s\%s$\%s' % (os.environ['COMPUTERNAME'],
|
|
|
|
fullname[0],
|
|
|
|
fullname[3:])
|
|
|
|
|
|
|
|
with test_support.EnvironmentVarGuard() as env:
|
|
|
|
env.unset("TCL_LIBRARY")
|
|
|
|
f = os.popen('%s -c "import Tkinter; print Tkinter"' % (unc_name,))
|
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertTrue('Tkinter.py' in f.read())
|
2010-06-04 16:39:07 -03:00
|
|
|
# exit code must be zero
|
|
|
|
self.assertEqual(f.close(), None)
|
|
|
|
|
|
|
|
|
2004-02-18 01:59:53 -04:00
|
|
|
|
2004-02-18 22:37:29 -04:00
|
|
|
def test_main():
|
2009-01-29 22:24:39 -04:00
|
|
|
test_support.run_unittest(TclTest, TkinterTest)
|
2004-02-18 01:59:53 -04:00
|
|
|
|
2004-02-18 22:37:29 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|