Coverage for skema/program_analysis/CAST/fortran/tests/test_expression_cast_fortran.py: 98%

47 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1import pytest 

2from tempfile import TemporaryDirectory 

3from pathlib import Path 

4 

5from skema.program_analysis.CAST.fortran.ts2cast import TS2CAST 

6from skema.program_analysis.CAST2FN.model.cast import ( 

7 Assignment, 

8 Var, 

9 Name, 

10 CASTLiteralValue, 

11 ScalarType 

12) 

13 

14def exp0(): 

15 return """ 

16program exp0 

17integer :: x = 2 

18end program exp0 

19 """ 

20 

21def exp1(): 

22 return """ 

23program exp1 

24integer :: x = 2 

25integer :: y = 3 

26end program exp1 

27""" 

28 

29def generate_cast(test_file_string): 

30 with TemporaryDirectory() as temp: 

31 source_path = Path(temp) / "source.f95" 

32 source_path.write_text(test_file_string) 

33 out_cast = TS2CAST(str(source_path)).out_cast 

34 

35 return out_cast[0] 

36 

37def test_exp0(): 

38 exp_cast = generate_cast(exp0()) 

39 # Test basic properties of assignment node 

40 asg_node = exp_cast.nodes[0].body[0] 

41 

42 assert isinstance(asg_node, Assignment) 

43 assert isinstance(asg_node.left, Var) 

44 assert isinstance(asg_node.left.val, Name) 

45 assert asg_node.left.val.name == "x" 

46 

47 assert isinstance(asg_node.right, CASTLiteralValue) 

48 assert asg_node.right.value_type == ScalarType.INTEGER 

49 assert asg_node.right.value == '2' 

50 

51 

52def test_exp1(): 

53 exp_cast = generate_cast(exp1()) 

54 

55 # Test basic properties of two assignment nodes 

56 asg_node = exp_cast.nodes[0].body[0] 

57 assert isinstance(asg_node, Assignment) 

58 assert isinstance(asg_node.left, Var) 

59 assert isinstance(asg_node.left.val, Name) 

60 assert asg_node.left.val.name == "x" 

61 assert asg_node.left.val.id == 0 

62 

63 assert isinstance(asg_node.right, CASTLiteralValue) 

64 assert asg_node.right.value_type == ScalarType.INTEGER 

65 assert asg_node.right.value == '2' 

66 

67 # ------ 

68 asg_node = exp_cast.nodes[0].body[1] 

69 assert isinstance(asg_node, Assignment) 

70 assert isinstance(asg_node.left, Var) 

71 assert isinstance(asg_node.left.val, Name) 

72 assert asg_node.left.val.name == "y" 

73 assert asg_node.left.val.id == 1 

74 

75 assert isinstance(asg_node.right, CASTLiteralValue) 

76 assert asg_node.right.value_type == ScalarType.INTEGER 

77 assert asg_node.right.value == '3' 

78 

79if __name__ == "__main__": 

80 cast = generate_cast(exp0())