Search in sources :

Example 1 with BreakException

use of org.eclipse.n4js.ui.organize.imports.BreakException 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;
        }
    }
}
Also used : Interaction(org.eclipse.n4js.ui.organize.imports.Interaction) ThisResource(org.eclipse.xpect.xtext.lib.setup.ThisResource) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) BreakException(org.eclipse.n4js.ui.organize.imports.BreakException) N4ContentAssistProcessorTestBuilder(org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) AssertionFailedError(junit.framework.AssertionFailedError) BreakException(org.eclipse.n4js.ui.organize.imports.BreakException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) ConsumedIssues(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Aggregations

ArrayList (java.util.ArrayList)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 N4JSResource (org.eclipse.n4js.resource.N4JSResource)1 BreakException (org.eclipse.n4js.ui.organize.imports.BreakException)1 Interaction (org.eclipse.n4js.ui.organize.imports.Interaction)1 N4ContentAssistProcessorTestBuilder (org.eclipse.n4js.xpect.ui.methods.contentassist.N4ContentAssistProcessorTestBuilder)1 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)1 Xpect (org.eclipse.xpect.runner.Xpect)1 ThisResource (org.eclipse.xpect.xtext.lib.setup.ThisResource)1 ConsumedIssues (org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1