use of org.eclipse.xpect.parameter.ParameterParser in project n4js by eclipse.
the class HyperlinkXpectMethod method hyperlinks.
/**
* @param expectation
* a list of expected URIs to jump to by this hyperlink
* @param resource
* the resource under test
* @param region
* the offset where hyperlinking should be invoked
* @throws Exception
* some exceptions
*/
@ParameterParser(syntax = "('at' arg2=STRING)?")
@Xpect
public void hyperlinks(// arg0
@CommaSeparatedValuesExpectation ICommaSeparatedValuesExpectation expectation, // arg1
@ThisResource XtextResource resource, // arg2
RegionWithCursor region) throws Exception {
ContentAssistProcessorTestBuilder fixture = n4ContentAssistProcessorTestBuilderHelper.createTestBuilderForResource(resource);
String currentModelToParse = resource.getParseResult().getRootNode().getText();
IXtextDocument xtextDocument = fixture.getDocument(resource, currentModelToParse);
// 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);
ITextViewer sourceViewer = fixture.getSourceViewer(currentModelToParse, xtextDocument);
IHyperlink[] hyperlinks = hyperlinkDetector.detectHyperlinks(sourceViewer, new Region(region.getGlobalCursorOffset(), 0), true);
// cleaned up resource, otherwise #createTestBuilder() above will fail next time this method is called
XtextResourceCleanUtil.cleanXtextResource(resource);
ArrayList<String> result = Lists.newArrayList();
if (hyperlinks != null) {
for (IHyperlink hyperlink : hyperlinks) {
result.add(getTargetDescription(resource, hyperlink));
}
}
expectation.assertEquals(result);
}
use of org.eclipse.xpect.parameter.ParameterParser 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.xpect.parameter.ParameterParser in project n4js by eclipse.
the class QuickFixXpectMethod method quickFixList.
/*-
contentAssistList kind 'smart' at 'a.<|>methodA' display 'methodA2' --> 'methodA2(): any - A'
quickFix at 'a.<|>method' apply 'methodA2' fileValid --> a.<|>methodA2();
kind offset checkType selected mode
arg2 arg3 arg4 arg5
*/
/**
* Example: {@code // XPECT quickFixList at 'a.<|>method' --> 'import A','do other things' }
*
* @param expectation
* comma separated strings, which are proposed as quick fix
* @param resource
* injected xtext-file
* @param offset
* cursor position at '<|>'
* @param checkType
* 'display': verify list of provided proposals comparing their user-displayed strings.
* @param selected
* which proposal to pick
* @param mode
* modus of operation
* @param offset2issue
* mapping of offset(!) to issues.
* @throws Exception
* if failing
*/
@Xpect
@ParameterParser(syntax = "('at' (arg2=STRING (arg3=ID (arg4=STRING)? (arg5=ID)? )? )? )?")
@ConsumedIssues({ Severity.INFO, Severity.ERROR, Severity.WARNING })
public void quickFixList(// arg0
@CommaSeparatedValuesExpectation(quoted = true, ordered = true) ICommaSeparatedValuesExpectation expectation, // arg1
@ThisResource XtextResource resource, // arg2
RegionWithCursor offset, // arg3
String checkType, // arg4
String selected, // arg5
String mode, @IssuesByLine Multimap<Integer, Issue> offset2issue) throws Exception {
List<IssueResolution> resolutions = collectAllResolutions(resource, offset, offset2issue);
List<String> resolutionNames = Lists.newArrayList();
for (IssueResolution resolution : resolutions) {
resolutionNames.add(resolution.getLabel());
}
expectation.assertEquals(resolutionNames);
}
use of org.eclipse.xpect.parameter.ParameterParser 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.xpect.parameter.ParameterParser in project n4js by eclipse.
the class ElementKeywordXpectMethod method elementKeyword.
/**
* Test the element keyword of an element. Examples of element keyword are getter, setter, field etc.
*/
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
@Xpect
public void elementKeyword(@StringExpectation IStringExpectation expectation, IEObjectCoveringRegion offset) {
EObject context = offset.getEObject();
// Identical behavior as in hover in the IDE! See class N4JSHoverProvider
// Get the cross-referenced element at the offset.
EObject element = offsetHelper.resolveCrossReferencedElementAt((XtextResource) context.eResource(), offset.getOffset());
// If not a cross-reference element, use context instead
if (element == null)
element = context;
String actual = calculateElementKeyword(element);
expectation.assertEquals(actual);
}
Aggregations