Coverage for skema/program_analysis/tests/test_expression_cast.py: 98%
41 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« 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.CAST.python.ts2cast import TS2CAST
4from skema.program_analysis.CAST2FN.model.cast import (
5 Assignment,
6 Var,
7 Name,
8 CASTLiteralValue
9)
11def exp0():
12 return """
13x = 2
14 """
16def exp1():
17 return """
18x = 2
19y = 3
20 """
22def generate_cast(test_file_string):
23 # use Python to CAST
24 out_cast = TS2CAST(test_file_string, from_file=False).out_cast
26 return out_cast
28def test_exp0():
29 exp_cast = generate_cast(exp0())
30 # Test basic properties of assignment node
31 asg_node = exp_cast.nodes[0].body[0]
33 assert isinstance(asg_node, Assignment)
34 assert isinstance(asg_node.left, Var)
35 assert isinstance(asg_node.left.val, Name)
36 assert asg_node.left.val.name == "x"
38 assert isinstance(asg_node.right, CASTLiteralValue)
39 assert asg_node.right.value_type == "Integer"
40 assert asg_node.right.value == '2'
43def test_exp1():
44 exp_cast = generate_cast(exp1())
46 # Test basic properties of two assignment nodes
47 asg_node = exp_cast.nodes[0].body[0]
48 assert isinstance(asg_node, Assignment)
49 assert isinstance(asg_node.left, Var)
50 assert isinstance(asg_node.left.val, Name)
51 assert asg_node.left.val.name == "x"
52 assert asg_node.left.val.id == 0
54 assert isinstance(asg_node.right, CASTLiteralValue)
55 assert asg_node.right.value_type == "Integer"
56 assert asg_node.right.value == '2'
58 # ------
59 asg_node = exp_cast.nodes[0].body[1]
60 assert isinstance(asg_node, Assignment)
61 assert isinstance(asg_node.left, Var)
62 assert isinstance(asg_node.left.val, Name)
63 assert asg_node.left.val.name == "y"
64 assert asg_node.left.val.id == 1
66 assert isinstance(asg_node.right, CASTLiteralValue)
67 assert asg_node.right.value_type == "Integer"
68 assert asg_node.right.value == '3'
70if __name__ == "__main__":
71 cast = generate_cast(exp0())