| 1 |
/* |
| 2 |
Copyright 2022 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package cel |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/google/cel-go/common/types" |
| 23 |
"github.com/stretchr/testify/require" |
| 24 |
) |
| 25 |
|
| 26 |
func TestTypes_ListType(t *testing.T) { |
| 27 |
list := NewListType(StringType, -1) |
| 28 |
if !list.IsList() { |
| 29 |
t.Error("list type not identifiable as list") |
| 30 |
} |
| 31 |
if list.TypeName() != "list" { |
| 32 |
t.Errorf("got %s, wanted list", list.TypeName()) |
| 33 |
} |
| 34 |
if list.DefaultValue() == nil { |
| 35 |
t.Error("got nil zero value for list type") |
| 36 |
} |
| 37 |
if list.ElemType.TypeName() != "string" { |
| 38 |
t.Errorf("got %s, wanted elem type of string", list.ElemType.TypeName()) |
| 39 |
} |
| 40 |
expT, err := list.ExprType() |
| 41 |
if err != nil { |
| 42 |
t.Errorf("fail to get cel type: %s", err) |
| 43 |
} |
| 44 |
if expT.GetListType() == nil { |
| 45 |
t.Errorf("got %v, wanted CEL list type", expT) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestTypes_MapType(t *testing.T) { |
| 50 |
mp := NewMapType(StringType, IntType, -1) |
| 51 |
if !mp.IsMap() { |
| 52 |
t.Error("map type not identifiable as map") |
| 53 |
} |
| 54 |
if mp.TypeName() != "map" { |
| 55 |
t.Errorf("got %s, wanted map", mp.TypeName()) |
| 56 |
} |
| 57 |
if mp.DefaultValue() == nil { |
| 58 |
t.Error("got nil zero value for map type") |
| 59 |
} |
| 60 |
if mp.KeyType.TypeName() != "string" { |
| 61 |
t.Errorf("got %s, wanted key type of string", mp.KeyType.TypeName()) |
| 62 |
} |
| 63 |
if mp.ElemType.TypeName() != "int" { |
| 64 |
t.Errorf("got %s, wanted elem type of int", mp.ElemType.TypeName()) |
| 65 |
} |
| 66 |
expT, err := mp.ExprType() |
| 67 |
if err != nil { |
| 68 |
t.Errorf("fail to get cel type: %s", err) |
| 69 |
} |
| 70 |
if expT.GetMapType() == nil { |
| 71 |
t.Errorf("got %v, wanted CEL map type", expT) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func testValue(t *testing.T, id int64, val interface{}) *DynValue { |
| 76 |
t.Helper() |
| 77 |
dv, err := NewDynValue(id, val) |
| 78 |
if err != nil { |
| 79 |
t.Fatalf("NewDynValue(%d, %v) failed: %v", id, val, err) |
| 80 |
} |
| 81 |
return dv |
| 82 |
} |
| 83 |
|
| 84 |
func TestDeclTypeProvider_FindStructType(t *testing.T) { |
| 85 |
obj := NewObjectType("foo", map[string]*DeclField{ |
| 86 |
"bar": NewDeclField("bar", StringType, true, nil, nil), |
| 87 |
}) |
| 88 |
base := types.NewEmptyRegistry() |
| 89 |
provider := NewDeclTypeProvider(obj) |
| 90 |
provider, err := provider.WithTypeProvider(base) |
| 91 |
require.NoError(t, err) |
| 92 |
wrappedType, found := provider.FindStructType("foo") |
| 93 |
require.True(t, found) |
| 94 |
require.Equal(t, types.TypeKind, wrappedType.Kind()) |
| 95 |
} |