|
15 | 15 | package dev.cel.checker; |
16 | 16 |
|
17 | 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | +import static org.junit.Assert.assertThrows; |
18 | 19 |
|
19 | 20 | import dev.cel.expr.Type; |
20 | 21 | import dev.cel.expr.Type.PrimitiveType; |
| 22 | +import com.google.protobuf.Duration; |
| 23 | +import com.google.protobuf.Timestamp; |
| 24 | +import com.google.testing.junit.testparameterinjector.TestParameter; |
| 25 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
21 | 26 | import dev.cel.common.CelAbstractSyntaxTree; |
| 27 | +import dev.cel.common.CelContainer; |
22 | 28 | import dev.cel.common.CelFunctionDecl; |
23 | 29 | import dev.cel.common.CelOverloadDecl; |
| 30 | +import dev.cel.common.CelValidationException; |
24 | 31 | import dev.cel.common.types.CelKind; |
25 | 32 | import dev.cel.common.types.CelProtoTypes; |
26 | 33 | import dev.cel.common.types.CelType; |
|
37 | 44 | import java.util.Map; |
38 | 45 | import org.junit.Test; |
39 | 46 | import org.junit.runner.RunWith; |
40 | | -import org.junit.runners.JUnit4; |
41 | 47 |
|
42 | | -@RunWith(JUnit4.class) |
| 48 | +@RunWith(TestParameterInjector.class) |
43 | 49 | public class TypesTest { |
44 | 50 |
|
45 | 51 | @Test |
@@ -350,6 +356,215 @@ public void compiler_typeParamInTypeType_resolvesReturnTypeString() throws Excep |
350 | 356 | assertThat(ast.getResultType()).isEqualTo(SimpleType.STRING); |
351 | 357 | } |
352 | 358 |
|
| 359 | + private enum WellKnownTypeIdentTestCase { |
| 360 | + DURATION_QUALIFIED("google.protobuf.Duration", TypeType.create(SimpleType.DURATION)), |
| 361 | + DURATION_LEADING_DOT(".google.protobuf.Duration", TypeType.create(SimpleType.DURATION)), |
| 362 | + DURATION_UNQUALIFIED("Duration", TypeType.create(SimpleType.DURATION)), |
| 363 | + TIMESTAMP_QUALIFIED("google.protobuf.Timestamp", TypeType.create(SimpleType.TIMESTAMP)), |
| 364 | + TIMESTAMP_LEADING_DOT(".google.protobuf.Timestamp", TypeType.create(SimpleType.TIMESTAMP)), |
| 365 | + TIMESTAMP_UNQUALIFIED("Timestamp", TypeType.create(SimpleType.TIMESTAMP)); |
| 366 | + |
| 367 | + private final String expression; |
| 368 | + private final CelType expectedType; |
| 369 | + |
| 370 | + WellKnownTypeIdentTestCase(String expression, CelType expectedType) { |
| 371 | + this.expression = expression; |
| 372 | + this.expectedType = expectedType; |
| 373 | + } |
| 374 | + } |
| 375 | + |
| 376 | + @Test |
| 377 | + public void compiler_wellKnownProtoTypeIdent_resolvesToSimpleType( |
| 378 | + @TestParameter WellKnownTypeIdentTestCase testCase) throws Exception { |
| 379 | + CelCompiler celCompiler = |
| 380 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 381 | + .addMessageTypes(Duration.getDescriptor(), Timestamp.getDescriptor()) |
| 382 | + .setContainer(CelContainer.ofName("google.protobuf")) |
| 383 | + .build(); |
| 384 | + |
| 385 | + CelAbstractSyntaxTree ast = celCompiler.compile(testCase.expression).getAst(); |
| 386 | + |
| 387 | + assertThat(ast.getResultType()).isEqualTo(testCase.expectedType); |
| 388 | + } |
| 389 | + |
| 390 | + private enum WellKnownTypeParamTestCase { |
| 391 | + DURATION_QUALIFIED_CAST("cast('1h', google.protobuf.Duration)", SimpleType.DURATION), |
| 392 | + DURATION_QUALIFIED_EQUALITY( |
| 393 | + "cast('1h', google.protobuf.Duration) == duration('1h')", SimpleType.BOOL), |
| 394 | + DURATION_UNQUALIFIED_COMPARISON("cast('1h', Duration) > duration('0s')", SimpleType.BOOL), |
| 395 | + TIMESTAMP_QUALIFIED_CAST("cast(0, google.protobuf.Timestamp)", SimpleType.TIMESTAMP), |
| 396 | + TIMESTAMP_QUALIFIED_EQUALITY( |
| 397 | + "cast(0, google.protobuf.Timestamp) == timestamp(0)", SimpleType.BOOL), |
| 398 | + TIMESTAMP_UNQUALIFIED_COMPARISON("cast(0, Timestamp) > timestamp(0)", SimpleType.BOOL); |
| 399 | + |
| 400 | + private final String expression; |
| 401 | + private final CelType expectedType; |
| 402 | + |
| 403 | + WellKnownTypeParamTestCase(String expression, CelType expectedType) { |
| 404 | + this.expression = expression; |
| 405 | + this.expectedType = expectedType; |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + @Test |
| 410 | + public void compiler_typeParamInTypeType_withWellKnownProto_resolvesWellKnownOverloads( |
| 411 | + @TestParameter WellKnownTypeParamTestCase testCase) throws Exception { |
| 412 | + TypeParamType typeParamT = TypeParamType.create("T"); |
| 413 | + CelCompiler celCompiler = |
| 414 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 415 | + .addMessageTypes(Duration.getDescriptor(), Timestamp.getDescriptor()) |
| 416 | + .setContainer(CelContainer.ofName("google.protobuf")) |
| 417 | + .addFunctionDeclarations( |
| 418 | + CelFunctionDecl.newFunctionDeclaration( |
| 419 | + "cast", |
| 420 | + CelOverloadDecl.newGlobalOverload( |
| 421 | + "cast_t", typeParamT, SimpleType.DYN, TypeType.create(typeParamT)))) |
| 422 | + .build(); |
| 423 | + |
| 424 | + CelAbstractSyntaxTree ast = celCompiler.compile(testCase.expression).getAst(); |
| 425 | + |
| 426 | + assertThat(ast.getResultType()).isEqualTo(testCase.expectedType); |
| 427 | + } |
| 428 | + |
| 429 | + @Test |
| 430 | + public void compiler_durationIdent_withoutMessageTypes_resolvesToSimpleType() throws Exception { |
| 431 | + CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 432 | + |
| 433 | + CelAbstractSyntaxTree ast = celCompiler.compile("google.protobuf.Duration").getAst(); |
| 434 | + |
| 435 | + assertThat(ast.getResultType()).isEqualTo(TypeType.create(SimpleType.DURATION)); |
| 436 | + } |
| 437 | + |
| 438 | + @Test |
| 439 | + public void compiler_timestampIdent_withoutMessageTypes_resolvesToSimpleType() throws Exception { |
| 440 | + CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 441 | + |
| 442 | + CelAbstractSyntaxTree ast = celCompiler.compile("google.protobuf.Timestamp").getAst(); |
| 443 | + |
| 444 | + assertThat(ast.getResultType()).isEqualTo(TypeType.create(SimpleType.TIMESTAMP)); |
| 445 | + } |
| 446 | + |
| 447 | + @Test |
| 448 | + public void compiler_durationStructCreation_withDescriptor_success() throws Exception { |
| 449 | + CelCompiler celCompiler = |
| 450 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 451 | + .addMessageTypes(Duration.getDescriptor()) |
| 452 | + .build(); |
| 453 | + |
| 454 | + CelAbstractSyntaxTree ast = |
| 455 | + celCompiler.compile("google.protobuf.Duration{seconds: 10, nanos: 20}").getAst(); |
| 456 | + |
| 457 | + assertThat(ast.getResultType()).isEqualTo(SimpleType.DURATION); |
| 458 | + } |
| 459 | + |
| 460 | + @Test |
| 461 | + public void compiler_timestampStructCreation_withDescriptor_success() throws Exception { |
| 462 | + CelCompiler celCompiler = |
| 463 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 464 | + .addMessageTypes(Timestamp.getDescriptor()) |
| 465 | + .build(); |
| 466 | + |
| 467 | + CelAbstractSyntaxTree ast = |
| 468 | + celCompiler.compile("google.protobuf.Timestamp{seconds: 100, nanos: 200}").getAst(); |
| 469 | + |
| 470 | + assertThat(ast.getResultType()).isEqualTo(SimpleType.TIMESTAMP); |
| 471 | + } |
| 472 | + |
| 473 | + @Test |
| 474 | + public void compiler_durationStructCreation_emptyFields_success() throws Exception { |
| 475 | + CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 476 | + |
| 477 | + CelAbstractSyntaxTree ast = celCompiler.compile("google.protobuf.Duration{}").getAst(); |
| 478 | + |
| 479 | + assertThat(ast.getResultType()).isEqualTo(SimpleType.DURATION); |
| 480 | + } |
| 481 | + |
| 482 | + @Test |
| 483 | + public void compiler_timestampStructCreation_emptyFields_success() throws Exception { |
| 484 | + CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 485 | + |
| 486 | + CelAbstractSyntaxTree ast = celCompiler.compile("google.protobuf.Timestamp{}").getAst(); |
| 487 | + |
| 488 | + assertThat(ast.getResultType()).isEqualTo(SimpleType.TIMESTAMP); |
| 489 | + } |
| 490 | + |
| 491 | + @Test |
| 492 | + public void compiler_durationStructCreation_withoutDescriptor_throws() { |
| 493 | + CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 494 | + |
| 495 | + CelValidationException e = |
| 496 | + assertThrows( |
| 497 | + CelValidationException.class, |
| 498 | + () -> celCompiler.compile("google.protobuf.Duration{seconds: 10}").getAst()); |
| 499 | + |
| 500 | + assertThat(e) |
| 501 | + .hasMessageThat() |
| 502 | + .contains( |
| 503 | + "Message type resolution failure while referencing field 'seconds'." |
| 504 | + + " Ensure that the descriptor for type 'google.protobuf.Duration' was added to the" |
| 505 | + + " environment"); |
| 506 | + } |
| 507 | + |
| 508 | + @Test |
| 509 | + public void compiler_timestampStructCreation_withoutDescriptor_throws() { |
| 510 | + CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 511 | + |
| 512 | + CelValidationException e = |
| 513 | + assertThrows( |
| 514 | + CelValidationException.class, |
| 515 | + () -> celCompiler.compile("google.protobuf.Timestamp{seconds: 10}").getAst()); |
| 516 | + |
| 517 | + assertThat(e) |
| 518 | + .hasMessageThat() |
| 519 | + .contains( |
| 520 | + "Message type resolution failure while referencing field 'seconds'. Ensure that the" |
| 521 | + + " descriptor for type 'google.protobuf.Timestamp' was added to the environment"); |
| 522 | + } |
| 523 | + |
| 524 | + @Test |
| 525 | + public void compiler_durationStructCreation_typeMismatch_throws() { |
| 526 | + CelCompiler celCompiler = |
| 527 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 528 | + .addMessageTypes(Duration.getDescriptor()) |
| 529 | + .build(); |
| 530 | + |
| 531 | + CelValidationException e = |
| 532 | + assertThrows( |
| 533 | + CelValidationException.class, |
| 534 | + () -> celCompiler.compile("google.protobuf.Duration{seconds: 'bad'}").getAst()); |
| 535 | + |
| 536 | + assertThat(e) |
| 537 | + .hasMessageThat() |
| 538 | + .contains("expected type of field 'seconds' is 'int' but provided type is 'string'"); |
| 539 | + } |
| 540 | + |
| 541 | + @Test |
| 542 | + public void compiler_timestampStructCreation_typeMismatch_throws() { |
| 543 | + CelCompiler celCompiler = |
| 544 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 545 | + .addMessageTypes(Timestamp.getDescriptor()) |
| 546 | + .build(); |
| 547 | + |
| 548 | + CelValidationException e = |
| 549 | + assertThrows( |
| 550 | + CelValidationException.class, |
| 551 | + () -> celCompiler.compile("google.protobuf.Timestamp{seconds: 'bad'}").getAst()); |
| 552 | + |
| 553 | + assertThat(e) |
| 554 | + .hasMessageThat() |
| 555 | + .contains("expected type of field 'seconds' is 'int' but provided type is 'string'"); |
| 556 | + } |
| 557 | + |
| 558 | + @Test |
| 559 | + public void compiler_structCreation_primitiveType_throws() { |
| 560 | + CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 561 | + |
| 562 | + CelValidationException e = |
| 563 | + assertThrows(CelValidationException.class, () -> celCompiler.compile("int{}").getAst()); |
| 564 | + |
| 565 | + assertThat(e).hasMessageThat().contains("'int' is not a message type"); |
| 566 | + } |
| 567 | + |
353 | 568 | @Test |
354 | 569 | public void compiler_typeParamInCompositeTypeType_resolvesReturnType() throws Exception { |
355 | 570 | TypeParamType typeParamT = TypeParamType.create("T"); |
|
0 commit comments