test error with xtend [message #900784] |
Wed, 08 August 2012 09:15  |
Eclipse User |
|
|
|
Hi,
I'm working on a DSL and i want to test with xtend my code
It's just a part of my dsl:
Program:
'program' name=STRING
'end'
;
terminal ID : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*;
terminal STRING :
'"' ( '\\' ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') | !('\\'|'"') )* '"' |
"'" ( '\\' ('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') | !('\\'|"'") )* "'";
terminal SL_COMMENT : (';') !('\n'|'\r')* ('\r'? '\n')?;
terminal WS : (' '|'\t'|'\r'|'\n')+;
i want to test the error of this test but i don't know how i can do it
@RunWith(typeof(XtextRunner))
@InjectWith(typeof(LanguageInjectorProvider))
class test{
@Inject extension ParseHelper<Program>
@Test def void testBadProgramDeclaration() {
val compilationUnit =
'''
program "MyProgramme"
ghjggjh
end_bad
'''.parse
Assert::assertNotNull(compilationUnit)
}
}
this test should return two errors ghjggjh and end_bad but i don't know how test this error
Maybe somebody has an idea or can point me into the right direction? Thanks in advance!
Best regards,
Jérôme
|
|
|
Re: test error with xtend [message #900856 is a reply to message #900784] |
Wed, 08 August 2012 13:56  |
Eclipse User |
|
|
|
Hi,
the parseHelper is not meant for testing specific errors.
you need to build your own one e.g.
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.junit4.util.ParseHelper;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.XtextResourceSet;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class ParseHelper2<T extends EObject> extends ParseHelper<T> {
@Inject
private IResourceFactory resourceFactory;
@Inject
private Provider<XtextResourceSet> resourceSetProvider;
public List<Diagnostic> parseToError(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet) {
Resource resource = resourceFactory.createResource(uriToUse);
resourceSet.getResources().add(resource);
try {
resource.load(in, options);
if (resource.getErrors().size() > 0) {
return resource.getErrors();
}
throw new IllegalStateException();
} catch (IOException e) {
throw new WrappedException(e);
}
}
public List<Diagnostic> parseToError(CharSequence text) throws Exception {
return parseToError(text, resourceSetProvider.get());
}
public List<Diagnostic> parseToError(CharSequence text, ResourceSet resourceSetToUse) throws Exception {
return parseToError(getAsStream(text), computeUnusedUri(resourceSetToUse), null, resourceSetToUse);
}
public List<Diagnostic> parseToError(CharSequence text, URI uriToUse, ResourceSet resourceSetToUse) throws Exception {
return parseToError(getAsStream(text), uriToUse, null, resourceSetToUse);
}
}
feel free to file an enhancement request.
~Christian
|
|
|
Powered by
FUDForum. Page generated in 0.10926 seconds