Skip to content

Commit

Permalink
FEEL tests refactoring + some new corner cases (#7)
Browse files Browse the repository at this point in the history
* Split FEELTest into separate test classes.

* Added some FEEL lists corner cases.

* Fixed typo.
  • Loading branch information
baldimir authored and etirelli committed Oct 26, 2016
1 parent 5de77c9 commit 669b3e5
Show file tree
Hide file tree
Showing 15 changed files with 732 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,32 @@

package org.kie.dmn.feel.runtime;

import org.kie.dmn.feel.FEEL;
import org.kie.dmn.feel.lang.impl.FEELImpl;

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.kie.dmn.feel.FEEL;

@RunWith(Parameterized.class)
public abstract class BaseFEELTest {

private final FEEL feel = FEEL.newInstance();

@Parameterized.Parameter(0)
public String expression;

@Parameterized.Parameter(1)
public Object result;

@Test
public void testExpression() {
assertResult( expression, result );
}

protected void assertResult( String expression, Object result ) {
if( result == null ) {
assertThat( "Evaluating: '" + expression + "'", feel.evaluate( expression ), is( nullValue() ) );
Expand All @@ -37,5 +51,4 @@ protected void assertResult( String expression, Object result ) {
assertThat( "Evaluating: '"+expression+"'", feel.evaluate( expression ), is( result ) );
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import org.junit.runners.Parameterized;

public class FEELConditionsAndLoopsTest extends BaseFEELTest {

@Parameterized.Parameters(name = "{index}: {0} ({1}) = {2}")
public static Collection<Object[]> data() {
final Object[][] cases = new Object[][] {
// if expressions
{ "if true then 10+5 else 10-5", BigDecimal.valueOf( 15 ) },
{ "if false then \"foo\" else \"bar\"", "bar" },
{ "if date(\"2016-08-02\") > date(\"2015-12-25\") then \"yey\" else \"nay\"", "yey" },
{ "if null then \"foo\" else \"bar\"", null },

// for
{"for x in [ 10, 20, 30 ], y in [ 1, 2, 3 ] return x * y",
Arrays.asList( 10, 20, 30, 20, 40, 60, 30, 60, 90 ).stream().map( x -> BigDecimal.valueOf( x ) ).collect( Collectors.toList() ) }

};
return Arrays.asList( cases );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import org.junit.runners.Parameterized;

public class FEELContextsTest extends BaseFEELTest {

@Parameterized.Parameters(name = "{index}: {0} ({1}) = {2}")
public static Collection<Object[]> data() {
final Object[][] cases = new Object[][] {
{ "{ first name : \"Bob\", birthday : date(\"1978-09-12\"), salutation : \"Hello \"+first name }",
new HashMap<String,Object>() {{
put( "first name", "Bob" );
put( "birthday", LocalDate.of(1978, 9, 12) );
put( "salutation", "Hello Bob" );
}} },
// nested contexts + qualified name
{ "{ full name : { first name: \"Bob\", last name : \"Doe\" }, birthday : date(\"1978-09-12\"), salutation : \"Hello \"+full name.first name }",
new HashMap<String,Object>() {{
put( "full name", new HashMap<String,Object>() {{
put( "first name", "Bob" );
put( "last name", "Doe" );
}} );
put( "birthday", LocalDate.of(1978, 9, 12) );
put( "salutation", "Hello Bob" );
}} },
// Example from spec. chapter "10.3.2.7 Ranges"
{ "{ startdate: date(\"1978-09-12\"), enddate: date(\"1978-10-12\"), rangedates: [startdate..enddate] }",
new HashMap<String,Object>() {{
put( "startdate", LocalDate.of(1978, 9, 12) );
put( "enddate", LocalDate.of(1978, 10, 13) );
put( "rangedates", "[\"1978-09-12\"..\"1978-10-12\"]" );
}} }
};
return Arrays.asList( cases );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runners.Parameterized;

public class FEELDateTimeDurationTest extends BaseFEELTest {

@Parameterized.Parameters(name = "{index}: {0} ({1}) = {2}")
public static Collection<Object[]> data() {
final Object[][] cases = new Object[][] {
// date/time/duration function invocations
{ "date(\"2016-07-29\")", DateTimeFormatter.ISO_DATE.parse( "2016-07-29", LocalDate::from ) },
{ "date(\"-0105-07-29\")", DateTimeFormatter.ISO_DATE.parse( "-0105-07-29", LocalDate::from ) }, // 105 BC
{ "date(\"2016-15-29\")", null },
{ "date(\"2016-12-48\")", null },
{ "date( 10 )", null },
{ "date( 2016, 8, 2 )", LocalDate.of( 2016, 8, 2 ) },
{ "date( -0105, 8, 2 )", LocalDate.of( -105, 8, 2 ) },
{ "date( 2016, 15, 2 )", null },
{ "date( 2016, 12, 48 )", null },
{ "date( date and time(\"2016-07-29T05:48:23.765-05:00\") )", LocalDate.of( 2016, 7, 29 ) },
{ "date( date and time(\"2016-07-29T05:48:23Z\") )", LocalDate.of( 2016, 7, 29 ) },
{ "time(\"23:59:00\")", DateTimeFormatter.ISO_TIME.parse( "23:59:00", LocalTime::from ) },
{ "time(\"05:48:23.765\")", DateTimeFormatter.ISO_TIME.parse( "05:48:23.765", LocalTime::from ) },
{ "time(\"23:59:00z\")", DateTimeFormatter.ISO_TIME.parse( "23:59:00z", OffsetTime::from ) },
{ "time(\"13:20:00-05:00\")", DateTimeFormatter.ISO_TIME.parse( "13:20:00-05:00", OffsetTime::from ) },
{ "time( 14, 52, 25, null )", LocalTime.of( 14, 52, 25 ) },
{ "time( 14, 52, 25, duration(\"PT5H\"))", OffsetTime.of( 14, 52, 25, 0, ZoneOffset.ofHours( 5 ) ) },
{ "time( date and time(\"2016-07-29T05:48:23Z\") )", OffsetTime.of( 5, 48, 23, 0, ZoneOffset.UTC ) },
{ "time( date and time(\"2016-07-29T05:48:23.765-05:00\") )", OffsetTime.of( 5, 48, 23, 765000000, ZoneOffset.ofHours( -5 ) ) },
{ "date and time(\"2016-07-29T05:48:23Z\")", ZonedDateTime.of(2016, 7, 29, 5, 48, 23, 0, ZoneId.of("Z").normalized()) },
{ "date and time(\"2016-07-29T05:48:23.765-05:00\")", DateTimeFormatter.ISO_DATE_TIME.parse( "2016-07-29T05:48:23.765-05:00", ZonedDateTime::from ) },
{ "date and time(date(\"2016-07-29\"), time(\"05:48:23.765-05:00\") )", DateTimeFormatter.ISO_DATE_TIME.parse( "2016-07-29T05:48:23.765-05:00", ZonedDateTime::from ) },
{ "duration( \"P2DT20H14M\" )", Duration.parse( "P2DT20H14M" ) },
{ "duration( \"P2Y2M\" )", Period.parse( "P2Y2M" ) },
{ "duration( \"P26M\" )", Period.parse( "P26M" ) },
{ "years and months duration( date(\"2011-12-22\"), date(\"2013-08-24\") )", Period.parse( "P1Y8M" ) }
};
return Arrays.asList( cases );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime;

import java.util.Arrays;
import java.util.Collection;
import org.junit.runners.Parameterized;

public class FEELExpressionsTest extends BaseFEELTest {

@Parameterized.Parameters(name = "{index}: {0} ({1}) = {2}")
public static Collection<Object[]> data() {
final Object[][] cases = new Object[][] {
// quantified expressions
{ "some price in [ 80, 11, 110 ] satisfies price > 100", Boolean.TRUE },
{ "some price in [ 80, 11, 90 ] satisfies price > 100", Boolean.FALSE },
{ "some x in [ 5, 6, 7 ], y in [ 10, 11, 6 ] satisfies x > y", Boolean.TRUE },
{ "every price in [ 80, 11, 90 ] satisfies price > 10", Boolean.TRUE },
{ "every price in [ 80, 11, 90 ] satisfies price > 70", Boolean.FALSE },
{ "some x in [ 5, 6, 7 ], y in [ 10, 11, 12 ] satisfies x < y", Boolean.TRUE },

// path expressions
{"{ full name: { first name: \"John\", last name: \"Doe\" } }.full name.last name", "Doe" },

// named parameters: in this case foo is null
{"{ is minor : function( foo, person's age ) foo = null and person's age < 18, bob is minor : is minor( person's age : 16 ) }.bob is minor", Boolean.TRUE },

// unary test invocation
{"{ is minor : < 18, bob is minor : is minor(16) }.bob is minor", Boolean.TRUE },
};
return Arrays.asList( cases );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.runtime;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runners.Parameterized;

public class FEELFunctionDefinitionTest extends BaseFEELTest {

@Parameterized.Parameters(name = "{index}: {0} ({1}) = {2}")
public static Collection<Object[]> data() {
final Object[][] cases = new Object[][] {
// function definition and invocation
{"{ hello world : function() \"Hello World!\", message : hello world() }.message", "Hello World!" },
{"{ is minor : function( person's age ) person's age < 18, bob is minor : is minor( 16 ) }.bob is minor", Boolean.TRUE },
{"{ maximum : function( v1, v2 ) external { java : { class : \"java.lang.Math\", method signature: \"max(long,long)\" } }, the max : maximum( 10, 20 ) }.the max",
BigDecimal.valueOf( 20 ) }
};
return Arrays.asList( cases );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@

package org.kie.dmn.feel.runtime;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.math.BigDecimal;
import java.util.*;

import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class FEELFunctionsTest extends BaseFEELTest {

@Parameterized.Parameters(name = "{index}: {0} ({1}) = {2}")
public static Collection<Object[]> data() {
Object[][] cases = new Object[][] {
final Object[][] cases = new Object[][] {
// constants
{ "string(1.1)", "1.1" },
{ "string(null)", null },
Expand Down Expand Up @@ -135,19 +130,8 @@ public static Collection<Object[]> data() {
{ "ceiling( 1.5 )", new BigDecimal("2") },
{ "ceiling( -1.5 )", new BigDecimal("-1") },
{ "ceiling( null )", null },
{ "ceiling( n : 1.5 )", new BigDecimal("2") },
};
{ "ceiling( n : 1.5 )", new BigDecimal("2") }
};
return Arrays.asList( cases );
}

@Parameterized.Parameter(0)
public String expression;

@Parameterized.Parameter(1)
public Object result;

@Test
public void testExpression() {
assertResult( expression, result );
}
}
Loading

0 comments on commit 669b3e5

Please sign in to comment.