David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
| 4 | import os |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 6 | |
| 7 | # Skip this test if the _tkinter module wasn't built. |
| 8 | _tkinter = support.import_module('_tkinter') |
| 9 | |
Guilherme Polo | fa8fba9 | 2009-02-07 02:33:47 +0000 | [diff] [blame] | 10 | from tkinter import Tcl |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 11 | from _tkinter import TclError |
| 12 | |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame] | 13 | |
| 14 | class TkinterTest(unittest.TestCase): |
| 15 | |
| 16 | def testFlattenLen(self): |
| 17 | # flatten(<object with no length>) |
| 18 | self.assertRaises(TypeError, _tkinter._flatten, True) |
| 19 | |
| 20 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 21 | class 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') |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 100 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 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) |
Neal Norwitz | 9a8d55e | 2004-02-29 15:37:50 +0000 | [diff] [blame] | 112 | os.remove(filename) |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 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) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 122 | except Exception as e: |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 123 | pass |
| 124 | self.assertRaises(TclError,tcl.evalfile,filename) |
| 125 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 126 | def testPackageRequireException(self): |
| 127 | tcl = self.interp |
| 128 | self.assertRaises(TclError,tcl.eval,'package require DNE') |
| 129 | |
Neal Norwitz | 63dfece | 2004-02-19 02:37:29 +0000 | [diff] [blame] | 130 | |
| 131 | def test_main(): |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame] | 132 | support.run_unittest(TclTest, TkinterTest) |
Neal Norwitz | 63dfece | 2004-02-19 02:37:29 +0000 | [diff] [blame] | 133 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 134 | if __name__ == "__main__": |
Neal Norwitz | 63dfece | 2004-02-19 02:37:29 +0000 | [diff] [blame] | 135 | test_main() |