View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.vm;
5   
6   import java.io.StringReader;
7   
8   import net.sourceforge.pmd.lang.LanguageVersion;
9   import net.sourceforge.pmd.lang.LanguageVersionHandler;
10  import net.sourceforge.pmd.lang.Parser;
11  import net.sourceforge.pmd.lang.ast.Node;
12  
13  import org.junit.Assert;
14  import org.junit.Test;
15  
16  /**
17   * Unit test for VM parsing.
18   * 
19   */
20  public class VmParserTest {
21  
22      private static final String VM_SRC = "<HTML><BODY>Hello $customer.Name <table> "
23              + "#foreach($mud in $mudsOnSpecial)" + "  #if ( $customer.hasPurchased($mud) )" + "     <tr>"
24              + "      <td>" + "       $flogger.getPromo( $mud )" + "    </td>" + "  </tr>"
25              + " #elseif ($customer.broke) do stuff #end" + "\n " + "#end " + "</table>";
26  
27      private static final String SRC2 = "#macro(tablerows $color $values ) " + "#foreach( $value in $values ) "
28              + "<tr><td bgcolor=$color>$value</td></tr> " + "#end " + "#end "
29              + "#set( $greatlakes = [\"Superior\",\"Michigan\",\"Huron\",\"Erie\",\"Ontario\"] ) "
30              + "#set( $color = \"blue\" ) " + "<table> " + " #tablerows( $color $greatlakes ) " + "</table>";
31  
32      private static final String SRC3 = "#if ( $c1 ) #if ( $c2)#end #end";
33  
34      // private static final String VM_SRC = "#if( $mud == 1 ) blah #if ($dirt == 2) stuff #end #end";
35  
36      @Test
37      public void testParser() {
38          final Node node = parse(VM_SRC);
39          Assert.assertNotNull(node);
40      }
41  
42      @Test
43      public void testParser2() {
44          final Node node = parse(SRC2);
45          Assert.assertNotNull(node);
46      }
47  
48      @Test
49      public void testParser3() {
50          final Node node = parse(SRC3);
51          Assert.assertNotNull(node);
52      }
53  
54      private Node parse(final String code) {
55          final LanguageVersionHandler vmLang = LanguageVersion.VM.getLanguageVersionHandler();
56          final Parser parser = vmLang.getParser(vmLang.getDefaultParserOptions());
57          final Node node = parser.parse(null, new StringReader(code));
58          return node;
59      }
60  }