Search in sources :

Example 1 with StringExpectation

use of org.eclipse.xpect.expectation.StringExpectation 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)

Aggregations

Lists (com.google.common.collect.Lists)1 Multimap (com.google.common.collect.Multimap)1 Inject (com.google.inject.Inject)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1 Date (java.util.Date)1 List (java.util.List)1 Optional (java.util.Optional)1 Logger (org.apache.log4j.Logger)1 IFile (org.eclipse.core.resources.IFile)1 ResourcesPlugin (org.eclipse.core.resources.ResourcesPlugin)1 Path (org.eclipse.core.runtime.Path)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 GeneratorOption (org.eclipse.n4js.generator.GeneratorOption)1 SystemLoaderInfo (org.eclipse.n4js.runner.SystemLoaderInfo)1 EditorsUtil (org.eclipse.n4js.tests.util.EditorsUtil)1 N4JSActivator (org.eclipse.n4js.ui.internal.N4JSActivator)1 N4JSOffsetAdapter (org.eclipse.n4js.xpect.common.N4JSOffsetAdapter)1 ResourceTweaker (org.eclipse.n4js.xpect.common.ResourceTweaker)1