public void assertCompilesTo(CharSequence source, final CharSequence expected) { try { compilationTestHelper.configureFreshWorkspace(); compilationTestHelper.assertCompilesTo(source, expected); } catch (IOException e) { Exceptions.sneakyThrow(e); } }
/** * creates a fresh temp directory and sets it as the workspace root. */ public void configureFreshWorkspace() { workspaceRoot = createFreshTempDir(); }
private void mkDir(File file) { if (!file.getParentFile().exists()) { mkDir(file.getParentFile()); } if (!file.exists()) { file.mkdir(); } }
public void compile(CharSequence source, final IAcceptor<CompilationResult> acceptor) { try { compilationTestHelper.configureFreshWorkspace(); compilationTestHelper.compile(source, new IAcceptor<CompilationTestHelper.Result>() { @Override public void accept(final Result t) {
/** * same as {@link #resourceSet(Pair...)} but without actually loading the created resources. */ public ResourceSet unLoadedResourceSet(@SuppressWarnings("unchecked") Pair<String,? extends CharSequence> ...resources ) throws IOException { XtextResourceSet result = newResourceSetWithUTF8Encoding(); for (Pair<String, ? extends CharSequence> entry : resources) { URI uri = copyToWorkspace(getSourceFolderPath()+"/"+entry.getKey(), entry.getValue()); Resource resource = result.createResource(uri); if (resource == null) throw new IllegalStateException("Couldn't create resource for URI "+uri+". Resource.Factory not registered?"); } return result; }
/** * Parses, validates and compiles the given source. Calls the given acceptor for each * resource which is generated from the source. * * @param source some code written in the language under test. * @param acceptor gets called once for each file generated in {@link IGenerator} * @throws IOException if the resource loading fails */ @SuppressWarnings("unchecked") public void compile(CharSequence source, IAcceptor<Result> acceptor) throws IOException { String fileName = "MyFile."+extensionProvider.getPrimaryFileExtension(); compile(resourceSet(new Pair<String, CharSequence>(fileName, source)), acceptor); }
@Inject private void setTemporaryFolder(TemporaryFolder folder) { this.temporaryFolder = folder; configureFreshWorkspace(); }
/** * Physically copies the given files to the currently used workspace root (a temporary folder). * Files are written with UTF-8 encoding. * @param workspacefilePath the workspace relative path * @param contents the file contents */ public URI copyToWorkspace(String workspacefilePath, CharSequence contents) { File fullPath = new File(workspaceRoot.getAbsolutePath()+"/"+workspacefilePath); if (fullPath.exists()) { fullPath.delete(); } else { mkDir(fullPath.getParentFile()); } URI uri = URI.createFileURI(fullPath.getAbsolutePath()); writeFileWithUTF8(uri.toFileString(), contents.toString()); return uri; }
/** * Asserts that the expected code is generated for the given source. * * @param source some valid source code written in the language under test * @param expected the expected Java source code. * @throws IOException if the resource loading fails */ public void assertCompilesTo(CharSequence source, final CharSequence expected) throws IOException { final boolean[] called = {false}; compile(source, new IAcceptor<CompilationTestHelper.Result>() { @Override public void accept(Result r) { Assert.assertEquals(expected.toString(), r.getSingleGeneratedCode()); called[0] = true; } }); Assert.assertTrue("Nothing was generated but the expectation was :\n"+expected, called[0]); }
result.setCheckMode(getCheckMode()); result.setResources(resourcesToCheck); result.setResourceSet(resourceSet); result.setOutputConfigurations(getOutputConfigurations()); result.doGenerate(); acceptor.accept(result);
/** * creates a fresh resource set with the given resources * * @param resources - pairs of file names and their contents * @return a ResourceSet, containing the given resources. * @throws IOException if the resource loading fails */ public ResourceSet resourceSet(@SuppressWarnings("unchecked") Pair<String,? extends CharSequence> ...resources ) throws IOException { XtextResourceSet result = newResourceSetWithUTF8Encoding(); FileProjectConfig projectConfig = new FileProjectConfig(new File(workspaceRoot,PROJECT_NAME), PROJECT_NAME); projectConfig.addSourceFolder("src"); ProjectConfigAdapter.install(result, projectConfig); for (Pair<String, ? extends CharSequence> entry : resources) { URI uri = copyToWorkspace(getSourceFolderPath()+"/"+entry.getKey(), entry.getValue()); Resource resource = result.createResource(uri); if (resource == null) throw new IllegalStateException("Couldn't create resource for URI "+uri+". Resource.Factory not registered?"); resource.load(result.getLoadOptions()); } return result; }
/** * Parses, validates and compiles the given sources. Calls the given acceptor for each * resource which is generated from the source. * * @param sources some inputs written in the language under test. * @param acceptor gets called once for each file generated in {@link IGenerator} * @throws IOException if the resource loading fails * * @since 2.8 */ @SuppressWarnings("unchecked") public void compile(Iterable<? extends CharSequence> sources, IAcceptor<Result> acceptor) throws IOException { int index = 0; List<Pair<String, ? extends CharSequence>> pairs = newArrayList(); for (CharSequence source : sources) { String fileName = "MyFile" + (++index) + "." + extensionProvider.getPrimaryFileExtension(); pairs.add(new Pair<String, CharSequence>(fileName, source)); } compile(resourceSet(((Pair<String, ? extends CharSequence>[])Conversions.unwrapArray(pairs, Pair.class))), acceptor); }