Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problems with linking in Xtext unit testing with multiple DSLs
Problems with linking in Xtext unit testing with multiple DSLs [message #1831781] Fri, 28 August 2020 20:45 Go to next message
Louis Detweiler is currently offline Louis DetweilerFriend
Messages: 69
Registered: August 2017
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:

concept Concept3


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 #1831782 is a reply to message #1831781] Fri, 28 August 2020 21:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13637
Registered: July 2009
Senior Member
Hi, is there any reason you do standalone / resourceset based tests with ui injectors providers?
In Ui visibility is done via projects, not resourceset


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
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 Go to previous messageGo to next message
Neeraj Bhusare is currently offline Neeraj BhusareFriend
Messages: 164
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

Re: Problems with linking in Xtext unit testing with multiple DSLs [message #1832175 is a reply to message #1831909] Thu, 10 September 2020 23:15 Go to previous message
Louis Detweiler is currently offline Louis DetweilerFriend
Messages: 69
Registered: August 2017
Member
Hello,

Thank you for the quick response. I was wondering if you could point me to some UI tests that have linking between multiple DSLs, where they are all contained in the same project for visibility?

Or if there are no examples of that, could you point me to where I could learn how to programmatically create Xtext projects in a UI test that would contain my resource set?

The RCP we are developing is set up so that I believe I need to run these validation tests as UI tests, even if that is not best practice, as some things are done when the RCP is running that I would like to test in my validation tests (for example we have built-in DSL files that are created at runtime when other DSL files are created, and while I could theoretically manually create them and run the validation test not as a UI test I would like to include this aspect in my test)

Best,

Louis
Previous Topic:How to get the 'Name/ID' of any EObject in the workspace
Next Topic:Problem installing 2.23.0 on Eclipse 2020-06
Goto Forum:
  


Current Time: Sat Oct 24 15:53:07 GMT 2020

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

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

Back to the top