Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » unit tests in the presence of imported packages
unit tests in the presence of imported packages [message #730528] Wed, 28 September 2011 14:53 Go to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

I'm developing a dsl (say BBB with extension bbb) which refers the
elements of another dsl (say AAA with extension aaa); in BBB I imported
the ecore package of AAA (and added the genmodel in the workflow) thus
in the new eclipse instance BBB can refer to AAA (using the default
xtext global scoping mechanism which uses all the files in the classpath).

However, for unit tests, which run in standalone mode, this mechanism
does not exist; and testing programs for BBB need to refer to AAA programs.

I found a solution but I wanted to make sure it is correct:

1. I load an AAA program with the injector for AAA
2. I create a resource for BBB and put it in the same resource set of
the program of AAA
3. I load the BBB program.

Here's the simplified snippet

Injector aaaInjector = new
AAAStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet =
aaaInjector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
Boolean.TRUE);
Resource resource = resourceSet.createResource(URI
.createURI("dummy:/example.aaa"));
InputStream in = new ByteArrayInputStream("delta D { }".getBytes());
resource.load(in, resourceSet.getLoadOptions());
EList<Diagnostic> errors = resource.getErrors();
// check possible errors

Injector bbbInjector = new
BBBStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet2 =
bbbInjector.getInstance(XtextResourceSet.class);
Resource resource2 = resourceSet2.createResource(URI
.createURI("dummy:/example.bbb"));

// put resource2 into the resource set of the first resource
resourceSet.getResources().add(resource2);
in = new ByteArrayInputStream("D with { empty }".getBytes());
// the 'D' in the bbb program refers to the 'D' in the aaa program
resource2.load(in, resourceSet.getLoadOptions());
errors = resource.getErrors();
// check possible errors

as I said it works, but it is correct?
Is there a cleaner solution?

thanks in advance
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Re: unit tests in the presence of imported packages [message #730530 is a reply to message #730528] Wed, 28 September 2011 15:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
No thats basically fine: in standalone the global scope is built of all resources in the resourceset.
of course you could try to use the methods in org.eclipse.xtext.junit.AbstractXtextTests to get more clearner test code

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: unit tests in the presence of imported packages [message #730536 is a reply to message #730530] Wed, 28 September 2011 15:10 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 09/28/2011 05:02 PM, Christian Dietrich wrote:
> No thats basically fine: in standalone the global scope is built of all
> resources in the resourceset.
> of course you could try to use the methods in
> org.eclipse.xtext.junit.AbstractXtextTests to get more clearner test code
>
> ~Christian

Hi

I usually use AbstractXtextTests (even in this case the injector for BBB
is actually initialize with the 'with' method in the overridden setup, I
just wanted to post a simplified snippet); however for the AAA injector
in the tests for BBB I cannot use AbstractXtextTests methods.

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Re: unit tests in the presence of imported packages [message #730547 is a reply to message #730536] Wed, 28 September 2011 15:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14677
Registered: July 2009
Senior Member
Hi,

yes you have actually do a little hack to the abstract test class to get it running with multiple files.
in your case this hack would look like

public class Test extends AbstractXtextTests {
	
	public void testIt() throws Exception {
		with(MyDslAStandaloneSetup.class);
		getModel("aelement A");
		with(MyDslBStandaloneSetup.class);
		getModel("belement B refs A");
	}
	
	XtextResourceSet rs = null;
	
	@Override
	public <T> T get(Class<T> clazz) {
		if (XtextResourceSet.class.equals(clazz)) {
			if (rs == null) {
				rs = super.get(XtextResourceSet.class);
			}
			return (T)rs;
		}
		return super.get(clazz);
	}

}



~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: unit tests in the presence of imported packages [message #731166 is a reply to message #730547] Fri, 30 September 2011 07:10 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
Oh cool!
I thought you shouldn't call 'with' twice and I didn't even try to;
thanks I'll try your solution!

cheers
Lorenzo

On 09/28/2011 05:32 PM, Christian Dietrich wrote:
> Hi,
>
> yes you have actually do a little hack to the abstract test class to get
> it running with multiple files.
> in your case this hack would look like
>
>
> public class Test extends AbstractXtextTests {
>
> public void testIt() throws Exception {
> with(MyDslAStandaloneSetup.class);
> getModel("aelement A");
> with(MyDslBStandaloneSetup.class);
> getModel("belement B refs A");
> }
>
> XtextResourceSet rs = null;
>
> @Override
> public <T> T get(Class<T> clazz) {
> if (XtextResourceSet.class.equals(clazz)) {
> if (rs == null) {
> rs = super.get(XtextResourceSet.class);
> }
> return (T)rs;
> }
> return super.get(clazz);
> }
>
> }
>
>
>
> ~Christian


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Previous Topic:Unique flags in generated ecore model file
Next Topic:part of model als content of xtext editor
Goto Forum:
  


Current Time: Mon May 06 09:37:10 GMT 2024

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

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

Back to the top