Package org.eclipse.xtext.xbase.testing
Class CompilationTestHelper
- java.lang.Object
-
- org.eclipse.xtext.xbase.testing.CompilationTestHelper
-
public class CompilationTestHelper extends java.lang.ObjectA utility class for testing Xtext languages that compile to Java code. It's designed to be used as an injected extension in unit tests written in Xtend. Example:@RunWith(XtextRunner) @InjectWith(MyLanguageInjectorProvider) class CompilerTest { @Rule @Inject public TemporaryFolder temporaryFolder @Inject extension CompilationTestHelper @Test def void myTest() { ''' // DSL code Foo bla '''.assertCompilesTo(''' class Foo { String bla } ''' } }- Since:
- 2.7
- Noextend:
- This class is not intended to be subclassed by clients.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCompilationTestHelper.ResultA result contains information about various aspects of a compiled piece of code.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringPROJECT_NAME
-
Constructor Summary
Constructors Constructor Description CompilationTestHelper()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidassertCompilesTo(java.lang.CharSequence source, java.lang.CharSequence expected)Asserts that the expected code is generated for the given source.voidcompile(java.lang.CharSequence source, org.eclipse.xtext.util.IAcceptor<CompilationTestHelper.Result> acceptor)Parses, validates and compiles the given source.voidcompile(java.lang.Iterable<? extends java.lang.CharSequence> sources, org.eclipse.xtext.util.IAcceptor<CompilationTestHelper.Result> acceptor)Parses, validates and compiles the given sources.voidcompile(org.eclipse.emf.ecore.resource.ResourceSet resourceSet, org.eclipse.xtext.util.IAcceptor<CompilationTestHelper.Result> acceptor)Parses, validates and compiles the given source.voidconfigureFreshWorkspace()creates a fresh temp directory and sets it as the workspace root.org.eclipse.emf.common.util.URIcopyToWorkspace(java.lang.String workspacefilePath, java.lang.CharSequence contents)Physically copies the given files to the currently used workspace root (a temporary folder).protected java.io.FilecreateFreshTempDir()protected org.eclipse.xtext.validation.CheckModegetCheckMode()protected java.lang.Iterable<? extends org.eclipse.xtext.generator.OutputConfiguration>getOutputConfigurations()protected java.lang.StringgetSourceFolderPath()org.eclipse.emf.ecore.resource.ResourceSetresourceSet(org.eclipse.xtext.xbase.lib.Pair<java.lang.String,? extends java.lang.CharSequence>... resources)creates a fresh resource set with the given resourcesvoidsetJavaCompilerClassPath(java.lang.Class<?>... classes)Add the class path entries of the given classes to the java compiler's class path.voidsetJavaCompilerClassPath(java.lang.ClassLoader classLoader)voidsetJavaVersion(org.eclipse.xtext.util.JavaVersion javaVersion)Sets the Java version both for the DSL generator, for example, Xbase compiler, and for the Java compiler.org.eclipse.emf.ecore.resource.ResourceSetunLoadedResourceSet(org.eclipse.xtext.xbase.lib.Pair<java.lang.String,? extends java.lang.CharSequence>... resources)same asresourceSet(Pair...)but without actually loading the created resources.
-
-
-
Field Detail
-
PROJECT_NAME
public static final java.lang.String PROJECT_NAME
- See Also:
- Constant Field Values
-
-
Method Detail
-
configureFreshWorkspace
public void configureFreshWorkspace()
creates a fresh temp directory and sets it as the workspace root.
-
getSourceFolderPath
protected java.lang.String getSourceFolderPath()
-
createFreshTempDir
protected java.io.File createFreshTempDir()
-
setJavaCompilerClassPath
public void setJavaCompilerClassPath(java.lang.Class<?>... classes)
Add the class path entries of the given classes to the java compiler's class path.
-
setJavaCompilerClassPath
public void setJavaCompilerClassPath(java.lang.ClassLoader classLoader)
- Since:
- 2.9
-
setJavaVersion
public void setJavaVersion(org.eclipse.xtext.util.JavaVersion javaVersion)
Sets the Java version both for the DSL generator, for example, Xbase compiler, and for the Java compiler.- Since:
- 2.11
-
assertCompilesTo
public void assertCompilesTo(java.lang.CharSequence source, java.lang.CharSequence expected) throws java.io.IOExceptionAsserts that the expected code is generated for the given source.- Parameters:
source- some valid source code written in the language under testexpected- the expected Java source code.- Throws:
java.io.IOException- if the resource loading fails
-
compile
public void compile(java.lang.CharSequence source, org.eclipse.xtext.util.IAcceptor<CompilationTestHelper.Result> acceptor) throws java.io.IOExceptionParses, validates and compiles the given source. Calls the given acceptor for each resource which is generated from the source.- Parameters:
source- some code written in the language under test.acceptor- gets called once for each file generated inIGenerator- Throws:
java.io.IOException- if the resource loading fails
-
compile
public void compile(java.lang.Iterable<? extends java.lang.CharSequence> sources, org.eclipse.xtext.util.IAcceptor<CompilationTestHelper.Result> acceptor) throws java.io.IOExceptionParses, validates and compiles the given sources. Calls the given acceptor for each resource which is generated from the source.- Parameters:
sources- some inputs written in the language under test.acceptor- gets called once for each file generated inIGenerator- Throws:
java.io.IOException- if the resource loading fails- Since:
- 2.8
-
compile
public void compile(org.eclipse.emf.ecore.resource.ResourceSet resourceSet, org.eclipse.xtext.util.IAcceptor<CompilationTestHelper.Result> acceptor)Parses, validates and compiles the given source. Calls the given acceptor for each resource which is generated from the source.- Parameters:
resourceSet- - theResourceSetto useacceptor- gets called once for each file generated inIGenerator
-
getCheckMode
protected org.eclipse.xtext.validation.CheckMode getCheckMode()
- Since:
- 2.8
-
getOutputConfigurations
protected java.lang.Iterable<? extends org.eclipse.xtext.generator.OutputConfiguration> getOutputConfigurations()
-
resourceSet
public org.eclipse.emf.ecore.resource.ResourceSet resourceSet(org.eclipse.xtext.xbase.lib.Pair<java.lang.String,? extends java.lang.CharSequence>... resources) throws java.io.IOExceptioncreates a fresh resource set with the given resources- Parameters:
resources- - pairs of file names and their contents- Returns:
- a ResourceSet, containing the given resources.
- Throws:
java.io.IOException- if the resource loading fails
-
copyToWorkspace
public org.eclipse.emf.common.util.URI copyToWorkspace(java.lang.String workspacefilePath, java.lang.CharSequence contents)Physically copies the given files to the currently used workspace root (a temporary folder). Files are written with UTF-8 encoding.- Parameters:
workspacefilePath- the workspace relative pathcontents- the file contents
-
unLoadedResourceSet
public org.eclipse.emf.ecore.resource.ResourceSet unLoadedResourceSet(org.eclipse.xtext.xbase.lib.Pair<java.lang.String,? extends java.lang.CharSequence>... resources) throws java.io.IOExceptionsame asresourceSet(Pair...)but without actually loading the created resources.- Throws:
java.io.IOException
-
-