示例#1
0
 def test_trampoline5(self):
     """calling tcl procs that use the 'args' special behavior"""
     tohil.eval(
         """proc arg_check_ab {a {b default_b} args} {
         return [list $a $b $args]
     }"""
     )
示例#2
0
 def setUp(self):
     tohil.eval(
         """proc ab_test {a {b b_default}} {return "a is '$a', b is '$b'"}"""
     )
     tohil.eval(
         """proc abc_test {a {b b_default} {c c_default}} {return "a is '$a', b is '$b', c is '$c'"}"""
     )
示例#3
0
    def test_with_callback(self):
        # Can't do this without a callback due to how unittest imports the tests.
        # Results in the global environment of this module not being visible from within Tcl.
        tohil.eval("""
        package require tohil
        proc isodd {num} {
            return [expr {![iseven $num]}]
        }
        """)

        def iseven(num):
            num = int(num)
            if num == 1:
                return False
            elif num == 0:
                return True
            else:
                return not tohil.call("isodd", num - 2, to=bool)

        tohil.register_callback("iseven", iseven)
        self.assertTrue(iseven(0))
        self.assertTrue(iseven(2))
        self.assertTrue(iseven(4))
        self.assertTrue(iseven(6))
        self.assertTrue(iseven(200))

        self.assertFalse(iseven(1))
        self.assertFalse(iseven(3))
        self.assertFalse(iseven(99))
示例#4
0
 def test_unset2(self):
     """unset of array element"""
     self.assertEqual(tohil.eval("info exists x(d)", to=int), 1)
     self.assertEqual(tohil.exists("x(d)"), True)
     tohil.unset("x(c)", "x(d)")
     self.assertEqual(tohil.eval("info exists x(d)", to=int), 0)
     self.assertEqual(tohil.exists("x(d)"), False)
示例#5
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)
示例#6
0
 def test_shadowdict7(self):
     """pop elements from shadow dict, with and without
     specified defaults"""
     tohil.eval("array set x [list a 1 b 2 c 3 d 4]")
     x = tohil.ShadowDict("x", to=int)
     self.assertEqual(x.pop('e', 5), 5)
     self.assertEqual(x.pop('d'), 4)
     self.assertEqual(x.pop('c', 4), 3)
示例#7
0
 def test_shadowdict6(self):
     """get element from shadow dict, with and without
     specified defaults"""
     tohil.eval("array set x [list a 1 b 2 c 3 d 4]")
     x = tohil.ShadowDict("x", to=int)
     self.assertEqual(x.get('d'), 4)
     self.assertEqual(x.get('d', 'defval'), 4)
     self.assertEqual(x.get('d', default='defval'), 4)
     self.assertEqual(x.get('nonesuch', to=str), '')
     self.assertEqual(x.get('nonesuch', 'defval', to=str), 'defval')
     self.assertEqual(x.get('nonesuch', 0), 0)
     self.assertEqual(x.get('nonesuch', default=16), 16)
示例#8
0
    def test_trampoline1(self):
        """create test proc and try different default values"""
        tohil.eval("""proc ab_test {a {b b_default}} {return "a is '$a', b is '$b'"}""")

        ab_test = tohil.TclProc("ab_test")

        self.assertEqual(ab_test("a_val"), "a is 'a_val', b is 'b_default'")
        self.assertEqual(ab_test("a_val", "b_val"), "a is 'a_val', b is 'b_val'")
        self.assertEqual(ab_test("a_val2", b="b_val2"), "a is 'a_val2', b is 'b_val2'")
        self.assertEqual(
            ab_test(b="b_val3", a="a_val3"), "a is 'a_val3', b is 'b_val3'"
        )
示例#9
0
 def test_tclobj19(self):
     """tohil.tclobj extend (lappend ... {*})"""
     x = tohil.eval("list 1 2 3 4 5 6", to=tohil.tclobj)
     y = tohil.eval("list 7 8 9", to=tohil.tclobj)
     x.append(y)
     self.assertEqual(repr(x), "<tohil.tclobj: '1 2 3 4 5 6 {7 8 9}'>")
     x = tohil.eval("list 1 2 3 4 5 6", to=tohil.tclobj)
     x.extend(y)
     self.assertEqual(repr(x), "<tohil.tclobj: '1 2 3 4 5 6 7 8 9'>")
     l = [10, 11, 12, 13]
     x.extend(l)
     self.assertEqual(repr(x),
                      "<tohil.tclobj: '1 2 3 4 5 6 7 8 9 10 11 12 13'>")
示例#10
0
 def test_trampoline5(self):
     """calling tcl procs that use the 'args' special behavior"""
     tohil.eval("""proc arg_check_ab {a {b default_b} args} {
         return [list $a $b $args]
     }""")
     arg_check_ab = tohil.TclProc("arg_check_ab")
     self.assertEqual(
         arg_check_ab("c_val4", b="b_val4", a="a_val4"),
         "a_val4 b_val4 c_val4",
     )
     self.assertEqual(
         arg_check_ab("c_val5", a="a_val5"),
         "a_val5 c_val5 {}",
     )
示例#11
0
 def test_tclobj10(self):
     """exercise tohil.tclobj subscripting, str(), and repr()"""
     x = tohil.eval("list 1 2 3 4 5", to=tohil.tclobj)
     self.assertEqual(str(x[2]), "3")
     self.assertEqual(repr(x[2]), "'3'")
     x.to = tohil.tclobj
     self.assertEqual(repr(x[2]), "<tohil.tclobj: '3'>")
示例#12
0
 def test_td2(self):
     """tohil.tcldict get and delete """
     x = tohil.eval("list a 1 b 2 c 3", to=tohil.tcldict)
     self.assertEqual(x["b"], "2")
     self.assertEqual(x.get("b", to=int), 2)
     del x["c"]
     self.assertEqual(str(x), 'a 1 b 2')
示例#13
0
 def test_subinterp1(self):
     """exercise loading tohil into a child interpreter, and make
     sure no child interpreters are left over"""
     interp = tohil.eval("interp create")
     tohil.call(interp, 'eval', 'package require tohil')
     self.assertEqual(tohil.call('interp', 'delete', interp), '')
     self.assertEqual(tohil.call('interp', 'slaves'), '')
示例#14
0
 def test_call3(self):
     """test with some arguments; compare to eval's output"""
     self.assertEqual(
         tohil.call("clock", "format", 1, "-gmt", 1, "-locale", "fr_FR"),
         "jeu. janv. 01 00:00:01 GMT 1970",
     )
     self.assertEqual(
         tohil.call("clock", "format", 1, "-gmt", 1, "-locale", "fr_FR"),
         tohil.eval("clock format 1 -gmt 1 -locale fr_FR"),
     )
示例#15
0
 def test_tclobj18(self):
     """tohil.tclobj comparing tclobjs with stuff"""
     x = tohil.eval("list 1 2 3 4 5 6 7", to=tohil.tclobj)
     self.assertTrue(x[2] == x[2])
     x.to = int
     self.assertTrue(x[2] == 3)
     x.to = str
     self.assertTrue(x[2] == "3")
     x.to = float
     self.assertTrue(x[2] < 4.0)
     self.assertFalse(x[2] > 4.0)
示例#16
0
 def test_eval4(self):
     """exercise tohil.eval and to=dict"""
     self.assertEqual(
         tohil.eval("list a 1 b 2 c 3 d 4", to=dict),
         {
             "a": "1",
             "b": "2",
             "c": "3",
             "d": "4"
         },
     )
示例#17
0
 def test_tclobj13(self):
     """tohil.tclobj slice stuff, 4 to the end"""
     x = tohil.eval("list 1 2 3 4 5 6 7", to=tohil.tclobj)
     self.assertEqual(
         x[4:],
         ['5', '6', '7'],
     )
     x.to = tohil.tclobj
     self.assertEqual(
         repr(x[4:]),
         "[<tohil.tclobj: '5'>, <tohil.tclobj: '6'>, <tohil.tclobj: '7'>]",
     )
示例#18
0
    def test_tclobj2(self):
        """exercise tohil.tclobj lindex method"""
        x = tohil.eval("list 1 2 3 4 5", to=tohil.tclobj)

        self.assertEqual(x.lindex(0), "1")
        self.assertEqual(len(x), 5)

        with self.assertRaises(IndexError):
            x.lindex(-1)

        with self.assertRaises(IndexError):
            x.lindex(6)
示例#19
0
 def test_tclobj14(self):
     """tohil.tclobj slice stuff, the beginning until 4"""
     x = tohil.eval("list 1 2 3 4 5 6 7", to=tohil.tclobj)
     self.assertEqual(
         x[:4],
         ['1', '2', '3', '4'],
     )
     x.to = tohil.tclobj
     self.assertEqual(
         repr(x[:4]),
         "[<tohil.tclobj: '1'>, <tohil.tclobj: '2'>, <tohil.tclobj: '3'>, <tohil.tclobj: '4'>]",
     )
示例#20
0
    def test_trampoline3(self):
        """different things with a 3-argument test proc"""
        tohil.eval(
            """proc abc_test {a {b b_default} {c c_default}} {return "a is '$a', b is '$b', c is '$c'"}"""
        )

        abc_test = tohil.TclProc("abc_test")

        self.assertEqual(
            abc_test("a_val"), "a is 'a_val', b is 'b_default', c is 'c_default'"
        )
        self.assertEqual(
            abc_test("a_val", "b_val"), "a is 'a_val', b is 'b_val', c is 'c_default'"
        )
        self.assertEqual(
            abc_test("a_val2", b="b_val2"),
            "a is 'a_val2', b is 'b_val2', c is 'c_default'",
        )
        self.assertEqual(
            abc_test(b="b_val3", a="a_val3"),
            "a is 'a_val3', b is 'b_val3', c is 'c_default'",
        )
        self.assertEqual(
            abc_test(b="b_val4", a="a_val4", c="c_val4"),
            "a is 'a_val4', b is 'b_val4', c is 'c_val4'",
        )
        self.assertEqual(
            abc_test(c="c_val5", a="a_val5"),
            "a is 'a_val5', b is 'b_default', c is 'c_val5'",
        )
        self.assertEqual(
            abc_test("a_val6", c="c_val6"),
            "a is 'a_val6', b is 'b_default', c is 'c_val6'",
        )
        self.assertEqual(
            abc_test(c="c_val8", a="a_val8"),
            "a is 'a_val8', b is 'b_default', c is 'c_val8'",
        )
示例#21
0
 def test_td1(self):
     """tohil.tcldict get """
     x = tohil.eval("list a 1 b 2 c 3", to=tohil.tcldict)
     self.assertEqual(x["a"], "1")
     x.to = int
     self.assertEqual(x["a"], 1)
     with self.assertRaises(KeyError):
         x.get("z")
     x.to = str
     self.assertEqual(x.get("z", default="bar"), "bar")
     self.assertEqual(x.get("z", default="bar", to=list), ["bar"])
     with self.assertRaises(ValueError):
         x.get("z", default="bar", to=int)
     self.assertEqual(x.get("z", default="1", to=int), 1)
示例#22
0
 def test_tclobj17(self):
     """tohil.tclobj slice stuff, the whole thing with a :"""
     x = tohil.eval("list 1 2 3 4 5 6 7", to=tohil.tclobj)
     self.assertEqual(
         x[:],
         ['1', '2', '3', '4', '5', '6', '7'],
     )
     x.to = float
     self.assertEqual(
         x[:],
         [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0],
     )
     x.to = tohil.tclobj
     self.assertEqual(
         repr(x[:]),
         "[<tohil.tclobj: '1'>, <tohil.tclobj: '2'>, <tohil.tclobj: '3'>, <tohil.tclobj: '4'>, <tohil.tclobj: '5'>, <tohil.tclobj: '6'>, <tohil.tclobj: '7'>]",
     )
示例#23
0
 def test_tclobj15(self):
     """tohil.tclobj slice stuff, from the beginning to 4 from the end"""
     x = tohil.eval("list 1 2 3 4 5 6 7", to=tohil.tclobj)
     self.assertEqual(
         x[:-4],
         ['1', '2', '3'],
     )
     x.to = int
     self.assertEqual(
         x[:-4],
         [1, 2, 3],
     )
     x.to = tohil.tclobj
     self.assertEqual(
         repr(x[:-4]),
         "[<tohil.tclobj: '1'>, <tohil.tclobj: '2'>, <tohil.tclobj: '3'>]",
     )
示例#24
0
    def test_tclvar2(self, x, y):
        """test tclvar tcobj shove stuff into tcl and see it from python and vice versa"""
        ns_name = "::tohil_test"
        xname = ns_name + "::varsync_x"
        yname = ns_name + "::varsync_y"

        tx = tohil.tclvar(xname)
        ty = tohil.tclvar(yname)

        tohil.eval(f"set {xname} {x}")
        tohil.eval(f"set {yname} {y}")

        assert tx == tx
        assert tx == x
        assert ty == ty

        tohil.incr(xname)
        assert tx == x + 1
        assert tx == tohil.expr(f"${xname}")
        tohil.eval(f"incr {xname} -1")
        assert tx == x
示例#25
0
 def test_eval10(self):
     """exercise tohil.eval and to=function"""
     with self.assertRaises(TypeError):
         tohil.eval("list 1 2 3", to=filter_minus_1)
示例#26
0
 def test_eval9(self):
     """exercise tohil.eval and an incorrect to=function"""
     with self.assertRaises(TypeError):
         tohil.eval("list 1 2 3", to=filter_too_many_args)
示例#27
0
 def test_eval8(self):
     """exercise tohil.eval and to=function"""
     self.assertEqual(tohil.eval("return 10", to=filter_minus_1), 9)
示例#28
0
 def test_eval7(self):
     """exercise tohil.eval and to=someting_wrong"""
     with self.assertRaises(NameError):
         tohil.eval("list 1 2 3", to=no_such_type)
示例#29
0
 def test_eval6(self):
     """exercise tohil.eval and to=set"""
     self.assertEqual(
         sorted(tohil.eval("list 1 2 3 4 5 6 6", to=set)),
         ["1", "2", "3", "4", "5", "6"],
     )
示例#30
0
 def test_eval5(self):
     """exercise tohil.eval and to=tuple"""
     self.assertEqual(
         tohil.eval("list a 1 b 2 c 3 d 4", to=tuple),
         ("a", "1", "b", "2", "c", "3", "d", "4"),
     )