David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
| 4 | import os |
Neal Norwitz | 63dfece | 2004-02-19 02:37:29 +0000 | [diff] [blame] | 5 | from test import test_support |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 6 | from Tkinter import Tcl |
| 7 | from _tkinter import TclError |
| 8 | |
| 9 | class 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') |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 88 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 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) |
Neal Norwitz | 9a8d55e | 2004-02-29 15:37:50 +0000 | [diff] [blame] | 100 | os.remove(filename) |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 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 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 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 | |
| 128 | def testLoadTkFailure(self): |
| 129 | import os |
| 130 | old_display = None |
| 131 | import sys |
Brett Cannon | 3684c87 | 2004-11-24 03:01:36 +0000 | [diff] [blame] | 132 | if (sys.platform.startswith('win') or |
| 133 | sys.platform.startswith('darwin') or |
| 134 | sys.platform.startswith('cygwin')): |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 135 | return # no failure possible on windows? |
| 136 | if 'DISPLAY' in os.environ: |
| 137 | old_display = os.environ['DISPLAY'] |
| 138 | del os.environ['DISPLAY'] |
| 139 | # on some platforms, deleting environment variables |
| 140 | # doesn't actually carry through to the process level |
| 141 | # because they don't support unsetenv |
| 142 | # If that's the case, abort. |
| 143 | display = os.popen('echo $DISPLAY').read().strip() |
| 144 | if display: |
| 145 | return |
| 146 | try: |
| 147 | tcl = Tcl() |
| 148 | self.assertRaises(TclError, tcl.winfo_geometry) |
| 149 | self.assertRaises(TclError, tcl.loadtk) |
| 150 | finally: |
| 151 | if old_display is not None: |
| 152 | os.environ['DISPLAY'] = old_display |
Neal Norwitz | 63dfece | 2004-02-19 02:37:29 +0000 | [diff] [blame] | 153 | |
| 154 | def test_main(): |
| 155 | test_support.run_unittest(TclTest) |
| 156 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 157 | if __name__ == "__main__": |
Neal Norwitz | 63dfece | 2004-02-19 02:37:29 +0000 | [diff] [blame] | 158 | test_main() |