(t *testing.T)
| 5890 | } |
| 5891 | } |
| 5892 | func TestLateBoundFunctions(t *testing.T) { |
| 5893 | env, err := NewEnv( |
| 5894 | Variable("x", IntType), |
| 5895 | Variable("y", IntType), |
| 5896 | Function("inc", |
| 5897 | Overload("inc_int", []*Type{IntType}, IntType, LateFunctionBinding()), |
| 5898 | ), |
| 5899 | Function("add", |
| 5900 | Overload("add_int_int", []*Type{IntType, IntType}, IntType, LateFunctionBinding()), |
| 5901 | ), |
| 5902 | Function("sum", |
| 5903 | Overload("sum_int_int_int", []*Type{IntType, IntType, IntType}, IntType, LateFunctionBinding()), |
| 5904 | ), |
| 5905 | Function("now_unix", |
| 5906 | Overload("now_unix_zero", []*Type{}, IntType, LateFunctionBinding()), |
| 5907 | ), |
| 5908 | Function("tag", |
| 5909 | MemberOverload("tag_int_string", []*Type{IntType, StringType}, IntType, LateFunctionBinding()), |
| 5910 | ), |
| 5911 | ) |
| 5912 | if err != nil { |
| 5913 | t.Fatalf("NewEnv() failed: %v", err) |
| 5914 | } |
| 5915 | |
| 5916 | tests := []struct { |
| 5917 | name string |
| 5918 | expr string |
| 5919 | vars map[string]any |
| 5920 | funcs map[string]LateBoundFunction |
| 5921 | want ref.Val |
| 5922 | wantErr string |
| 5923 | }{ |
| 5924 | { |
| 5925 | name: "unary call", |
| 5926 | expr: "inc(x)", |
| 5927 | vars: map[string]any{"x": 5}, |
| 5928 | funcs: map[string]LateBoundFunction{ |
| 5929 | "inc": func(overloadID string, args ...ref.Val) ref.Val { |
| 5930 | return types.Int(args[0].(types.Int) + 1) |
| 5931 | }, |
| 5932 | }, |
| 5933 | want: types.Int(6), |
| 5934 | }, |
| 5935 | { |
| 5936 | name: "unary missing from activation", |
| 5937 | expr: "inc(x)", |
| 5938 | vars: map[string]any{"x": 5}, |
| 5939 | wantErr: "no such overload: inc", |
| 5940 | }, |
| 5941 | { |
| 5942 | name: "unary shadowed by a variable of the same name", |
| 5943 | expr: "inc(x)", |
| 5944 | vars: map[string]any{"x": 5, "inc": "not a function"}, |
| 5945 | funcs: map[string]LateBoundFunction{ |
| 5946 | "inc": func(overloadID string, args ...ref.Val) ref.Val { |
| 5947 | return types.Int(args[0].(types.Int) + 1) |
| 5948 | }, |
| 5949 | }, |
nothing calls this directly
no test coverage detected