Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Testing multiple Xtext Dsl's
Testing multiple Xtext Dsl's [message #1701469] Mon, 13 July 2015 14:44 Go to next message
Neeraj Bhusare is currently offline Neeraj BhusareFriend
Messages: 177
Registered: July 2009
Location: Canada
Senior Member
Hi,

I am trying to write unit-tests for a set of simple dsl's [0] - allows defining classes [1] and functions [2]. These languages reuse a set of common rules defined in a base language [3]. I have a hypothetical usecase [4] that I am trying to test.

I have read this article [5] on unit-testing multple dsl's. I have defined a test injector provider [6] that calls the injectior providers inject method for the the other dsl's.

The problem I am facing is that the corss-references are not getting resolved. To be specific, the Library cross reference in the funciton definition [7] is not getting resolved. I have made sure that all the resources are loaded in a single resource set. The test injector provider makes sure that the emf registery is initialized properly.

Am I missing something here ??? Would be great if I could get some help. Tx.

[0] https://github.com/nbhusare/xtext-tests
[1] https://github.com/nbhusare/xtext-tests/blob/master/org.neclipse.xtext.testing.smallJava/src/org/neclipse/xtext/testing/smallJava/SmallJavaDsl.xtext
[2] https://github.com/nbhusare/xtext-tests/blob/master/org.neclipse.xtext.testing.smallfunc/src/org/neclipse/xtext/testing/smallfunc/SmallFuncDsl.xtext
[3] https://github.com/nbhusare/xtext-tests/blob/master/org.neclipse.xtext.testing.smallArtifact/src/org/neclipse/xtext/testing/smallArtifact/SmallArtifactDsl.xtext
[4] https://github.com/nbhusare/xtext-tests/blob/master/org.neclipse.xtext.testing.smallfunc/src/org/neclipse/xtext/testing/smallfunc/validation/SmallFuncDslValidator.xtend
[5] http://zarnekow.blogspot.in/2014/10/testing-multiple-xtext-dsls.html
[6] https://github.com/nbhusare/xtext-tests/blob/master/org.neclipse.xtext.testing.smallfunc.tests/src/org/neclipse/xtext/testing/smallfunc/tests/setup/SmallFuncDslTestInjectorProvider.xtend
[7] https://github.com/nbhusare/xtext-tests/blob/master/org.neclipse.xtext.testing.smallfunc.tests/src/org/neclipse/xtext/testing/smallfunc/tests/SmallFuncDslValidatorTest.xtend


Twitter : @NeerajBhusare
Blog : https://nbhusare.github.io/
Best regards, Neeraj
Re: Testing multiple Xtext Dsl's [message #1701490 is a reply to message #1701469] Mon, 13 July 2015 16:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

ParseHelper is not meant to be used in that usecase. you have to fix it to be able to handle it

@RunWith(XtextRunner)
@InjectWith(SmallFuncDslTestInjectorProvider)
class SmallFuncDslValidatorTest {

	@Inject extension SmartParseHelper<Namespace>

	@Inject extension ValidationTestHelper

	@Inject Provider<XtextResourceSet> resourceSetProvider

	@Test
	def void testCheckParamIfSmallClassHasAttributes() {
		val rs = resourceSetProvider.get

		val sc = '''
			namespace tests.smalllang
			class Library {
			}
		'''.parse(rs,"sjava")

		val smallFunc = '''
			namespace tests.smalllang
			import tests.smalllang.*
			def CheckLibraryHasBooks(Library library) {
			}
		'''.parse(rs).artifact as SmallFunction

		smallFunc.params.head => [
			assertError(SmallFuncDslPackage.Literals.PARAM, "", "Small class has no attributes...")
		]

	}

}

class SmartParseHelper<T extends EObject> extends ParseHelper<T> {
	
	@Inject
	IResourceFactory resourceFactory;
	
	def T parse(CharSequence text, ResourceSet resourceSetToUse, String fileExtension) throws Exception {
		return parse(getAsStream(text), computeUnusedUri(resourceSetToUse, fileExtension), null, resourceSetToUse, fileExtension) as T
	}
	
	def URI computeUnusedUri(ResourceSet resourceSet, String fileExtension) {
		val String name = "__synthetic";
		for (i : 0..Integer.MAX_VALUE) {
			val URI syntheticUri = URI.createURI(name + i + "." + fileExtension);
			if (resourceSet.getResource(syntheticUri, false) == null)
				return syntheticUri;
		}
		throw new IllegalStateException();
	}
	
	def T parse(InputStream in, URI uriToUse, Map<?, ?> options, ResourceSet resourceSet, String fileExtension) {
		val Resource resource = resourceSet.createResource(uriToUse);
		try {
			resource.load(in, options);
			val T root = if (resource.getContents().isEmpty()) null else resource.getContents().get(0) as T;
			return root;
		} catch (IOException e) {
			throw new WrappedException(e);
		}
		
	}
	
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Testing multiple Xtext Dsl's [message #1702028 is a reply to message #1701490] Fri, 17 July 2015 16:23 Go to previous message
Neeraj Bhusare is currently offline Neeraj BhusareFriend
Messages: 177
Registered: July 2009
Location: Canada
Senior Member
Thanks. It helped Smile

Twitter : @NeerajBhusare
Blog : https://nbhusare.github.io/
Best regards, Neeraj
Previous Topic:Unable to generate separate project in SimpleProjectWizardFragment
Next Topic:Serialising Xcore model to Xtext
Goto Forum:
  


Current Time: Tue Mar 19 02:04:36 GMT 2024

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

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

Back to the top