* @version $Revision: 5875 $
*/
public class GStringTest extends TestSupport {
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!")))));
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();