Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unit Test cross reference from one DSL to another DSL
Unit Test cross reference from one DSL to another DSL [message #1436114] Thu, 02 October 2014 12:30 Go to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
Hey,

I have a junit test for my DSL that should test a specific part where I can reference an element from another DSL. How would I be able to come up with something I could reference? I.e. I'm inside a unit test for DSL 'b', and need an element of DSL 'a' that I can reference to test whether referencing & validating works...

Edit: I already tried putting a test.a file with an element of DSL 'a' next to the source file for the test, with no effect...

Cheers,
Markus

[Updated on: Thu, 02 October 2014 12:32]

Report message to a moderator

Re: Unit Test cross reference from one DSL to another DSL [message #1436124 is a reply to message #1436114] Thu, 02 October 2014 12:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11311
Registered: July 2009
Senior Member
Hi,

in standalone mode you have (general question general answer)

- make sure all standalonesetups are called (see injectorprovider)
- manually load all relevant resources to the resourceset


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Unit Test cross reference from one DSL to another DSL [message #1436148 is a reply to message #1436124] Thu, 02 October 2014 13:26 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
What is "standalone mode" in the context of a junit test? I think my question was not so general, right?

- I have a unit test for one DSL
- I want to test a reference to another DSL
- How can come up with a model element for the "other DSL"
- either programmatically (EMF Factory)
- by loading a resource of that type.

do i really need to call standalone setups in junit tests? I see there is a whole lot of infrastructure for Xtext junit tests, that does much to setup for one DSL. Do I have to manually do everything again? Or is it enough to just create a fake URI with the correct ending and feed that to the resource set?
Re: Unit Test cross reference from one DSL to another DSL [message #1436156 is a reply to message #1436148] Thu, 02 October 2014 13:43 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Hi Markus,

"standalone" means no equinox and no plug-in registry. EMF has some registries that are populated through extension points, and which have to be manually populated when Equinox is not used.
I assume you are running stock junit test and not PDE junit tests (i.e. no Equinox).

You need to subclass the generated StandaloneSetup of your language like this:

public class MyDslInjectorProviderCustom extends MyDslInjectorProvider {
....
protected Injector internalCreateInjector() {
MyOtherLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();
return new MyDSLStandaloneSetup().createInjectorAndDoEMFRegistration();
}
}

than you use the custom injector provider like this:

@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProviderCustom)
class MyTest {
...
}
Re: Unit Test cross reference from one DSL to another DSL [message #1436159 is a reply to message #1436148] Thu, 02 October 2014 13:44 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3097
Registered: July 2009
Senior Member
Hi Markus,

did you try what is described in the docs at
https://www.eclipse.org/Xtext/documentation.html#testing ?

Kind regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Unit Test cross reference from one DSL to another DSL [message #1436166 is a reply to message #1436148] Thu, 02 October 2014 13:52 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
[Edit]: THANKS for the pointers, I will check those too.

based on your suggestion, I tried this now:

        private def createWDLPersistent() {
		val fake = URI.createFileURI("/some/where/over/the/rainbow.wdl")
		val rs = resourceSetProvider.get()
		
		val wdlInjector = new WDLStandaloneSetup().createInjector
		val resourceFactory = wdlInjector.getInstance(IResourceFactory)

		val r = resourceFactory.createResource(fake);
		rs.resources.add(r)
		
		try {
			r.load(
				new LazyStringInputStream(
					'''
						package tests;
						
						persistent TestPersistent {
							
						}
					'''), null)
			for(e : r.errors) {
				System.err.println(e)
			}
		} catch (IOException e) {
			throw new WrappedException(e)
		}
	}


and call that in the test:

        @Test
	def void parseDefaultOrdering() {
		createWDLPersistent()
		
		val m = parseAndValidate(
			'''
				parameter TestPara1 {
					default "Something";
					persistent-type tests.TestPersistent;
				}
			''')

		val p = m.elements.head as Parameter
		assertThat(p.^default.value, is(equalTo("Something")))
		assertThat((p.type as EntityRefDecl).type, is(any(Persistent)))
	}


to no effect. i still get java.lang.AssertionError: Expected no errors, but got :ERROR (org.eclipse.xtext.diagnostics.Diagnostic.Linking) 'Couldn't resolve reference to Persistent 'tests.TestPersistent'.' on EntityRefDecl

I also tried to pass the resource set out of the createWDLPersistent method and use that with ParseHelper to load the resource with the reference - no success

[Updated on: Thu, 02 October 2014 13:54]

Report message to a moderator

Re: Unit Test cross reference from one DSL to another DSL [message #1436171 is a reply to message #1436166] Thu, 02 October 2014 14:04 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
with your suggestions, I'm now able to load the resource for the second DSL without tricks, just by calling rs.createResource with an appropriate URI.

I've put this in the createWDLPersistent method to verify:

val root = r.contents.get(0) as ModelFragment
val p = root.elements.head as Persistent
System.err.println(p)


and that works (so loading the resource did what i expect, and i get this output:)

com.wamas.wdl.wDL.impl.PersistentImpl@1735af2d (name: TestPersistent) (category: UNSET)


I also test-wise tried to call a QualifiedNameProvider for the second DSL to see whether i misspelled the reference, the result is tests.TestPersistent, which is exactly what i reference (i checked and copy paste replaced the name to be really, really sure).

Still; i get the error that it is not able to resolve... Sad more hints?

[Updated on: Thu, 02 October 2014 14:04]

Report message to a moderator

Re: Unit Test cross reference from one DSL to another DSL [message #1436200 is a reply to message #1436171] Thu, 02 October 2014 14:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11311
Registered: July 2009
Senior Member
hi as i said before: you have to load all resources to the resource set.

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Unit Test cross reference from one DSL to another DSL [message #1436205 is a reply to message #1436200] Thu, 02 October 2014 14:55 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
but: i did that and nothing changed Sad i'll try again and check every bit...

Edit: it works now, thanks! I have no idea what changed. I did the exact same steps as before, and now it works Neutral

Thanks again!

[Updated on: Thu, 02 October 2014 14:57]

Report message to a moderator

Re: Unit Test cross reference from one DSL to another DSL [message #1436752 is a reply to message #1436205] Fri, 03 October 2014 11:00 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3097
Registered: July 2009
Senior Member
Am 02.10.14 16:55, schrieb Markus Duft:
> but: i did that and nothing changed :( i'll try again and check every
> bit...

Hi Markus,

I wrote a short blogpost on that topic.
http://zarnekow.blogspot.de/2014/10/testing-multiple-xtext-dsls.html

The example can be found on Github.
Testclass:
https://github.com/szarnekow/testing-multiple-dsls/blob/master/zarnekow.goodbye.tests/src/zarnekow/goodbye/tests/LinkingTest.xtend
InjectorProvider:
https://github.com/szarnekow/testing-multiple-dsls/blob/master/zarnekow.goodbye.tests/src/zarnekow/goodbye/tests/BothLanguagesInjectorProvider.xtend

You have to make sure that you configured the generator fragment for
Junit4 in your mwe2 file
(https://github.com/szarnekow/testing-multiple-dsls/blob/master/zarnekow.hello/src/zarnekow/hello/GenerateHelloDSL.mwe2#L111).
It generates the base injector providers for you.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Previous Topic:avoid @SuppressWarnings("all") in Xtend batch compiler
Next Topic:Unresolved proxy, make sure the EPackage has been registered
Goto Forum:
  


Current Time: Mon Oct 23 00:52:51 GMT 2017

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

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