Coverage for skema/program_analysis/tests/test_closures.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 

13def fun_nested_def1(): 

14 return """ 

15def bar(): # defines module.bar() 

16 print("module.bar()") 

17 

18def foo(): # defines module.foo() 

19 # NOTE: needs global 'bar' call to run in Python appropriately 

20 bar() # calling module.bar() 

21 def bar(): # defining module.foo.bar() 

22 print("module.foo.bar()") 

23 bar() # calling module.foo.bar() 

24 

25foo() 

26bar() # calls module.bar() 

27 """ 

28 

29def fun_nested_def2(): 

30 return """ 

31def foo(): 

32 x = 10 

33 y = x + 2 

34 

35 def bar(y): # translate these inner y's to y-inner1 or something to differentiate local and arguments that share the same name 

36 print(x) 

37 print(y) 

38 bar(y) 

39 

40foo() 

41 """ 

42 

43def fun_nested_def3(): 

44 return """ 

45def foo(z): 

46 x = 10 

47 y = x + 2 

48 

49 def bar(y): # translate these inner y's to y-inner1 or something to differentiate local and arguments that share the same name 

50 a = x + y 

51 x += 1 

52 

53 def baz(a): 

54 b = a + 1 

55 z = x + y 

56 

57 baz(x) 

58 

59 bar(y) 

60 

61foo(10) 

62 """ 

63 

64def generate_gromet(test_file_string): 

65 # use ast.Parse to get Python AST 

66 contents = ast.parse(test_file_string) 

67 

68 # use Python to CAST 

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

70 convert = py_ast_to_cast.PyASTToCAST("temp") 

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

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

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

74 

75 # use AnnCastPipeline to create GroMEt 

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

77 

78 return gromet 

79 

80def test_closures(): 

81 prog1_gromet = generate_gromet(fun_nested_def1()) 

82 prog2_gromet = generate_gromet(fun_nested_def2()) 

83 prog3_gromet = generate_gromet(fun_nested_def3()) 

84 

85 return