Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unit Test with @Traced
Unit Test with @Traced [message #1779402] Sat, 06 January 2018 13:52 Go to next message
Steve Hostettler is currently offline Steve HostettlerFriend
Messages: 81
Registered: June 2016
Member
Hello,

Trying to write unit test with @Traced . Before @Traced I did simple string comparison in my unit tests to check whether the generated code is ok. Using @Traced the generate methods return instances of TraceNode, the toString of which is generating strings with lots of TraceNode and TemplateNode which interfere with the comparison.

What is the recommended way of doing unit testing in that case. Is there a simple way to get the generated code without the extraneous TraceNode and TemplateNode?

Many thanks in advance
Re: Unit Test with @Traced [message #1779403 is a reply to message #1779402] Sat, 06 January 2018 13:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12187
Registered: July 2009
Senior Member
i cant follow you. have a look what generateTracedFile does. (org.eclipse.xtext.generator.trace.node.TracingSugar.generateTracedFile(IFileSystemAccess2, String, CompositeGeneratorNode))


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit Test with @Traced [message #1779404 is a reply to message #1779403] Sat, 06 January 2018 14:06 Go to previous messageGo to next message
Steve Hostettler is currently offline Steve HostettlerFriend
Messages: 81
Registered: June 2016
Member
Hello Cristian,

trying to unit test so not having everything end to end.

val code = '''
.... //Some DSL code
'''.parse(testModel)
		
		
//No syntax error
code.assertNoErrors

//Pick one of the statement I want to test
val statement = code.activities.get(0).eAllContents.filter(ForAllStatement).next
//Invoke the generation, because the generate method is annotated with @Traced, the return type is TraceNode
val result = statement.generate(new DefaultGeneratorContext(Mocker.mock(IFileSystemAccess2), null, null))

//The expected java code 
val expected = '''
...
'''

//Before adding @Traced, this was working because the generate method returned a string (or something of which the toString was straithforward). With @Traced, it does not work anymore because I get a lot of TraceNode in the string.
assertEquals(expected, result)


Does it clarify?
Re: Unit Test with @Traced [message #1779405 is a reply to message #1779404] Sat, 06 January 2018 14:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12187
Registered: July 2009
Senior Member
Have a look at the method I pointed you. This is how to get text for generator node

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit Test with @Traced [message #1779406 is a reply to message #1779405] Sat, 06 January 2018 14:17 Go to previous message
Steve Hostettler is currently offline Steve HostettlerFriend
Messages: 81
Registered: June 2016
Member
Thanks Cristian,

for the sake of documenting it : here is the previous code revisited.
simply inject the GeneratorNodeProcessor and use it before doing the comparison
@Inject
protected GeneratorNodeProcessor processor;

val code = '''
.... //Some DSL code
'''.parse(testModel)
		
		
//No syntax error
code.assertNoErrors

//Pick one of the statement I want to test
val statement = code.activities.get(0).eAllContents.filter(ForAllStatement).next
//Invoke the generation, because the generate method is annotated with @Traced, the return type is TraceNode
val result = processor.process(   statement.generate(new DefaultGeneratorContext(Mocker.mock(IFileSystemAccess2), null, null))  )

//The expected java code 
val expected = '''
...
'''

//Before adding @Traced, this was working because the generate method returned a string (or something of which the toString was straithforward). With @Traced, it does not work anymore because I get a lot of TraceNode in the string.
assertEquals(expected, result)
Previous Topic:Xtext Trace and TemplateNode
Next Topic:Use object as type instead of String
Goto Forum:
  


Current Time: Sun Aug 19 02:00:34 GMT 2018

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

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

Back to the top