PageRenderTime 570ms CodeModel.GetById 152ms app.highlight 211ms RepoModel.GetById 204ms app.codeStats 1ms

/Lib/test/test_tcl.py

http://unladen-swallow.googlecode.com/
Python | 157 lines | 137 code | 19 blank | 1 comment | 0 complexity | 370ce29cf1bdc15094ac32ba044754d3 MD5 | raw file
  1#!/usr/bin/env python
  2
  3import unittest
  4import os
  5from test import test_support
  6from 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        os.remove(filename)
101        self.assertEqual(tcl.eval('set a'),'1')
102        self.assertEqual(tcl.eval('set b'),'2')
103        self.assertEqual(tcl.eval('set c'),'3')
104
105    def testEvalFileException(self):
106        tcl = self.interp
107        filename = "doesnotexists"
108        try:
109            os.remove(filename)
110        except Exception,e:
111            pass
112        self.assertRaises(TclError,tcl.evalfile,filename)
113
114    def testPackageRequireException(self):
115        tcl = self.interp
116        self.assertRaises(TclError,tcl.eval,'package require DNE')
117
118    def testLoadTk(self):
119        import os
120        if 'DISPLAY' not in os.environ:
121            # skipping test of clean upgradeability
122            return
123        tcl = Tcl()
124        self.assertRaises(TclError,tcl.winfo_geometry)
125        tcl.loadtk()
126        self.assertEqual('1x1+0+0', tcl.winfo_geometry())
127        tcl.destroy()
128
129    def testLoadTkFailure(self):
130        import os
131        old_display = None
132        import sys
133        if sys.platform.startswith(('win', 'darwin', 'cygwin')):
134            return  # no failure possible on windows?
135        if 'DISPLAY' in os.environ:
136            old_display = os.environ['DISPLAY']
137            del os.environ['DISPLAY']
138            # on some platforms, deleting environment variables
139            # doesn't actually carry through to the process level
140            # because they don't support unsetenv
141            # If that's the case, abort.
142            display = os.popen('echo $DISPLAY').read().strip()
143            if display:
144                return
145        try:
146            tcl = Tcl()
147            self.assertRaises(TclError, tcl.winfo_geometry)
148            self.assertRaises(TclError, tcl.loadtk)
149        finally:
150            if old_display is not None:
151                os.environ['DISPLAY'] = old_display
152
153def test_main():
154    test_support.run_unittest(TclTest)
155
156if __name__ == "__main__":
157    test_main()