Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Quickfix Unit Test
Quickfix Unit Test [message #1695714] Mon, 18 May 2015 10:27 Go to next message
Eclipse UserFriend
Hy,
i want to test my quick fix via a unit Test. Did someone knows a tutorial for that. Because i couldn't find anything about that.
Re: Quickfix Unit Test [message #1695763 is a reply to message #1695714] Tue, 19 May 2015 01:46 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

i dont know any tutorial on this and it is not quite easy. thus i fear you have to build this yourself. maybe using https://github.com/eclipse/xtext/tree/master/tests/org.eclipse.xtext.ui.tests/src-gen/org/eclipse/xtext/ui/tests/quickfix and/or https://github.com/eclipse/xtext/tree/master/tests/org.eclipse.xtext.xbase.ui.tests/src/org/eclipse/xtext/xbase/ui/tests/quickfix as starting point

the basic idea is to manually create a IModificationContext with a XtextDocument (a XtextDocument is created in Content Assist Tests for example)
and call the apply method of the IModification you get from the quickfix provider with that IModificationContext
Re: Quickfix Unit Test [message #1695887 is a reply to message #1695763] Wed, 20 May 2015 02:26 Go to previous messageGo to next message
Eclipse UserFriend
Thx for the fast answer, this helps me a lot!
Re: Quickfix Unit Test [message #1695914 is a reply to message #1695887] Wed, 20 May 2015 05:28 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Would you mind sharing your solution if you manage to make it work?
I tried something as Christian suggested, but i didn't manage to make it work yet.

Re: Quickfix Unit Test [message #1701994 is a reply to message #1695914] Fri, 17 July 2015 06:08 Go to previous messageGo to next message
Eclipse UserFriend
Hey,

I'm facing the same issue. I did some mocking being able to verify certain quick fix behavior. Here is my try:

class TestValidator extends AbstractTestValidator {
	@Check
	def checkLogin(Login o) {
		if (o.name.containsLowerCaseChars) {
			error(MSG__CAPITAL_LETTERS.format(o.name), TestPackage::eINSTANCE.login_Name, MSG__CAPITAL_LETTERS, o.name.toUpperCase)
		}
	}
}


@RunWith(XtextRunner)
@InjectWith(TestInjectorProvider)
class TestValidatorTest {

	@Inject extension ParseHelper<Model>
	@Inject extension ValidationTestHelper

	@Test
	def void testLogin_LowerCase() {
		val model = '''logins ADMIN admin2'''.initialisation.parse
		model.assertError(eINSTANCE.login, MSG__CAPITAL_LETTERS, MSG__CAPITAL_LETTERS.format('admin2'))
	}
}


class TestQuickfixProvider extends DefaultQuickfixProvider {

	@Fix(TestValidator::MSG__CAPITAL_LETTERS)
	def capitalizeName(Issue issue, IssueResolutionAcceptor acceptor) {
		val label = 'Capitalize login name'
		val description = 'Change login to "%s"'.format(issue.data.head)
		acceptor.accept(
			issue,
			label,
			description,
			null,
			[ context |
				val xd = context.getXtextDocument();
				xd.replace(issue.offset, issue.length, issue.data.head)
			]
		);
	}
}



import org.eclipse.xtext.ui.editor.model.IXtextDocument
import org.eclipse.xtext.ui.editor.model.edit.IModification
import org.eclipse.xtext.ui.editor.model.edit.IModificationContext
import org.eclipse.xtext.ui.editor.quickfix.Fix
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor
import org.eclipse.xtext.validation.Issue
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentCaptor

import static org.assertj.core.api.Assertions.*
import static org.mockito.Mockito.*

import static extension java.lang.String.*

class TestQuickfixProviderTest {

	TestQuickfixProvider testQuickfixProvider
	Issue issue
	IssueResolutionAcceptor issueResolutionAcceptor

	@Before
	def void before() {
		testQuickfixProvider = new TestQuickfixProvider
		issue = mock(Issue)
		issueResolutionAcceptor = mock(IssueResolutionAcceptor)
	}

	@Test
	def void testCapitalizeName() {
		val data = #['ADMIN']
		when(issue.data).thenReturn(data)
		val label = 'Capitalize login name'
		val description = 'Change login to "%s"'.format(data.head)

		testQuickfixProvider.capitalizeName(issue, issueResolutionAcceptor)

		assertQuickFix(label, description, data.head, 10, 5, "capitalizeName", MSG__CAPITAL_LETTERS)
	}

	def assertQuickFix(String label, String description, String result, int offset, int length, String method,
		String code) {
		val issueArg = ArgumentCaptor.forClass(Issue);
		val labelArg = ArgumentCaptor.forClass(String);
		val descriptionArg = ArgumentCaptor.forClass(String);
		val iconArg = ArgumentCaptor.forClass(String);
		val modificationArg = ArgumentCaptor.forClass(IModification);

		verify(issueResolutionAcceptor).accept(issueArg.capture, labelArg.capture, descriptionArg.capture,
			iconArg.capture, modificationArg.capture)

		assertThat(issueArg.value).isEqualTo(issue)
		assertThat(labelArg.value).isEqualTo(label)
		assertThat(descriptionArg.value).isEqualTo(description)
		assertThat(iconArg.value).isEqualTo(null)

		val m = mock(IModificationContext)
		val xd = mock(IXtextDocument)
		when(m.xtextDocument).thenReturn(xd)
		when(issue.offset).thenReturn(10)
		when(issue.length).thenReturn(5)

		modificationArg.value.apply(m)

		verify(xd).replace(offset, length, result)

		val methods = TestQuickfixProvider.getMethod(method, Issue, IssueResolutionAcceptor)
		val value = methods.getAnnotation(Fix).value

		assertThat(value).isEqualTo(code)
	}
}


It's verbose and luckily I do not have that many quick fixes. Note, there is a point of failure since the issue.data content is NOT evaluated. The following change won't be detected and leads to an NPE while performing the quick fix Sad

error(MSG__CAPITAL_LETTERS.format(o.name), TestPackage::eINSTANCE.login_Name, MSG__CAPITAL_LETTERS, o.name.toUpperCase)


to:

error(MSG__CAPITAL_LETTERS.format(o.name), TestPackage::eINSTANCE.login_Name, MSG__CAPITAL_LETTERS)


Does someone know a better solution?

Kon
Re: Quickfix Unit Test [message #1702001 is a reply to message #1701994] Fri, 17 July 2015 06:44 Go to previous messageGo to next message
Eclipse UserFriend
your issue mock thingy does not mock the code. why?
Re: Quickfix Unit Test [message #1702124 is a reply to message #1702001] Mon, 20 July 2015 03:53 Go to previous messageGo to next message
Eclipse UserFriend
Hey Christian,
I'm creating a real instance of my TestQuickfixProvider and mock the dependencies since I want to know if acts in the expected way. Mocking is just a utility a create surrounding context with certain conditions without introducing further "real" dependencies. Does this answer your question?

Kon
Re: Quickfix Unit Test [message #1702129 is a reply to message #1702124] Mon, 20 July 2015 04:44 Go to previous messageGo to next message
Eclipse UserFriend
no,

i asked: why do you not mock issue.getData

[Updated on: Mon, 20 July 2015 04:48] by Moderator

Re: Quickfix Unit Test [message #1702130 is a reply to message #1702129] Mon, 20 July 2015 04:46 Go to previous messageGo to next message
Eclipse UserFriend
P.s. maybe i do not understand what you actually try to test and what is not working
Re: Quickfix Unit Test [message #1702338 is a reply to message #1702130] Tue, 21 July 2015 11:46 Go to previous messageGo to next message
Eclipse UserFriend
Hey Christian,
I mocked issue.getData:

val data = #['ADMIN']
when(issue.data).thenReturn(data)


Apparently there is no common integration testing procedure for quick fixes (like for validation), so I share my solution that is working. It's just a "unit" test and it's verbose Smile
Re: Quickfix Unit Test [message #1702341 is a reply to message #1702338] Tue, 21 July 2015 11:49 Go to previous messageGo to next message
Eclipse UserFriend
It sounded like the code was not working
Re: Quickfix Unit Test [message #1799319 is a reply to message #1702341] Tue, 04 December 2018 11:09 Go to previous message
Eclipse UserFriend
The Xtext UI Test Infrastucture has been extended to enable the comfortable testing of Quickfixes. See Xtext 2.16 Release Notes
Previous Topic:Why my grammar don't validate the program and how to fix it
Next Topic:Xtext & Xtend 2.16 release
Goto Forum:
  


Current Time: Fri Jun 20 16:42:09 EDT 2025

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

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

Back to the top