Coverage for skema/program_analysis/tests/test_fun_default.py: 100%
229 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
1# import json NOTE: json and Path aren't used right now,
2# from pathlib import Path but will be used in the future
3from skema.program_analysis.multi_file_ingester import process_file_system
4from skema.gromet.fn import (
5 GrometFNModuleCollection,
6 FunctionType,
7 ImportType,
8 TypedValue
9)
10import ast
12from skema.program_analysis.CAST.pythonAST import py_ast_to_cast
13from skema.program_analysis.CAST2FN.model.cast import SourceRef
14from skema.program_analysis.CAST2FN import cast
15from skema.program_analysis.CAST2FN.cast import CAST
16from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline
18def fun_default1():
19 return """
20def foo(x=1, y=2):
21 return x + y
23p = foo()
24q = foo(y=5)
25r = foo(4)
26s = foo(3, 4)
27"""
29def fun_default2():
30 return """
31def foo(x=1, y=2):
32 return x + y
34a = foo(10, y=20)
35b = foo(y=2, x=1)
36"""
38def generate_gromet(test_file_string):
39 # use ast.Parse to get Python AST
40 contents = ast.parse(test_file_string)
42 # use Python to CAST
43 line_count = len(test_file_string.split("\n"))
44 convert = py_ast_to_cast.PyASTToCAST("temp")
45 C = convert.visit(contents, {}, {})
46 C.source_refs = [SourceRef("temp", None, None, 1, line_count)]
47 out_cast = cast.CAST([C], "python")
49 # use AnnCastPipeline to create GroMEt
50 gromet = ann_cast_pipeline(out_cast, gromet=True, to_file=False, from_obj=True)
52 return gromet
54def test_fun_default1():
55 exp_gromet = generate_gromet(fun_default1())
56 base_fn = exp_gromet.fn
58 assert len(base_fn.b) == 1
59 assert base_fn.b[0].function_type == FunctionType.MODULE
61 assert len(base_fn.bf) == 8
62 assert base_fn.bf[0].function_type == FunctionType.IMPORTED_METHOD and base_fn.bf[0].import_type == ImportType.OTHER
63 assert base_fn.bf[0].body == 1 and base_fn.bf[0].name == "foo_id0"
64 assert base_fn.bf[1].function_type == FunctionType.IMPORTED_METHOD and base_fn.bf[1].import_type == ImportType.OTHER
65 assert base_fn.bf[1].body == 1 and base_fn.bf[1].name == "foo_id0"
66 assert base_fn.bf[2].function_type == FunctionType.EXPRESSION and base_fn.bf[2].body == 2
67 assert base_fn.bf[3].function_type == FunctionType.IMPORTED_METHOD and base_fn.bf[3].import_type == ImportType.OTHER
68 assert base_fn.bf[3].body == 1 and base_fn.bf[3].name == "foo_id0"
70 assert base_fn.bf[4].function_type == FunctionType.LITERAL and base_fn.bf[4].value.value == 4
71 assert base_fn.bf[4].value.value_type == "Integer"
72 assert base_fn.bf[5].function_type == FunctionType.IMPORTED_METHOD and base_fn.bf[5].import_type == ImportType.OTHER
73 assert base_fn.bf[5].body == 1 and base_fn.bf[5].name == "foo_id0"
74 assert base_fn.bf[6].function_type == FunctionType.LITERAL and base_fn.bf[6].value.value == 3
75 assert base_fn.bf[6].value.value_type == "Integer"
76 assert base_fn.bf[7].function_type == FunctionType.LITERAL and base_fn.bf[7].value.value == 4
77 assert base_fn.bf[7].value.value_type == "Integer"
79 assert len(base_fn.pif) == 8
80 assert base_fn.pif[0].box == 1
81 assert base_fn.pif[1].box == 1
82 assert base_fn.pif[2].box == 2
83 assert base_fn.pif[3].box == 2
84 assert base_fn.pif[4].box == 4
85 assert base_fn.pif[5].box == 4
86 assert base_fn.pif[6].box == 6
87 assert base_fn.pif[7].box == 6
89 assert len(base_fn.pof) == 8
90 assert base_fn.pof[0].box == 1
91 assert base_fn.pof[0].name == "p"
93 assert base_fn.pof[1].box == 3
94 assert base_fn.pof[1].name == "y"
96 assert base_fn.pof[2].box == 2
97 assert base_fn.pof[2].name == "q"
99 assert base_fn.pof[3].box == 5
101 assert base_fn.pof[4].box == 4
102 assert base_fn.pof[4].name == "r"
104 assert base_fn.pof[5].box == 7
105 assert base_fn.pof[6].box == 8
107 assert base_fn.pof[7].box == 6
108 assert base_fn.pof[7].name == "s"
110 assert len(base_fn.wff) == 4
111 assert base_fn.wff[0].src == 4
112 assert base_fn.wff[0].tgt == 2
114 assert base_fn.wff[1].src == 5
115 assert base_fn.wff[1].tgt == 4
117 assert base_fn.wff[2].src == 7
118 assert base_fn.wff[2].tgt == 6
120 assert base_fn.wff[3].src == 8
121 assert base_fn.wff[3].tgt == 7
124 ###############################
125 func_fn = exp_gromet.fn_array[0]
126 assert len(func_fn.b) == 1
127 assert func_fn.b[0].function_type == FunctionType.FUNCTION
128 assert func_fn.b[0].name == "foo_id0"
130 assert len(func_fn.opi) == 2
131 assert func_fn.opi[0].box == 1
132 assert func_fn.opi[0].name == "x"
133 assert func_fn.opi[0].default_value == 1
135 assert func_fn.opi[1].box == 1
136 assert func_fn.opi[1].name == "y"
137 assert func_fn.opi[1].default_value == 2
139 assert len(func_fn.opo) == 1
140 assert func_fn.opo[0].box == 1
142 assert len(func_fn.bf) == 1
143 assert func_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE
144 assert func_fn.bf[0].name == "ast.Add"
146 assert len(func_fn.pif) == 2
147 assert func_fn.pif[0].box == 1
148 assert func_fn.pif[1].box == 1
150 assert len(func_fn.pof) == 1
151 assert func_fn.pof[0].box == 1
153 assert len(func_fn.wfopi) == 2
154 assert func_fn.wfopi[0].src == 1
155 assert func_fn.wfopi[0].tgt == 1
156 assert func_fn.wfopi[1].src == 2
157 assert func_fn.wfopi[1].tgt == 2
159 assert len(func_fn.wfopo) == 1
160 assert func_fn.wfopo[0].src == 1
161 assert func_fn.wfopo[0].tgt == 1
163 ###############################
164 assign_fn = exp_gromet.fn_array[1]
165 assert len(assign_fn.opo) == 1
166 assert assign_fn.opo[0].box == 1
168 assert len(assign_fn.b) == 1
169 assert assign_fn.b[0].function_type == FunctionType.EXPRESSION
171 assert len(assign_fn.bf) == 1
172 assert assign_fn.bf[0].function_type == FunctionType.LITERAL
173 assert assign_fn.bf[0].value.value_type == "Integer"
174 assert assign_fn.bf[0].value.value == 5
176 assert len(assign_fn.pof) == 1
177 assert assign_fn.pof[0].box == 1
179 assert len(assign_fn.wfopo) == 1
180 assert assign_fn.wfopo[0].src == 1
181 assert assign_fn.wfopo[0].tgt == 1
183def test_fun_default2():
184 exp_gromet = generate_gromet(fun_default2())
185 base_fn = exp_gromet.fn
187 assert len(base_fn.b) == 1
188 assert base_fn.b[0].function_type == FunctionType.MODULE
190 assert len(base_fn.bf) == 6
191 assert base_fn.bf[0].function_type == FunctionType.IMPORTED_METHOD and base_fn.bf[0].import_type == ImportType.OTHER
192 assert base_fn.bf[0].body == 1 and base_fn.bf[0].name == "foo_id0"
193 assert base_fn.bf[1].function_type == FunctionType.LITERAL and base_fn.bf[1].value.value == 10
194 assert base_fn.bf[1].value.value_type == "Integer"
195 assert base_fn.bf[2].function_type == FunctionType.EXPRESSION and base_fn.bf[2].body == 2
196 assert base_fn.bf[3].function_type == FunctionType.IMPORTED_METHOD and base_fn.bf[3].import_type == ImportType.OTHER
197 assert base_fn.bf[3].body == 1 and base_fn.bf[3].name == "foo_id0"
198 assert base_fn.bf[4].function_type == FunctionType.EXPRESSION and base_fn.bf[4].body == 3
199 assert base_fn.bf[5].function_type == FunctionType.EXPRESSION and base_fn.bf[5].body == 4
201 assert len(base_fn.pif) == 4
202 assert base_fn.pif[0].box == 1
203 assert base_fn.pif[1].box == 1
204 assert base_fn.pif[2].box == 4
205 assert base_fn.pif[3].box == 4
207 assert len(base_fn.pof) == 6
208 assert base_fn.pof[0].box == 2
210 assert base_fn.pof[1].box == 3
211 assert base_fn.pof[1].name == "y"
213 assert base_fn.pof[2].box == 1
214 assert base_fn.pof[2].name == "a"
216 assert base_fn.pof[3].box == 5
217 assert base_fn.pof[3].name == "y"
219 assert base_fn.pof[4].box == 6
220 assert base_fn.pof[4].name == "x"
222 assert base_fn.pof[5].box == 4
223 assert base_fn.pof[5].name == "b"
225 assert len(base_fn.wff) == 4
226 assert base_fn.wff[0].src == 1
227 assert base_fn.wff[0].tgt == 1
229 assert base_fn.wff[1].src == 2
230 assert base_fn.wff[1].tgt == 2
232 assert base_fn.wff[2].src == 4
233 assert base_fn.wff[2].tgt == 4
235 assert base_fn.wff[3].src == 3
236 assert base_fn.wff[3].tgt == 5
238 ###############################
239 func_fn = exp_gromet.fn_array[0]
240 assert len(func_fn.b) == 1
241 assert func_fn.b[0].function_type == FunctionType.FUNCTION
242 assert func_fn.b[0].name == "foo_id0"
244 assert len(func_fn.opi) == 2
245 assert func_fn.opi[0].box == 1
246 assert func_fn.opi[0].name == "x"
247 assert func_fn.opi[0].default_value == 1
249 assert func_fn.opi[1].box == 1
250 assert func_fn.opi[1].name == "y"
251 assert func_fn.opi[1].default_value == 2
253 assert len(func_fn.opo) == 1
254 assert func_fn.opo[0].box == 1
256 assert len(func_fn.bf) == 1
257 assert func_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE
258 assert func_fn.bf[0].name == "ast.Add"
260 assert len(func_fn.pif) == 2
261 assert func_fn.pif[0].box == 1
262 assert func_fn.pif[1].box == 1
264 assert len(func_fn.pof) == 1
265 assert func_fn.pof[0].box == 1
267 assert len(func_fn.wfopi) == 2
268 assert func_fn.wfopi[0].src == 1
269 assert func_fn.wfopi[0].tgt == 1
270 assert func_fn.wfopi[1].src == 2
271 assert func_fn.wfopi[1].tgt == 2
273 assert len(func_fn.wfopo) == 1
274 assert func_fn.wfopo[0].src == 1
275 assert func_fn.wfopo[0].tgt == 1
277 ###############################
278 assign_fn = exp_gromet.fn_array[1]
279 assert len(assign_fn.opo) == 1
280 assert assign_fn.opo[0].box == 1
282 assert len(assign_fn.b) == 1
283 assert assign_fn.b[0].function_type == FunctionType.EXPRESSION
285 assert len(assign_fn.bf) == 1
286 assert assign_fn.bf[0].function_type == FunctionType.LITERAL
287 assert assign_fn.bf[0].value.value_type == "Integer"
288 assert assign_fn.bf[0].value.value == 20
290 assert len(assign_fn.pof) == 1
291 assert assign_fn.pof[0].box == 1
293 assert len(assign_fn.wfopo) == 1
294 assert assign_fn.wfopo[0].src == 1
295 assert assign_fn.wfopo[0].tgt == 1
297 ###############################
298 assign_fn = exp_gromet.fn_array[2]
299 assert len(assign_fn.opo) == 1
300 assert assign_fn.opo[0].box == 1
302 assert len(assign_fn.b) == 1
303 assert assign_fn.b[0].function_type == FunctionType.EXPRESSION
305 assert len(assign_fn.bf) == 1
306 assert assign_fn.bf[0].function_type == FunctionType.LITERAL
307 assert assign_fn.bf[0].value.value_type == "Integer"
308 assert assign_fn.bf[0].value.value == 2
310 assert len(assign_fn.pof) == 1
311 assert assign_fn.pof[0].box == 1
313 assert len(assign_fn.wfopo) == 1
314 assert assign_fn.wfopo[0].src == 1
315 assert assign_fn.wfopo[0].tgt == 1
317 ###############################
318 assign_fn = exp_gromet.fn_array[3]
319 assert len(assign_fn.opo) == 1
320 assert assign_fn.opo[0].box == 1
322 assert len(assign_fn.b) == 1
323 assert assign_fn.b[0].function_type == FunctionType.EXPRESSION
325 assert len(assign_fn.bf) == 1
326 assert assign_fn.bf[0].function_type == FunctionType.LITERAL
327 assert assign_fn.bf[0].value.value_type == "Integer"
328 assert assign_fn.bf[0].value.value == 1
330 assert len(assign_fn.pof) == 1
331 assert assign_fn.pof[0].box == 1
333 assert len(assign_fn.wfopo) == 1
334 assert assign_fn.wfopo[0].src == 1
335 assert assign_fn.wfopo[0].tgt == 1