Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext Unit tests with JUnitParam
Xtext Unit tests with JUnitParam [message #1699672] Thu, 25 June 2015 16:58 Go to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey,

I do some testing with Xtext. Somehow I ended up with a lot of boilerplate code and would like to get rid of it by using JUnitParam. Both Xtext and JUnitParam require the use of @RunWith annotation.

@RunWith(JUnitParamsRunner.class)
@RunWith(XtextRunner)
class MyDslValidationTest {
   // test code ... 
}


This will not work ... Sad How do I get both to work Smile ?

Thank you.

Kon
Re: Xtext Unit tests with JUnitParam [message #1699673 is a reply to message #1699672] Thu, 25 June 2015 17:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi you can do the injecting manually

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Unit tests with JUnitParam [message #1699927 is a reply to message #1699673] Mon, 29 June 2015 10:22 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Christian,

is there an example somewhere? I could find anything on the web Sad Thank you.
Re: Xtext Unit tests with JUnitParam [message #1699932 is a reply to message #1699927] Mon, 29 June 2015 11:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Injector i = new MyDslStandaloneSetup().createInjectorAndDoEmfRegistration();
i.injectMembers(...)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Unit tests with JUnitParam [message #1699978 is a reply to message #1699932] Mon, 29 June 2015 14:04 Go to previous message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Thank you Christian Smile Just in case if someone else is having the same issue:

import com.google.inject.Inject
import junitparams.JUnitParamsRunner
import junitparams.Parameters
import org.eclipse.xtext.formatting.INodeModelFormatter
import org.eclipse.xtext.formatting.impl.DefaultNodeModelFormatter
import org.eclipse.xtext.junit4.util.ParseHelper
import org.eclipse.xtext.resource.XtextResource
import org.eclipse.xtext.util.Pair
import org.eclipse.xtext.util.Tuples
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

import static extension org.assertj.core.api.Assertions.*

@RunWith(JUnitParamsRunner)
class TestFormatterTest {

	@Inject extension ParseHelper<Model> parseHelper = new ParseHelper
	@Inject extension INodeModelFormatter nodeModelFormatter = new DefaultNodeModelFormatter

	@Before
	def void before() {
		var i = new TestStandaloneSetup().createInjectorAndDoEMFRegistration
		i.injectMembers(parseHelper)
		i.injectMembers(nodeModelFormatter)
	}

	@Test
	@Parameters
	def void testFormat(Pair<CharSequence, CharSequence> pair) {
		pair.first.assertFormattedAs(pair.second)
	}

	def static parametersForTestFormat() {
		#[
			Tuples.create('''test { bla }''',
					'''test {
                                                bla
                                               }''')
		]
	}

	def void assertFormattedAs(CharSequence input, CharSequence expected) {
		(input.parse.eResource as XtextResource).parseResult.rootNode.format(0, input.length).formattedText.assertThat.
			isEqualTo(expected)
	}
}

[Updated on: Mon, 29 June 2015 14:04]

Report message to a moderator

Previous Topic:Xtext parser/scoping understanding issue
Next Topic:Serialize extended Model with its parent language serializer.
Goto Forum:
  


Current Time: Fri Apr 26 15:21:11 GMT 2024

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

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

Back to the top