Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » test error with xtend
test error with xtend [message #900784] Wed, 08 August 2012 13:15 Go to next message
Laigle jérôme is currently offline Laigle jérômeFriend
Messages: 26
Registered: August 2012
Junior Member
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 17:56 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13661
Registered: July 2009
Senior Member
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


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:XbaseWithAnnotations - Error for parameters
Next Topic:Custom extensions when using EcoreGeneratorFragment?
Goto Forum:
  


Current Time: Sun Nov 29 00:16:49 GMT 2020

Powered by FUDForum. Page generated in 0.02255 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top