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

Function TestCustomExistsMacro

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

Source from the content-addressed store, hash-verified

1017}
1018
1019func TestCustomExistsMacro(t *testing.T) {
1020 env := testEnv(t,
1021 Variable("attr", MapType(StringType, BoolType)),
1022 Macros(
1023 NewGlobalVarArgMacro("kleeneOr",
1024 func(meh MacroExprHelper, unused *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) {
1025 inputs := meh.NewList(args...)
1026 eqOne, err := ExistsMacroExpander(meh, inputs, []*exprpb.Expr{
1027 meh.Ident("__iter__"),
1028 meh.GlobalCall(operators.Equals, meh.Ident("__iter__"), meh.LiteralInt(1)),
1029 })
1030 if err != nil {
1031 return nil, err
1032 }
1033 eqZero, err := ExistsMacroExpander(meh, meh.Copy(inputs), []*exprpb.Expr{
1034 meh.Ident("__iter__"),
1035 meh.GlobalCall(operators.Equals, meh.Ident("__iter__"), meh.LiteralInt(0)),
1036 })
1037 if err != nil {
1038 return nil, err
1039 }
1040 return meh.GlobalCall(
1041 operators.Conditional,
1042 eqOne,
1043 meh.LiteralInt(1),
1044 meh.GlobalCall(
1045 operators.Conditional,
1046 eqZero,
1047 meh.LiteralInt(0),
1048 meh.LiteralInt(-1),
1049 ),
1050 ), nil
1051 },
1052 ),
1053 NewGlobalMacro("kleeneEq", 2,
1054 func(meh MacroExprHelper, unused *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) {
1055 attr := args[0]
1056 value := args[1]
1057 hasAttr, err := HasMacroExpander(meh, nil, []*exprpb.Expr{meh.Copy(attr)})
1058 if err != nil {
1059 return nil, err
1060 }
1061 return meh.GlobalCall(
1062 operators.Conditional,
1063 meh.GlobalCall(operators.LogicalNot, hasAttr),
1064 meh.LiteralInt(0),
1065 meh.GlobalCall(
1066 operators.Conditional,
1067 meh.GlobalCall(operators.Equals, attr, value),
1068 meh.LiteralInt(1),
1069 meh.LiteralInt(-1),
1070 ),
1071 ), nil
1072 },
1073 ),
1074 ),
1075 )
1076 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