use of org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder in project n4js by eclipse.
the class ProposalXpectMethod method exactlyMatchingProposal.
/**
* Searches for the proposal matching to selected.
*
* Throws exception if there are more then one or no proposals matching 'selected' found.
*/
private ICompletionProposal exactlyMatchingProposal(RegionWithCursor offset, N4ContentAssistProcessorTestBuilder fixture, String selected) {
ICompletionProposal[] computeCompletionProposals = allProposalsAt(offset, fixture);
List<ICompletionProposal> candidates = Arrays.stream(computeCompletionProposals).filter(proposal -> proposal.getDisplayString().contains(selected)).collect(Collectors.toList());
if (candidates.size() > 1) {
throw new AssertionFailedError("The selection of contentassist is not precise enough more then one assist matched the selection '" + selected + "': " + asString2(candidates) + " Please be more precise.");
} else if (candidates.size() < 1) {
throw new AssertionFailedError("No content assist matching the selection '" + selected + "' found. Available are " + asString2(Arrays.asList(computeCompletionProposals)));
}
ICompletionProposal proposal = candidates.get(0);
return proposal;
}
use of org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder in project n4js by eclipse.
the class ProposalXpectMethod method checkProposals.
/**
* Iterates over all proposed completion entries at the given offset and checks whether its application would cause
* validation issues in the resource.
*
* @param expectation
* the expected proposals causing validation errors
* @param resource
* the resource under test
* @param offset
* the offset of where to invoke content assist (note: it must be named arg2 as Xpect injects the
* parameter values by position)
* @throws Exception
* some exception
*/
@ParameterParser(syntax = "'at' arg2=STRING")
@Xpect
public void checkProposals(@CommaSeparatedValuesExpectation ICommaSeparatedValuesExpectation expectation, @ThisResource XtextResource resource, RegionWithCursor offset) throws Exception {
N4ContentAssistProcessorTestBuilder fixture = n4ContentAssistProcessorTestBuilderHelper.createTestBuilderForResource(resource);
ICompletionProposal[] computeCompletionProposals = allProposalsAt(offset, fixture);
List<String> proposalsWithError = Lists.newArrayList();
for (int proposal = 0; proposal < computeCompletionProposals.length; proposal++) {
fixture = fixture.reset();
String content = resource.getParseResult().getRootNode().getText();
fixture = fixture.append(content);
IXtextDocument document = fixture.getDocument(resource, content);
int index = proposal;
String newContent = applyProposal(computeCompletionProposals[index], document);
IXtextDocument newXtextDocument = fixture.getDocument(getNewResource(newContent, resource.getURI()), content);
newXtextDocument.readOnly(new IUnitOfWork<Object, XtextResource>() {
@Override
public Object exec(XtextResource state) throws Exception {
EcoreUtil.resolveAll(state);
if (!state.getErrors().isEmpty()) {
proposalsWithError.add(computeCompletionProposals[index].getDisplayString());
} else if (!state.validateConcreteSyntax().isEmpty()) {
proposalsWithError.add(computeCompletionProposals[index].getDisplayString());
}
return null;
}
});
}
expectation.assertEquals(proposalsWithError);
}
use of org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder in project n4js by eclipse.
the class OrganizeImportXpectMethod method organizeImports.
/**
* Give the result as a multiline diff. If cancellation due to multiple possible resolution is expected, provide the
* expected Exception with 'XPECT organizeImports ambiguous "Exception-Message" -->'.
*
* If the parameter is not provided, always the first computed solution in the list will be taken
*
* @param ambiguous
* - String Expectation in {@link BreakException}
*/
@ParameterParser(syntax = "('ambiguous' arg0=STRING)?")
@Xpect
@ConsumedIssues({ Severity.INFO, Severity.ERROR, Severity.WARNING })
public void organizeImports(// arg0
String ambiguous, @StringDiffExpectation(whitespaceSensitive = false, allowSingleSegmentDiff = false, allowSingleLineDiff = false) IStringDiffExpectation expectation, @ThisResource XtextResource resource) throws Exception {
logger.info("organize imports ...");
boolean bAmbiguityCheck = ambiguous != null && ambiguous.trim().length() > 0;
Interaction iaMode = bAmbiguityCheck ? Interaction.breakBuild : Interaction.takeFirst;
try {
if (expectation == null) /* || expectation.isEmpty() */
{
// Cannot access the region which could be asked for it's length.
throw new AssertionFailedError("The test is missing a diff: // XPECT organizeImports --> [old string replaced|new string expected] ");
}
// capture text for comparison:
String beforeApplication = resource.getParseResult().getRootNode().getText();
N4ContentAssistProcessorTestBuilder fixture = n4ContentAssistProcessorTestBuilderHelper.createTestBuilderForResource(resource);
IXtextDocument xtextDoc = fixture.getDocument(resource, beforeApplication);
// in case of cross-file hyperlinks, we have to make sure the target resources are fully resolved
final ResourceSet resSet = resource.getResourceSet();
for (Resource currRes : new ArrayList<>(resSet.getResources())) {
N4JSResource.postProcess(currRes);
}
// Calling organize imports
Display.getDefault().syncExec(() -> imortsOrganizer.unsafeOrganizeDocument(xtextDoc, iaMode));
if (bAmbiguityCheck) {
// should fail if here
assertEquals("Expected ambiguous resolution to break the organize import command.", ambiguous, "");
}
// checking that no errors are left.
String textAfterApplication = xtextDoc.get();
// compare with expectation, it's a multiline-diff expectation.
String before = XpectCommentRemovalUtil.removeAllXpectComments(beforeApplication);
String after = XpectCommentRemovalUtil.removeAllXpectComments(textAfterApplication);
expectation.assertDiffEquals(before, after);
} catch (Exception exc) {
if (exc instanceof RuntimeException && exc.getCause() instanceof BreakException) {
String breakMessage = exc.getCause().getMessage();
assertEquals(ambiguous, breakMessage);
} else {
throw exc;
}
}
}
use of org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder in project n4js by eclipse.
the class ProposalXpectMethod method proposalChange.
// @formatter:off
/**
* Test the application of the given content assist entry. After applying the content assist there should be the
* expected diff produced when comparing the document before and after applying the content assist.
*
* The diff has the following semantics:
*
* " ": (whitespace) nothing changed
* "+ ": line was added
* "- ": line was deleted
* "| ": line was modified, displayed as [oldText|newText]
*
* If the given expected content assist entry cannot be found, an error message is inserted at the offset resulting
* in expectation mismatch and so also to a failing test.
*
* Also clf.
* https://github.com/meysholdt/Xpect/tree/master/org.eclipse.xtext.example.domainmodel.xpect.tests/src/org
* /eclipse/xtext/example/domainmodel/xpect/tests/modify for some more examples.
*
* /\* XPECT proposalChange 'methodA2' at 'methodA' ---
* (...)
* }
* }
* var A a = new A();
* |a.[methodA|methodA2methodA]
* --- *\/
*
* Please note, that the comparison always contains also some lines around the line that contains the diff.
* Xpect currently doesn't allow to configure the number of those lines.
*
* @param expectation
* the expected diff comparing the document before and after applying the content assist
* @param resource
* the resource under test
* @param arg2
* the offset of where to invoke content assist (note: it must be named arg2 as Xpect injects the
* parameter values by position)
* @param arg3
* the content assist entry to select
* @throws Exception
* some exception
*/
// @formatter:on
@ParameterParser(syntax = "(arg3=STRING 'at' arg2=STRING)?")
@Xpect
public void proposalChange(@StringDiffExpectation IStringDiffExpectation expectation, @ThisResource XtextResource resource, /* @ThisOffset int */
RegionWithCursor arg2, String arg3) throws Exception {
// val int offset = arg2;
RegionWithCursor offset = arg2;
String text = arg3;
// println("proposal change with offset="+arg2+" selection="+arg3)
N4ContentAssistProcessorTestBuilder fixture = n4ContentAssistProcessorTestBuilderHelper.createTestBuilderForResource(resource);
ICompletionProposal proposal = exactlyMatchingProposal(offset, fixture, text);
String before = resource.getParseResult().getRootNode().getText();
if (proposal != null) {
IXtextDocument document = fixture.getDocument(XtextResourceCleanUtil.cleanXtextResource(resource), before);
String after = applyProposal(proposal, document);
before = XpectCommentRemovalUtil.removeAllXpectComments(before);
after = XpectCommentRemovalUtil.removeAllXpectComments(after);
expectation.assertDiffEquals(before, after);
}
}
Aggregations