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");
}
}
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;
}
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;
}
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");
}
}
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;
}
Aggregations