Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » how to unit test of generator(how to unit test of generator)
how to unit test of generator [message #840721] Tue, 10 April 2012 12:47 Go to next message
Michal S is currently offline Michal SFriend
Messages: 74
Registered: July 2011
Member
Hello,
I am writing unit tests for my DSL. There is no problem with testing of scopes & validator, there is documentation for this on the XText webpage.
There is one more thing, which I would like to test: code generator... I have not found any how-to in this field.
My generator is written according to http://www.eclipse.org/Xtext/documentation/2_1_0/040-first-code-generator.php .
Is there any simple way to test generator output?
Thank you,
Michal
Re: how to unit test of generator [message #840754 is a reply to message #840721] Tue, 10 April 2012 13:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Simply call it with a test resource and a InMemoryFileSystemAccess
doesn't work?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to unit test of generator [message #840787 is a reply to message #840754] Tue, 10 April 2012 14:09 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
Michal,

I'm using Xtend to test the generator because the syntax is much more simple.

In principle you can simply @Inject the generator and call

InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess();
generator.doGenerate( resource, fsa );


I've written a GeneratorHelper that works along the same lines as the ParseHelper.

Christian, I'm willing to contribute this code because it makes testing very simple:

	@Test
	def void testMissingID() {
		parser.input('''
			options {
				inline: true
			}
			
			base-language de {}
		''')
		.expectError( "ERROR:Missing option 'id' (test.i18n line : 1)" )
	}

	
	@Test
	def void testLocalizedFormatter() {
		val model = parser.input(
			'''
			options {
				id: test.Formatters
				inline: true
			}

			formatter-definitions {
				yyyymmdd: DateFormatter( yyyymmdd )
			}
			
			base-language en {
				yyyymmdd: "yyyy-MM-dd"
			}
			''').uri( "test/Formatters.i18n" ).parse()
		
		"DateFormatter".assertEquals( model.formatterDefinitions.get( 0 ).type )
	}

	@Test
	def void testSimpleInput() {
		val model = generator.parse(
			'''
			options {
				/** id comment */ 
				id: test.SimpleInput
				inline: true
			}
			
			base-language en {
				/** Test for comments */
				comment: "This message has a comment"
			}
			''', URI::createURI( "test/SimpleInput.i18n" ) )
		
		generator.generate( model )
		
		generator.assertEquals( "PROPERTIEStest/SimpleInput.properties",  
"# Generated from platform:/resource/com.avanon.eclipse.i18n_dsl.demo/src/main/resources/test/SimpleInput.i18n
test.SimpleInput.comment=This message has a comment
" )
	}


Is there a chance to get that into Xtext?
Re: how to unit test of generator [message #840794 is a reply to message #840787] Tue, 10 April 2012 14:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Simply file an enhancement request with the code attached

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to unit test of generator [message #841375 is a reply to message #840794] Wed, 11 April 2012 08:17 Go to previous messageGo to next message
Michal S is currently offline Michal SFriend
Messages: 74
Registered: July 2011
Member
Thank you gentlemen,
InMemoryFileSystemAccess class was the information, which I missed before...
Hopefully, I will be able to manage now.
Regards,
Michal
Re: how to unit test of generator [message #847635 is a reply to message #840754] Tue, 17 April 2012 13:59 Go to previous messageGo to next message
Michal S is currently offline Michal SFriend
Messages: 74
Registered: July 2011
Member
Hello,
I have tried following:
@Inject MyDslGenerator generator
@Inject Provider<XtextResourceSet> resourceSetProvider
InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess()

...

val resource = resourceSetProvider.get.createResource(URI::createFileURI("xyz.mydsl"))
generator.doGenerate(resource,fsa)



When I debug the test, my doGenerate method is called. Problem is, that resource.getAllContents() is empty and therefore nothing is generated.
When I try to do this in my runtime of Eclipse (with copy of the same file), resource.getAllContents() is not empty and generator works.
What am I doing wrong?
Thank you & Best regards,
Michal
Re: how to unit test of generator [message #847680 is a reply to message #847635] Tue, 17 April 2012 14:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
please Post a complete test file

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to unit test of generator [message #847687 is a reply to message #847635] Tue, 17 April 2012 15:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Btw you never call resource.load

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to unit test of generator [message #847688 is a reply to message #847680] Tue, 17 April 2012 15:03 Go to previous messageGo to next message
Michal S is currently offline Michal SFriend
Messages: 74
Registered: July 2011
Member
My test file is as follows:
@RunWith(typeof(XtextRunner))
@InjectWith(typeof(MyDslInjectorProvider))
class GeneratorTest {
	@Inject
	ParseHelper<MyDslFramework> parser
	
	@Inject
	MyDslGenerator generator
	
	@Inject
	Provider<XtextResourceSet> resourceSetProvider
	
	InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess()
		
	@Test
	def testX(){
		val resource =resourceSetProvider.get.createResource(URI::createFileURI("xyz.mydsl"))
		generator.doGenerate(resource,fsa)
	}
}

(I understand, that I do not test anything there. I need to get something generated first...)
Thank you,
Michal
Re: how to unit test of generator [message #847697 is a reply to message #847688] Tue, 17 April 2012 15:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
http://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to unit test of generator [message #847809 is a reply to message #847697] Tue, 17 April 2012 17:18 Go to previous messageGo to next message
Michal S is currently offline Michal SFriend
Messages: 74
Registered: July 2011
Member
Thank you Christian, now my tests are working...
Michal
Re: how to unit test of generator [message #870536 is a reply to message #847809] Tue, 08 May 2012 15:08 Go to previous messageGo to next message
egger is currently offline eggerFriend
Messages: 13
Registered: November 2011
Junior Member
Hello Michal,

can you share our complete, working Text.xtend file? (also with imports please) i have some fundamental problems with my generator unit test.

best regards
Re: how to unit test of generator [message #870571 is a reply to message #870536] Tue, 08 May 2012 19:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

you question inspired me to this blog post: http://christiandietrich.wordpress.com/2012/05/08/unittesting-xtend-generators/

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to unit test of generator [message #1790233 is a reply to message #870571] Thu, 07 June 2018 10:14 Go to previous messageGo to next message
Anton Hughes is currently offline Anton HughesFriend
Messages: 66
Registered: January 2013
Member
Hi Christian

The above link is out of date.

Do you have a working link to that blog post?

Thanks
Re: how to unit test of generator [message #1790246 is a reply to message #1790233] Thu, 07 June 2018 11:17 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
https://dietrich-it.de/xtext/generator/tests/2012/05/08/unittesting-xtend-generators.html

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:LanguageServer with Apache HTTP Client, endless wait on read
Next Topic:Scoping on an external EMF Model
Goto Forum:
  


Current Time: Thu Mar 28 18:44:57 GMT 2024

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

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

Back to the top