use of org.apache.commons.jexl3.internal.TemplateDebugger in project commons-jexl by apache.
the class Issues200Test method testTemplate6565a.
@Test
public void testTemplate6565a() throws Exception {
final JexlEngine jexl = new JexlBuilder().create();
final JxltEngine jexlt = jexl.createJxltEngine();
final String source = "$$ var res = '';\n" + "$$ var meta = session.data['METADATA'];\n" + "$$ if (meta) {\n" + "$$ var entry = meta['ID'];\n" + "$$ if (entry) {\n" + "$$ var value = session.data[entry];\n" + "$$ res = value?: '';\n" + "$$ }\n" + "$$ }\n" + "${res}\n";
final JxltEngine.Template script = jexlt.createTemplate("$$", new StringReader(source));
Assert.assertNotNull(script);
final TemplateDebugger dbg = new TemplateDebugger();
final String refactored = dbg.debug(script) ? dbg.toString() : "";
Assert.assertNotNull(refactored);
Assert.assertEquals(source, refactored);
}
use of org.apache.commons.jexl3.internal.TemplateDebugger in project commons-jexl by apache.
the class JXLTTest method testReport.
@Test
public void testReport() throws Exception {
final String rpt = "<report>\n" + "\n" + "\n$$ var a = 1;" + "\n$$ var x = 2;" + "\n" + "\n$$ var y = 9;" + "\n" + "\n ${x + y}" + "\n</report>\n";
final JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader(rpt));
final StringWriter strw = new StringWriter();
t.evaluate(context, strw);
final String output = strw.toString();
final String ctl = "<report>\n\n\n\n\n 11\n</report>\n";
Assert.assertEquals(ctl, output);
final TemplateDebugger td = new TemplateDebugger();
final String refactored = refactor(td, t);
Assert.assertNotNull(refactored);
Assert.assertEquals(rpt, refactored);
}
use of org.apache.commons.jexl3.internal.TemplateDebugger in project commons-jexl by apache.
the class JXLTTest method test42.
@Test
public void test42() throws Exception {
final String test42 = "$$ for(var x : list) {\n" + "$$ if (x == 42) {\n" + "Life, the universe, and everything\n" + "$$ } else if (x > 42) {\n" + "The value ${x} is over fourty-two\n" + "$$ } else {\n" + "The value ${x} is under fourty-two\n" + "$$ }\n" + "$$ }\n";
final JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader(test42), "list");
final StringWriter strw = new StringWriter();
final int[] list = { 1, 3, 5, 42, 169 };
t.evaluate(context, strw, list);
final String output = strw.toString();
final String out42 = "The value 1 is under fourty-two\n" + "The value 3 is under fourty-two\n" + "The value 5 is under fourty-two\n" + "Life, the universe, and everything\n" + "The value 169 is over fourty-two\n";
Assert.assertEquals(out42, output);
final String dstr = t.asString();
Assert.assertNotNull(dstr);
final TemplateDebugger td = new TemplateDebugger();
final String refactored = refactor(td, t);
Assert.assertNotNull(refactored);
Assert.assertEquals(test42, refactored);
}
use of org.apache.commons.jexl3.internal.TemplateDebugger in project commons-jexl by apache.
the class JXLTTest method testInheritedDebugger.
@Test
public void testInheritedDebugger() throws Exception {
final String src = "if ($A) { $B + 1; } else { $C - 2 }";
final JexlEngine jexl = JXLT.getEngine();
final JexlScript script = jexl.createScript(src);
final Debugger sd = new Debugger();
final String rscript = sd.debug(script) ? sd.toString() : null;
Assert.assertNotNull(rscript);
final TemplateDebugger td = new TemplateDebugger();
final String refactored = td.debug(script) ? td.toString() : null;
Assert.assertNotNull(refactored);
Assert.assertEquals(refactored, rscript);
}
use of org.apache.commons.jexl3.internal.TemplateDebugger in project commons-jexl by apache.
the class JXLTTest method testReport2.
@Test
public void testReport2() throws Exception {
final String rpt = "<report>\n" + "this is ${x}\n" + "${x + 1}\n" + "${x + 2}\n" + "${x + 3}\n" + "</report>\n";
final JxltEngine.Template t = JXLT.createTemplate("$$", new StringReader(rpt), "x");
final StringWriter strw = new StringWriter();
t.evaluate(context, strw, 42);
final String output = strw.toString();
int count = 0;
for (int i = 0; i < output.length(); ++i) {
final char c = output.charAt(i);
if ('\n' == c) {
count += 1;
}
}
Assert.assertEquals(6, count);
Assert.assertTrue(output.indexOf("42") > 0);
Assert.assertTrue(output.indexOf("43") > 0);
Assert.assertTrue(output.indexOf("44") > 0);
Assert.assertTrue(output.indexOf("45") > 0);
final TemplateDebugger td = new TemplateDebugger();
final String xxx = refactor(td, t);
Assert.assertNotNull(xxx);
Assert.assertEquals(rpt, xxx);
}
Aggregations