use of org.eclipse.n4js.xpect.ui.methods.contentassist.RegionWithCursor 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.RegionWithCursor in project n4js by eclipse.
the class QuickFixXpectMethod method quickFixAndRun.
/*-
contentAssistList kind 'smart' at 'a.<|>methodA' display 'methodA2' --> 'methodA2(): any - A'
quickFix at 'a.<|>method' apply 'methodA2' fileValid --> a.<|>methodA2();
quickFixAndRun at 'a.<|>method' apply 'methodHelloWorld' --> Hello World
kind offset checkType selected mode
arg2 arg3
*/
/**
* Apply quick fix, compile and run the result. Compares the generated stdout-result to the expectation on the right
* hand side.
*
* @param expectation
* - expected output of running script, just stdout no error-stream. Expecting the error-stream to be
* empty.
* @param resource
* - injected resource
* @param offset
* - parsed arg2 - cursor position
* @param selected
* - parsed arg3 - selection from list of expectations
* @param offset2issue
* - injected Map of issues
* @param init
* - injected xpect-initizalizer
* @param fileSetupContext
* - injected xpect meta-info about file under test.
* @throws Exception
* in failure case
*/
@Xpect
@ParameterParser(syntax = "('at' arg2=STRING)? ('apply' arg3=STRING )?")
@ConsumedIssues({ Severity.INFO, Severity.ERROR, Severity.WARNING })
public void quickFixAndRun(// arg0
@StringExpectation(caseSensitive = true) IStringExpectation expectation, // arg1
@ThisResource XtextResource resource, // arg2
RegionWithCursor offset, // arg3
String selected, @IssuesByLine Multimap<Integer, Issue> offset2issue, ISetupInitializer<Object> init, FileSetupContext fileSetupContext) throws Exception {
try {
long timeStart = System.currentTimeMillis();
logger.info("Execution started: " + new Date(timeStart));
// System.out.println(
// "##-Qr-## we got it selected='" + selected + "' at " + offset + " in " + resource.toString() + "");
String executionResult;
ExecutionResult exRes = new ExecutionResult();
ResourceTweaker resourceTweaker = resourceToTweak -> {
try {
quickFix(null, resourceToTweak, offset, selected, "fileValid", "", offset2issue, false);
} catch (Exception e) {
Exceptions.sneakyThrow(e);
}
};
Display.getDefault().syncExec(() -> exRes.result = compileAndExecute(resource, init, fileSetupContext, resourceTweaker));
executionResult = exRes.result;
long timeEnd = System.currentTimeMillis();
logger.info("Execution finished: " + new Date(timeEnd));
logger.info("Execution took " + (timeEnd - timeStart + 0.0) / 1000.0 + " seconds.");
expectation.assertEquals(executionResult);
// Reset resource after quick fix application and code execution
resource.reparse(getContentForResourceUri(resource.getURI()));
} finally {
logger.info("Closing all editors");
EditorsUtil.forceCloseAllEditors();
}
logger.info("Successful End of Execution");
}
use of org.eclipse.n4js.xpect.ui.methods.contentassist.RegionWithCursor 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