public void testWithModelElementList() { final TestModelRoot root = TestModelRoot.TYPE.instantiate(); final ModelElementList<TestModelElementA> list = root.getList1(); final FunctionContext context = new ModelElementFunctionContext(root); TestModelElementA a; testForExpectedValue(context, "${ Avg( List1 ) }", new BigDecimal("0")); testForExpectedValue(context, "${ Avg( List1, 'Value1' ) }", new BigDecimal("0")); testForExpectedValue(context, "${ Avg( List1, 'Value2' ) }", new BigDecimal("0")); a = list.insert(); a.setValue1("1"); a.setValue2(2); testForExpectedValue(context, "${ Avg( List1 ) }", new BigDecimal("1")); testForExpectedValue(context, "${ Avg( List1, 'Value1' ) }", new BigDecimal("1")); testForExpectedValue(context, "${ Avg( List1, 'Value2' ) }", new BigDecimal("2")); a = list.insert(); a.setValue1("2"); a.setValue2(3); a = list.insert(); a.setValue1("3.5"); a.setValue2(4); testForExpectedValue(context, "${ Avg( List1 ) }", new BigDecimal("2.2")); testForExpectedValue(context, "${ Avg( List1, 'Value1' ) }", new BigDecimal("2.2")); testForExpectedValue(context, "${ Avg( List1, 'Value2' ) }", new BigDecimal("3")); testForExpectedError( context, "${ Avg( List1, 'abc' ) }", "Property TestModelElementA.abc could not be found."); testForExpectedError( context, "${ Avg( List1, 'Element1' ) }", "Property TestModelElementA.Element1 is not a value property."); testForExpectedError( context, "${ Avg( List2 ) }", "Element type TestModelElementB does not contain a value property."); final FunctionResult result = ExpressionLanguageParser.parse("${ Avg( List1, 'Value1' ) }").evaluate(context); assertEquals(new BigDecimal("2.2"), result.value()); list.get(0).setValue1("2"); list.get(1).setValue1("3.5"); assertEquals(new BigDecimal("3.0"), result.value()); a = list.insert(); a.setValue1("5.2"); assertEquals(new BigDecimal("3.6"), result.value()); }
private void test(final String expr, final Object expected, final FunctionContext context) { final Object actual = ExpressionLanguageParser.parse(expr).evaluate(context).value(); assertEquals(expected, actual); }