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

Function TestMathVersions

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

Source from the content-addressed store, hash-verified

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

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