Search in sources :

Example 1 with RegionWithCursor

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;
}
Also used : VarDef(org.eclipse.n4js.xpect.config.VarDef) Arrays(java.util.Arrays) URI(org.eclipse.emf.common.util.URI) ThisResource(org.eclipse.xpect.xtext.lib.setup.ThisResource) Inject(com.google.inject.Inject) ICommaSeparatedValuesExpectation(org.eclipse.xpect.expectation.ICommaSeparatedValuesExpectation) IStringDiffExpectation(org.eclipse.xpect.expectation.IStringDiffExpectation) ContentAssistXpectMethod(org.eclipse.n4js.xpect.ui.methods.contentassist.ContentAssistXpectMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceCleanUtil(org.eclipse.n4js.xpect.ui.common.XtextResourceCleanUtil) Logger(org.apache.log4j.Logger) Lists(com.google.common.collect.Lists) Config(org.eclipse.n4js.xpect.config.Config) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) N4ContentAssistProcessorTestBuilder(org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder) QuickFixXpectMethod(org.eclipse.n4js.xpect.ui.methods.quickfix.QuickFixXpectMethod) XpectImport(org.eclipse.xpect.XpectImport) ParameterParser(org.eclipse.xpect.parameter.ParameterParser) CommaSeparatedValuesExpectation(org.eclipse.xpect.expectation.CommaSeparatedValuesExpectation) IParseResult(org.eclipse.xtext.parser.IParseResult) XtextResource(org.eclipse.xtext.resource.XtextResource) StringDiffExpectation(org.eclipse.xpect.expectation.StringDiffExpectation) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) N4JSOffsetAdapter(org.eclipse.n4js.xpect.common.N4JSOffsetAdapter) QuickFixTestHelper.asString2(org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper.asString2) RegionWithCursor(org.eclipse.n4js.xpect.ui.methods.contentassist.RegionWithCursor) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) N4ContentAssistProcessorTestBuilderHelper(org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilderHelper) AssertionFailedError(junit.framework.AssertionFailedError) Display(org.eclipse.swt.widgets.Display) IParser(org.eclipse.xtext.parser.IParser) Collectors(java.util.stream.Collectors) XpectCommentRemovalUtil(org.eclipse.n4js.xpect.common.XpectCommentRemovalUtil) List(java.util.List) Provider(com.google.inject.Provider) StringReader(java.io.StringReader) XpEnvironmentData(org.eclipse.n4js.xpect.config.XpEnvironmentData) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Xpect(org.eclipse.xpect.runner.Xpect) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) AssertionFailedError(junit.framework.AssertionFailedError)

Example 2 with RegionWithCursor

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");
}
Also used : VarDef(org.eclipse.n4js.xpect.config.VarDef) EditorsUtil(org.eclipse.n4js.tests.util.EditorsUtil) Exceptions(org.eclipse.xtext.xbase.lib.Exceptions) ISetupInitializer(org.eclipse.xpect.setup.ISetupInitializer) Date(java.util.Date) IssueResolutionProvider(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProvider) Inject(com.google.inject.Inject) ICommaSeparatedValuesExpectation(org.eclipse.xpect.expectation.ICommaSeparatedValuesExpectation) IStringDiffExpectation(org.eclipse.xpect.expectation.IStringDiffExpectation) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) N4JSActivator(org.eclipse.n4js.ui.internal.N4JSActivator) Logger(org.apache.log4j.Logger) Config(org.eclipse.n4js.xpect.config.Config) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ConsumedIssues(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues) StringDiffExpectation(org.eclipse.xpect.expectation.StringDiffExpectation) RegionWithCursor(org.eclipse.n4js.xpect.ui.methods.contentassist.RegionWithCursor) EObject(org.eclipse.emf.ecore.EObject) Display(org.eclipse.swt.widgets.Display) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IssueResolution(org.eclipse.xtext.ui.editor.quickfix.IssueResolution) TestingResourceValidator(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.TestingResourceValidator) List(java.util.List) XpEnvironmentData(org.eclipse.n4js.xpect.config.XpEnvironmentData) Path(org.eclipse.core.runtime.Path) Optional(java.util.Optional) Xpect(org.eclipse.xpect.runner.Xpect) CheckMode(org.eclipse.xtext.validation.CheckMode) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) URI(org.eclipse.emf.common.util.URI) ThisResource(org.eclipse.xpect.xtext.lib.setup.ThisResource) ResourceTweaker(org.eclipse.n4js.xpect.common.ResourceTweaker) StringExpectation(org.eclipse.xpect.expectation.StringExpectation) IssuesByLine(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.IssuesByLine) Multimap(com.google.common.collect.Multimap) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) ValidationTestModuleSetup(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup) Lists(com.google.common.collect.Lists) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) IFile(org.eclipse.core.resources.IFile) QuickFixTestHelper(org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper) XpectImport(org.eclipse.xpect.XpectImport) ParameterParser(org.eclipse.xpect.parameter.ParameterParser) CommaSeparatedValuesExpectation(org.eclipse.xpect.expectation.CommaSeparatedValuesExpectation) GeneratorOption(org.eclipse.n4js.generator.GeneratorOption) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSOffsetAdapter(org.eclipse.n4js.xpect.common.N4JSOffsetAdapter) Severity(org.eclipse.xtext.diagnostics.Severity) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) XpectN4JSES5TranspilerHelper(org.eclipse.n4js.xpect.ui.common.XpectN4JSES5TranspilerHelper) XpectCommentRemovalUtil(org.eclipse.n4js.xpect.common.XpectCommentRemovalUtil) Issue(org.eclipse.xtext.validation.Issue) FileSetupContext(org.eclipse.xpect.xtext.lib.setup.FileSetupContext) ITokenAdapter(org.eclipse.xpect.expectation.IStringDiffExpectation.ITokenAdapter) Collections(java.util.Collections) IStringExpectation(org.eclipse.xpect.expectation.IStringExpectation) Assert.assertEquals(org.junit.Assert.assertEquals) SystemLoaderInfo(org.eclipse.n4js.runner.SystemLoaderInfo) ResourceTweaker(org.eclipse.n4js.xpect.common.ResourceTweaker) Date(java.util.Date) IOException(java.io.IOException) ConsumedIssues(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 3 with RegionWithCursor

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);
    }
}
Also used : RegionWithCursor(org.eclipse.n4js.xpect.ui.methods.contentassist.RegionWithCursor) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) N4ContentAssistProcessorTestBuilder(org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Aggregations

RegionWithCursor (org.eclipse.n4js.xpect.ui.methods.contentassist.RegionWithCursor)3 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)3 Xpect (org.eclipse.xpect.runner.Xpect)3 Lists (com.google.common.collect.Lists)2 Inject (com.google.inject.Inject)2 List (java.util.List)2 Logger (org.apache.log4j.Logger)2 URI (org.eclipse.emf.common.util.URI)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 N4JSOffsetAdapter (org.eclipse.n4js.xpect.common.N4JSOffsetAdapter)2 XpectCommentRemovalUtil (org.eclipse.n4js.xpect.common.XpectCommentRemovalUtil)2 Config (org.eclipse.n4js.xpect.config.Config)2 VarDef (org.eclipse.n4js.xpect.config.VarDef)2 XpEnvironmentData (org.eclipse.n4js.xpect.config.XpEnvironmentData)2 Display (org.eclipse.swt.widgets.Display)2 XpectImport (org.eclipse.xpect.XpectImport)2 CommaSeparatedValuesExpectation (org.eclipse.xpect.expectation.CommaSeparatedValuesExpectation)2 ICommaSeparatedValuesExpectation (org.eclipse.xpect.expectation.ICommaSeparatedValuesExpectation)2 IStringDiffExpectation (org.eclipse.xpect.expectation.IStringDiffExpectation)2 StringDiffExpectation (org.eclipse.xpect.expectation.StringDiffExpectation)2