(t *testing.T)
| 634 | } |
| 635 | |
| 636 | func TestExprDeclToDeclaration(t *testing.T) { |
| 637 | paramT := chkdecls.NewTypeParamType("T") |
| 638 | eq, err := ExprDeclToDeclaration( |
| 639 | chkdecls.NewFunction(operators.Equals, |
| 640 | chkdecls.NewParameterizedOverload(overloads.Equals, |
| 641 | []*exprpb.Type{paramT, paramT}, chkdecls.Bool, []string{"T"})), |
| 642 | ) |
| 643 | if err != nil { |
| 644 | t.Fatalf("ExprDeclToDeclaration(equals) failed: %v", err) |
| 645 | } |
| 646 | size, err := ExprDeclToDeclaration( |
| 647 | chkdecls.NewFunction(overloads.Size, |
| 648 | chkdecls.NewOverload(overloads.SizeString, |
| 649 | []*exprpb.Type{chkdecls.String}, chkdecls.Int), |
| 650 | chkdecls.NewInstanceOverload(overloads.SizeStringInst, |
| 651 | []*exprpb.Type{chkdecls.String}, chkdecls.Int)), |
| 652 | ) |
| 653 | if err != nil { |
| 654 | t.Fatalf("ExprDeclToDeclaration(size) failed: %v", err) |
| 655 | } |
| 656 | x, err := ExprDeclToDeclaration(chkdecls.NewVar("x", chkdecls.String)) |
| 657 | if err != nil { |
| 658 | t.Fatalf("ExprDeclToDeclaration(x) failed: %v", err) |
| 659 | } |
| 660 | constant, err := ExprDeclToDeclaration( |
| 661 | chkdecls.NewConst("constant", chkdecls.Bool, &exprpb.Constant{ |
| 662 | ConstantKind: &exprpb.Constant_BoolValue{BoolValue: true}, |
| 663 | })) |
| 664 | if err != nil { |
| 665 | t.Fatalf("ExprDeclToDeclaration(constant) failed: %v", err) |
| 666 | } |
| 667 | e, err := NewCustomEnv(size, eq, x, constant) |
| 668 | if err != nil { |
| 669 | t.Fatalf("NewCustomEnv() failed: %v", err) |
| 670 | } |
| 671 | ast, iss := e.Compile("(size(x) == x.size()) == constant") |
| 672 | if iss.Err() != nil { |
| 673 | t.Fatalf("Compile((size(x) == x.size()) == constant) failed: %v", iss.Err()) |
| 674 | } |
| 675 | prg, err := e.Program(ast, Functions(&functions.Overload{ |
| 676 | Operator: overloads.SizeString, |
| 677 | Unary: func(arg ref.Val) ref.Val { |
| 678 | str, ok := arg.(types.String) |
| 679 | if !ok { |
| 680 | return types.MaybeNoSuchOverloadErr(arg) |
| 681 | } |
| 682 | return types.Int(len([]rune(string(str)))) |
| 683 | }, |
| 684 | }, &functions.Overload{ |
| 685 | Operator: overloads.SizeStringInst, |
| 686 | Unary: func(arg ref.Val) ref.Val { |
| 687 | str, ok := arg.(types.String) |
| 688 | if !ok { |
| 689 | return types.MaybeNoSuchOverloadErr(arg) |
| 690 | } |
| 691 | return types.Int(len([]rune(string(str)))) |
| 692 | }, |
| 693 | })) |
nothing calls this directly
no test coverage detected