Coverage for skema/program_analysis/tests/test_recursion.py: 100%

28 statements  

« 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 GrometFNModuleCollection 

5import ast 

6 

7from skema.program_analysis.CAST.pythonAST import py_ast_to_cast 

8from skema.program_analysis.CAST2FN.model.cast import SourceRef 

9from skema.program_analysis.CAST2FN import cast 

10from skema.program_analysis.CAST2FN.cast import CAST 

11from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline 

12 

13# NOTE: these examples are very trivial for the realm of recursion 

14# more complex ones will follow later as needed 

15 

16def recurse1(): 

17 return """ 

18def rec(x): 

19 rec(x + 1) 

20 

21z = rec(12) 

22 """ 

23 

24def recurse2(): 

25 return """ 

26def rec1(x): 

27 rec2(x + 1) 

28 

29def rec2(y): 

30 rec1(y + 2) 

31 

32z = rec1(12) 

33 """ 

34 

35def recurse3(): 

36 return """ 

37def rec(x): 

38 if x < 10: 

39 return x 

40 y = rec(x + 1) 

41 return y 

42 

43z = rec(1)  

44 """ 

45 

46def generate_gromet(test_file_string): 

47 # use ast.Parse to get Python AST 

48 contents = ast.parse(test_file_string) 

49 

50 # use Python to CAST 

51 line_count = len(test_file_string.split("\n")) 

52 convert = py_ast_to_cast.PyASTToCAST("temp") 

53 C = convert.visit(contents, {}, {}) 

54 C.source_refs = [SourceRef("temp", None, None, 1, line_count)] 

55 out_cast = cast.CAST([C], "python") 

56 

57 # use AnnCastPipeline to create GroMEt 

58 gromet = ann_cast_pipeline(out_cast, gromet=True, to_file=False, from_obj=True) 

59 

60 return gromet 

61 

62def test_recursion(): 

63 prog1_gromet = generate_gromet(recurse1()) 

64 prog2_gromet = generate_gromet(recurse2()) 

65 prog3_gromet = generate_gromet(recurse3()) 

66 

67 return