Friday, October 3, 2014

Testing multiple Xtext DSLs

Recently there was a question in the Eclipse forum about testing multiple Xtext languages. The described scenario involves two language and one should have cross references to the other. Since this usecase caused some headaches in the past, Michael Vorburger provided a patch (Thanks for that!) that adds information about that particular topic to the official Xtext documentation. The updated docs are available since the recent 2.7 release. To provide some additional guidance, I hacked a small example that can be used as a blueprint if you want to get a jump start on that issue. This example also documents briefly how Xtext's unit test facilities can be used for effective language testing.

Key to testing the infrastructure of a DSL is the setup of a proper test environment. The Guice services have to be registered, EMF has to be initialized and obviously everything has to be cleaned up again after a test has been executed. For that purpose, Xtext provides a dedicated Junit4 test runner that uses an injector provider to do the initialization. The nice thing about that approach is that you can directly inject the necessary services into your unit test class. Exaclty as you are used to in your production code, too.

When it comes to testing multiple DSLs, basically the same infrastructure can be used, though you have to make sure, that all involved languages are properly initialized. For that purpose, a custom injector provider has to be derived from the one, that was already generated for your language. The to-be-written subclass needs to takes care of all the prerequisites and register the dependent languages. This mainly involves delegation to their injector providers.

Now that the setup is ready, we can test cross references between multiple DSLs. It is important to know that these references are only properly resolved if all models are available in the same resource set. That's why we need to use an explicit resource set in the tests. Besides that, it's the programming model that you know from Xtext and EMF in general.

A complete examplary test is available on Github.


Chunbaixue Yang said...

Hi, this post helped a lot, thanks!

aliya seen said...

The spelling and grammar check is going t be very useful because students are now free to get help online. Also there english will also be improved.

Anakin said...


I tried your example, but when I try to create the the Resource
val helloLang = resourceSet.createResource(URI.createURI("sample.hello"))

my helloLang is always null.

I added the MyLanguageStandaloneSetup.doSetup in the MySecondLanguageStandaloneSetup.

If I try your code of the BothLanguagesInjectorProvider, when I'm defining the Injector I get an error in the XTend.

What am I doing wrong? I added the firstLanguage as a dependency in the manifest and in the generator.mwe2 file