diff --git a/rosetta-interpreter/.project b/rosetta-interpreter/.project index a9093cfab..9215ddf5c 100644 --- a/rosetta-interpreter/.project +++ b/rosetta-interpreter/.project @@ -16,12 +16,12 @@ - org.eclipse.m2e.core.maven2Builder + net.sf.eclipsecs.core.CheckstyleBuilder - net.sf.eclipsecs.core.CheckstyleBuilder + org.eclipse.m2e.core.maven2Builder diff --git a/rosetta-interpreter/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterCompilerComparisonTest2.xtend b/rosetta-interpreter/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterCompilerComparisonTest2.xtend index cf54b2a9a..8983f7616 100644 --- a/rosetta-interpreter/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterCompilerComparisonTest2.xtend +++ b/rosetta-interpreter/src/test/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterCompilerComparisonTest2.xtend @@ -10,16 +10,127 @@ import com.regnosys.rosetta.tests.util.CodeGeneratorTestHelper import static com.google.common.collect.ImmutableMap.* import static org.junit.jupiter.api.Assertions.* +import com.google.inject.AbstractModule +import com.google.inject.Guice +import com.google.inject.Injector +import com.regnosys.rosetta.rosetta.RosettaModel +import com.regnosys.rosetta.rosetta.simple.Function +import com.regnosys.rosetta.tests.util.CodeGeneratorTestHelper +import com.regnosys.rosetta.tests.util.ModelHelper +import com.rosetta.model.lib.functions.ConditionValidator +import com.rosetta.model.lib.functions.DefaultConditionValidator +import com.rosetta.model.lib.functions.ModelObjectValidator +import com.rosetta.model.lib.functions.NoOpModelObjectValidator +import com.rosetta.model.lib.functions.RosettaFunction +import java.util.Map +import java.util.function.Consumer +import org.eclipse.xtext.xbase.testing.RegisteringFileSystemAccess + +import static org.junit.jupiter.api.Assertions.* +import com.regnosys.rosetta.generator.java.RosettaJavaPackages.RootPackage +import java.lang.reflect.InvocationTargetException +import javax.inject.Inject +import com.regnosys.rosetta.generator.java.function.FunctionGenerator +import com.regnosys.rosetta.rosetta.expression.RosettaExpression +import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue +import com.regnosys.rosetta.tests.util.ExpressionParser +import static org.junit.jupiter.api.Assertions.*; + +import javax.inject.Inject; + +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.extensions.InjectionExtension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBooleanValue; +import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterError; +import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterErrorValue; +import com.regnosys.rosetta.rosetta.expression.ExpressionFactory; +import com.regnosys.rosetta.rosetta.expression.RosettaExpression; +import com.regnosys.rosetta.rosetta.expression.impl.ExpressionFactoryImpl; +import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterValue; +import com.regnosys.rosetta.tests.RosettaInjectorProvider; +import com.regnosys.rosetta.tests.util.ExpressionParser; +import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterNumberValue @ExtendWith(InjectionExtension) @InjectWith(RosettaInjectorProvider) class RosettaInterpreterCompilerComparisonTest2 { + @Inject extension CodeGeneratorTestHelper + @Inject extension FunctionGeneratorHelper + + @Inject + ExpressionParser parser; + @Inject + RosettaInterpreterNew interpreter; @Test - def simpleTest(){ - val code = ''' - 2 + def void simpleTest() { + val model = ''' + func Foo: + output: b int (1..1) + set b: 5 '''.generateCode + val classes = model.compileToClasses + val foo = classes.createFunc('Foo') + val output = foo.invokeFunc(Integer) + + val expr = parser.parseExpression("5") + val value = interpreter.interp(expr) + assertEquals(value, new RosettaInterpreterNumberValue(output)) + } +} + +// I couldn't get a way to import this so I just put it here?????? +class FunctionGeneratorHelper { + + @Inject FunctionGenerator generator + @Inject extension ModelHelper + @Inject extension CodeGeneratorTestHelper + @Inject RegisteringFileSystemAccess fsa + + final Injector injector + + new() { + injector = Guice.createInjector(new AbstractModule() { + override protected configure() { + bind(ConditionValidator).toInstance(new DefaultConditionValidator) + bind(ModelObjectValidator).toInstance(new NoOpModelObjectValidator) + } + }) + } + + def createFunc(Map> classes, String funcName) { + injector.getInstance(classes.get(rootPackage.functions + '.' + funcName)) as RosettaFunction + } + + def invokeFunc(RosettaFunction func, Class resultClass, Object... inputs) { + val evaluateMethod = func.class.getMatchingMethod("evaluate", inputs.map[it?.class]) + try { + evaluateMethod.invoke(func, inputs) as T + } catch (InvocationTargetException e) { + throw e.cause + } + } + + def void assertToGeneratedFunction(CharSequence actualModel, CharSequence expected) throws AssertionError { + actualModel.assertToGenerated(expected, [ + generator.generate(new RootPackage(it), fsa, it.elements.filter(Function).filter[operations.nullOrEmpty].head, "test") + ]) + } + + def void assertToGeneratedCalculation(CharSequence actualModel, CharSequence expected) throws AssertionError { + actualModel.assertToGenerated(expected, [ + generator.generate(new RootPackage(it), fsa, it.elements.filter(Function).filter[!operations.nullOrEmpty].head, "test") + ]) + } + + def protected void assertToGenerated(CharSequence actualModel, CharSequence expected, + Consumer genCall) throws AssertionError { + val model = actualModel.parseRosettaWithNoErrors + genCall.accept(model) + assertEquals(expected.toString, fsa.textFiles.entrySet.head.value) } } \ No newline at end of file