Search in sources :

Example 1 with TestingResourceValidator

use of org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.TestingResourceValidator in project n4js by eclipse.

the class QuickFixXpectMethod method quickFix.

/**
 * Choose quick fix and apply
 *
 * @param expectation
 *            from right hand side - expected changes to code, especially cursor position.
 * @param resource
 *            injected resource under test
 * @param offset
 *            parsed arg2 offset cursor position
 * @param selected
 *            parsed arg3 - chosen quick fix to apply
 * @param mode
 *            parsed arg4 if 'fileValid' additional validation check after application
 *
 * @param specifiedResourcePath
 *            Specifies the relative path of the resource in which the quickfix applies
 *
 * @param reparseResource
 *            Specifies if the resource is reset after application of the quick fix or not.
 *
 *            Note: In case of passing false the caller has to reset the resource on its own as it is essential for
 *            the following quick fix xpect method calls.
 *
 * @throws Exception
 *             in test failure.
 */
private void quickFix(IStringDiffExpectation expectation, XtextResource resource, RegionWithCursor offset, String selected, String mode, String specifiedResourcePath, Multimap<Integer, Issue> offset2issue, boolean reparseResource) throws Exception {
    Optional<XtextEditor> editor = Optional.empty();
    // Optional<XtextEditor> specifiedResourceEditor = Optional.empty();
    try {
        List<IssueResolution> resolutions = collectAllResolutions(resource, offset, offset2issue);
        IssueResolution res = QuickFixTestHelper.selectSingleOrFail(resolutions, selected);
        String beforeApplication;
        URI targetResourceUri = resource.getURI();
        // Get the content of the specified resource
        if (specifiedResourcePath != null && !specifiedResourcePath.isEmpty()) {
            URI specifiedURI = resource.getURI().trimSegments(1).appendSegments(specifiedResourcePath.split("/"));
            targetResourceUri = specifiedURI;
            beforeApplication = getContentForResourceUri(specifiedURI);
        } else {
            // capture text for comparison from disk:
            // 
            beforeApplication = getContentForResourceUri(resource.getURI());
        // beforeApplication = resource.getParseResult().getRootNode().getText();
        }
        Display.getDefault().syncExec(() -> res.apply());
        // obtain new text from editor and reparse it into the resource, otherwise the resource is outdated.
        // necessary if the resource is further used in compilation after this QF-Application.
        editor = EditorsUtil.openXtextEditor(targetResourceUri, N4JSActivator.ORG_ECLIPSE_N4JS_N4JS);
        assertTrue("No editor for provided resource " + targetResourceUri.path(), editor.isPresent());
        String textAfterApplication = editor.get().getDocument().get();
        // as it would replace the test code with the specified resource
        if (specifiedResourcePath == null || specifiedResourcePath.isEmpty()) {
            // reparse the text into the resource.
            resource.reparse(textAfterApplication);
            // in case of @IssuesByLine our Validator is of type:
            TestingResourceValidator trVal = (TestingResourceValidator) resource.getResourceServiceProvider().getResourceValidator();
            // Check for no other Issues
            if ("fileValid".equals(mode)) {
                List<Issue> remainingIssues = trVal.validateDelegate(resource, CheckMode.ALL, CancelIndicator.NullImpl, null);
                assertEquals("Expecting all issues resolved, but got still left: " + remainingIssues, 0, remainingIssues.size());
            }
        }
        if (reparseResource) {
            // Reset resource after quick fix application
            resource.reparse(getContentForResourceUri(resource.getURI()));
        }
        if (expectation != null) {
            // TODO assert cursor position after application.
            String before = XpectCommentRemovalUtil.removeAllXpectComments(beforeApplication);
            String after = XpectCommentRemovalUtil.removeAllXpectComments(textAfterApplication);
            if ("whitespaceSensitive".equals(mode)) {
                LineBasedTokenizer tokenizer = new LineBasedTokenizer();
                expectation.assertDiffEquals(tokenizer.apply(before), tokenizer.apply(after), new WSAwareTokenAdapter());
            } else {
                expectation.assertDiffEquals(before, after);
            }
        }
    } finally {
        // TODO initial state aware context
        /*
			 * If editor was created it should be closed, but if it was opened before running tests, we should only undo
			 * changes (and get rid of dirty state), but we should not close editor
			 */
        editor.ifPresent(e -> EditorsUtil.forceCloseAllEditors());
    // editor.ifPresent(e -> EditorsUtil.forceCloseEditor(e));
    }
}
Also used : TestingResourceValidator(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.TestingResourceValidator) Issue(org.eclipse.xtext.validation.Issue) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IssueResolution(org.eclipse.xtext.ui.editor.quickfix.IssueResolution) URI(org.eclipse.emf.common.util.URI)

Aggregations

URI (org.eclipse.emf.common.util.URI)1 TestingResourceValidator (org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.TestingResourceValidator)1 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)1 IssueResolution (org.eclipse.xtext.ui.editor.quickfix.IssueResolution)1 Issue (org.eclipse.xtext.validation.Issue)1