Search in sources :

Example 1 with AssertStatement

use of org.codehaus.groovy.ast.stmt.AssertStatement in project groovy-core by groovy.

the class GStringTest method testConstructor.

public void testConstructor() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    //Statement printStatement = createPrintlnStatement(new VariableExpression("str"));
    // simulate "Hello ${user}!"
    GStringExpression compositeStringExpr = new GStringExpression("hello ${user}!");
    compositeStringExpr.addString(new ConstantExpression("Hello "));
    compositeStringExpr.addValue(new VariableExpression("user"));
    compositeStringExpr.addString(new ConstantExpression("!"));
    BlockStatement block = new BlockStatement();
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("user"), Token.newSymbol("=", -1, -1), new ConstantExpression("World"))));
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("str"), Token.newSymbol("=", -1, -1), compositeStringExpr)));
    block.addStatement(new ExpressionStatement(new MethodCallExpression(VariableExpression.THIS_EXPRESSION, "println", new VariableExpression("str"))));
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("text"), Token.newSymbol("=", -1, -1), new MethodCallExpression(new VariableExpression("str"), "toString", MethodCallExpression.NO_ARGUMENTS))));
    block.addStatement(new AssertStatement(new BooleanExpression(new BinaryExpression(new VariableExpression("text"), Token.newSymbol("==", -1, -1), new ConstantExpression("Hello World!"))), // TODO FIX if empty, AssertionWriter fails because source text is null
    new ConstantExpression("Assertion failed")));
    classNode.addMethod(new MethodNode("stringDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    try {
        InvokerHelper.invokeMethod(bean, "stringDemo", null);
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodNode(org.codehaus.groovy.ast.MethodNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement)

Example 2 with AssertStatement

use of org.codehaus.groovy.ast.stmt.AssertStatement in project groovy by apache.

the class AntlrParserPlugin method statement.

// Statements
//-------------------------------------------------------------------------
protected Statement statement(AST node) {
    Statement statement = null;
    int type = node.getType();
    switch(type) {
        case SLIST:
        case LITERAL_finally:
            statement = statementList(node);
            break;
        case METHOD_CALL:
            statement = methodCall(node);
            break;
        case VARIABLE_DEF:
            statement = variableDef(node);
            break;
        case LABELED_STAT:
            return labelledStatement(node);
        case LITERAL_assert:
            statement = assertStatement(node);
            break;
        case LITERAL_break:
            statement = breakStatement(node);
            break;
        case LITERAL_continue:
            statement = continueStatement(node);
            break;
        case LITERAL_if:
            statement = ifStatement(node);
            break;
        case LITERAL_for:
            statement = forStatement(node);
            break;
        case LITERAL_return:
            statement = returnStatement(node);
            break;
        case LITERAL_synchronized:
            statement = synchronizedStatement(node);
            break;
        case LITERAL_switch:
            statement = switchStatement(node);
            break;
        case LITERAL_try:
            statement = tryStatement(node);
            break;
        case LITERAL_throw:
            statement = throwStatement(node);
            break;
        case LITERAL_while:
            statement = whileStatement(node);
            break;
        default:
            statement = new ExpressionStatement(expression(node));
    }
    if (statement != null) {
        configureAST(statement, node);
    }
    return statement;
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 3 with AssertStatement

use of org.codehaus.groovy.ast.stmt.AssertStatement in project groovy by apache.

the class AntlrParserPlugin method assertStatement.

protected Statement assertStatement(AST assertNode) {
    AST node = assertNode.getFirstChild();
    BooleanExpression booleanExpression = booleanExpression(node);
    Expression messageExpression = null;
    node = node.getNextSibling();
    if (node != null) {
        messageExpression = expression(node);
    } else {
        messageExpression = ConstantExpression.NULL;
    }
    AssertStatement assertStatement = new AssertStatement(booleanExpression, messageExpression);
    configureAST(assertStatement, assertNode);
    return assertStatement;
}
Also used : AST(antlr.collections.AST) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement)

Example 4 with AssertStatement

use of org.codehaus.groovy.ast.stmt.AssertStatement in project groovy by apache.

the class GStringTest method testConstructor.

public void testConstructor() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    //Statement printStatement = createPrintlnStatement(new VariableExpression("str"));
    // simulate "Hello ${user}!"
    GStringExpression compositeStringExpr = new GStringExpression("hello ${user}!");
    compositeStringExpr.addString(new ConstantExpression("Hello "));
    compositeStringExpr.addValue(new VariableExpression("user"));
    compositeStringExpr.addString(new ConstantExpression("!"));
    BlockStatement block = new BlockStatement();
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("user"), Token.newSymbol("=", -1, -1), new ConstantExpression("World"))));
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("str"), Token.newSymbol("=", -1, -1), compositeStringExpr)));
    block.addStatement(new ExpressionStatement(new MethodCallExpression(VariableExpression.THIS_EXPRESSION, "println", new VariableExpression("str"))));
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("text"), Token.newSymbol("=", -1, -1), new MethodCallExpression(new VariableExpression("str"), "toString", MethodCallExpression.NO_ARGUMENTS))));
    block.addStatement(new AssertStatement(new BooleanExpression(new BinaryExpression(new VariableExpression("text"), Token.newSymbol("==", -1, -1), new ConstantExpression("Hello World!"))), // TODO FIX if empty, AssertionWriter fails because source text is null
    new ConstantExpression("Assertion failed")));
    classNode.addMethod(new MethodNode("stringDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    try {
        InvokerHelper.invokeMethod(bean, "stringDemo", null);
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodNode(org.codehaus.groovy.ast.MethodNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement)

Example 5 with AssertStatement

use of org.codehaus.groovy.ast.stmt.AssertStatement in project gcontracts by andresteingress.

the class ExpressionUtils method getBooleanExpressionsFromAssertionStatements.

/**
     * Returns all {@link BooleanExpression} instances found in the given {@link BlockStatement}.
     */
public static List<BooleanExpression> getBooleanExpressionsFromAssertionStatements(BlockStatement blockStatement) {
    AssertStatementCollector collector = new AssertStatementCollector();
    collector.visitBlockStatement(blockStatement);
    List<AssertStatement> assertStatements = collector.assertStatements;
    if (assertStatements.isEmpty())
        return Collections.emptyList();
    List<BooleanExpression> booleanExpressions = new ArrayList<BooleanExpression>();
    for (AssertStatement assertStatement : assertStatements) {
        booleanExpressions.add(assertStatement.getBooleanExpression());
    }
    return booleanExpressions;
}
Also used : AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) ArrayList(java.util.ArrayList)

Aggregations

AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)5 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)3 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)3 ClassNode (org.codehaus.groovy.ast.ClassNode)2 MethodNode (org.codehaus.groovy.ast.MethodNode)2 InvokerInvocationException (org.codehaus.groovy.runtime.InvokerInvocationException)2 AST (antlr.collections.AST)1 ArrayList (java.util.ArrayList)1 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)1 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)1 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)1 ContinueStatement (org.codehaus.groovy.ast.stmt.ContinueStatement)1 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)1 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)1 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)1 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)1 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)1 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)1