MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestMathVersions

Function TestMathVersions

ext/math_test.go:584–660  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

582}
583
584func TestMathVersions(t *testing.T) {
585 versionCases := []struct {
586 version uint32
587 supportedFunctions map[string]string
588 }{
589 {
590 version: 0,
591 supportedFunctions: map[string]string{
592 "greatest": `math.greatest(1, 2) == 2`,
593 "least": `math.least(2.1, -1.0) == -1.0`,
594 },
595 },
596 {
597 version: 1,
598 supportedFunctions: map[string]string{
599 "ceil": `math.ceil(1.5) == 2.0`,
600 "floor": `math.floor(1.2) == 1.0`,
601 "round": `math.round(1.5) == 2.0`,
602 "trunc": `math.trunc(1.222) == 1.0`,
603 "isInf": `!math.isInf(0.0)`,
604 "isNaN": `math.isNaN(0.0/0.0)`,
605 "isFinite": `math.isFinite(0.0)`,
606 "abs": `math.abs(1.2) == 1.2`,
607 "sign": `math.sign(-1) == -1`,
608 "bitAnd": `math.bitAnd(1, 2) == 0`,
609 "bitOr": `math.bitOr(1, 2) == 3`,
610 "bitXor": `math.bitXor(1, 3) == 2`,
611 "bitNot": `math.bitNot(-1) == 0`,
612 "bitShiftLeft": `math.bitShiftLeft(4, 2) == 16`,
613 "bitShiftRight": `math.bitShiftRight(4, 2) == 1`,
614 },
615 },
616 {
617 version: 2,
618 supportedFunctions: map[string]string{
619 "sqrt": `math.sqrt(25) == 5.0`,
620 },
621 },
622 }
623 for _, lib := range versionCases {
624 env, err := cel.NewEnv(Math(MathVersion(lib.version)))
625 if err != nil {
626 t.Fatalf("cel.NewEnv(Math(MathVersion(%d))) failed: %v", lib.version, err)
627 }
628 t.Run(fmt.Sprintf("version=%d", lib.version), func(t *testing.T) {
629 for _, tc := range versionCases {
630 for name, expr := range tc.supportedFunctions {
631 supported := lib.version >= tc.version
632 t.Run(fmt.Sprintf("%s-supported=%t", name, supported), func(t *testing.T) {
633 ast, iss := env.Compile(expr)
634 if supported {
635 if iss.Err() != nil {
636 t.Errorf("unexpected error: %v", iss.Err())
637 }
638 } else {
639 if iss.Err() == nil || !strings.Contains(iss.Err().Error(), "undeclared reference") {
640 t.Errorf("got error %v, wanted error %s for expr: %s, version: %d", iss.Err(), "undeclared reference", expr, tc.version)
641 }

Callers

nothing calls this directly

Calls 10

CompileMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
NoVarsFunction · 0.92
MathFunction · 0.85
MathVersionFunction · 0.85
ErrMethod · 0.80
ContainsMethod · 0.65
EvalMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected