gitgabrio commented on code in PR #6632: URL: https://github.com/apache/incubator-kie-drools/pull/6632#discussion_r3098871510
########## kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/dialectHandlers/BFEELDialectHandlerTest.java: ########## @@ -0,0 +1,235 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.kie.dmn.feel.lang.ast.dialectHandlers; + +import java.math.BigDecimal; +import java.time.Duration; +import java.time.LocalDate; +import java.time.Period; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.kie.dmn.feel.lang.EvaluationContext; +import org.kie.dmn.feel.lang.types.impl.ComparablePeriod; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for BFEELDialectHandler - BFEEL uses type coercion and returns default values + */ +class BFEELDialectHandlerTest { + + private BFEELDialectHandler handler; + private EvaluationContext ctx; + + @BeforeEach + void setUp() { + handler = new BFEELDialectHandler(); + ctx = EvaluationContextTestUtil.newEmptyEvaluationContext(); + } + + @Test + void testAddOperations() { + // String concatenation with type coercion + assertThat(handler.executeAdd("Hello", "World", ctx)).isEqualTo("HelloWorld"); + assertThat(handler.executeAdd("Hello", 123, ctx)).isEqualTo("Hello123"); + assertThat(handler.executeAdd("Hello", null, ctx)).isEqualTo("Hello"); + + // Numbers + assertThat(handler.executeAdd(10, 20, ctx)).isEqualTo(new BigDecimal("30")); + assertThat(handler.executeAdd(10, null, ctx)).isEqualTo(10); + + // Date operations - returns number instead of null + assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), 5, ctx)).isEqualTo(5); + assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), Duration.ofDays(5), ctx)) + .isEqualTo(LocalDate.of(2024, 1, 6)); + } + + @Test + void testLogicalOperations() { + // AND with boolean coercion + assertThat(handler.executeAnd(Boolean.TRUE, Boolean.TRUE, ctx)).isEqualTo(Boolean.TRUE); + assertThat(handler.executeAnd(Boolean.TRUE, Boolean.FALSE, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executeAnd("test", Boolean.TRUE, ctx)).isEqualTo(Boolean.FALSE); // Non-boolean coerced to false + + // OR operations + assertThat(handler.executeOr(Boolean.TRUE, Boolean.FALSE, ctx)).isEqualTo(Boolean.TRUE); + assertThat(handler.executeOr(Boolean.FALSE, Boolean.FALSE, ctx)).isEqualTo(Boolean.FALSE); + } + + @Test + void testEqualityOperations() { + // Equal - returns false for different types (not null) + assertThat(handler.executeEqual(null, null, ctx)).isEqualTo(Boolean.TRUE); + assertThat(handler.executeEqual(10, 10, ctx)).isEqualTo(Boolean.TRUE); + assertThat(handler.executeEqual(10, 20, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executeEqual("10", 10, ctx)).isEqualTo(Boolean.FALSE); // Different types + assertThat(handler.executeEqual(null, 10, ctx)).isEqualTo(Boolean.FALSE); + + // Not Equal + assertThat(handler.executeNotEqual(null, null, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executeNotEqual(10, 20, ctx)).isEqualTo(Boolean.TRUE); + } + + @Test + void testComparisonOperations() { + // BFEEL returns false for invalid comparisons (not null) + assertThat(handler.executeGte(Boolean.FALSE, Boolean.FALSE, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executeGte("test", Boolean.TRUE, ctx)).isEqualTo(Boolean.TRUE); + assertThat(handler.executeGte(20, 10, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executeGte("test", 10, ctx)).isEqualTo(Boolean.FALSE); // Incomparable + + assertThat(handler.executeGt(20, 10, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executeLte(5, 10, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executeLt(5, 10, ctx)).isEqualTo(Boolean.FALSE); + } + + @Test + void testArithmeticOperations() { + // Power - B-FEEL implicit coercion: string "2" can be coerced to number 2 + assertThat(handler.executePow("2", 3, ctx)).isEqualTo(new BigDecimal("8")); // "2" coerced to 2, then 2^3 = 8 + assertThat(handler.executePow(2, 3, ctx)).isEqualTo(new BigDecimal("8")); + assertThat(handler.executePow(2, "3", ctx)).isEqualTo(new BigDecimal("8")); // "3" coerced to 3 + assertThat(handler.executePow(2, "invalid", ctx)).isEqualTo(BigDecimal.ZERO); // Invalid string returns 0 + assertThat(handler.executePow(null, 3, ctx)).isEqualTo(BigDecimal.ZERO); // Null returns 0 + assertThat(handler.executePow(2, null, ctx)).isEqualTo(BigDecimal.ZERO); // Null returns 0 + + // Subtraction - returns empty string for invalid + assertThat(handler.executeSub("10", 5, ctx)).isEqualTo(""); + assertThat(handler.executeSub(20, 10, ctx)).isEqualTo(new BigDecimal("10")); + assertThat(handler.executeSub(LocalDate.of(2024, 1, 10), Duration.ofDays(5), ctx)) Review Comment: @ChinchuAjith would it be possible to create a test for each row in the BFEEL table , with parameters at both left and right side ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
