PageRenderTime 78ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/test/net/ion/framework/mte/TestMte.java

https://github.com/bleujin/ionframework
Java | 105 lines | 68 code | 29 blank | 8 comment | 0 complexity | b70a09291ab548340675a1663d18b0ea MD5 | raw file
  1. package net.ion.framework.mte;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import junit.framework.TestCase;
  5. import net.ion.framework.util.ListUtil;
  6. import net.ion.framework.util.MapUtil;
  7. public class TestMte extends TestCase{
  8. // // sample template
  9. // ${foreach cart.items item}
  10. // ${item.amount}x ${item.name} ${item.price} each = ${item.total}
  11. // ${end}
  12. //
  13. // ${if cart.addShipping}
  14. // Shipping = ${cart.shippingCost}
  15. // ${end}
  16. private Engine engine;
  17. @Override
  18. protected void setUp() throws Exception {
  19. super.setUp();
  20. this.engine = Engine.createDefaultEngine() ;
  21. }
  22. public void testHello() throws Exception {
  23. String result = engine.transform("greeting ${name}", MapUtil.<String,Object>create("name", "bleujin"));
  24. assertEquals("greeting bleujin", result) ;
  25. }
  26. public void testCondition() throws Exception {
  27. String result = engine.transform("${if name}greeting ${name}${end}", MapUtil.<String,Object>create("name", "bleujin"));
  28. assertEquals("greeting bleujin", result) ;
  29. }
  30. public void testConditionDefault() throws Exception {
  31. assertEquals("bleujin", engine.transform("${name[empty]}", MapUtil.<String,Object>create("name", "bleujin")));
  32. assertEquals("empty", engine.transform("${name[empty]}", new HashMap<String, Object>()));
  33. }
  34. public void testConditionShortcuts() throws Exception {
  35. assertEquals("unknown", engine.transform("${if name}${name}${else}unknown${end}", new HashMap<String, Object>()));
  36. assertEquals("unknown", engine.transform("${name[unknown]}", new HashMap<String, Object>()));
  37. assertEquals("", engine.transform("${<h1>,name,</h1>}", new HashMap<String, Object>()));
  38. assertEquals("<h1>bleujin</h1>", engine.transform("${<h1>,name,</h1>}", MapUtil.<String,Object>create("name", "bleujin")));
  39. }
  40. public void testLoop() throws Exception {
  41. List<String> set = ListUtil.toList("bleujin", "hero") ;
  42. String result = engine.transform("${foreach names item}${item} ${end}", MapUtil.<String,Object>create("names", set));
  43. assertEquals("bleujin hero ", result) ;
  44. }
  45. public void testLoopSeperator() throws Exception {
  46. List<String> set = ListUtil.toList("bleujin", "hero") ;
  47. String result = engine.transform("${foreach names item ,}${item}${end}", MapUtil.<String,Object>create("names", set));
  48. assertEquals("bleujin,hero", result) ;
  49. }
  50. public void testLoopSpecial() throws Exception {
  51. List<String> set = ListUtil.toList("bleujin", "hero") ;
  52. String first = "${foreach names item} " +
  53. "${if first_item}${item} is first" +
  54. "${else}${item}" +
  55. "${end}" +
  56. "${end}" ;
  57. String result = engine.transform(first, MapUtil.<String,Object>create("names", set));
  58. assertEquals("bleujin is first hero", result.trim()) ;
  59. String last = "${foreach names item} " +
  60. "${if last_item}${item} is last" +
  61. "${else}${item}" +
  62. "${end}" +
  63. "${end}" ;
  64. assertEquals("bleujin hero is last", engine.transform(last, MapUtil.<String,Object>create("names", set)).trim()) ;
  65. String odd = "${foreach names item ,} " +
  66. "${if odd_item}${item} is odd" +
  67. "${else}${item}" +
  68. "${end}" +
  69. "${end}" ;
  70. assertEquals("bleujin, hero is odd", engine.transform(odd, MapUtil.<String,Object>create("names", set)).trim()) ;
  71. }
  72. public void testEscape() throws Exception {
  73. String result = engine.transform("greeting \\${name}${name}", MapUtil.<String,Object>create("name", "bleujin"));
  74. assertEquals("greeting ${name}bleujin", result) ;
  75. }
  76. }