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

Function TestCustomExistsMacro

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

Source from the content-addressed store, hash-verified

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