Skip to content

Commit

Permalink
Add support for typed conformance tests. (#1089)
Browse files Browse the repository at this point in the history
* 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

* Bump cel-spec version in workspace
  • Loading branch information
jnthntatum authored Dec 10, 2024
1 parent 0091f8d commit aacca17
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 6 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 42 additions & 1 deletion conformance/conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion conformance/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions conformance/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit aacca17

Please sign in to comment.