Salome HOME
Yacs decorator
[modules/yacs.git] / src / py2yacs / Test / testDeco.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2020  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import unittest
22 import tempfile
23 import os
24 import subprocess
25
26 import SALOMERuntime
27 import loader
28 import pilot
29
30 dir_test = tempfile.mkdtemp(suffix=".yacstest")
31
32 class TestDeco(unittest.TestCase):
33
34     def setUp(self):
35       SALOMERuntime.RuntimeSALOME_setRuntime()
36
37     def test_t1(self):
38       """
39       Schema:
40       jdd -> foreach -> post
41            > f2
42          /
43       f1 -> f3 -> f1
44       """
45       import testforeach
46       expected_1, expected_2 = testforeach.main()
47       yacs_schema_file = os.path.join(dir_test, "schema_t1.xml")
48       yacs_build_command = "yacsbuild.py"
49       test_script = "testforeach.py"
50       main_function_name = "main"
51       subprocess.run([yacs_build_command,
52                       test_script, main_function_name, yacs_schema_file])
53       l = loader.YACSLoader()
54       ex = pilot.ExecutorSwig()
55       proc = l.load(yacs_schema_file)
56       ex.RunW(proc,0)
57       obtained_1 = proc.getChildByName("post_0").getOutputPort("s").getPyObj()
58       obtained_2 = proc.getChildByName("f1_1").getOutputPort("r").getPyObj()
59       self.assertEqual(expected_1, obtained_1)
60       self.assertEqual(expected_2, obtained_2)
61
62     def test_t2(self):
63       """
64       Foreach initialized by value.
65       """
66       import testforeach
67       expected_1, expected_2 = testforeach.mainbloc()
68       yacs_schema_file = os.path.join(dir_test, "schema_t2.xml")
69       yacs_build_command = "yacsbuild.py"
70       test_script = "testforeach.py"
71       main_function_name = "mainbloc"
72       subprocess.run([yacs_build_command,
73                       test_script, main_function_name, yacs_schema_file])
74       l = loader.YACSLoader()
75       ex = pilot.ExecutorSwig()
76       proc = l.load(yacs_schema_file)
77       ex.RunW(proc,0)
78       obtained_1 = proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj()
79       obtained_2 = proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj()
80       self.assertEqual(expected_1, obtained_1)
81       self.assertEqual(expected_2, obtained_2)
82
83     def test_t3(self):
84       """
85       Foreach on 2 levels.
86       """
87       import testforeach
88       expected = testforeach.maindoublefr()
89       yacs_schema_file = os.path.join(dir_test, "schema_t3.xml")
90       yacs_build_command = "yacsbuild.py"
91       test_script = "testforeach.py"
92       main_function_name = "maindoublefr"
93       subprocess.run([yacs_build_command,
94                       test_script, main_function_name, yacs_schema_file])
95       l = loader.YACSLoader()
96       ex = pilot.ExecutorSwig()
97       proc = l.load(yacs_schema_file)
98       ex.RunW(proc,0)
99       obtained = proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj()
100       self.assertEqual(expected, obtained)
101
102 if __name__ == '__main__':
103   file_test = os.path.join(dir_test,"UnitTestsResult")
104   with open(file_test, 'a') as f:
105       f.write("  --- TEST src/py2yacs: testDeco.py\n")
106       suite = unittest.makeSuite(TestDeco)
107       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
108   sys.exit(not result.wasSuccessful())