Esempio n. 1
0
    def test_tclvar1(self, x, y):
        """test 'tclvar' tcolbj var sync into tcl and see it from python and vice versa"""
        ns_name = "::tohil_test"
        xname = ns_name + "::varsync_x"
        yname = ns_name + "::varsync_y"

        tohil.unset(xname, yname)

        tx = tohil.tclvar(xname, default=x)
        ty = tohil.tclvar(yname, default=y)

        # compare the tclvar to tcl via a few different approaches
        assert tx == x
        assert tx == tohil.getvar(xname, to=int)
        assert tx == tohil.eval(f"set {xname}", to=int)
        assert tx == tohil.eval(f"return ${xname}", to=int)
        assert tx == tohil.expr(f"${xname}", to=int)

        # mutate tx tclvar and make sure the variable is changing
        # on the tcl side
        tx += ty
        assert tx == tohil.getvar(xname, to=int)
        assert tx == x + y
        assert tx == tohil.getvar(xname, to=int)
        assert tx == x + tohil.getvar(yname, to=int)
        tx -= ty
        assert tx == x
        assert tx == tohil.getvar(xname, to=int)
Esempio n. 2
0
    def test_tclvar3(self, x):
        """interoperate python lists back and forth with tcl lists"""
        ns_name = "::tohil_test"
        list_name = ns_name + "::varsync_list"

        tohil.unset(list_name)
        tl = tohil.tclvar(list_name, default=x)
        assert(str(tl) == tohil.getvar(list_name))
        assert(list(tl) == tohil.getvar(list_name, to=list))
Esempio n. 3
0
 def test_tclobj8(self):
     """exercise tohil.tclobj setvar()"""
     x = tohil.eval("list 1 2 3 4 5", to=tohil.tclobj)
     x.setvar("foo")
     self.assertEqual(str(x), tohil.getvar("foo"))
Esempio n. 4
0
 def test_getvar1(self):
     """exercise setvar and getvar of a variable"""
     tohil.setvar("z", "frammistan")
     self.assertEqual(tohil.getvar("z"), "frammistan")
     self.assertEqual(tohil.eval("info exists z", to=int), 1)
     self.assertEqual(tohil.eval("return $z"), "frammistan")
Esempio n. 5
0
 def test_unset3(self):
     # make sure unset doesn't do anything if the var or element doesn't exist
     tohil.unset("x(d)")
     with self.assertRaises(NameError):
         tohil.getvar("x(d)")
Esempio n. 6
0
 def test_getvar2(self):
     """exercise getvar of an array element"""
     tohil.eval("array unset x; array set x [list a 1 b 2 c 3 d 4]")
     self.assertEqual(tohil.getvar("x(a)"), "1")
     self.assertEqual(tohil.getvar("x(c)", to=int), 3)
     self.assertEqual(tohil.getvar("x(d)", to=float), 4.0)