Search in sources :

Example 1 with DefaultErrorReporter

use of org.lflang.DefaultErrorReporter in project lingua-franca by lf-lang.

the class TestBase method configure.

/**
 * Configure a test by applying the given configurator and return a
 * generator context. Also, if the given level is less than
 * `TestLevel.BUILD`, add a `no-compile` flag to the generator context. If
 * the configurator was not applied successfully, throw an AssertionError.
 *
 * @param test the test to configure.
 * @param configurator The configurator to apply to the test.
 * @param level The level of testing in which the generator context will be
 * used.
 * @return a generator context with a fresh resource, unaffected by any AST
 * transformation that may have occured in other tests.
 * @throws IOException if there is any file access problem
 */
private LFGeneratorContext configure(LFTest test, Configurator configurator, TestLevel level) throws IOException {
    var context = new MainContext(LFGeneratorContext.Mode.STANDALONE, CancelIndicator.NullImpl, (m, p) -> {
    }, new Properties(), true, fileConfig -> new DefaultErrorReporter());
    var r = resourceSetProvider.get().getResource(URI.createFileURI(test.srcFile.toFile().getAbsolutePath()), true);
    if (r.getErrors().size() > 0) {
        test.result = Result.PARSE_FAIL;
        throw new AssertionError("Test did not parse correctly.");
    }
    fileAccess.setOutputPath(FileConfig.findPackageRoot(test.srcFile, s -> {
    }).resolve(FileConfig.DEFAULT_SRC_GEN_DIR).toString());
    test.context = context;
    test.fileConfig = new FileConfig(r, FileConfig.getSrcGenRoot(fileAccess), context.useHierarchicalBin());
    // Set the no-compile flag the test is not supposed to reach the build stage.
    if (level.compareTo(TestLevel.BUILD) < 0) {
        context.getArgs().setProperty("no-compile", "");
    }
    addExtraLfcArgs(context.getArgs());
    // Update the test by applying the configuration. E.g., to carry out an AST transformation.
    if (configurator != null && !configurator.configure(test)) {
        test.result = Result.CONFIG_FAIL;
        throw new AssertionError("Test configuration unsuccessful.");
    }
    return context;
}
Also used : Arrays(java.util.Arrays) Inject(com.google.inject.Inject) LFGeneratorContext(org.lflang.generator.LFGeneratorContext) InjectionExtension(org.eclipse.xtext.testing.extensions.InjectionExtension) Result(org.lflang.tests.LFTest.Result) GeneratorResult(org.lflang.generator.GeneratorResult) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Path(java.nio.file.Path) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) TestCategory(org.lflang.tests.TestRegistry.TestCategory) Set(java.util.Set) LFCommand(org.lflang.util.LFCommand) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) List(java.util.List) Target(org.lflang.Target) LFStandaloneSetup(org.lflang.LFStandaloneSetup) FileConfig(org.lflang.FileConfig) IGeneratorContext(org.eclipse.xtext.generator.IGeneratorContext) CheckMode(org.eclipse.xtext.validation.CheckMode) URI(org.eclipse.emf.common.util.URI) HashMap(java.util.HashMap) Configurator(org.lflang.tests.Configurators.Configurator) Constructor(java.lang.reflect.Constructor) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ArrayList(java.util.ArrayList) JavaIoFileSystemAccess(org.eclipse.xtext.generator.JavaIoFileSystemAccess) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) LFGenerator(org.lflang.generator.LFGenerator) PrintStream(java.io.PrintStream) Properties(java.util.Properties) LFRuntimeModule(org.lflang.LFRuntimeModule) BufferedWriter(java.io.BufferedWriter) Severity(org.eclipse.xtext.diagnostics.Severity) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) DefaultErrorReporter(org.lflang.DefaultErrorReporter) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) MainContext(org.lflang.generator.MainContext) IOException(java.io.IOException) File(java.io.File) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) TimeUnit(java.util.concurrent.TimeUnit) Injector(com.google.inject.Injector) Provider(com.google.inject.Provider) FileFilter(java.io.FileFilter) InjectWith(org.eclipse.xtext.testing.InjectWith) Collections(java.util.Collections) DefaultErrorReporter(org.lflang.DefaultErrorReporter) FileConfig(org.lflang.FileConfig) MainContext(org.lflang.generator.MainContext) Properties(java.util.Properties)

Example 2 with DefaultErrorReporter

use of org.lflang.DefaultErrorReporter in project lingua-franca by lf-lang.

the class LinguaFrancaDependencyAnalysisTest method circularInstantiation.

/**
 * Check that circular instantiations are detected.
 */
@Test
public void circularInstantiation() throws Exception {
    // Java 17:
    // String testCase = """
    // target C;
    // 
    // reactor X {
    // reaction() {=
    // //
    // =}
    // p = new Y();
    // }
    // 
    // reactor Y {
    // q = new X();
    // }
    // """
    // Java 11:
    String testCase = String.join(System.getProperty("line.separator"), "target C;", "", "reactor X {", "    reaction() {=", "    //", "    =}", "    p = new Y();", "}", "", "reactor Y {", "    q = new X();", "}");
    Model model = parser.parse(testCase);
    Assertions.assertNotNull(model);
    ModelInfo info = new ModelInfo();
    info.update(model, new DefaultErrorReporter());
    Assertions.assertTrue(info.instantiationGraph.hasCycles() == true, "Did not detect cyclic instantiation.");
}
Also used : DefaultErrorReporter(org.lflang.DefaultErrorReporter) ModelInfo(org.lflang.ModelInfo) Model(org.lflang.lf.Model) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultErrorReporter

use of org.lflang.DefaultErrorReporter in project lingua-franca by lf-lang.

the class LinguaFrancaDependencyAnalysisTest method cyclicDependency.

/**
 * Check that circular dependencies between reactions are detected.
 */
@Test
public void cyclicDependency() throws Exception {
    // Java 17:
    // String testCase = """
    // target C;
    // 
    // reactor Clock {
    // timer t(0, 10 msec);
    // input x:int;
    // output y:int;
    // reaction(t) -> y {=
    // 
    // =}
    // reaction(x) -> y {=
    // 
    // =}
    // }
    // 
    // reactor A {
    // input x:int;
    // output y:int;
    // reaction(x) -> y {=
    // 
    // =}
    // }
    // 
    // main reactor Loop {
    // c = new Clock();
    // a = new A();
    // c.y -> a.x;
    // a.y -> c.x;
    // }
    // """
    // Java 11:
    String testCase = String.join(System.getProperty("line.separator"), "target C;", "", "reactor Clock {", "    timer t(0, 10 msec);", "    input x:int;", "    output y:int;", "    reaction(t) -> y {=", "        ", "    =}", "    reaction(x) -> y {=", "        ", "    =}", "}", "", "reactor A {", "    input x:int;", "    output y:int;", "    reaction(x) -> y {=", "        ", "    =}", "}", "", "main reactor Loop {", "    c = new Clock();", "    a = new A();", "    c.y -> a.x;", "    a.y -> c.x;", "}");
    Model model = parser.parse(testCase);
    Assertions.assertNotNull(model);
    Instantiation mainDef = null;
    TreeIterator<EObject> it = model.eResource().getAllContents();
    while (it.hasNext()) {
        EObject obj = it.next();
        if (!(obj instanceof Reactor)) {
            continue;
        }
        Reactor reactor = (Reactor) obj;
        if (reactor.isMain()) {
            // Creating an definition for the main reactor because
            // there isn't one.
            mainDef = LfFactory.eINSTANCE.createInstantiation();
            mainDef.setName(reactor.getName());
            mainDef.setReactorClass(reactor);
        }
    }
    ReactorInstance instance = new ReactorInstance(toDefinition(mainDef.getReactorClass()), new DefaultErrorReporter());
    Assertions.assertFalse(instance.getCycles().isEmpty());
}
Also used : DefaultErrorReporter(org.lflang.DefaultErrorReporter) ReactorInstance(org.lflang.generator.ReactorInstance) EObject(org.eclipse.emf.ecore.EObject) Model(org.lflang.lf.Model) Instantiation(org.lflang.lf.Instantiation) Reactor(org.lflang.lf.Reactor) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultErrorReporter (org.lflang.DefaultErrorReporter)3 Test (org.junit.jupiter.api.Test)2 Model (org.lflang.lf.Model)2 Inject (com.google.inject.Inject)1 Injector (com.google.inject.Injector)1 Provider (com.google.inject.Provider)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1