Problems with linking in Xtext unit testing with multiple DSLs [message #1831781] |
Fri, 28 August 2020 20:45 |
Louis Detweiler Messages: 100 Registered: August 2017 |
Senior Member |
|
|
Hello,
I am trying to write unit tests for multiple DSLs which can link to each other. Basically they can define "concepts", with the availability to define sub/super concept relations with the syntax of concept Concept1 is a Concept2 meaning that the concept Concept1 is a subconcept of Concept2.
This is a example of what my test is like:
@RunWith(typeof(XtextRunner))
@InjectWith(typeof(DslTwoUITestInjectorProvider))
class DslTwoValidationTest {
@Inject
ValidationTestHelper validationTestHelper
@Inject
Provider<XtextResourceSet> resourceSetProvider
static val DSL_ONE = '''
DslOne 1.0
concept Concept1
'''
static val DSL_TWO = '''
DslTwo 1.0
concept Concept2 is a Concept3
'''
@Test
def void test() {
val resourceSet = resourceSetProvider.get
val dslOne = resourceSet.createResource(URI.createURI("dslOne.dslOne")) as XtextResource
dslOne.load(new StringInputStream(DSL_ONE), emptyMap)
val dslTwo = resourceSet.createResource(URI.createURI("dslTwo.dslTwo")) as XtextResource
validationTestHelper.assertNoErrors(dsl Two)
}
}
With my injector provider as follows
public class DslTwoUITestInjectorProvider implements IInjectorProvider, IRegistryConfigurator {
private GlobalStateMemento stateBeforeInjectorCreation;
private GlobalStateMemento stateAfterInjectorCreation;
private Injector injector;
private Injector dslOneInjector;
private static DslTwoUITestInjectorProvider instance;
static {
GlobalRegistries.initializeDefaults();
}
@Override
public Injector getInjector() {
if (injector == null) {
stateBeforeInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
this.injector = internalCreateInjector();
instance = this;
stateAfterInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
}
return injector;
}
public Injector getDslOneInjectorInjector() {
return dslOneInjector;
}
private Injector internalCreateInjector() {
dslOneInjector = new DslOneUiInjectorProvider().getInjector();
return new DslTwoUiInjectorProvider().getInjector();
}
@Override
public void restoreRegistry() {
stateBeforeInjectorCreation.restoreGlobalState();
}
@Override
public void setupRegistry() {
getInjector();
stateAfterInjectorCreation.restoreGlobalState();
}
public static DslTwoUITestInjectorProvider getInstance() {
return instance;
}
}
When dslOne is loaded, in my code a model object of DSLTwo is created and stored in the contents of the dslOne resource, and the created DSLTwo looks like this:
So the dslTwo resource should by linking have access to Concept3. However, when I run the test, it fails because there is an error on the dslTwo resource that states that it could not resolve the reference to concept Concept3. When I actually run my application and do the same thing that the test is doing, the dslTwo resource does not have a problem resolving the reference to concept Concept3.
Any ideas what is going wrong?
[Updated on: Fri, 28 August 2020 20:46] Report message to a moderator
|
|
|
|
Re: Problems with linking in Xtext unit testing with multiple DSLs [message #1831909 is a reply to message #1831782] |
Wed, 02 September 2020 12:20 |
Neeraj Bhusare Messages: 177 Registered: July 2009 Location: Canada |
Senior Member |
|
|
Hi Louis,
I have created a sample project to demonstrate how you can write unit-tests for DSL's, where one DSL has reference to the other - https://bit.ly/2ESE08C
Here is the link to the Validator (https://bit.ly/2YVuaKg), and the Parser (https://bit.ly/2ESveHL) unit-tests. Here is the link to the parser helper - https://bit.ly/34Vrr7l
Let me try to elaborate Christian's comments - 1) The Validator belongs to the runtime (AKA Non UI/standalone) project. Typically, the code and the unit-tests should not have any UI dependencies. All the UI integration code and the tests should go in the UI and the ui.tests project. 2) In Xtext/Eclipse, the visibility between the DSL's is controlled through a Container. A project represents a container, and all its references are visible containers.
Refer to the section "About the Index, Containers and Their Manager" in the Xtext documentation to learn more about the above concept - https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html.
I hope this helps.
Twitter : @NeerajBhusare
Blog : https://nbhusare.github.io/
Best regards, Neeraj
[Updated on: Wed, 02 September 2020 13:33] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.02738 seconds