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

Function TestSubsetStdLib

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

Source from the content-addressed store, hash-verified

401}
402
403func TestSubsetStdLib(t *testing.T) {
404 env, err := NewCustomEnv(
405 StdLib(StdLibSubset(
406 &env.LibrarySubset{
407 IncludeMacros: []string{"has"},
408 IncludeFunctions: []*env.Function{
409 {Name: operators.Equals},
410 {Name: operators.NotEquals},
411 {Name: operators.LogicalAnd},
412 {Name: operators.LogicalOr},
413 {Name: operators.LogicalNot},
414 {Name: overloads.Size, Overloads: []*env.Overload{{ID: "list_size"}}},
415 },
416 },
417 )))
418 if err != nil {
419 t.Fatalf("StdLib() subsetting failed: %v", err)
420 }
421 tests := []struct {
422 name string
423 expr string
424 compiles bool
425 want ref.Val
426 }{
427 {
428 name: "has macro",
429 expr: "!has({}.a)",
430 compiles: true,
431 want: types.True,
432 },
433 {
434 name: "not equals",
435 expr: "has({}.a) != true",
436 compiles: true,
437 want: types.True,
438 },
439 {
440 name: "logical operators",
441 expr: "has({}.a) != true && has({'b': 1}.b) == true",
442 compiles: true,
443 want: types.True,
444 },
445 {
446 name: "list size - allowed",
447 expr: "[1, 2, 3].size()",
448 compiles: true,
449 want: types.Int(3),
450 },
451 {
452 name: "excluded macro",
453 expr: "[1, 2, 3].exists(i, i != 0)",
454 compiles: false,
455 },
456 {
457 name: "string size - not allowed",
458 expr: "'hello'.size()",
459 compiles: false,
460 },

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