Search in sources :

Example 1 with GrAssertStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrAssertStatement in project intellij-community by JetBrains.

the class ConvertJunitAssertionToAssertStatementIntention method getReplacementElement.

@Nullable
private static GrStatement getReplacementElement(@NotNull PsiMethod method, @NotNull GrMethodCall methodCall) {
    String replacementStatement = getReplacementStatement(method, methodCall);
    if (replacementStatement == null)
        return null;
    GrExpression[] arguments = methodCall.getArgumentList().getExpressionArguments();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    GrAssertStatement statement = (GrAssertStatement) factory.createStatementFromText(replacementStatement);
    final Map<GrExpression, GrExpression> replaceMap = new HashMap<>();
    statement.acceptChildren(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitExpression(@NotNull GrExpression expression) {
            Matcher matcher = PATTERN.matcher(expression.getText());
            if (matcher.matches()) {
                int index = Integer.parseInt(matcher.group(1));
                replaceMap.put(expression, arguments[index]);
            } else {
                super.visitExpression(expression);
            }
        }
    });
    for (Map.Entry<GrExpression, GrExpression> entry : replaceMap.entrySet()) {
        entry.getKey().replaceWithExpression(entry.getValue(), true);
    }
    return statement;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrAssertStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrAssertStatement) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)1 GrAssertStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrAssertStatement)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1