@Test public void test_Misc_fromOriginalFEELInterpretedTestSuite() { assertThat(parseCompileEvaluate("if null then \"foo\" else \"bar\""), is("bar")); assertThat(parseCompileEvaluate("{ hello world : function() \"Hello World!\", message : hello world() }.message"), is("Hello World!")); assertThat(parseCompileEvaluate("1 + if true then 1 else 2"), is(new BigDecimal("2"))); assertThat(parseCompileEvaluate("\"string with \\\"quotes\\\"\""), is("string with \"quotes\"")); assertThat(parseCompileEvaluate("date( -0105, 8, 2 )"), is(LocalDate.of(-105, 8, 2))); assertThat(parseCompileEvaluate("string(null)"), is(nullValue())); assertThat(parseCompileEvaluate("[ null ]"), is(Arrays.asList(new Object[]{null}))); assertThat(parseCompileEvaluate("[ null, null ]"), is(Arrays.asList(new Object[]{null, null}))); assertThat(parseCompileEvaluate("[ null, 47, null ]"), is(Arrays.asList(new Object[]{null, BigDecimal.valueOf(47), null}))); }
@Test public void test_primary_parens() { assertThat(parseCompileEvaluate("(\"some string\")"), is( "some string" )); assertThat(parseCompileEvaluate("(123)"), is( BigDecimal.valueOf(123) )); assertThat(parseCompileEvaluate("(-123)"), is( BigDecimal.valueOf(-123) )); assertThat(parseCompileEvaluate("-(123)"), is( BigDecimal.valueOf(-123) )); assertThat(parseCompileEvaluate("(false)"), is( false )); assertThat(parseCompileEvaluate("(true)"), is( true )); }
@Test public void test_between() { assertThat(parseCompileEvaluate("10 between 5 and 12"), is(true)); assertThat(parseCompileEvaluate("10 between 20 and 30"), is(false)); assertThat(parseCompileEvaluate("10 between 20 and \"foo\""), nullValue()); assertThat(parseCompileEvaluate("\"foo\" between 5 and 12"), nullValue()); assertThat(parseCompileEvaluate("\"foo\" between \"bar\" and \"zap\""), is(true)); assertThat(parseCompileEvaluate("\"foo\" between null and \"zap\""), nullValue()); }
@Test public void test_listExpression() { assertThat(parseCompileEvaluate("[]"), is(Collections.emptyList())); assertThat(parseCompileEvaluate("[ ]"), is(Collections.emptyList())); assertThat(parseCompileEvaluate("[1]"), is(Arrays.asList(BigDecimal.valueOf(1)))); assertThat(parseCompileEvaluate("[1, 2,3]"), is(Arrays.asList(BigDecimal.valueOf(1), BigDecimal.valueOf(2), BigDecimal.valueOf(3)))); }
@Test public void test_for() { // for assertThat(parseCompileEvaluate("for x in [ 10, 20, 30 ], y in [ 1, 2, 3 ] return x * y"), is(Arrays.asList(10, 20, 30, 20, 40, 60, 30, 60, 90).stream().map(x -> BigDecimal.valueOf(x)).collect(Collectors.toList()))); // normal: assertThat(parseCompileEvaluate("for x in [1, 2, 3] return x+1"), is(Arrays.asList(1, 2, 3).stream().map(x -> BigDecimal.valueOf(x + 1)).collect(Collectors.toList()))); // TODO in order to parse correctly the enhanced for loop it is required to configure the FEEL Profiles }
@Test public void test_additiveExpression() { assertThat(parseCompileEvaluate( "1 + 2"), is(BigDecimal.valueOf( 3 ))); assertThat(parseCompileEvaluate( "1 + null"), nullValue()); assertThat(parseCompileEvaluate( "1 - 2"), is(BigDecimal.valueOf( -1 ))); assertThat(parseCompileEvaluate( "1 - null"), nullValue()); assertThat(parseCompileEvaluate( "\"Hello, \" + \"World\""), is("Hello, World")); }
@Test public void test_filterPathSelection() { // Selection assertThat(parseCompileEvaluate("[ {x:1, y:2}, {x:2, y:3} ].y"), is(Arrays.asList(BigDecimal.valueOf(2), BigDecimal.valueOf(3)))); assertThat(parseCompileEvaluate("[ {x:1, y:2}, {x:2} ].y"), is(Arrays.asList(BigDecimal.valueOf(2)))); assertThat(parseCompileEvaluate("[ {x:1, y:2}, {x:2, y:3} ].z"), is(Collections.emptyList())); }
@Test public void test_logicalNegationExpression() { // this is all invalid syntax assertThat(parseCompileEvaluate("not true"), nullValue()); assertThat(parseCompileEvaluate("not false"), nullValue()); assertThat(parseCompileEvaluate("not null"), nullValue()); assertThat(parseCompileEvaluate("not 3"), nullValue()); }
@Test public void test_quantifiedExpressions() { // quantified expressions assertThat(parseCompileEvaluate("some price in [ 80, 11, 110 ] satisfies price > 100"), is(Boolean.TRUE)); assertThat(parseCompileEvaluate("some price in [ 80, 11, 90 ] satisfies price > 100"), is(Boolean.FALSE)); assertThat(parseCompileEvaluate("some x in [ 5, 6, 7 ], y in [ 10, 11, 6 ] satisfies x > y"), is(Boolean.TRUE)); assertThat(parseCompileEvaluate("every price in [ 80, 11, 90 ] satisfies price > 10"), is(Boolean.TRUE)); assertThat(parseCompileEvaluate("every price in [ 80, 11, 90 ] satisfies price > 70"), is(Boolean.FALSE)); assertThat(parseCompileEvaluate("some x in [ 5, 6, 7 ], y in [ 10, 11, 12 ] satisfies x < y"), is(Boolean.TRUE)); assertThat(parseCompileEvaluate("some price in [ 80, 11, 110 ] satisfies price > max(100, 50, 10)"), is(Boolean.TRUE)); }
/** * Partially from {@link FEELConditionsAndLoopsTest} */ @Test public void test_if() { assertThat(parseCompileEvaluate( "if true then 15 else 5"), is(BigDecimal.valueOf( 15 ))); assertThat(parseCompileEvaluate( "if false then 15 else 5"), is(BigDecimal.valueOf( 5 ))); assertThat(parseCompileEvaluate("if null then 15 else 5"), is(BigDecimal.valueOf(5))); assertThat(parseCompileEvaluate("if \"hello\" then 15 else 5"), is(BigDecimal.valueOf(5))); }
@Test public void test_multiplicativeExpression() { assertThat(parseCompileEvaluate("3 * 5"), is(BigDecimal.valueOf(15))); assertThat(parseCompileEvaluate("3 * null"), nullValue()); assertThat(parseCompileEvaluate("10 / 2"), is(BigDecimal.valueOf(5))); assertThat(parseCompileEvaluate("10 / null"), nullValue()); }
@Test public void test_FEEL_boolean() { assertThat(parseCompileEvaluate("false"), is( false )); assertThat(parseCompileEvaluate("true"), is( true )); assertThat(parseCompileEvaluate("null"), nullValue()); }
@Test public void test_FEEL_DROOLS_2143() { // DROOLS-2143: Allow ''--1' expression as per FEEL grammar rule 26 assertThat(parseCompileEvaluate("--10"), is(BigDecimal.valueOf(10))); assertThat(parseCompileEvaluate("---10"), is(BigDecimal.valueOf(-10))); assertThat(parseCompileEvaluate("+10"), is(BigDecimal.valueOf(10))); }
@Test public void test_instanceOfExpression() { assertThat(parseCompileEvaluate("123 instance of number"), is(true)); assertThat(parseCompileEvaluate("\"ciao\" instance of number"), is(false)); assertThat(parseCompileEvaluate("123 instance of string"), is(false)); assertThat(parseCompileEvaluate("\"ciao\" instance of string"), is(true)); }
@Test public void test_Benchmark_feelExpressions() { assertThat(parseCompileEvaluate("{ full name: { first name: \"John\", last name: \"Doe\" } }.full name.last name"), is("Doe")); assertThat(parseCompileEvaluate("some price in [ 80, 11, 110 ] satisfies price > 100"), is(Boolean.TRUE)); assertThat(parseCompileEvaluate("every price in [ 80, 11, 90 ] satisfies price > 10"), is(Boolean.TRUE)); }
@Test public void test_basicFunctionDefinition() { assertThat(parseCompileEvaluate("function (a, b) a + b"), is(instanceOf(CompiledCustomFEELFunction.class))); assertThat(parseCompileEvaluate("{ s : function (a, b) a + b, x : 1, y : 2, r : s(x,y) }.r"), is(new BigDecimal(3))); }
@Test public void test_exponentiationExpression() { assertThat(parseCompileEvaluate("3 ** 3"), is(BigDecimal.valueOf(27))); assertThat(parseCompileEvaluate("3 ** null"), nullValue()); }
@Test public void test_namedFunctionInvocation() { assertThat(parseCompileEvaluate("substring(start position: 2, string: \"FOOBAR\")"), is("OOBAR")); assertThat(parseCompileEvaluate("ceiling( n : 1.5 )"), is(new BigDecimal("2"))); }
@Test public void test_contextExpression() { assertThat(parseCompileEvaluate("{}"), is(Collections.emptyMap())); assertThat(parseCompileEvaluate("{ }"), is(Collections.emptyMap())); assertThat(parseCompileEvaluate("{ a : 1 }"), is(mapOf(entry("a", new BigDecimal(1))))); assertThat(parseCompileEvaluate("{ \"a\" : 1 }"), is(mapOf(entry("a", new BigDecimal(1))))); assertThat(parseCompileEvaluate("{ \" a\" : 1 }"), is(mapOf(entry(" a", new BigDecimal(1))))); // Demonstrating a bad practice. assertThat(parseCompileEvaluate("{ a : 1, b : 2, c : 3 }"), is(mapOf(entry("a", new BigDecimal(1)), entry("b", new BigDecimal(2)), entry("c", new BigDecimal(3))))); assertThat(parseCompileEvaluate("{ a : 1, a name : \"John Doe\" }"), is(mapOf(entry("a", new BigDecimal(1)), entry("a name", "John Doe")))); assertThat(parseCompileEvaluate("{ a : 1, b : a }"), is(mapOf(entry("a", new BigDecimal(1)), entry("b", new BigDecimal(1))))); }
/** * See {@link FEELParserTest} */ @Test public void testContextWithMultipleEntries() { String inputExpression = "{ \"a string key\" : 10," + "\n" + " a non-string key : 11," + "\n" + " a key.with + /' odd chars : 12 }"; assertThat(parseCompileEvaluate(inputExpression), is(mapOf(entry("a string key", new BigDecimal(10)), entry("a non-string key", new BigDecimal(11)), entry("a key.with + /' odd chars", new BigDecimal(12))))); }