package freemarker.introspection;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.math.BigDecimal;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class TemplateTestUtils {
    public static Element loadTemplateRoot(String templateText) {
        StringTemplateLoader tloader = new StringTemplateLoader();
        tloader.putTemplate("template", templateText);
        Configuration templateConfig = new Configuration();
        templateConfig.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
        templateConfig.setTemplateLoader(tloader);
        try {
            Template template = templateConfig.getTemplate("template");
            return TemplateIntrospector.getRootNode(template);
        } catch (IOException e) {
            throw new RuntimeException("Error loading template", e);
        }
    }

    public static void assertIdentifier(TemplateNode node, int paramIdx, String name) {
        Expr identifier = node.getParams().get(paramIdx);
        assertEquals(ExprType.IDENTIFIER, identifier.getType());
        assertEquals(name, identifier.toString());
    }

    public static void assertValue(TemplateNode node, int paramIdx, Object expectedValue) {
        Expr valueExpr = node.getParams().get(paramIdx);
        assertEquals(ExprType.VALUE, valueExpr.getType());
        assertEquals(expectedValue, ((ObjectExpr) valueExpr).getValue());
    }

    public static void assertStringExpr(TemplateNode node, int paramIdx, String expectedValue) {
        Expr strLiteral = node.getParams().get(paramIdx);
        assertEquals(ExprType.STRING_LITERAL, strLiteral.getType());
        assertEquals(expectedValue, ((StringExpr) strLiteral).getValue());
    }

    public static void assertNumberExpr(TemplateNode node, int paramIdx, int expectedValue) {
        Expr numLiteral = node.getParams().get(paramIdx);
        assertEquals(ExprType.NUMBER_LITERAL, numLiteral.getType());
        assertEquals(new BigDecimal(expectedValue),
                ((NumberLiteralExpr) numLiteral).getValue());
    }
}

@@ -1310,93 +1310,258 @@
     
-Expr valueExpr = node.getParams().get(paramIdx);%0A        assertEquals(ExprType.VALUE,
+// a value may be a literal expression or a value expression. We'll accept both kinds. %0A        Expr valueExpr = node.getParams().get(paramIdx);%0A        Object value = null;%0A        if (valueExpr instanceof ValueExpr)%0A            value = ((ValueExpr)
  val
@@ -1566,27 +1566,28 @@
 alueExpr
+)
 .get
-Typ
+Valu
 e()
-)
 ;%0A      
@@ -1592,44 +1592,80 @@
     
-assertEquals(expectedV
+else if (valueExpr instanceof LiteralExpr)%0A            v
 alue
-, ((Object
+ = ((Literal
 Expr
@@ -1679,32 +1679,130 @@
 Expr).getValue()
+;%0A        else%0A            value = valueExpr.toString();%0A        assertEquals(expectedValue, value
 );%0A    %7D%0A%0A    pu

Fix test to work with latest Freemarker