Search in sources :

Example 1 with ExpressionInfo

use of org.spockframework.runtime.model.ExpressionInfo in project spock by spockframework.

the class ExpressionInfoBuilder method build.

public ExpressionInfo build() {
    try {
        SourceUnit unit = SourceUnit.create("Spec expression", adjustedText);
        unit.parse();
        unit.completePhase();
        unit.convert();
        BlockStatement blockStat = unit.getAST().getStatementBlock();
        Assert.that(blockStat != null && blockStat.getStatements().size() == 1);
        Statement stat = blockStat.getStatements().get(0);
        Assert.that(stat instanceof ExpressionStatement);
        Expression expr = ((ExpressionStatement) stat).getExpression();
        ExpressionInfo exprInfo = new ExpressionInfoConverter(lines).convert(expr);
        // IDEA: rest of this method could be moved to ExpressionInfoConverter (but: might make EIC less testable)
        // IDEA: could make ExpressionInfo immutable
        List<ExpressionInfo> inPostfixOrder = exprInfo.inPostfixOrder(false);
        for (int variableNumber = 0; variableNumber < inPostfixOrder.size(); variableNumber++) {
            ExpressionInfo info = inPostfixOrder.get(variableNumber);
            info.setText(findText(info.getRegion()));
            if (notRecordedVarNumberBecauseOfException != null && variableNumber == notRecordedVarNumberBecauseOfException) {
                info.setValue(exception);
            } else if (values.size() > variableNumber) {
                //we have this value
                info.setValue(values.get(variableNumber));
            } else {
                info.setValue(ExpressionInfo.VALUE_NOT_AVAILABLE);
            }
            if (startPos.getLineIndex() > 0)
                info.shiftVertically(startPos.getLineIndex());
        }
        return exprInfo;
    } catch (Throwable t) {
        final ExpressionInfo expressionInfo = new ExpressionInfo(TextRegion.create(TextPosition.create(1, 1), TextPosition.create(1, 1)), TextPosition.create(1, 1), null);
        expressionInfo.setText(text);
        expressionInfo.setValue(lastOrNull(values));
        return expressionInfo;
    }
}
Also used : Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) SourceUnit(org.codehaus.groovy.control.SourceUnit) ExpressionInfo(org.spockframework.runtime.model.ExpressionInfo)

Example 2 with ExpressionInfo

use of org.spockframework.runtime.model.ExpressionInfo in project spock by spockframework.

the class ExpressionInfoValueRenderer method renderAsFailedEqualityComparison.

private String renderAsFailedEqualityComparison(ExpressionInfo expr) {
    if (!(Boolean.FALSE.equals(expr.getValue())))
        return null;
    if (!expr.isEqualityComparison())
        return null;
    ExpressionInfo expr1 = expr.getChildren().get(0);
    ExpressionInfo expr2 = expr.getChildren().get(1);
    if (expr1.getEffectiveRenderedValue().equals(expr2.getEffectiveRenderedValue())) {
        addTypeHint(expr1);
        addTypeHint(expr2);
    }
    return "false";
}
Also used : ExpressionInfo(org.spockframework.runtime.model.ExpressionInfo)

Aggregations

ExpressionInfo (org.spockframework.runtime.model.ExpressionInfo)2 Expression (org.codehaus.groovy.ast.expr.Expression)1 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)1 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1 SourceUnit (org.codehaus.groovy.control.SourceUnit)1