View Javadoc
1   /**
2    * 
3    */
4   package org.melati.template.velocity.test;
5   
6   import org.melati.template.velocity.WebMacroConverter;
7   
8   import junit.framework.TestCase;
9   
10  /**
11   * @author timp
12   * @since 11 Oct 2010
13   */
14  public class WebMacroConverterTest extends TestCase {
15  
16      /**
17       * @param name
18       */
19      public WebMacroConverterTest(String name) {
20          super(name);
21      }
22  
23      /* (non-Javadoc)
24       * @see junit.framework.TestCase#setUp()
25       */
26      protected void setUp() throws Exception {
27          super.setUp();
28      }
29  
30      /* (non-Javadoc)
31       * @see junit.framework.TestCase#tearDown()
32       */
33      protected void tearDown() throws Exception {
34          super.tearDown();
35      }
36  
37      /**
38       * Test method for {@link org.melati.template.velocity.WebMacroConverter#convert(java.io.InputStream)}.
39       */
40      public final void testConvert() {
41          
42          // Make #if directive match the Velocity directive style.
43          //"#if\\s*[(]\\s*(.*\\S)\\s*[)]\\s*(#begin|{)[ \\t]?",
44          //"#if( $1 )",
45          
46          assertEquals(WebMacroConverter.convert("#if ( $foo ) #begin "),"#if( $foo )");
47  
48          // Remove the WM #end #else #begin usage.
49          //"([ \\t])?(#end|})\\s*#else\\s*(#begin|{)([ \\t])?(\\w)",
50          //"#{else}$5", // avoid touching a followup word with embedded comment
51  
52          assertEquals(WebMacroConverter.convert(" #end #else #begin do"),"#{else}do");
53          
54          //"[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?",
55          //"#else",
56          assertEquals(WebMacroConverter.convert(" #end #else #begin "),"#else");
57  
58          // Convert nulls.
59          // This converts
60          // $var != null
61          // to
62          // $var
63          //"\\s*(\\$[^\\s\\!]+)\\s*!=\\s*null\\s*",
64          //" $1 ",
65          assertEquals(WebMacroConverter.convert(" $var != null ")," $var ");
66  
67          // This converts
68          // $var == null
69          // to
70          // !$var
71          //"\\s*(\\$[^\\s\\!]+)\\s*==\\s*null\\s*",
72          //" !$1 ",
73          assertEquals(WebMacroConverter.convert(" $var == null ")," !$var ");
74  
75          // Convert WM style #foreach to Velocity directive style.
76          //"#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$[^\\s#]+)\\s*(#begin|{)[ \\t]?",
77          //"#foreach( $1 in $2 )",
78          assertEquals(WebMacroConverter.convert("#foreach $foo in $bar #begin "),"#foreach( $foo in $bar )");
79  
80          // Change the "}" to #end. Have to get more
81          // sophisticated here. Will assume either {}
82          // and no javascript, or #begin/#end with the
83          // possibility of javascript.
84          //"\n}", // assumes that javascript is indented, WMs not!!!
85          //"\n#end",
86          assertEquals(WebMacroConverter.convert("\n}"),"\n#end");
87          assertEquals(WebMacroConverter.convert(" }")," }");
88  
89          // Convert WM style #set to Velocity directive style.
90          //"#set\\s+(\\$[^\\s=]+)\\s*=\\s*(.*\\S)[ \\t]*",
91          //"#set\\s+(\\$[^\\s=]+)\\s*=\\s*([\\S \\t]+)",
92          //"#set( $1 = $2 )",
93          assertEquals(WebMacroConverter.convert("#set $foo = $bar"),"#set( $foo = $bar )");
94          
95          // TPP 2010/10/11 Not convinced
96          //"(##[# \\t\\w]*)\\)", // fix comments included at end of line
97          //")$1",
98          assertEquals(WebMacroConverter.convert("apparently legal ## comment )"),"apparently legal )## comment ");
99  
100         // Convert WM style #parse to Velocity directive style.
101         //"#parse\\s+([^\\s#]+)[ \\t]?",
102         //"#parse( $1 )",
103         assertEquals(WebMacroConverter.convert("#parse $file "),"#parse( $file )");
104 
105         // parse is now deprecated for web macro
106         // include as template is recommended.
107         // Velocity supports only parse
108         // added for melati conversion
109         //"#include\\s+as\\s+template\\s+([^\\s#]+)[ \\t]?",
110         //"#parse( $1 )",
111         assertEquals(WebMacroConverter.convert("#include as template $template "),"#parse( $template )");
112 
113         // Convert WM style #include to Velocity directive style.
114         //"#include\\s+([^\\s#]+)[ \\t]?",
115         //"#include( $1 )",
116         assertEquals(WebMacroConverter.convert("#include $file "),"#include( $file )");
117         
118         // Convert WM formal reference to VL syntax.
119         //"\\$\\(([^\\)]+)\\)",
120         //"\\${$1}",
121         assertEquals(WebMacroConverter.convert("$(var)"),"${var}");
122 
123         // TPP 2010/10/11 Not convinced
124         //"\\${([^}\\(]+)\\(([^}]+)}\\)", // fix encapsulated brackets: {(})
125         //"\\${$1($2)}",
126         assertEquals(WebMacroConverter.convert("${que($bar)}"),"${que($bar)}");
127 
128         // Velocity currently does not permit leading underscore.
129         //"\\$_",
130         //"\\$l_",
131         assertEquals(WebMacroConverter.convert("$_"),"$l_");
132 
133         // TPP 2010/10/11 Did WM formal syntax change to ()?
134         //"\\${(_[^}]+)}", // within a formal reference
135         //"\\${l$1}",
136         assertEquals(WebMacroConverter.convert("${_ff}"),"${l_ff}");
137 
138     
139     }
140 
141 }