(t *testing.T)
| 307 | } |
| 308 | |
| 309 | func TestSingletonFunctionBinding(t *testing.T) { |
| 310 | env, err := NewCustomEnv( |
| 311 | Variable("unk", DynType), |
| 312 | Variable("err", DynType), |
| 313 | Function("dyn", |
| 314 | Overload("dyn", []*Type{DynType}, DynType), |
| 315 | SingletonUnaryBinding(func(arg ref.Val) ref.Val { |
| 316 | return arg |
| 317 | })), |
| 318 | Function("max", |
| 319 | Overload("max_int", []*Type{IntType}, IntType), |
| 320 | Overload("max_int_int", []*Type{IntType, IntType}, IntType), |
| 321 | Overload("max_int_int_int", []*Type{IntType, IntType, IntType}, IntType), |
| 322 | SingletonFunctionBinding(func(args ...ref.Val) ref.Val { |
| 323 | max := types.Int(math.MinInt64) |
| 324 | for _, arg := range args { |
| 325 | i, ok := arg.(types.Int) |
| 326 | if !ok { |
| 327 | // With a singleton implementation, the error handling must be explicitly |
| 328 | // performed as a binding detail of the singleton function. |
| 329 | // With custom overload implementations, a function guard is automatically |
| 330 | // added to the function to validate that the runtime types are compatible |
| 331 | // to provide some basic invocation protections. |
| 332 | return decls.MaybeNoSuchOverload("max", args...) |
| 333 | } |
| 334 | if i > max { |
| 335 | max = i |
| 336 | } |
| 337 | } |
| 338 | return max |
| 339 | }), |
| 340 | ), |
| 341 | ) |
| 342 | if err != nil { |
| 343 | t.Fatalf("NewCustomEnv() failed: %v", err) |
| 344 | } |
| 345 | |
| 346 | for _, tc := range dispatchTests { |
| 347 | tc := tc |
| 348 | t.Run(fmt.Sprintf("Parse(%s)", tc.expr), func(t *testing.T) { |
| 349 | testParse(t, env, tc.expr, tc.out) |
| 350 | }) |
| 351 | } |
| 352 | for _, tc := range dispatchTests { |
| 353 | tc := tc |
| 354 | t.Run(fmt.Sprintf("Compile(%s)", tc.expr), func(t *testing.T) { |
| 355 | testCompile(t, env, tc.expr, tc.out) |
| 356 | }) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | func TestUnaryBinding(t *testing.T) { |
| 361 | _, err := NewCustomEnv( |
nothing calls this directly
no test coverage detected