Salome HOME
dc414231c2f8a422c318942d8d397d46d6bc5496
[modules/yacs.git] / src / yacsloader / Test / testEdit.py
1 #  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 #
3 #  This library is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU Lesser General Public
5 #  License as published by the Free Software Foundation; either
6 #  version 2.1 of the License.
7 #
8 #  This library is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 #  Lesser General Public License for more details.
12 #
13 #  You should have received a copy of the GNU Lesser General Public
14 #  License along with this library; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 import sys
20 import pilot
21 import SALOMERuntime
22 import loader
23 import unittest
24
25 class TestEdit(unittest.TestCase):
26
27     def setUp(self):
28         SALOMERuntime.RuntimeSALOME_setRuntime()
29         self.r = pilot.getRuntime()
30         self.l = loader.YACSLoader()
31         self.e = pilot.ExecutorSwig()
32         pass
33
34     def test1_edit(self):
35         p = self.r.createProc("pr")
36         print p.typeMap
37         t=p.getTypeCode("double")
38         print t.kind()
39         t=p.typeMap["double"]
40         
41         td=p.createType("double","double")
42         ti=p.createType("int","int")
43         tc1=p.createInterfaceTc("","Obj",[])
44         print tc1.name(),tc1.id()
45         tc2=p.createInterfaceTc("","Obj2",[tc1])
46         print tc2.name(),tc2.id()
47         tc3=p.createSequenceTc("","seqdbl",td)
48         print tc3.name(),tc3.id(),tc3.contentType()
49         tc4=p.createSequenceTc("","seqObj2",tc2)
50         tc5=p.createSequenceTc("","seqint",ti)
51         print tc4.name(),tc4.id()
52         print tc4.isA(tc1),0
53         print tc2.isA(tc1),1
54         print tc1.isA(tc2),0
55         print td.isA(ti),0
56         print td.isAdaptable(ti),1
57         print ti.isAdaptable(td),0
58         print tc5.isAdaptable(tc3),0
59         print tc3.isAdaptable(tc5),1
60         
61         n=self.r.createScriptNode("","node1")
62         n.setScript("print 'coucou1'")
63         n.edAddInputPort("p1",ti)
64         n.edAddOutputPort("p1",ti)
65         p.edAddChild(n)
66         inport=n.getInputPort("p1");
67         retex=None
68         try:
69             inport.edInitXML("<value><intt>5</int></value>")
70         except ValueError, ex:
71             print "Value Error: ", ex
72             retex=ex
73         except pilot.Exception,ex:
74             print "YACS exception:",ex.what()
75             retex=ex.what()
76         self.assert_(retex is not None, "exception not raised, or wrong type")
77         inport.edInitXML("<value><int>5</int></value>")
78
79         # --- create script node node2
80         n2=self.r.createScriptNode("","node2")
81         n2.setScript("print 'coucou2'")
82         n2.edAddInputPort("p1",ti)
83         p.edAddChild(n2)
84         # --- end of node
85
86         # --- control link between nodes n and n2
87         p.edAddCFLink(n,n2)
88         # --- end control link
89
90         # --- datalink between ports p1 of nodes n1 and n2
91         p.edAddLink(n.getOutputPort("p1"),n2.getInputPort("p1"))
92         # --- end datalink
93
94         n=self.r.createFuncNode("","node3")
95         n.setScript("""
96         def f():
97         print 'coucou3'
98         """)
99         n.setFname("f")
100         p.edAddChild(n)
101
102         n4=self.r.createRefNode("","node4")
103         n4.setRef("corbaname:rir:#test.my_context/Echo.Object")
104         n4.setMethod("echoDouble")
105         n4.edAddInputDataStreamPort("pin",ti)
106         n4.edAddOutputDataStreamPort("pout",ti)
107         p.edAddChild(n4)
108         
109         n5=self.r.createRefNode("","node5")
110         n5.setRef("corbaname:rir:#test.my_context/Echo.Object")
111         n5.setMethod("echoDouble")
112         n5.edAddInputDataStreamPort("pin",ti)
113         n5.edAddOutputDataStreamPort("pout",ti)
114         p.edAddChild(n5)
115
116         p.edAddLink(n4.getOutputDataStreamPort("pout"),n5.getInputDataStreamPort("pin"))
117
118         #n=self.r.createCompoNode("","node5")
119         #n.setRef("PYHELLO")
120         #n.setMethod("makeBanner")
121         #p.edAddChild(n)
122
123         # --- create a bloc with one node
124         b=self.r.createBloc("b1")
125         p.edAddChild(b)
126         
127         n=self.r.createScriptNode("","b1.node2")
128         n.setScript("print 'coucou2'")
129         b.edAddChild(n)
130         # --- end bloc
131
132         # --- create a for loop with one node
133         lo=self.r.createForLoop("l1")
134         p.edAddChild(lo)
135         ip=lo.edGetNbOfTimesInputPort()
136         ip.edInitInt(3)
137
138         n=self.r.createScriptNode("","l1.node2")
139         n.setScript("print 'coucou2'")
140         lo.edSetNode(n)
141         # --- end loop
142
143         # --- control link between bloc b1 and loop l1
144         p.edAddCFLink(b,lo)
145         # --- end control link
146
147         # --- create a while loop with one node
148         wh=self.r.createWhileLoop("w1")
149         p.edAddChild(wh)
150         n=self.r.createFuncNode("","w1.node3")
151         n.setScript("""
152 def f():
153   print 'coucou3'
154   return 0
155 """)
156         n.setFname("f")
157         n.edAddOutputPort("p1",ti)
158         wh.edSetNode(n)
159         cport=wh.edGetConditionPort()
160         cport.edInitBool(True)
161         # --- end loop
162         p.edAddLink(n.getOutputPort("p1"),wh.getInputPort("condition")) #or cport
163
164         # --- create a switch 
165         sw=self.r.createSwitch("sw1")
166         p.edAddChild(sw)
167         n=self.r.createFuncNode("","sw1.node3")
168         n.setScript("""
169 def f():
170   print 'case1'
171   return 0
172 """)
173         n.setFname("f")
174         n.edAddOutputPort("p1",ti)
175         sw.edSetNode(1,n)
176         n=self.r.createFuncNode("","sw1.node4")
177         n.setScript("""
178 def f():
179   print 'default'
180   return 0
181 """)
182         n.setFname("f")
183         n.edAddOutputPort("p1",ti)
184         sw.edSetDefaultNode(n)
185         sw.edGetConditionPort().edInitInt(1)
186         # --- end switch
187
188         try:
189           self.e.RunW(p,0)
190         except pilot.Exception,ex:
191           print ex.what()
192           self.fail(ex)
193         
194         #self.e.displayDot(p)
195
196
197 import os
198 U = os.getenv('USER')
199 f=open("/tmp/" + U + "/UnitTestsResult", 'a')
200 f.write("  --- TEST src/yacsloader: testEdit.py\n")
201 suite = unittest.makeSuite(TestEdit)
202 unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
203 f.close()