Search in sources :

Example 1 with Configurator

use of org.lflang.tests.Configurators.Configurator 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)

Aggregations

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 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1