Quickfix Unit Test [message #1695714] |
Mon, 18 May 2015 10:27  |
Eclipse User |
|
|
|
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 #1701994 is a reply to message #1695914] |
Fri, 17 July 2015 06:08   |
Eclipse User |
|
|
|
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 
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
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05159 seconds