unit tests in the presence of imported packages [message #730528] |
Wed, 28 September 2011 14:53 |
|
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
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
|
|
Re: unit tests in the presence of imported packages [message #730547 is a reply to message #730536] |
Wed, 28 September 2011 15:32 |
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02600 seconds