Skip to content

Commit 10734c1

Browse files
Merge branch 'master' into fix/issue-1333
2 parents d33c83a + 258e7c8 commit 10734c1

19 files changed

Lines changed: 2439 additions & 407 deletions

cel/env_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,3 +1280,65 @@ func (p *customCELProvider) FindStructFieldType(structType, fieldName string) (*
12801280
func (p *customCELProvider) NewValue(structType string, fields map[string]ref.Val) ref.Val {
12811281
return p.provider.NewValue(structType, fields)
12821282
}
1283+
1284+
func TestCELTypeAdapter(t *testing.T) {
1285+
env, err := NewEnv()
1286+
if err != nil {
1287+
t.Fatalf("NewEnv() failed: %v", err)
1288+
}
1289+
adapter := env.CELTypeAdapter()
1290+
if adapter == nil {
1291+
t.Error("CELTypeAdapter() returned nil")
1292+
}
1293+
}
1294+
1295+
type mockTypeProvider struct {
1296+
ref.TypeProvider
1297+
}
1298+
1299+
func TestMaybeInteropProvider_Error(t *testing.T) {
1300+
_, err := maybeInteropProvider(123)
1301+
if err == nil {
1302+
t.Error("maybeInteropProvider(int) should return error")
1303+
}
1304+
}
1305+
1306+
func TestMaybeInteropProvider_LegacyTypeProvider(t *testing.T) {
1307+
tp := &mockTypeProvider{}
1308+
p, err := maybeInteropProvider(tp)
1309+
if err != nil {
1310+
t.Fatalf("maybeInteropProvider(mockTypeProvider) failed: %v", err)
1311+
}
1312+
if _, ok := p.(*interopCELTypeProvider); !ok {
1313+
t.Errorf("expected *interopCELTypeProvider, got %T", p)
1314+
}
1315+
}
1316+
1317+
func TestParserErrorRecoveryLimit(t *testing.T) {
1318+
env, err := NewEnv(ParserErrorRecoveryLimit(10))
1319+
if err != nil {
1320+
t.Fatalf("NewEnv(ParserErrorRecoveryLimit(10)) failed: %v", err)
1321+
}
1322+
if env.limits[limitParseErrorRecovery] != 10 {
1323+
t.Errorf("limitParseErrorRecovery = %d, want 10", env.limits[limitParseErrorRecovery])
1324+
}
1325+
}
1326+
1327+
func TestEnableHiddenAccumulatorName(t *testing.T) {
1328+
_, err := NewEnv(EnableHiddenAccumulatorName(true))
1329+
if err != nil {
1330+
t.Fatalf("NewEnv(EnableHiddenAccumulatorName(true)) failed: %v", err)
1331+
}
1332+
}
1333+
1334+
func TestDeclareContextProto_Duplicate(t *testing.T) {
1335+
desc := (&proto3pb.TestAllTypes{}).ProtoReflect().Descriptor()
1336+
env, err := NewEnv(DeclareContextProto(desc))
1337+
if err != nil {
1338+
t.Fatalf("NewEnv(DeclareContextProto()) failed: %v", err)
1339+
}
1340+
_, err = DeclareContextProto(desc)(env)
1341+
if err == nil {
1342+
t.Error("DeclareContextProto() twice should fail")
1343+
}
1344+
}

cel/folding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (opt *constantFoldingOptimizer) Optimize(ctx *OptimizerContext, a *ast.AST)
9797
continue
9898
}
9999
// Late-bound function calls cannot be folded.
100-
if fold.Kind() == ast.CallKind && isLateBoundFunctionCall(ctx, a, fold) {
100+
if fold.Kind() == ast.CallKind && isLateBoundFunctionCall(ctx, fold) {
101101
continue
102102
}
103103
// Otherwise, assume all context is needed to evaluate the expression.
@@ -168,7 +168,7 @@ func (opt *constantFoldingOptimizer) tryFold(ctx *OptimizerContext, a *ast.AST,
168168
return nil
169169
}
170170

171-
func isLateBoundFunctionCall(ctx *OptimizerContext, a *ast.AST, expr ast.Expr) bool {
171+
func isLateBoundFunctionCall(ctx *OptimizerContext, expr ast.Expr) bool {
172172
call := expr.AsCall()
173173
function := ctx.Functions()[call.FunctionName()]
174174
if function == nil {
@@ -518,7 +518,7 @@ func (opt *constantFoldingOptimizer) constantExprMatcher(ctx *OptimizerContext,
518518
constantExprs = false
519519
}
520520
// Late-bound function calls cannot be folded.
521-
if e.Kind() == ast.CallKind && isLateBoundFunctionCall(ctx, a, e) {
521+
if e.Kind() == ast.CallKind && isLateBoundFunctionCall(ctx, e) {
522522
constantExprs = false
523523
}
524524
})

cel/io_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,32 @@ func TestExpressionNestingDepthLimitConfigRoundTrip(t *testing.T) {
420420
t.Errorf("env config limits %v missing 'cel.limit.max_ast_depth'", conf.Limits)
421421
}
422422
}
423+
424+
func TestRefValueToValue_Error(t *testing.T) {
425+
_, err := RefValueToValue(types.NewErr("test error"))
426+
if err == nil {
427+
t.Error("RefValueToValue(err) should return error")
428+
}
429+
}
430+
431+
func TestExprValueAsAlphaProto(t *testing.T) {
432+
// Exercise the ExprValueAsAlphaProto wrapper (0% coverage)
433+
res, err := ExprValueAsAlphaProto(types.Int(42))
434+
if err != nil {
435+
t.Fatalf("ExprValueAsAlphaProto() failed: %v", err)
436+
}
437+
if res.GetValue() == nil {
438+
t.Error("ExprValueAsAlphaProto() returned nil value")
439+
}
440+
}
441+
442+
func TestRefValToExprValue_Wrappers(t *testing.T) {
443+
// Exercise the RefValToExprValue wrapper (0% coverage)
444+
res, err := RefValToExprValue(types.String("hello"))
445+
if err != nil {
446+
t.Fatalf("RefValToExprValue() failed: %v", err)
447+
}
448+
if res.GetValue() == nil {
449+
t.Error("RefValToExprValue() returned nil value")
450+
}
451+
}

0 commit comments

Comments
 (0)