Search in sources :

Example 1 with CommaSeparatedValuesExpectation

use of org.eclipse.xpect.expectation.CommaSeparatedValuesExpectation in project n4js by eclipse.

the class ContentAssistXpectMethod method contentAssistList.

/*-
	 contentAssistList              at ’a.<|>methodA’       display    ’methodA2’           --> ’methodA2(): any - A’
	 contentAssistList              at 'a.<|>methodA'       proposals                       --> <$objectProposals>, methodA2
	 contentAssistList              at 'a.<|>methodA'       proposals                       --> <$objectProposals>, methodA2
	 contentAssistList              at 'a.<|>methodA'       proposals                       --> methodA, methodA2
	 contentAssistList              at 'a.<|>methodA'       proposals             contains  --> methodA2
	 contentAssistList              at 'a.<|>methodA'       proposals             exactly   --> methodA, methodA2
	 contentAssistList              at 'a.<|>methodA'       proposals             not       --> methodB
	 contentAssistList              at 'a.<|>methodA'       proposals             ordered   --> methodA, methodA2
	 contentAssistList              at 'a.<|>methodA'       proposals             unordered --> methodA2, methodA
	 contentAssistList              at 'a.<|>methodA'       display   'methodA2'            --> 'methodA2(): any - A'
	 contentAssistList kind 'smart' at 'a.<|>methodA'       display   'methodA2'            --> 'methodA2(): any - A'
	
	                    kind        offset                  checkType  selected    mode
	                    arg4        arg2                    arg3       arg5        arg6
	 */
/**
 * Compares the expected list of expected display strings with the actual computed one by the proposal provider.
 *
 * @param expect
 *            injected from right hand side - a list expected display strings
 * @param resource
 *            injected - the resource under test
 * @param offset
 *            arg2 - the offset of where to invoke content assist given as string matching next line with cursor
 *            position
 * @param checkType
 *            arg3 - one of {proposals | display }
 * @param kind
 *            arg4 - contentAssist - cycling: current kind like 'n4js'(default) or 'recommenders'
 * @param selected
 *            arg5 - chosen selection form the proposal list
 * @param mode
 *            arg6 - depending on checkTye: apply->{insert(default)|override}, proposals->{contains,
 *            exactly(default), not}
 * @param orderMod
 *            arg7 - for proposal-modes contains/exactly there are two mode available{ordered, unordered(default)}
 * @throws Exception
 *             some exception
 */
@Xpect
@ParameterParser(syntax = "( ('kind' arg4=STRING)? 'at' (arg2=STRING (arg3=ID  (arg5=STRING)?  (arg6=ID (arg7=ID)? )? )? )? )?")
@ConsumedIssues({ Severity.INFO, Severity.ERROR, Severity.WARNING })
public void contentAssistList(// arg0
@CommaSeparatedValuesExpectation(quoted = true) ICommaSeparatedValuesExpectation expect, // arg1
@ThisResource XtextResource resource, // arg2 //@ThisOffset is obsolete
RegionWithCursor offset, // arg3
String checkType, // arg4
String kind, // arg5
String selected, // arg6
String mode, // arg7
String orderMod, ISetupInitializer<XpEnvironmentData> uiTestRunInit) throws Exception {
    XpEnvironmentData xpEnvData = new XpEnvironmentData();
    uiTestRunInit.initialize(xpEnvData);
    xpEnvData.setResourceUnderTest(resource);
    // Expansion of Variables. This changes the original expectation:
    CommaSeparatedValuesExpectationImpl csvE = (CommaSeparatedValuesExpectationImpl) expect;
    Pair<CommaSeparatedValuesExpectationImpl, CharSequence> exptectationAndText = expandVariables(csvE, xpEnvData);
    CommaSeparatedValuesExpectationImpl expectation = exptectationAndText.getKey();
    CharSequence expectedText = exptectationAndText.getValue();
    // System.out.println("---|" + expectedText + "|---");
    List<String> proposals = getProposalDisplayStrings(resource, offset, kind);
    if (("display").equals(checkType)) {
        // TODO check original code, seems like errors wehn porting
        // expectation exactly one string:
        // pick the proposal, test that only one is Picked && verify with expectation.
        List<String> candidates = proposals.stream().filter(p -> p.contains(selected)).collect(Collectors.toList());
        if (candidates.size() > 1) {
            StringBuilder sb = new StringBuilder();
            sb.append("more then one proposal matches the selection '").append(selected).append("' matches:[");
            candidates.forEach(m -> sb.append(m).append(","));
            sb.append("]");
            throw new RuntimeException(sb.toString());
        }
        if (candidates.size() == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("nothing matches the selection '").append(selected).append("' available are [");
            candidates.forEach(m -> sb.append(m).append(","));
            sb.append("]");
            throw new RuntimeException(sb.toString());
        }
        // exactly one:
        expectation.assertEquals(candidates);
        return;
    } else if ("proposals".equals(checkType)) {
        // order-mode, default is 'unordered'
        boolean ordered = ("ordered" == orderMod || "ordered" == mode);
        if (mode == null) {
            if (ordered) {
                assertExactlyOrdered(proposals, separateOnCommaAndQuote(expectedText), expectation);
            } else {
                assertExactly(proposals, separateOnCommaAndQuote(expectedText), expectation);
            }
            return;
        }
        switch(mode) {
            case "":
            // just in case mode is default, then orderMod gets assigned to mode
            case "ordered":
            // just in case mode is default, then orderMod gets assigned to mode
            case "unordered":
            case // default case.
            "exactly":
                if (ordered) {
                    assertExactlyOrdered(proposals, separateOnCommaAndQuote(expectedText), expectation);
                } else {
                    assertExactly(proposals, separateOnCommaAndQuote(expectedText), expectation);
                }
                return;
            case "contains":
                {
                    if (ordered) {
                        assertContainingMatchAllOrdered(proposals, separateOnCommaAndQuote(expectedText), expectation);
                    } else {
                        assertContainingMatchAll(proposals, separateOnCommaAndQuote(expectedText), expectation);
                    }
                    return;
                }
            case "not":
                // ordered / unordered doesn't apply here
                assertNoMatch(proposals, separateOnCommaAndQuote(expectedText), expectation);
                return;
            default:
                throw new RuntimeException("unrecognized mode for proposal-test : '" + mode + "'");
        }
    } else {
        throw new UnsupportedOperationException("unrecognized checktype: '" + checkType + "'");
    }
}
Also used : VarDef(org.eclipse.n4js.xpect.config.VarDef) Arrays(java.util.Arrays) IExpectationRegion(org.eclipse.xpect.expectation.IExpectationRegion) ISetupInitializer(org.eclipse.xpect.setup.ISetupInitializer) ThisResource(org.eclipse.xpect.xtext.lib.setup.ThisResource) Inject(com.google.inject.Inject) ICommaSeparatedValuesExpectation(org.eclipse.xpect.expectation.ICommaSeparatedValuesExpectation) HashMap(java.util.HashMap) QuickFixTestHelper.extractSingleChangedLine(org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper.extractSingleChangedLine) AtomicReference(java.util.concurrent.atomic.AtomicReference) Point(org.eclipse.swt.graphics.Point) XtextResourceCleanUtil(org.eclipse.n4js.xpect.ui.common.XtextResourceCleanUtil) Logger(org.apache.log4j.Logger) QuickFixTestHelper.separateOnCommaAndQuote(org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper.separateOnCommaAndQuote) ValidationTestModuleSetup(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup) Lists(com.google.common.collect.Lists) Config(org.eclipse.n4js.xpect.config.Config) Map(java.util.Map) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) ComparisonFailure(junit.framework.ComparisonFailure) XpectImport(org.eclipse.xpect.XpectImport) ParameterParser(org.eclipse.xpect.parameter.ParameterParser) ConsumedIssues(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues) CommaSeparatedValuesExpectation(org.eclipse.xpect.expectation.CommaSeparatedValuesExpectation) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSOffsetAdapter(org.eclipse.n4js.xpect.common.N4JSOffsetAdapter) QuickFixTestHelper.asString2(org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper.asString2) Severity(org.eclipse.xtext.diagnostics.Severity) AbstractExpectation(org.eclipse.xpect.expectation.impl.AbstractExpectation) ExpectationCollection(org.eclipse.xpect.expectation.impl.ExpectationCollection) AssertionFailedError(junit.framework.AssertionFailedError) Display(org.eclipse.swt.widgets.Display) Collectors(java.util.stream.Collectors) ChangeInfo(org.eclipse.n4js.xpect.ui.common.QuickFixTestHelper.ChangeInfo) IteratorExtensions(org.eclipse.xtext.xbase.lib.IteratorExtensions) List(java.util.List) XpEnvironmentData(org.eclipse.n4js.xpect.config.XpEnvironmentData) Predicate(com.google.common.base.Predicate) CommaSeparatedValuesExpectationImpl(org.eclipse.xpect.expectation.impl.CommaSeparatedValuesExpectationImpl) Entry(java.util.Map.Entry) Optional(java.util.Optional) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Pair(org.eclipse.xtext.xbase.lib.Pair) Comparator(java.util.Comparator) IStringExpectation(org.eclipse.xpect.expectation.IStringExpectation) Xpect(org.eclipse.xpect.runner.Xpect) XpEnvironmentData(org.eclipse.n4js.xpect.config.XpEnvironmentData) CommaSeparatedValuesExpectationImpl(org.eclipse.xpect.expectation.impl.CommaSeparatedValuesExpectationImpl) ConsumedIssues(org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Aggregations

Predicate (com.google.common.base.Predicate)1 Lists (com.google.common.collect.Lists)1 Inject (com.google.inject.Inject)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 AssertionFailedError (junit.framework.AssertionFailedError)1 ComparisonFailure (junit.framework.ComparisonFailure)1 Logger (org.apache.log4j.Logger)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 TemplateProposal (org.eclipse.jface.text.templates.TemplateProposal)1 N4JSOffsetAdapter (org.eclipse.n4js.xpect.common.N4JSOffsetAdapter)1 Config (org.eclipse.n4js.xpect.config.Config)1 VarDef (org.eclipse.n4js.xpect.config.VarDef)1