Coverage for skema/program_analysis/CAST/matlab/tests/test_conditional.py: 100%

14 statements  

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

1from skema.program_analysis.CAST.matlab.tests.utils import (check, cast) 

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

3 Assignment, 

4 ModelIf, 

5 Operator 

6) 

7 

8def test_if(): 

9 """ Test CAST from MATLAB 'if' conditional logic.""" 

10 source = """ 

11 if x == 5 

12 y = 6 

13 end 

14 """ 

15 check( 

16 cast(source)[0], 

17 ModelIf( 

18 expr = Operator(op = "==", operands = ["x", 5]), 

19 body = [Assignment(left="y", right = 6)], 

20 orelse = [] 

21 ) 

22 ) 

23 

24def test_if_else(): 

25 """ Test CAST from MATLAB 'if else' conditional logic.""" 

26 source = """ 

27 if x > 5 

28 y = 6 

29 three = 3 

30 else 

31 y = x 

32 foo = 'bar' 

33 end 

34 """ 

35 check( 

36 cast(source)[0], 

37 ModelIf( 

38 expr = Operator(op = ">", operands = ["x", 5]), 

39 body = [ 

40 Assignment(left="y", right = 6), 

41 Assignment(left="three", right = 3) 

42 ], 

43 orelse = [ 

44 Assignment(left="y", right = "x"), 

45 Assignment(left="foo", right = "'bar'") 

46 ] 

47 ) 

48 ) 

49 

50 

51def test_if_elseif(): 

52 """ Test CAST from MATLAB 'if elseif else' conditional logic.""" 

53 source = """ 

54 if x >= 5 

55 y = 6 

56 elseif x <= 0 

57 y = x 

58 end 

59 """ 

60 check( 

61 cast(source)[0], 

62 ModelIf( 

63 expr = Operator(op = ">=", operands = ["x", 5]), 

64 body = [Assignment(left="y", right = 6)], 

65 orelse = [ModelIf( 

66 expr = Operator(op = "<=", operands = ["x", 0]), 

67 body = [Assignment(left="y", right = "x")], 

68 orelse = []) 

69 ] 

70 ) 

71 ) 

72 

73 

74def test_if_elseif_else(): 

75 """ Test CAST from MATLAB 'if elseif else' conditional logic.""" 

76 source = """ 

77 if x > 5 

78 a = 6 

79 elseif x > 0 

80 b = x 

81 else 

82 c = 0 

83 end 

84 """ 

85 check( 

86 cast(source)[0], 

87 ModelIf( 

88 expr = Operator(op = ">", operands = ["x", 5]), 

89 body = [Assignment(left="a", right = 6)], 

90 orelse = [ 

91 ModelIf( 

92 expr = Operator(op = ">", operands = ["x", 0]), 

93 body = [Assignment(left="b", right = "x")], 

94 orelse = [Assignment(left="c", right = 0)] 

95 ) 

96 ] 

97 ) 

98 )