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

Function TestSubsetStdLib

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

Source from the content-addressed store, hash-verified

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

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