| 1 |
// Copyright 2023 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package types |
| 16 |
|
| 17 |
import ( |
| 18 |
"reflect" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"google.golang.org/protobuf/proto" |
| 24 |
|
| 25 |
chkdecls "github.com/google/cel-go/checker/decls" |
| 26 |
"github.com/google/cel-go/common/types/traits" |
| 27 |
|
| 28 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 29 |
) |
| 30 |
|
| 31 |
func TestTypeString(t *testing.T) { |
| 32 |
tests := []struct { |
| 33 |
in *Type |
| 34 |
out string |
| 35 |
}{ |
| 36 |
{ |
| 37 |
in: NewListType(IntType), |
| 38 |
out: "list(int)", |
| 39 |
}, |
| 40 |
{ |
| 41 |
in: NewMapType(UintType, DoubleType), |
| 42 |
out: "map(uint, double)", |
| 43 |
}, |
| 44 |
{ |
| 45 |
in: BoolType, |
| 46 |
out: "bool", |
| 47 |
}, |
| 48 |
{ |
| 49 |
in: DynType, |
| 50 |
out: "dyn", |
| 51 |
}, |
| 52 |
{ |
| 53 |
in: NullType, |
| 54 |
out: "null_type", |
| 55 |
}, |
| 56 |
{ |
| 57 |
in: NewNullableType(BoolType), |
| 58 |
out: "wrapper(bool)", |
| 59 |
}, |
| 60 |
{ |
| 61 |
in: NewOptionalType(NewListType(StringType)), |
| 62 |
out: "optional_type(list(string))", |
| 63 |
}, |
| 64 |
{ |
| 65 |
in: NewObjectType("my.type.Message"), |
| 66 |
out: "my.type.Message", |
| 67 |
}, |
| 68 |
{ |
| 69 |
in: NewObjectType("google.protobuf.Int32Value"), |
| 70 |
out: "wrapper(int)", |
| 71 |
}, |
| 72 |
{ |
| 73 |
in: NewObjectType("google.protobuf.UInt32Value"), |
| 74 |
out: "wrapper(uint)", |
| 75 |
}, |
| 76 |
{ |
| 77 |
in: NewObjectType("google.protobuf.Value"), |
| 78 |
out: "dyn", |
| 79 |
}, |
| 80 |
{ |
| 81 |
in: NewTypeTypeWithParam(StringType), |
| 82 |
out: "type(string)", |
| 83 |
}, |
| 84 |
{ |
| 85 |
in: NewTypeParamType("T"), |
| 86 |
out: "<T>", |
| 87 |
}, |
| 88 |
// nil-safety tests |
| 89 |
{ |
| 90 |
in: nil, |
| 91 |
out: "", |
| 92 |
}, |
| 93 |
{ |
| 94 |
in: ListType, |
| 95 |
out: "list(dyn)", |
| 96 |
}, |
| 97 |
{ |
| 98 |
in: MapType, |
| 99 |
out: "map(dyn, dyn)", |
| 100 |
}, |
| 101 |
} |
| 102 |
for _, tst := range tests { |
| 103 |
if tst.in.String() != tst.out { |
| 104 |
t.Errorf("String() got %v, wanted %v", tst.in, tst.out) |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestTypeIsExactType(t *testing.T) { |
| 110 |
tests := []struct { |
| 111 |
t1 *Type |
| 112 |
t2 *Type |
| 113 |
isExact bool |
| 114 |
}{ |
| 115 |
{ |
| 116 |
t1: StringType, |
| 117 |
t2: StringType, |
| 118 |
isExact: true, |
| 119 |
}, |
| 120 |
{ |
| 121 |
t1: StringType, |
| 122 |
t2: IntType, |
| 123 |
isExact: false, |
| 124 |
}, |
| 125 |
{ |
| 126 |
t1: NewOptionalType(StringType), |
| 127 |
t2: NewOptionalType(IntType), |
| 128 |
isExact: false, |
| 129 |
}, |
| 130 |
{ |
| 131 |
t1: NewOptionalType(UintType), |
| 132 |
t2: NewOptionalType(UintType), |
| 133 |
isExact: true, |
| 134 |
}, |
| 135 |
{ |
| 136 |
t1: NewOptionalType(NewTypeParamType("T")), |
| 137 |
t2: NewOptionalType(NewTypeParamType("T")), |
| 138 |
isExact: true, |
| 139 |
}, |
| 140 |
{ |
| 141 |
t1: NewMapType(BoolType, IntType), |
| 142 |
t2: NewMapType(BoolType, IntType), |
| 143 |
isExact: true, |
| 144 |
}, |
| 145 |
{ |
| 146 |
t1: NewMapType(NewTypeParamType("K1"), IntType), |
| 147 |
t2: NewMapType(NewTypeParamType("K2"), IntType), |
| 148 |
isExact: false, |
| 149 |
}, |
| 150 |
{ |
| 151 |
t1: NewMapType(NewTypeParamType("K1"), NewObjectType("my.msg.First")), |
| 152 |
t2: NewMapType(NewTypeParamType("K2"), NewObjectType("my.msg.Last")), |
| 153 |
isExact: false, |
| 154 |
}, |
| 155 |
} |
| 156 |
for _, tst := range tests { |
| 157 |
if tst.t1.IsExactType(tst.t2) != tst.isExact { |
| 158 |
t.Errorf("%v.IsExactsType(%v) got %v, wanted %v", tst.t1, tst.t2, !tst.isExact, tst.isExact) |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func TestTypeIsEquivalentType(t *testing.T) { |
| 164 |
tests := []struct { |
| 165 |
t1 *Type |
| 166 |
t2 *Type |
| 167 |
isEquivalent bool |
| 168 |
}{ |
| 169 |
{ |
| 170 |
t1: StringType, |
| 171 |
t2: StringType, |
| 172 |
isEquivalent: true, |
| 173 |
}, |
| 174 |
{ |
| 175 |
t1: StringType, |
| 176 |
t2: IntType, |
| 177 |
isEquivalent: false, |
| 178 |
}, |
| 179 |
{ |
| 180 |
t1: NewOptionalType(StringType), |
| 181 |
t2: NewOptionalType(IntType), |
| 182 |
isEquivalent: false, |
| 183 |
}, |
| 184 |
{ |
| 185 |
t1: NewOptionalType(UintType), |
| 186 |
t2: NewOptionalType(UintType), |
| 187 |
isEquivalent: true, |
| 188 |
}, |
| 189 |
{ |
| 190 |
t1: NewMapType(BoolType, IntType), |
| 191 |
t2: NewMapType(BoolType, IntType), |
| 192 |
isEquivalent: true, |
| 193 |
}, |
| 194 |
{ |
| 195 |
t1: NewMapType(NewTypeParamType("K1"), IntType), |
| 196 |
t2: NewMapType(NewTypeParamType("K2"), IntType), |
| 197 |
isEquivalent: true, |
| 198 |
}, |
| 199 |
{ |
| 200 |
t1: NewMapType(NewTypeParamType("K1"), NewObjectType("my.msg.First")), |
| 201 |
t2: NewMapType(NewTypeParamType("K2"), NewObjectType("my.msg.Last")), |
| 202 |
isEquivalent: false, |
| 203 |
}, |
| 204 |
} |
| 205 |
for _, tst := range tests { |
| 206 |
if tst.t1.IsEquivalentType(tst.t2) != tst.isEquivalent { |
| 207 |
t.Errorf("%v.IsEquivalentType(%v) got %v, wanted %v", tst.t1, tst.t2, !tst.isEquivalent, tst.isEquivalent) |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func TestTypeIsAssignableType(t *testing.T) { |
| 213 |
tests := []struct { |
| 214 |
t1 *Type |
| 215 |
t2 *Type |
| 216 |
isAssignable bool |
| 217 |
}{ |
| 218 |
{ |
| 219 |
t1: NewNullableType(DoubleType), |
| 220 |
t2: NullType, |
| 221 |
isAssignable: true, |
| 222 |
}, |
| 223 |
{ |
| 224 |
t1: NewNullableType(DoubleType), |
| 225 |
t2: DoubleType, |
| 226 |
isAssignable: true, |
| 227 |
}, |
| 228 |
{ |
| 229 |
t1: NewOpaqueType("vector", NewNullableType(DoubleType)), |
| 230 |
t2: NewOpaqueType("vector", NullType), |
| 231 |
isAssignable: true, |
| 232 |
}, |
| 233 |
{ |
| 234 |
t1: NewOpaqueType("vector", NewNullableType(DoubleType)), |
| 235 |
t2: NewOpaqueType("vector", DoubleType), |
| 236 |
isAssignable: true, |
| 237 |
}, |
| 238 |
{ |
| 239 |
t1: NewOpaqueType("vector", DynType), |
| 240 |
t2: NewOpaqueType("vector", NewNullableType(IntType)), |
| 241 |
isAssignable: true, |
| 242 |
}, |
| 243 |
{ |
| 244 |
t1: NewObjectType("my.msg.MsgName"), |
| 245 |
t2: NewObjectType("my.msg.MsgName"), |
| 246 |
isAssignable: true, |
| 247 |
}, |
| 248 |
{ |
| 249 |
t1: NewMapType(NewTypeParamType("K"), IntType), |
| 250 |
t2: NewMapType(StringType, IntType), |
| 251 |
isAssignable: true, |
| 252 |
}, |
| 253 |
{ |
| 254 |
t1: NewMapType(StringType, IntType), |
| 255 |
t2: NewMapType(NewTypeParamType("K"), IntType), |
| 256 |
isAssignable: false, |
| 257 |
}, |
| 258 |
{ |
| 259 |
t1: NewOpaqueType("vector", DoubleType), |
| 260 |
t2: NewOpaqueType("vector", NewNullableType(IntType)), |
| 261 |
isAssignable: false, |
| 262 |
}, |
| 263 |
{ |
| 264 |
t1: NewOpaqueType("vector", NewNullableType(DoubleType)), |
| 265 |
t2: NewOpaqueType("vector", DynType), |
| 266 |
isAssignable: false, |
| 267 |
}, |
| 268 |
{ |
| 269 |
t1: NewObjectType("my.msg.MsgName"), |
| 270 |
t2: NewObjectType("my.msg.MsgName2"), |
| 271 |
isAssignable: false, |
| 272 |
}, |
| 273 |
} |
| 274 |
for _, tst := range tests { |
| 275 |
if tst.t1.IsAssignableType(tst.t2) != tst.isAssignable { |
| 276 |
t.Errorf("%v.IsAssignableType(%v) got %v, wanted %v", tst.t1, tst.t2, !tst.isAssignable, tst.isAssignable) |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func TestTypeIsAssignableRuntimeType(t *testing.T) { |
| 282 |
tests := []struct { |
| 283 |
t *Type |
| 284 |
v any |
| 285 |
isRuntimeAssignable bool |
| 286 |
}{ |
| 287 |
{ |
| 288 |
t: NewNullableType(DoubleType), |
| 289 |
v: NullValue, |
| 290 |
isRuntimeAssignable: true, |
| 291 |
}, |
| 292 |
{ |
| 293 |
t: NewNullableType(DoubleType), |
| 294 |
v: 0.0, |
| 295 |
isRuntimeAssignable: true, |
| 296 |
}, |
| 297 |
{ |
| 298 |
t: NewListType(StringType), |
| 299 |
v: map[string]time.Duration{}, |
| 300 |
isRuntimeAssignable: false, |
| 301 |
}, |
| 302 |
{ |
| 303 |
t: NewListType(StringType), |
| 304 |
v: []time.Duration{}, |
| 305 |
isRuntimeAssignable: true, |
| 306 |
}, |
| 307 |
{ |
| 308 |
t: NewListType(StringType), |
| 309 |
v: []time.Duration{time.Duration(1)}, |
| 310 |
isRuntimeAssignable: false, |
| 311 |
}, |
| 312 |
{ |
| 313 |
t: NewListType(StringType), |
| 314 |
v: []string{"hello"}, |
| 315 |
isRuntimeAssignable: true, |
| 316 |
}, |
| 317 |
{ |
| 318 |
t: NewMapType(StringType, DurationType), |
| 319 |
v: map[string]time.Duration{}, |
| 320 |
isRuntimeAssignable: true, |
| 321 |
}, |
| 322 |
{ |
| 323 |
t: NewMapType(StringType, DurationType), |
| 324 |
v: map[string]string{}, |
| 325 |
isRuntimeAssignable: true, |
| 326 |
}, |
| 327 |
{ |
| 328 |
t: NewMapType(StringType, DurationType), |
| 329 |
v: map[string]time.Duration{"one": time.Duration(1)}, |
| 330 |
isRuntimeAssignable: true, |
| 331 |
}, |
| 332 |
{ |
| 333 |
t: NewMapType(StringType, DynType), |
| 334 |
v: map[string]time.Duration{"one": time.Duration(1)}, |
| 335 |
isRuntimeAssignable: true, |
| 336 |
}, |
| 337 |
{ |
| 338 |
t: NewMapType(DynType, DynType), |
| 339 |
v: map[string]time.Duration{"one": time.Duration(1)}, |
| 340 |
isRuntimeAssignable: true, |
| 341 |
}, |
| 342 |
{ |
| 343 |
t: NewMapType(DynType, DynType), |
| 344 |
v: map[string]time.Duration{}, |
| 345 |
isRuntimeAssignable: true, |
| 346 |
}, |
| 347 |
{ |
| 348 |
t: NewMapType(StringType, DurationType), |
| 349 |
v: map[string]int32{"one": 1}, |
| 350 |
isRuntimeAssignable: false, |
| 351 |
}, |
| 352 |
} |
| 353 |
for _, tst := range tests { |
| 354 |
val := DefaultTypeAdapter.NativeToValue(tst.v) |
| 355 |
if tst.t.IsAssignableRuntimeType(val) != tst.isRuntimeAssignable { |
| 356 |
t.Errorf("%v.IsAssignableRuntimeType(%v) got %v, wanted %v", tst.t, val, !tst.isRuntimeAssignable, tst.isRuntimeAssignable) |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
func TestTypeToExprType(t *testing.T) { |
| 362 |
tests := []struct { |
| 363 |
in *Type |
| 364 |
out *exprpb.Type |
| 365 |
unidirectional bool |
| 366 |
}{ |
| 367 |
{ |
| 368 |
in: NewOpaqueType("vector", DoubleType, DoubleType), |
| 369 |
out: chkdecls.NewAbstractType("vector", chkdecls.Double, chkdecls.Double), |
| 370 |
}, |
| 371 |
{ |
| 372 |
in: AnyType, |
| 373 |
out: chkdecls.Any, |
| 374 |
}, |
| 375 |
{ |
| 376 |
in: BoolType, |
| 377 |
out: chkdecls.Bool, |
| 378 |
}, |
| 379 |
{ |
| 380 |
in: BytesType, |
| 381 |
out: chkdecls.Bytes, |
| 382 |
}, |
| 383 |
{ |
| 384 |
in: DoubleType, |
| 385 |
out: chkdecls.Double, |
| 386 |
}, |
| 387 |
{ |
| 388 |
in: DurationType, |
| 389 |
out: chkdecls.Duration, |
| 390 |
}, |
| 391 |
{ |
| 392 |
in: DynType, |
| 393 |
out: chkdecls.Dyn, |
| 394 |
}, |
| 395 |
{ |
| 396 |
in: IntType, |
| 397 |
out: chkdecls.Int, |
| 398 |
}, |
| 399 |
{ |
| 400 |
in: NewListType(NewTypeParamType("T")), |
| 401 |
out: chkdecls.NewListType(chkdecls.NewTypeParamType("T")), |
| 402 |
}, |
| 403 |
{ |
| 404 |
in: NewMapType(NewTypeParamType("K"), NewTypeParamType("V")), |
| 405 |
out: chkdecls.NewMapType(chkdecls.NewTypeParamType("K"), chkdecls.NewTypeParamType("V")), |
| 406 |
}, |
| 407 |
{ |
| 408 |
in: NullType, |
| 409 |
out: chkdecls.Null, |
| 410 |
}, |
| 411 |
{ |
| 412 |
in: NewObjectType("google.type.Expr"), |
| 413 |
out: chkdecls.NewObjectType("google.type.Expr"), |
| 414 |
}, |
| 415 |
{ |
| 416 |
in: StringType, |
| 417 |
out: chkdecls.String, |
| 418 |
}, |
| 419 |
{ |
| 420 |
in: TimestampType, |
| 421 |
out: chkdecls.Timestamp, |
| 422 |
}, |
| 423 |
{ |
| 424 |
in: TypeType, |
| 425 |
out: chkdecls.NewTypeType(nil), |
| 426 |
}, |
| 427 |
{ |
| 428 |
in: UintType, |
| 429 |
out: chkdecls.Uint, |
| 430 |
}, |
| 431 |
{ |
| 432 |
in: NewNullableType(BoolType), |
| 433 |
out: chkdecls.NewWrapperType(chkdecls.Bool), |
| 434 |
}, |
| 435 |
{ |
| 436 |
in: NewNullableType(BytesType), |
| 437 |
out: chkdecls.NewWrapperType(chkdecls.Bytes), |
| 438 |
}, |
| 439 |
{ |
| 440 |
in: NewNullableType(DoubleType), |
| 441 |
out: chkdecls.NewWrapperType(chkdecls.Double), |
| 442 |
}, |
| 443 |
{ |
| 444 |
in: NewNullableType(IntType), |
| 445 |
out: chkdecls.NewWrapperType(chkdecls.Int), |
| 446 |
}, |
| 447 |
{ |
| 448 |
in: NewNullableType(StringType), |
| 449 |
out: chkdecls.NewWrapperType(chkdecls.String), |
| 450 |
}, |
| 451 |
{ |
| 452 |
in: NewNullableType(UintType), |
| 453 |
out: chkdecls.NewWrapperType(chkdecls.Uint), |
| 454 |
}, |
| 455 |
{ |
| 456 |
in: NewTypeTypeWithParam(NewTypeTypeWithParam(DynType)), |
| 457 |
out: chkdecls.NewTypeType(chkdecls.NewTypeType(chkdecls.Dyn)), |
| 458 |
}, |
| 459 |
{ |
| 460 |
in: ErrorType, |
| 461 |
out: chkdecls.Error, |
| 462 |
}, |
| 463 |
{ |
| 464 |
in: NewObjectType("google.protobuf.Any"), |
| 465 |
out: chkdecls.Any, |
| 466 |
unidirectional: true, |
| 467 |
}, |
| 468 |
{ |
| 469 |
in: NewObjectType("google.protobuf.Duration"), |
| 470 |
out: chkdecls.Duration, |
| 471 |
unidirectional: true, |
| 472 |
}, |
| 473 |
{ |
| 474 |
in: NewObjectType("google.protobuf.Timestamp"), |
| 475 |
out: chkdecls.Timestamp, |
| 476 |
unidirectional: true, |
| 477 |
}, |
| 478 |
{ |
| 479 |
in: NewObjectType("google.protobuf.Value"), |
| 480 |
out: chkdecls.Dyn, |
| 481 |
unidirectional: true, |
| 482 |
}, |
| 483 |
{ |
| 484 |
in: NewObjectType("google.protobuf.ListValue"), |
| 485 |
out: chkdecls.NewListType(chkdecls.Dyn), |
| 486 |
unidirectional: true, |
| 487 |
}, |
| 488 |
{ |
| 489 |
in: NewObjectType("google.protobuf.Struct"), |
| 490 |
out: chkdecls.NewMapType(chkdecls.String, chkdecls.Dyn), |
| 491 |
unidirectional: true, |
| 492 |
}, |
| 493 |
{ |
| 494 |
in: NewObjectType("google.protobuf.BoolValue"), |
| 495 |
out: chkdecls.NewWrapperType(chkdecls.Bool), |
| 496 |
unidirectional: true, |
| 497 |
}, |
| 498 |
{ |
| 499 |
in: NewObjectType("google.protobuf.BytesValue"), |
| 500 |
out: chkdecls.NewWrapperType(chkdecls.Bytes), |
| 501 |
unidirectional: true, |
| 502 |
}, |
| 503 |
{ |
| 504 |
in: NewObjectType("google.protobuf.DoubleValue"), |
| 505 |
out: chkdecls.NewWrapperType(chkdecls.Double), |
| 506 |
unidirectional: true, |
| 507 |
}, |
| 508 |
{ |
| 509 |
in: NewObjectType("google.protobuf.FloatValue"), |
| 510 |
out: chkdecls.NewWrapperType(chkdecls.Double), |
| 511 |
unidirectional: true, |
| 512 |
}, |
| 513 |
{ |
| 514 |
in: NewObjectType("google.protobuf.Int32Value"), |
| 515 |
out: chkdecls.NewWrapperType(chkdecls.Int), |
| 516 |
unidirectional: true, |
| 517 |
}, |
| 518 |
{ |
| 519 |
in: NewObjectType("google.protobuf.Int64Value"), |
| 520 |
out: chkdecls.NewWrapperType(chkdecls.Int), |
| 521 |
unidirectional: true, |
| 522 |
}, |
| 523 |
{ |
| 524 |
in: NewObjectType("google.protobuf.StringValue"), |
| 525 |
out: chkdecls.NewWrapperType(chkdecls.String), |
| 526 |
unidirectional: true, |
| 527 |
}, |
| 528 |
{ |
| 529 |
in: NewObjectType("google.protobuf.UInt32Value"), |
| 530 |
out: chkdecls.NewWrapperType(chkdecls.Uint), |
| 531 |
unidirectional: true, |
| 532 |
}, |
| 533 |
{ |
| 534 |
in: NewObjectType("google.protobuf.UInt64Value"), |
| 535 |
out: chkdecls.NewWrapperType(chkdecls.Uint), |
| 536 |
unidirectional: true, |
| 537 |
}, |
| 538 |
{ |
| 539 |
in: ErrorType, |
| 540 |
out: chkdecls.Error, |
| 541 |
unidirectional: true, |
| 542 |
}, |
| 543 |
} |
| 544 |
|
| 545 |
for _, tc := range tests { |
| 546 |
tc := tc |
| 547 |
t.Run(tc.in.String(), func(t *testing.T) { |
| 548 |
got, err := TypeToExprType(tc.in) |
| 549 |
if err != nil { |
| 550 |
t.Fatalf("TypeToExprType(%v) failed: %v", tc.in, err) |
| 551 |
} |
| 552 |
if !proto.Equal(got, tc.out) { |
| 553 |
t.Errorf("TypeToExprType(%v) returned %v, wanted %v", tc.in, got, tc.out) |
| 554 |
} |
| 555 |
if tc.unidirectional { |
| 556 |
return |
| 557 |
} |
| 558 |
roundTrip, err := ExprTypeToType(got) |
| 559 |
if err != nil { |
| 560 |
t.Fatalf("ExprTypeToType(%v) failed: %v", got, err) |
| 561 |
} |
| 562 |
if !tc.in.IsEquivalentType(roundTrip) { |
| 563 |
t.Errorf("ExprTypeToType(%v) returned %v, wanted %v", got, roundTrip, tc.in) |
| 564 |
} |
| 565 |
}) |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
func TestTypeToExprTypeInvalid(t *testing.T) { |
| 570 |
tests := []struct { |
| 571 |
in *Type |
| 572 |
out string |
| 573 |
}{ |
| 574 |
{ |
| 575 |
in: &Type{kind: ListKind, runtimeTypeName: "list"}, |
| 576 |
out: "invalid list", |
| 577 |
}, |
| 578 |
{ |
| 579 |
in: &Type{ |
| 580 |
kind: ListKind, |
| 581 |
parameters: []*Type{ |
| 582 |
{kind: MapKind, runtimeTypeName: "map"}, |
| 583 |
}, |
| 584 |
runtimeTypeName: "list", |
| 585 |
}, |
| 586 |
out: "invalid map", |
| 587 |
}, |
| 588 |
{ |
| 589 |
in: &Type{kind: MapKind, runtimeTypeName: "map"}, |
| 590 |
out: "invalid map", |
| 591 |
}, |
| 592 |
{ |
| 593 |
in: &Type{ |
| 594 |
kind: MapKind, |
| 595 |
parameters: []*Type{ |
| 596 |
StringType, |
| 597 |
{kind: MapKind, runtimeTypeName: "map"}, |
| 598 |
}, |
| 599 |
runtimeTypeName: "map", |
| 600 |
}, |
| 601 |
out: "invalid map", |
| 602 |
}, |
| 603 |
{ |
| 604 |
in: &Type{ |
| 605 |
kind: MapKind, |
| 606 |
parameters: []*Type{ |
| 607 |
{kind: MapKind, runtimeTypeName: "map"}, |
| 608 |
StringType, |
| 609 |
}, |
| 610 |
runtimeTypeName: "map", |
| 611 |
}, |
| 612 |
out: "invalid map", |
| 613 |
}, |
| 614 |
{ |
| 615 |
in: &Type{ |
| 616 |
kind: TypeKind, |
| 617 |
parameters: []*Type{{kind: ListKind, runtimeTypeName: "list"}}, |
| 618 |
runtimeTypeName: "type", |
| 619 |
}, |
| 620 |
out: "invalid list", |
| 621 |
}, |
| 622 |
{ |
| 623 |
in: NewOpaqueType("bad_list", &Type{ |
| 624 |
kind: ListKind, |
| 625 |
runtimeTypeName: "list", |
| 626 |
}), |
| 627 |
out: "invalid list", |
| 628 |
}, |
| 629 |
{ |
| 630 |
in: &Type{}, |
| 631 |
out: "missing type conversion", |
| 632 |
}, |
| 633 |
} |
| 634 |
for _, tc := range tests { |
| 635 |
tc := tc |
| 636 |
t.Run(tc.in.String(), func(t *testing.T) { |
| 637 |
_, err := TypeToExprType(tc.in) |
| 638 |
if err == nil || !strings.Contains(err.Error(), tc.out) { |
| 639 |
t.Fatalf("TypeToExprType(%v) got %v, wanted error %v", tc.in, err, tc.out) |
| 640 |
} |
| 641 |
}) |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
func TestExprTypeToType(t *testing.T) { |
| 646 |
tests := []struct { |
| 647 |
in *exprpb.Type |
| 648 |
out *Type |
| 649 |
}{ |
| 650 |
{ |
| 651 |
in: chkdecls.NewObjectType("google.protobuf.Any"), |
| 652 |
out: AnyType, |
| 653 |
}, |
| 654 |
{ |
| 655 |
in: chkdecls.NewObjectType("google.protobuf.Duration"), |
| 656 |
out: DurationType, |
| 657 |
}, |
| 658 |
{ |
| 659 |
in: chkdecls.NewObjectType("google.protobuf.Timestamp"), |
| 660 |
out: TimestampType, |
| 661 |
}, |
| 662 |
{ |
| 663 |
in: chkdecls.NewObjectType("google.protobuf.Value"), |
| 664 |
out: DynType, |
| 665 |
}, |
| 666 |
{ |
| 667 |
in: chkdecls.NewObjectType("google.protobuf.ListValue"), |
| 668 |
out: NewListType(DynType), |
| 669 |
}, |
| 670 |
{ |
| 671 |
in: chkdecls.NewObjectType("google.protobuf.Struct"), |
| 672 |
out: NewMapType(StringType, DynType), |
| 673 |
}, |
| 674 |
{ |
| 675 |
in: chkdecls.NewObjectType("google.protobuf.BoolValue"), |
| 676 |
out: NewNullableType(BoolType), |
| 677 |
}, |
| 678 |
{ |
| 679 |
in: chkdecls.NewObjectType("google.protobuf.BytesValue"), |
| 680 |
out: NewNullableType(BytesType), |
| 681 |
}, |
| 682 |
{ |
| 683 |
in: chkdecls.NewObjectType("google.protobuf.DoubleValue"), |
| 684 |
out: NewNullableType(DoubleType), |
| 685 |
}, |
| 686 |
{ |
| 687 |
in: chkdecls.NewObjectType("google.protobuf.FloatValue"), |
| 688 |
out: NewNullableType(DoubleType), |
| 689 |
}, |
| 690 |
{ |
| 691 |
in: chkdecls.NewObjectType("google.protobuf.Int32Value"), |
| 692 |
out: NewNullableType(IntType), |
| 693 |
}, |
| 694 |
{ |
| 695 |
in: chkdecls.NewObjectType("google.protobuf.Int64Value"), |
| 696 |
out: NewNullableType(IntType), |
| 697 |
}, |
| 698 |
{ |
| 699 |
in: chkdecls.NewObjectType("google.protobuf.StringValue"), |
| 700 |
out: NewNullableType(StringType), |
| 701 |
}, |
| 702 |
{ |
| 703 |
in: chkdecls.NewObjectType("google.protobuf.UInt32Value"), |
| 704 |
out: NewNullableType(UintType), |
| 705 |
}, |
| 706 |
{ |
| 707 |
in: chkdecls.NewObjectType("google.protobuf.UInt64Value"), |
| 708 |
out: NewNullableType(UintType), |
| 709 |
}, |
| 710 |
{ |
| 711 |
in: chkdecls.Error, |
| 712 |
out: ErrorType, |
| 713 |
}, |
| 714 |
} |
| 715 |
|
| 716 |
for _, tc := range tests { |
| 717 |
tc := tc |
| 718 |
t.Run(tc.in.String(), func(t *testing.T) { |
| 719 |
got, err := ExprTypeToType(tc.in) |
| 720 |
if err != nil { |
| 721 |
t.Fatalf("ExprTypeToType(%v) failed: %v", tc.in, err) |
| 722 |
} |
| 723 |
if !got.IsEquivalentType(tc.out) { |
| 724 |
t.Errorf("ExprTypeToType(%v) returned %v, wanted %v", tc.in, got, tc.out) |
| 725 |
} |
| 726 |
}) |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
func TestExprTypeToTypeInvalid(t *testing.T) { |
| 731 |
tests := []struct { |
| 732 |
in *exprpb.Type |
| 733 |
out string |
| 734 |
}{ |
| 735 |
{ |
| 736 |
in: &exprpb.Type{}, |
| 737 |
out: "unsupported type", |
| 738 |
}, |
| 739 |
{ |
| 740 |
in: &exprpb.Type{TypeKind: &exprpb.Type_Primitive{}}, |
| 741 |
out: "unsupported primitive type", |
| 742 |
}, |
| 743 |
{ |
| 744 |
in: &exprpb.Type{TypeKind: &exprpb.Type_WellKnown{}}, |
| 745 |
out: "unsupported well-known type", |
| 746 |
}, |
| 747 |
{ |
| 748 |
in: chkdecls.NewListType(&exprpb.Type{}), |
| 749 |
out: "unsupported type", |
| 750 |
}, |
| 751 |
{ |
| 752 |
in: chkdecls.NewMapType(&exprpb.Type{}, chkdecls.Dyn), |
| 753 |
out: "unsupported type", |
| 754 |
}, |
| 755 |
{ |
| 756 |
in: chkdecls.NewMapType(chkdecls.Dyn, &exprpb.Type{}), |
| 757 |
out: "unsupported type", |
| 758 |
}, |
| 759 |
{ |
| 760 |
in: chkdecls.NewAbstractType("bad", &exprpb.Type{}), |
| 761 |
out: "unsupported type", |
| 762 |
}, |
| 763 |
{ |
| 764 |
in: &exprpb.Type{TypeKind: &exprpb.Type_Wrapper{}}, |
| 765 |
out: "unsupported primitive type", |
| 766 |
}, |
| 767 |
{ |
| 768 |
in: &exprpb.Type{TypeKind: &exprpb.Type_Type{Type: &exprpb.Type{TypeKind: &exprpb.Type_Function{}}}}, |
| 769 |
out: "unsupported type", |
| 770 |
}, |
| 771 |
} |
| 772 |
|
| 773 |
for _, tc := range tests { |
| 774 |
tc := tc |
| 775 |
t.Run(tc.in.String(), func(t *testing.T) { |
| 776 |
_, err := ExprTypeToType(tc.in) |
| 777 |
if err == nil || !strings.Contains(err.Error(), tc.out) { |
| 778 |
t.Fatalf("ExprTypeToType(%v) got %v, wanted error %v", tc.in, err, tc.out) |
| 779 |
} |
| 780 |
}) |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
func TestTypeHasTrait(t *testing.T) { |
| 785 |
if !BoolType.HasTrait(traits.ComparerType) { |
| 786 |
t.Error("BoolType.HasTrait(ComparerType) returned false") |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
func TestTypeWithTraits(t *testing.T) { |
| 791 |
vec := NewOpaqueType("vector", NewTypeParamType("T")) |
| 792 |
if vec.HasTrait(traits.SizerType) { |
| 793 |
t.Error("vec.HasTrait(SizerType) returned true") |
| 794 |
} |
| 795 |
vec = vec.WithTraits(traits.SizerType | traits.ContainerType | traits.IndexerType) |
| 796 |
if !vec.HasTrait(traits.SizerType) { |
| 797 |
t.Errorf("vec.HasTrait(SizerType) returned false after WithTraits() call") |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
func TestTypeConvertToType(t *testing.T) { |
| 802 |
if BoolType.ConvertToType(TypeType) != TypeType { |
| 803 |
t.Error("ConvertToType(TypeType) did not produce type value") |
| 804 |
} |
| 805 |
if BoolType.ConvertToType(StringType) != String("bool") { |
| 806 |
t.Error("ConvertToType(StringType) did not produce 'bool'") |
| 807 |
} |
| 808 |
if !IsError(BoolType.ConvertToType(IntType)) { |
| 809 |
t.Error("ConvertToType(IntType) did not produce error") |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
func TestTypeConvertToNative(t *testing.T) { |
| 814 |
_, err := BoolType.ConvertToNative(reflect.TypeOf(true)) |
| 815 |
if err == nil { |
| 816 |
t.Error("ConvertToNative() did not error") |
| 817 |
} |
| 818 |
} |