File: test_funcgenr.py

package info (click to toggle)
python-clips 1.0.7.348%2Bclips-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,376 kB
  • sloc: ansic: 17,065; python: 5,668; sh: 20; makefile: 12
file content (163 lines) | stat: -rw-r--r-- 5,540 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# test_funcgenr.py


"""revision: $Id: test_funcgenr.py 215 2004-11-26 16:10:56Z Franz $
TESTS:
BuildFunction
RegisterPythonFunction
UnregisterPythonFunction
BuildGeneric
FindFunction
FindGeneric

Function:
  Name
  Module
  Deletable
  Watch

Generic:
  AddMethod
  RemoveMethod
  MethodList
  Module
  Watch
  MethodWatched
  WatchMethod
  UnwatchMethod
  MethodDeletable

"""



class ctc_Function(ctestcase):
    """test Function objects"""

    def ctf_Function_01(self):
        """Testing: BuildFunction, Function.Name, Function.Module"""
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            f1 = e.BuildFunction("tf", "?a ?b", "(str-cat ?a ?b)")
            self.assertEqual(f1.Module, "MAIN")
            self.assertEqual(e.Eval('(%s "s1" "s2")' % f1.Name), "s1s2")

    def ctf_Function_02(self):
        """Testing: RegisterPythonFunction, UnregisterPythonFunction"""
        def tfunc(a, b):
            return a + b
        clips.RegisterPythonFunction(tfunc)
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            self.assertEqual(e.Eval("(python-call tfunc 13 29)"), 42)
            self.assertEqual(e.Eval('(python-call tfunc "4" "2")'), "42")
        clips.UnregisterPythonFunction(tfunc)

    def ctf_Function_03(self):
        """Testing: FindFunction, Function.Deletable, Function.Watch, {...}"""
        """         Function.PPForm"""
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            f1 = e.BuildFunction("tf", "?a ?b", "(str-cat ?a ?b)")
            f2 = e.BuildFunction("tfd", "?a ?b", "(tf ?a ?b)")
            self.assert_(f2.Deletable)
            self.assert_(not f1.Deletable)
            self.assert_(f1.Watch)
            self.assert_(f2.Watch)
            self.assertEqual(e.FindFunction("tfd").PPForm(), f2.PPForm())


class ctc_Generic(ctestcase):
    """test Generic objects"""

    def ctf_Generic_01(self):
        """Testing: BuildGeneric, Generic.AddMethod, Generic.Name, Generic.Watch"""
        def mulstr(s, n):
            return clips.String(s * n)
        clips.RegisterPythonFunction(mulstr)
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            g1 = e.BuildGeneric("g1")
            self.assertEqual(g1.Name, "g1")
            self.assert_(g1.Watch)
            g1.AddMethod(
                [('?p1', clips.ClipsStringType), ('?p2', clips.ClipsStringType)],
                '(str-cat ?p1 ?p2)')
            g1.AddMethod(
                ['?p1 STRING', '?p2 INTEGER'], "(python-call mulstr ?p1 ?p2)")
            g1.AddMethod("(?p1 NUMBER)(?p2 NUMBER)", "(+ ?p1 ?p2)")
            self.assertEqual(e.Eval("(g1 13 29)"), clips.Integer(42))
            self.assertEqual(e.Eval("(g1 13.0 29.0)"), clips.Float(42.0))
            self.assertEqual(e.Eval('(g1 "sp" "am")'), "spam")
            self.assertEqual(len(e.Eval('(g1 "spam" 42)')) / len("spam"), 42)
            g1.RemoveMethod(0)
            self.assertEqual(len(g1.MethodList()), 0)

    def ctf_Generic_02(self):
        """Testing: Generic.MethodList, Generic.RemoveMethod, Generic.Module"""
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            g1 = e.BuildGeneric("g1")
            self.assertEqual(g1.Module, "MAIN")
            g1.AddMethod('(?p1 STRING)(?p2 STRING)', "(str-cat ?p1 ?p2)")
            g1.AddMethod("(?p1 NUMBER)(?p2 NUMBER)", "(+ ?p1 ?p2)")
            self.assertEqual(len(g1.MethodList()), 2)
            for i in g1.MethodList():
                g1.RemoveMethod(int(i))
            self.assertEqual(len(g1.MethodList()), 0)

    def ctf_Generic_03(self):
        """Testing: FindGeneric, Generic.Deletable, Generic.PPForm"""
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            g1 = e.BuildGeneric("g1")
            g1.AddMethod("(?p1 NUMBER)(?p2 NUMBER)", "(+ ?p1 ?p2)")
            self.assert_(g1.Deletable)
            f1 = e.BuildFunction("f1", "?a ?b", "(g1 ?a ?b)")
            self.assert_(not g1.Deletable)
            self.assertEqual(e.FindGeneric("g1").PPForm(), g1.PPForm())

    def ctf_Generic_04(self):
        """Testing: Generic.MethodWatched, Generic.WatchMethod, {...}"""
        """         Generic.UnwatchMethod, Generic.MethodDeletable"""
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            g1 = e.BuildGeneric("g1")
            g1.AddMethod("(?p1 NUMBER)(?p2 NUMBER)", "(+ ?p1 ?p2)")
            self.assert_(g1.MethodDeletable(1))
            g1.UnwatchMethod(1)
            self.assert_(not g1.MethodWatched(1))
            g1.WatchMethod(1)
            self.assert_(g1.MethodWatched(1))

    def ctf_Generic_05(self):
        """Testing: Generic.MethodRestrictions, Generic.MethodDescription"""
        for x in self.envdict.keys():
            e = self.envdict[x]
            e.Clear()
            e.Reset()
            g1 = e.BuildGeneric("g1")
            g1.AddMethod("(?p1 NUMBER)(?p2 NUMBER)", "(+ ?p1 ?p2)")
            self.assertEqual(
                e.Eval("(get-method-restrictions g1 1)"),
                g1.MethodRestrictions(1))
            mdesc = g1.MethodDescription(1).split(None, 1)[1]
            self.assertEqual(mdesc, "(NUMBER) (NUMBER)")



# end.