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

17 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 Call, 

5 FunctionDef, 

6 Operator 

7) 

8 

9# Test CAST from functions 

10def test_definition(): 

11 """ Test function definition """ 

12 source = """ 

13 function both = add_them(x, y) 

14 both = x + y 

15 end 

16 """ 

17 nodes = cast(source)[0] 

18 check( 

19 cast(source)[0], 

20 FunctionDef( 

21 name = "both", 

22 func_args = ["x", "y"], 

23 body = [ 

24 Assignment( 

25 left = "both", 

26 right = Operator (op = "+", operands = ["x", "y"]) 

27 ) 

28 ] 

29 ) 

30 ) 

31 

32def test_call_with_literal_args(): 

33 """ Test function call with literal arguments """ 

34 check(cast("both(3, 5)")[0], Call(func = "both", arguments = [3, 5])) 

35 

36def test_call_with_operator_args(): 

37 """ Test function call with Operator arguments """ 

38 nodes = cast("foo(x < a, -6)") 

39 check( 

40 nodes[0], 

41 Call( 

42 func = "foo", 

43 arguments = [ 

44 Operator(op = "<", operands = ["x", "a"]), 

45 Operator(op = "-", operands = [6]), 

46 ] 

47 ) 

48 ) 

49 

50def test_call_with_call_args(): 

51 """ Test function call with matrix of function call arguments """ 

52 nodes = cast("foo(bar(x), baz(y))") 

53 check( 

54 nodes[0], 

55 Call( 

56 func = "foo", 

57 arguments = [ 

58 Call (func = "bar", arguments = ["x"]), 

59 Call (func = "baz", arguments = ["y"]), 

60 ] 

61 ) 

62 ) 

63 

64def test_call_with_anonymous_call_arg(): 

65 nodes = cast("foo{x}(y)") 

66 check( 

67 nodes[0], 

68 Call(func = Call (func = "foo", arguments = ["x"]),arguments = ["y"]) 

69 )