Coverage for skema/program_analysis/CAST/matlab/tests/utils.py: 87%

55 statements  

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

1from skema.program_analysis.CAST.matlab.matlab_to_cast import MatlabToCast 

2from typing import List 

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

4 Assignment, 

5 AstNode, 

6 Call, 

7 FunctionDef, 

8 CASTLiteralValue, 

9 Loop, 

10 Operator, 

11 ModelIf, 

12 Module, 

13 Name, 

14 Var 

15) 

16from skema.program_analysis.CAST2FN.visitors.cast_to_agraph_visitor import ( 

17 CASTToAGraphVisitor, 

18) 

19from skema.program_analysis.CAST2FN.cast import CAST 

20 

21 

22def check(result, expected = None): 

23 """ Test for match with the same datatypes. """ 

24 if isinstance(result, List): 

25 assert len(result) == len(expected) 

26 for i, _ in enumerate(result): 

27 check(_, expected[i]) 

28 elif isinstance(result, Assignment): 

29 check(result.left, expected.left) 

30 check(result.right, expected.right) 

31 elif isinstance(result, Operator): 

32 check(result.op, expected.op) 

33 check(result.operands, expected.operands) 

34 elif isinstance(result, Call): 

35 check(result.func, expected.func) 

36 check(result.arguments, expected.arguments) 

37 elif isinstance(result, FunctionDef): 

38 check(result.name, expected.name) 

39 check(result.func_args, expected.func_args) 

40 check(result.body, expected.body) 

41 elif isinstance(result, ModelIf): 

42 check(result.expr, expected.expr) 

43 check(result.body, expected.body) 

44 check(result.orelse, expected.orelse) 

45 elif isinstance(result, Loop): 

46 check(result.pre, expected.pre) 

47 check(result.expr, expected.expr) 

48 check(result.body, expected.body) 

49 check(result.post, expected.post) 

50 elif isinstance(result, CASTLiteralValue): 

51 check(result.value, expected) 

52 elif isinstance(result, Var): 

53 check(result.val, expected) 

54 elif isinstance(result, Name): 

55 check(result.name, expected) 

56 else: 

57 assert result == expected 

58 

59 # every CAST node has a source_refs element 

60 if isinstance(result, AstNode): 

61 assert result.source_refs is not None 

62 

63# we curently produce a CAST object with a single Module in the nodes list. 

64def cast(source): 

65 """ Return the MatlabToCast output """ 

66 # there should only be one CAST object in the cast output list 

67 cast = MatlabToCast(source = source).out_cast 

68 # the CAST should be parsable into a graph 

69 assert validate_graph_visit(cast) == True 

70 # there should be one Module object in the CAST object 

71 assert len(cast.nodes) == 1 

72 module = cast.nodes[0] 

73 assert isinstance(module, Module) 

74 # return the module body node list 

75 return module.body 

76 

77def validate_graph_visit(cast): 

78 """ Test that the graph visitor can fully traverse the CAST object """ 

79 try: 

80 foo = CASTToAGraphVisitor(cast).to_agraph() 

81 return True 

82 except Exception as e: 

83 print(f"EXCEPTION: {e}") 

84 return False