|
| 1 | +package dev.cel.maven; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static dev.cel.common.CelFunctionDecl.newFunctionDeclaration; |
| 5 | +import static dev.cel.common.CelOverloadDecl.newGlobalOverload; |
| 6 | +import static org.junit.Assert.assertThrows; |
| 7 | + |
| 8 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 9 | +import dev.cel.checker.CelChecker; |
| 10 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 11 | +import dev.cel.common.CelContainer; |
| 12 | +import dev.cel.common.CelOptions; |
| 13 | +import dev.cel.common.CelValidationException; |
| 14 | +import dev.cel.common.CelValidationResult; |
| 15 | +import dev.cel.common.ast.CelConstant; |
| 16 | +import dev.cel.common.ast.CelExpr; |
| 17 | +import dev.cel.common.types.ProtoMessageTypeProvider; |
| 18 | +import dev.cel.common.types.SimpleType; |
| 19 | +import dev.cel.common.types.StructTypeReference; |
| 20 | +import dev.cel.compiler.CelCompiler; |
| 21 | +import dev.cel.compiler.CelCompilerFactory; |
| 22 | +import dev.cel.expr.conformance.proto3.TestAllTypes; |
| 23 | +import dev.cel.parser.CelParser; |
| 24 | +import dev.cel.parser.CelParserFactory; |
| 25 | +import dev.cel.parser.CelStandardMacro; |
| 26 | +import dev.cel.parser.CelUnparser; |
| 27 | +import dev.cel.parser.CelUnparserFactory; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | + |
| 31 | +@RunWith(TestParameterInjector.class) |
| 32 | +public class CompilerArtifactTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void parse() throws Exception { |
| 36 | + CelParser parser = CelParserFactory.standardCelParserBuilder().build(); |
| 37 | + CelUnparser unparser = CelUnparserFactory.newUnparser(); |
| 38 | + |
| 39 | + CelAbstractSyntaxTree ast = parser.parse("'Hello World'").getAst(); |
| 40 | + |
| 41 | + assertThat(ast.getExpr()).isEqualTo(CelExpr.ofConstant(1L, CelConstant.ofValue("Hello World"))); |
| 42 | + assertThat(unparser.unparse(ast)).isEqualTo("\"Hello World\""); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void typeCheck() throws Exception { |
| 47 | + CelParser parser = CelParserFactory.standardCelParserBuilder().build(); |
| 48 | + CelChecker checker = CelCompilerFactory.standardCelCheckerBuilder().build(); |
| 49 | + |
| 50 | + CelAbstractSyntaxTree ast = checker.check(parser.parse("'Hello World'").getAst()).getAst(); |
| 51 | + |
| 52 | + assertThat(ast.getResultType()).isEqualTo(SimpleType.STRING); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void compile() throws Exception { |
| 57 | + CelCompiler compiler = CelCompilerFactory |
| 58 | + .standardCelCompilerBuilder() |
| 59 | + .addFunctionDeclarations( |
| 60 | + newFunctionDeclaration("getThree", newGlobalOverload("getThree_overload", SimpleType.INT)) |
| 61 | + ) |
| 62 | + .setOptions(CelOptions.DEFAULT) |
| 63 | + .setStandardMacros(CelStandardMacro.STANDARD_MACROS) |
| 64 | + .setTypeProvider(ProtoMessageTypeProvider.newBuilder().addFileDescriptors(TestAllTypes.getDescriptor().getFile()).build()) |
| 65 | + .setContainer(CelContainer.ofName("cel.expr.conformance.proto3")) |
| 66 | + .addVar("msg", StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())) |
| 67 | + .build(); |
| 68 | + CelUnparser unparser = CelUnparserFactory.newUnparser(); |
| 69 | + |
| 70 | + CelAbstractSyntaxTree ast = compiler.compile("msg == TestAllTypes{} && 3 == getThree()").getAst(); |
| 71 | + |
| 72 | + assertThat(unparser.unparse(ast)).isEqualTo("msg == cel.expr.conformance.proto3.TestAllTypes{} && 3 == getThree()"); |
| 73 | + assertThat(ast.getResultType()).isEqualTo(SimpleType.BOOL); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void compile_error() { |
| 78 | + CelCompiler compiler = CelCompilerFactory.standardCelCompilerBuilder().build(); |
| 79 | + |
| 80 | + CelValidationResult result = compiler.compile("'foo' + 1"); |
| 81 | + |
| 82 | + assertThat(result.hasError()).isTrue(); |
| 83 | + assertThat(assertThrows(CelValidationException.class, result::getAst)).hasMessageThat().contains("found no matching overload for '_+_' applied to '(string, int)'"); |
| 84 | + } |
| 85 | +} |
0 commit comments