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