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

Function TestSubsetStdLib

cel/cel_test.go:507–589  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

505}
506
507func TestSubsetStdLib(t *testing.T) {
508 env, err := NewCustomEnv(
509 StdLib(StdLibSubset(
510 &env.LibrarySubset{
511 IncludeMacros: []string{"has"},
512 IncludeFunctions: []*env.Function{
513 {Name: operators.Equals},
514 {Name: operators.NotEquals},
515 {Name: operators.LogicalAnd},
516 {Name: operators.LogicalOr},
517 {Name: operators.LogicalNot},
518 {Name: overloads.Size, Overloads: []*env.Overload{{ID: "list_size"}}},
519 },
520 },
521 )))
522 if err != nil {
523 t.Fatalf("StdLib() subsetting failed: %v", err)
524 }
525 tests := []struct {
526 name string
527 expr string
528 compiles bool
529 want ref.Val
530 }{
531 {
532 name: "has macro",
533 expr: "!has({}.a)",
534 compiles: true,
535 want: types.True,
536 },
537 {
538 name: "not equals",
539 expr: "has({}.a) != true",
540 compiles: true,
541 want: types.True,
542 },
543 {
544 name: "logical operators",
545 expr: "has({}.a) != true && has({'b': 1}.b) == true",
546 compiles: true,
547 want: types.True,
548 },
549 {
550 name: "list size - allowed",
551 expr: "[1, 2, 3].size()",
552 compiles: true,
553 want: types.Int(3),
554 },
555 {
556 name: "excluded macro",
557 expr: "[1, 2, 3].exists(i, i != 0)",
558 compiles: false,
559 },
560 {
561 name: "string size - not allowed",
562 expr: "'hello'.size()",
563 compiles: false,
564 },

Callers

nothing calls this directly

Calls 10

IntTypeAlias · 0.92
NewCustomEnvFunction · 0.85
StdLibFunction · 0.85
StdLibSubsetFunction · 0.85
NoVarsFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
CompileMethod · 0.65
EvalMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected