ChinchuAjith commented on code in PR #6632: URL: https://github.com/apache/incubator-kie-drools/pull/6632#discussion_r3111828924
########## kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/dialectHandlers/BFEELDialectHandlerTest.java: ########## @@ -0,0 +1,285 @@ +/* + * 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 testAdditionWithPrecedenceOrder() { + //Basic addition operation + assertThat(handler.executeAdd(10, 20, ctx)).isEqualTo(new BigDecimal("30")); + assertThat(handler.executeAdd(10, null, ctx)).isEqualTo(10); + // Case 1 : STRING - If either operand is a string, convert non-string to string + // Left: string + other + assertThat(handler.executeAdd("Hello", 123, ctx)).isEqualTo("Hello123"); + assertThat(handler.executeAdd("Date:", LocalDate.of(2024, 1, 1), ctx)).isEqualTo("Date:2024-01-01"); + assertThat(handler.executeAdd("Duration:", Duration.ofDays(5), ctx)).isEqualTo("Duration:PT120H"); + assertThat(handler.executeAdd("Period:", Period.ofMonths(3), ctx)).isEqualTo("Period:P3M"); + + // Right: other + string + assertThat(handler.executeAdd(123, "World", ctx)).isEqualTo("123World"); + assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), " is the date", ctx)).isEqualTo("2024-01-01 is the date"); + assertThat(handler.executeAdd(Duration.ofDays(5), " duration", ctx)).isEqualTo("PT120H duration"); + assertThat(handler.executeAdd(Period.ofMonths(3), " period", ctx)).isEqualTo("P3M period"); + + // Case 2 : NUMBER - If either operand is a number (and neither is string), convert non-number to number + // Per BFEEL Section 11.3: number(date) = 0, number(duration) = seconds, number(period) = months Review Comment: @gitgabrio The DMN specification does not define a signature for the number() function that accepts a date type. Standard FEEL treats it as a semantic error and returns null (find the points below), while B-FEEL modifies this result to 0. In standard FEEL, the number() function has a restricted domain for its parameters. According to Table 73 (Semantics of conversion functions), the valid signatures for number() are: * number(from, grouping separator, decimal separator) where from must be a string. * number(from) where from must be a string. * number(from) where from must be a number. There is no signature provided in the specification that allows a date type as the from parameter. Section 10.3.4 (Built-in functions) defines ”Whenever a parameter is outside its domain, the result of the built-in is null". This is reiterated in Section 10.3.2.13.1, which states that actual parameters must conform to the parameter domains or the result is null. According to Section 11.3 (Built-in functions returning a number): "Several FEEL built-in functions return a numeric result. In B-FEEL, those functions’ semantics are modified to return 0 everywhere FEEL would return null for them". This is the maximum we will get from spec :) -- 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]
