From e6d843765fb600514fa22134d02a409c4eeb0cc1 Mon Sep 17 00:00:00 2001 From: Jonathan Tatum Date: Fri, 6 Dec 2024 20:07:00 +0000 Subject: [PATCH 1/2] Add support for typed conformance tests. - Update test runner to support typed tests - add conversion from native type to canonical proto rep - bump cel-spec version to 0.19.1 --- common/types/types.go | 13 ++++++++++ conformance/BUILD.bazel | 1 + conformance/conformance_test.go | 43 ++++++++++++++++++++++++++++++++- conformance/go.mod | 2 +- conformance/go.sum | 4 +++ go.mod | 2 +- go.sum | 4 +++ vendor/modules.txt | 2 +- 8 files changed, 67 insertions(+), 4 deletions(-) diff --git a/common/types/types.go b/common/types/types.go index 1c5b6c40c..f419beabd 100644 --- a/common/types/types.go +++ b/common/types/types.go @@ -768,6 +768,19 @@ func ProtoAsType(t *celpb.Type) (*Type, error) { } } +// TypeToProto converts from a CEL-native type representation to canonical CEL celpb.Type protobuf type. +func TypeToProto(t *Type) (*celpb.Type, error) { + exprType, err := TypeToExprType(t) + if err != nil { + return nil, err + } + var pbtype celpb.Type + if err = convertProto(exprType, &pbtype); err != nil { + return nil, err + } + return &pbtype, nil +} + func maybeWrapper(t *Type, pbType *exprpb.Type) *exprpb.Type { if t.IsAssignableType(NullType) { return chkdecls.NewWrapperType(pbType) diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel index a9630d4fa..38e395bca 100644 --- a/conformance/BUILD.bazel +++ b/conformance/BUILD.bazel @@ -30,6 +30,7 @@ _ALL_TESTS = [ "@dev_cel_expr//tests/simple:testdata/string.textproto", "@dev_cel_expr//tests/simple:testdata/string_ext.textproto", "@dev_cel_expr//tests/simple:testdata/timestamps.textproto", + "@dev_cel_expr//tests/simple:testdata/type_deduction.textproto", "@dev_cel_expr//tests/simple:testdata/unknowns.textproto", "@dev_cel_expr//tests/simple:testdata/wrappers.textproto", "@dev_cel_expr//tests/simple:testdata/block_ext.textproto", diff --git a/conformance/conformance_test.go b/conformance/conformance_test.go index b8b7388bb..2beb7fd5d 100644 --- a/conformance/conformance_test.go +++ b/conformance/conformance_test.go @@ -168,6 +168,23 @@ func exprValueToRefValue(adapter types.Adapter, ev *valuepb.ExprValue) (ref.Val, return nil, errors.New("unknown ExprValue kind") } +func diffType(want *valuepb.Type, t *cel.Type) (string, error) { + got, err := types.TypeToProto(t) + if err != nil { + return "", err + } + return cmp.Diff(want, got, protocmp.Transform()), nil + +} + +func diffValue(want *valuepb.Value, got *valuepb.ExprValue) string { + return cmp.Diff( + &valuepb.ExprValue{Kind: &valuepb.ExprValue_Value{Value: want}}, + got, + protocmp.Transform(), + protocmp.SortRepeatedFields(&valuepb.MapValue{}, "entries")) +} + func conformanceTest(t *testing.T, name string, pb *testpb.SimpleTest) { if shouldSkipTest(name) { t.SkipNow() @@ -206,6 +223,16 @@ func conformanceTest(t *testing.T, name string, pb *testpb.SimpleTest) { t.Fatal(err) } } + if pb.GetCheckOnly() { + m, ok := pb.GetResultMatcher().(*testpb.SimpleTest_TypedResult) + if !ok { + t.Fatalf("unexpected matcher kind for check only test: %T", pb.GetResultMatcher()) + } + if diff, err := diffType(m.TypedResult.DeducedType, ast.OutputType()); err != nil || diff != "" { + t.Errorf("env.Check() output type err: %v (-want +got):\n%s", err, diff) + } + return + } program, err := env.Program(ast) if err != nil { t.Fatal(err) @@ -227,9 +254,23 @@ func conformanceTest(t *testing.T, name string, pb *testpb.SimpleTest) { if err != nil { t.Fatal(err) } - if diff := cmp.Diff(&valuepb.ExprValue{Kind: &valuepb.ExprValue_Value{Value: m.Value}}, val, protocmp.Transform(), protocmp.SortRepeatedFields(&valuepb.MapValue{}, "entries")); diff != "" { + if diff := diffValue(m.Value, val); diff != "" { t.Errorf("program.Eval() diff (-want +got):\n%s", diff) } + case *testpb.SimpleTest_TypedResult: + if err != nil { + t.Fatalf("program.Eval(): got %v, want nil", err) + } + val, err := refValueToExprValue(ret) + if err != nil { + t.Fatal(err) + } + if diff := diffValue(m.TypedResult.Result, val); diff != "" { + t.Errorf("program.Eval() diff (-want +got):\n%s", diff) + } + if diff, err := diffType(m.TypedResult.DeducedType, ast.OutputType()); err != nil || diff != "" { + t.Errorf("env.Check() output type err: %v (-want +got):\n%s", err, diff) + } case *testpb.SimpleTest_EvalError: if err == nil && types.IsError(ret) { err = ret.(*types.Err).Unwrap() diff --git a/conformance/go.mod b/conformance/go.mod index 0dcf43aa6..65aa6edd6 100644 --- a/conformance/go.mod +++ b/conformance/go.mod @@ -3,7 +3,7 @@ module github.com/google/cel-go/conformance go 1.21.1 require ( - cel.dev/expr v0.18.0 + cel.dev/expr v0.19.1 github.com/bazelbuild/rules_go v0.49.0 github.com/google/cel-go v0.21.0 github.com/google/go-cmp v0.6.0 diff --git a/conformance/go.sum b/conformance/go.sum index baae06f82..8e4f44922 100644 --- a/conformance/go.sum +++ b/conformance/go.sum @@ -1,5 +1,9 @@ cel.dev/expr v0.18.0 h1:CJ6drgk+Hf96lkLikr4rFf19WrU0BOWEihyZnI2TAzo= cel.dev/expr v0.18.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw= +cel.dev/expr v0.19.0 h1:lXuo+nDhpyJSpWxpPVi5cPUwzKb+dsdOiw6IreM5yt0= +cel.dev/expr v0.19.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw= +cel.dev/expr v0.19.1 h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4= +cel.dev/expr v0.19.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw= github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI= github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g= github.com/bazelbuild/rules_go v0.49.0 h1:5vCbuvy8Q11g41lseGJDc5vxhDjJtfxr6nM/IC4VmqM= diff --git a/go.mod b/go.mod index b8393842b..7b976324f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21.1 toolchain go1.23.0 require ( - cel.dev/expr v0.18.0 + cel.dev/expr v0.19.1 github.com/antlr4-go/antlr/v4 v4.13.0 github.com/stoewer/go-strcase v1.2.0 golang.org/x/text v0.16.0 diff --git a/go.sum b/go.sum index 83faae3c2..fdf55b76d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ cel.dev/expr v0.18.0 h1:CJ6drgk+Hf96lkLikr4rFf19WrU0BOWEihyZnI2TAzo= cel.dev/expr v0.18.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw= +cel.dev/expr v0.19.0 h1:lXuo+nDhpyJSpWxpPVi5cPUwzKb+dsdOiw6IreM5yt0= +cel.dev/expr v0.19.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw= +cel.dev/expr v0.19.1 h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4= +cel.dev/expr v0.19.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw= github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI= github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= diff --git a/vendor/modules.txt b/vendor/modules.txt index 68a6e2ba4..f1e3ce32c 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# cel.dev/expr v0.18.0 +# cel.dev/expr v0.19.1 ## explicit; go 1.21.1 cel.dev/expr # github.com/antlr4-go/antlr/v4 v4.13.0 From a5795314370e6729028c8a81b26ada823cef43ab Mon Sep 17 00:00:00 2001 From: Jonathan Tatum Date: Sat, 7 Dec 2024 00:06:57 +0000 Subject: [PATCH 2/2] Bump cel-spec version in workspace --- WORKSPACE | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 7e4b0bd81..133a6b8e3 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -101,8 +101,8 @@ go_repository( go_repository( name = "dev_cel_expr", importpath = "cel.dev/expr", - sum = "h1:CJ6drgk+Hf96lkLikr4rFf19WrU0BOWEihyZnI2TAzo=", - version = "v0.18.0", + sum = "h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4=", + version = "v0.19.1", ) # local_repository(