convert the expectedValues to be cel.Val.

This commit is contained in:
Jiahui Feng 2024-01-25 13:52:39 -08:00
parent d699163802
commit c89dcf52b1

View File

@ -17,9 +17,13 @@ limitations under the License.
package mutation package mutation
import ( import (
"reflect"
"strings" "strings"
"testing" "testing"
celtypes "github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"k8s.io/apiserver/pkg/cel/mutation/common"
) )
// TestCELOptional is an exploration test to demonstrate how CEL optional library // TestCELOptional is an exploration test to demonstrate how CEL optional library
@ -28,7 +32,7 @@ func TestCELOptional(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
name string name string
expression string expression string
expectedValue any expectedVal ref.Val
expectedCompileError string expectedCompileError string
}{ }{
{ {
@ -59,16 +63,16 @@ func TestCELOptional(t *testing.T) {
expression: `Object{ expression: `Object{
?existing: optional.none() ?existing: optional.none()
}`, }`,
expectedValue: map[string]any{ expectedVal: common.NewObjectVal(nil, map[string]ref.Val{
// "existing" field was not set. // "existing" field was not set.
}, }),
}, },
{ {
name: "object of zero value, ofNonZeroValue", name: "object of zero value, ofNonZeroValue",
expression: `Object{?spec: optional.ofNonZeroValue(Object.spec{?replicas: Object{}.?replicas})}`, expression: `Object{?spec: optional.ofNonZeroValue(Object.spec{?replicas: Object{}.?replicas})}`,
expectedValue: map[string]any{ expectedVal: common.NewObjectVal(nil, map[string]ref.Val{
// "spec" field was not set. // "existing" field was not set.
}, }),
}, },
{ {
name: "access non-existing field, return none", name: "access non-existing field, return none",
@ -76,21 +80,19 @@ func TestCELOptional(t *testing.T) {
expectedCompileError: `undefined field 'nonExisting'`, expectedCompileError: `undefined field 'nonExisting'`,
}, },
{ {
name: "access existing field, return none", name: "access existing field, return none",
expression: `Object{}.?existing`, expression: `Object{}.?existing`,
// types.OptionalNone.Value() is nil expectedVal: celtypes.OptionalNone,
expectedValue: nil,
}, },
{ {
name: "map non-existing field, return none", name: "map non-existing field, return none",
expression: `{"foo": 1}[?"bar"]`, expression: `{"foo": 1}[?"bar"]`,
// types.OptionalNone.Value() is nil expectedVal: celtypes.OptionalNone,
expectedValue: nil,
}, },
{ {
name: "map existing field, return actual value", name: "map existing field, return actual value",
expression: `{"foo": 1}[?"foo"]`, expression: `{"foo": 1}[?"foo"]`,
expectedValue: int64(1), // CEL int converts into int64 expectedVal: celtypes.OptionalOf(celtypes.Int(1)),
}, },
{ {
// Map has a different behavior than Object // Map has a different behavior than Object
@ -105,14 +107,14 @@ func TestCELOptional(t *testing.T) {
// //
name: "has on a map, de-sugared, non-existing field, returns false", name: "has on a map, de-sugared, non-existing field, returns false",
// has marco supports only the dot access syntax. // has marco supports only the dot access syntax.
expression: `has({"foo": 1}.bar)`, expression: `has({"foo": 1}.bar)`,
expectedValue: false, expectedVal: celtypes.False,
}, },
{ {
name: "has on a map, de-sugared, existing field, returns true", name: "has on a map, de-sugared, existing field, returns true",
// has marco supports only the dot access syntax. // has marco supports only the dot access syntax.
expression: `has({"foo": 1}.foo)`, expression: `has({"foo": 1}.foo)`,
expectedValue: true, expectedVal: celtypes.True,
}, },
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
@ -137,8 +139,8 @@ func TestCELOptional(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error during evaluation: %v", err) t.Fatalf("unexpected error during evaluation: %v", err)
} }
if v := r.Value(); !reflect.DeepEqual(tc.expectedValue, v) { if equals := tc.expectedVal.Equal(r); equals.Value() != true {
t.Errorf("expected %v but got %v", tc.expectedValue, v) t.Errorf("expected %v but got %v", tc.expectedVal, r)
} }
}) })
} }