Search in sources :

Example 21 with Xpect

use of org.eclipse.xpect.runner.Xpect in project n4js by eclipse.

the class AccessModifierXpectMethod method accessModifier.

/**
 * This xpect method can evaluate the accessibility of {@link TMember}s. For example, given a field of a class or a
 * {@link ParameterizedPropertyAccessExpression}, the xpect methods returns their explicit or implicit declared
 * accessibility such as {@code public} or {@code private}.
 */
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
@Xpect
public void accessModifier(@StringExpectation IStringExpectation expectation, IEObjectCoveringRegion offset) {
    EObject context = offset.getEObject();
    String actual = calculateActual(context);
    expectation.assertEquals(actual);
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 22 with Xpect

use of org.eclipse.xpect.runner.Xpect in project n4js by eclipse.

the class FormatterXpectMethod method formattedLines.

/**
 */
@Xpect
@ParameterParser(syntax = "arg1=INT")
public void formattedLines(@StringExpectation(whitespaceSensitive = true) IStringExpectation exp, // arg1
int lines, XpectInvocation inv, TargetSyntaxSupport syntax, ITextRegionAccess reg, ISetupInitializer<Preferences> prefInit) {
    ITextSegment region = getRegionForLines(reg, syntax.findFirstSemanticCharAfterStatement(inv), lines);
    Preferences prefs = new Preferences();
    // First put some defaults
    prefs.put(N4JSFormatterPreferenceKeys.FORMAT_PARENTHESIS, true);
    prefs.put(FormatterPreferenceKeys.lineSeparator, "\n");
    // Second init from concrete tests - will override defaults.
    prefInit.initialize(prefs);
    IFormatter2 formatter = formatterProvider.get();
    FormatterRequest request = formatterRequestProvider.get();
    request.setTextRegionAccess(reg);
    request.setExceptionHandler(ExceptionAcceptor.THROWING);
    // needed in case a check like this will be implemented:
    // org.eclipse.xtext.testing.formatter.FormatterTester.assertAllWhitespaceIsFormatted()
    request.setAllowIdentityEdits(true);
    request.setFormatUndefinedHiddenRegionsOnly(false);
    request.addRegion(region);
    request.setPreferences(prefs);
    List<ITextReplacement> replacements = formatter.format(request);
    String fmt = reg.getRewriter().renderToString(replacements);
    ITextSegment doc = reg.regionForDocument();
    int endIndex = region.getEndOffset() + (fmt.length() - doc.getLength()) - 1;
    String selection = fmt.substring(region.getOffset(), endIndex);
    exp.assertEquals(selection);
}
Also used : IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) Preferences(org.eclipse.n4js.xpect.config.Preferences) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 23 with Xpect

use of org.eclipse.xpect.runner.Xpect in project n4js by eclipse.

the class FlowgraphsXpectMethod method astOrder.

/**
 * This xpect method can evaluate the direct predecessors of a code element. The predecessors can be limited when
 * specifying the edge type.
 * <p>
 * <b>Attention:</b> The type parameter <i>does not</i> work on self loops!
 */
@ParameterParser(syntax = "('of' arg2=OFFSET)?")
@Xpect
public void astOrder(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion offset) {
    EObject context = offset.getEObject();
    Iterator<ControlFlowElement> astIter = new ASTIterator(context);
    int idx = 0;
    List<String> astElements = new LinkedList<>();
    while (astIter.hasNext()) {
        ControlFlowElement cfe = astIter.next();
        String elem = idx + ": " + FGUtils.getSourceText(cfe);
        astElements.add(elem);
        idx++;
    }
    expectation.assertEquals(astElements);
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ASTIterator(org.eclipse.n4js.flowgraphs.ASTIterator) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) LinkedList(java.util.LinkedList) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 24 with Xpect

use of org.eclipse.xpect.runner.Xpect in project n4js by eclipse.

the class FlowgraphsXpectMethod method allBranches.

/**
 * This xpect method can evaluate all branches from a given start code element. If no start code element is
 * specified, the first code element of the containing function.
 */
@ParameterParser(syntax = "('from' arg1=OFFSET)? ('direction' arg2=STRING)? ('pleaseNeverUseThisParameterSinceItExistsOnlyToGetAReferenceOffset' arg3=OFFSET)?")
@Xpect
public void allBranches(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion offset, String directionName, IEObjectCoveringRegion referenceOffset) {
    AllBranchPrintVisitor appw = performBranchAnalysis(offset, directionName, referenceOffset);
    List<String> branchStrings = appw.getBranchStrings();
    expectation.assertEquals(branchStrings);
}
Also used : AllBranchPrintVisitor(org.eclipse.n4js.flowgraphs.analysers.AllBranchPrintVisitor) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Example 25 with Xpect

use of org.eclipse.xpect.runner.Xpect in project n4js by eclipse.

the class FlowgraphsXpectMethod method path.

/**
 * This xpect method can evaluate if the tested element is a transitive predecessor of the given element.
 */
@ParameterParser(syntax = "'from' arg0=OFFSET ('to' arg1=OFFSET)? ('notTo' arg2=OFFSET)? ('via' arg3=OFFSET)? ('notVia' arg4=OFFSET)? ('pleaseNeverUseThisParameterSinceItExistsOnlyToGetAReferenceOffset' arg5=OFFSET)?")
@Xpect
public void path(IEObjectCoveringRegion fromOffset, IEObjectCoveringRegion toOffset, IEObjectCoveringRegion notToOffset, IEObjectCoveringRegion viaOffset, IEObjectCoveringRegion notViaOffset, IEObjectCoveringRegion referenceOffset) {
    EObjectCoveringRegion toOffsetImpl = (EObjectCoveringRegion) toOffset;
    EObjectCoveringRegion notToOffsetImpl = (EObjectCoveringRegion) notToOffset;
    EObjectCoveringRegion viaOffsetImpl = (EObjectCoveringRegion) viaOffset;
    EObjectCoveringRegion notViaOffsetImpl = (EObjectCoveringRegion) notViaOffset;
    EObjectCoveringRegion referenceOffsetImpl = (EObjectCoveringRegion) referenceOffset;
    ControlFlowElement fromCFE = getCFE(fromOffset);
    ControlFlowElement toCFE = getCFEWithReference(toOffsetImpl, referenceOffsetImpl);
    ControlFlowElement notToCFE = getCFEWithReference(notToOffsetImpl, referenceOffsetImpl);
    ControlFlowElement viaCFE = getCFEWithReference(viaOffsetImpl, referenceOffsetImpl);
    ControlFlowElement notViaCFE = getCFEWithReference(notViaOffsetImpl, referenceOffsetImpl);
    ControlFlowElement targetCFE = (toCFE != null) ? toCFE : notToCFE;
    boolean expectPathExists = toCFE != null;
    if (fromCFE == null) {
        fail("Element 'from' could not be found");
    }
    if (targetCFE == null) {
        fail("Element 'to' or 'notTo' could not be found or before 'from'");
    }
    boolean actualPathExists;
    if (viaCFE != null) {
        actualPathExists = getFlowAnalyzer(fromCFE).isTransitiveSuccessor(fromCFE, viaCFE, notViaCFE);
        actualPathExists &= getFlowAnalyzer(fromCFE).isTransitiveSuccessor(viaCFE, targetCFE, notViaCFE);
    } else {
        actualPathExists = getFlowAnalyzer(fromCFE).isTransitiveSuccessor(fromCFE, targetCFE, notViaCFE);
    }
    if (expectPathExists && !actualPathExists) {
        fail("Path not found");
    }
    if (!expectPathExists && actualPathExists) {
        fail("A path was found");
    }
}
Also used : IEObjectCoveringRegion(org.eclipse.n4js.xpect.common.N4JSOffsetAdapter.IEObjectCoveringRegion) EObjectCoveringRegion(org.eclipse.n4js.xpect.common.N4JSOffsetAdapter.EObjectCoveringRegion) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Aggregations

ParameterParser (org.eclipse.xpect.parameter.ParameterParser)30 Xpect (org.eclipse.xpect.runner.Xpect)30 EObject (org.eclipse.emf.ecore.EObject)11 ControlFlowElement (org.eclipse.n4js.n4JS.ControlFlowElement)9 LinkedList (java.util.LinkedList)6 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)6 ConsumedIssues (org.eclipse.xpect.xtext.lib.tests.ValidationTestModuleSetup.ConsumedIssues)5 ICrossEReferenceAndEObject (org.eclipse.xpect.xtext.lib.util.XtextOffsetAdapter.ICrossEReferenceAndEObject)5 XtextResource (org.eclipse.xtext.resource.XtextResource)5 URI (org.eclipse.emf.common.util.URI)4 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)4 List (java.util.List)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 N4JSResource (org.eclipse.n4js.resource.N4JSResource)3 ThisResource (org.eclipse.xpect.xtext.lib.setup.ThisResource)3 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)3 Lists (com.google.common.collect.Lists)2 Inject (com.google.inject.Inject)2 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2