Coverage for skema/program_analysis/CAST/fortran/tests/test_compound_conditional_cast_fortran.py: 100%

67 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 ModelIf, 

12 Operator, 

13 ScalarType 

14) 

15 

16def cond_compound1(): 

17 return """ 

18program cond_compound1 

19integer :: a = 3 

20if (a .gt. 1 .and. a .lt. 10) then 

21 a = 40 

22end if 

23end program cond_compound1 

24 """ 

25 

26 

27def generate_cast(test_file_string): 

28 with TemporaryDirectory() as temp: 

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

30 source_path.write_text(test_file_string) 

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

32 

33 return out_cast[0] 

34 

35def test_cond_compound1(): 

36 exp_cast = generate_cast(cond_compound1()) 

37 

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

39 

40 assert isinstance(asg_node, Assignment) 

41 assert isinstance(asg_node.left, Var) 

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

43 assert asg_node.left.val.name == "a" 

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

45 

46 assert isinstance(asg_node.right, CASTLiteralValue) 

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

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

49 

50 cond_node = exp_cast.nodes[0].body[1] 

51 cond_expr = cond_node.expr 

52 assert isinstance(cond_node, ModelIf) 

53 assert isinstance(cond_expr, ModelIf) 

54 

55 if_node = cond_expr 

56 assert isinstance(if_node, ModelIf) 

57 

58 expr = if_node.expr 

59 assert isinstance(expr, Operator) 

60 assert expr.op == ".gt." 

61 assert len(expr.operands) == 2 

62 assert isinstance(expr.operands[1], CASTLiteralValue) 

63 assert expr.operands[1].value_type == ScalarType.INTEGER 

64 assert expr.operands[1].value == "1" 

65 

66 assert isinstance(expr.operands[0], Name) 

67 assert expr.operands[0].name == "a" 

68 assert expr.operands[0].id == 0 

69 

70 assert len(if_node.body) == 1 

71 body = if_node.body[0] 

72 assert isinstance(body, Operator) 

73 assert body.op == ".lt." 

74 assert len(body.operands) == 2 

75 assert isinstance(body.operands[0], Name) 

76 assert body.operands[0].name == "a" 

77 assert body.operands[0].id == 0 

78 

79 assert isinstance(body.operands[1], CASTLiteralValue) 

80 assert body.operands[1].value_type == ScalarType.INTEGER 

81 assert body.operands[1].value == "10" 

82 

83 assert len(if_node.orelse) == 1 

84 orelse = if_node.orelse[0] 

85 assert isinstance(orelse, CASTLiteralValue) 

86 assert orelse.value_type == ScalarType.BOOLEAN 

87 assert orelse.value == False 

88 

89 cond_body = cond_node.body 

90 assert len(cond_body) == 1 

91 assert isinstance(cond_body[0], Assignment) 

92 assert isinstance(cond_body[0].left, Var) 

93 assert cond_body[0].left.val.name == "a" 

94 assert cond_body[0].left.val.id == 0 

95 

96 assert isinstance(cond_body[0].right, CASTLiteralValue) 

97 assert cond_body[0].right.value_type == ScalarType.INTEGER 

98 assert cond_body[0].right.value == '40' 

99 

100 cond_else = cond_node.orelse 

101 assert len(cond_else) == 0