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

Function TestCustomExistsMacro

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

Source from the content-addressed store, hash-verified

1101}
1102
1103func TestCustomExistsMacro(t *testing.T) {
1104 env := testEnv(t,
1105 Variable("attr", MapType(StringType, BoolType)),
1106 Macros(
1107 NewGlobalVarArgMacro("kleeneOr",
1108 func(meh MacroExprHelper, unused *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) {
1109 inputs := meh.NewList(args...)
1110 eqOne, err := ExistsMacroExpander(meh, inputs, []*exprpb.Expr{
1111 meh.Ident("__iter__"),
1112 meh.GlobalCall(operators.Equals, meh.Ident("__iter__"), meh.LiteralInt(1)),
1113 })
1114 if err != nil {
1115 return nil, err
1116 }
1117 eqZero, err := ExistsMacroExpander(meh, meh.Copy(inputs), []*exprpb.Expr{
1118 meh.Ident("__iter__"),
1119 meh.GlobalCall(operators.Equals, meh.Ident("__iter__"), meh.LiteralInt(0)),
1120 })
1121 if err != nil {
1122 return nil, err
1123 }
1124 return meh.GlobalCall(
1125 operators.Conditional,
1126 eqOne,
1127 meh.LiteralInt(1),
1128 meh.GlobalCall(
1129 operators.Conditional,
1130 eqZero,
1131 meh.LiteralInt(0),
1132 meh.LiteralInt(-1),
1133 ),
1134 ), nil
1135 },
1136 ),
1137 NewGlobalMacro("kleeneEq", 2,
1138 func(meh MacroExprHelper, unused *exprpb.Expr, args []*exprpb.Expr) (*exprpb.Expr, *Error) {
1139 attr := args[0]
1140 value := args[1]
1141 hasAttr, err := HasMacroExpander(meh, nil, []*exprpb.Expr{meh.Copy(attr)})
1142 if err != nil {
1143 return nil, err
1144 }
1145 return meh.GlobalCall(
1146 operators.Conditional,
1147 meh.GlobalCall(operators.LogicalNot, hasAttr),
1148 meh.LiteralInt(0),
1149 meh.GlobalCall(
1150 operators.Conditional,
1151 meh.GlobalCall(operators.Equals, attr, value),
1152 meh.LiteralInt(1),
1153 meh.LiteralInt(-1),
1154 ),
1155 ), nil
1156 },
1157 ),
1158 ),
1159 )
1160 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