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

Function TestCustomExistsMacro

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

Source from the content-addressed store, hash-verified

997}
998
999func TestCustomExistsMacro(t *testing.T) {
1000 env := testEnv(t,
1001 Variable("attr", MapType(StringType, BoolType)),
1002 Macros(
1003 NewGlobalVarArgMacro("kleeneOr",
1004 func(meh MacroExprHelper, unused *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) {
1005 inputs := meh.NewList(args...)
1006 eqOne, err := ExistsMacroExpander(meh, inputs, []*exprpb.Expr{
1007 meh.Ident("__iter__"),
1008 meh.GlobalCall(operators.Equals, meh.Ident("__iter__"), meh.LiteralInt(1)),
1009 })
1010 if err != nil {
1011 return nil, err
1012 }
1013 eqZero, err := ExistsMacroExpander(meh, meh.Copy(inputs), []*exprpb.Expr{
1014 meh.Ident("__iter__"),
1015 meh.GlobalCall(operators.Equals, meh.Ident("__iter__"), meh.LiteralInt(0)),
1016 })
1017 if err != nil {
1018 return nil, err
1019 }
1020 return meh.GlobalCall(
1021 operators.Conditional,
1022 eqOne,
1023 meh.LiteralInt(1),
1024 meh.GlobalCall(
1025 operators.Conditional,
1026 eqZero,
1027 meh.LiteralInt(0),
1028 meh.LiteralInt(-1),
1029 ),
1030 ), nil
1031 },
1032 ),
1033 NewGlobalMacro("kleeneEq", 2,
1034 func(meh MacroExprHelper, unused *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) {
1035 attr := args[0]
1036 value := args[1]
1037 hasAttr, err := HasMacroExpander(meh, nil, []*exprpb.Expr{meh.Copy(attr)})
1038 if err != nil {
1039 return nil, err
1040 }
1041 return meh.GlobalCall(
1042 operators.Conditional,
1043 meh.GlobalCall(operators.LogicalNot, hasAttr),
1044 meh.LiteralInt(0),
1045 meh.GlobalCall(
1046 operators.Conditional,
1047 meh.GlobalCall(operators.Equals, attr, value),
1048 meh.LiteralInt(1),
1049 meh.LiteralInt(-1),
1050 ),
1051 ), nil
1052 },
1053 ),
1054 ),
1055 )
1056 prg := compile(t, env, "kleeneOr(kleeneEq(attr.value, true), kleeneOr(0, 1, 1)) == 1")

Callers

nothing calls this directly

Calls 14

ExistsMacroExpanderFunction · 0.85
HasMacroExpanderFunction · 0.85
testEnvFunction · 0.70
VariableFunction · 0.70
MacrosFunction · 0.70
NewGlobalVarArgMacroFunction · 0.70
NewGlobalMacroFunction · 0.70
compileFunction · 0.70
NewListMethod · 0.65
IdentMethod · 0.65
GlobalCallMethod · 0.65
LiteralIntMethod · 0.65

Tested by

no test coverage detected